blob: 782fbfccf303bcd52e1255a4d5460affab89531b [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);
125 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000126 if (VT.isInteger()) {
Craig Topper0faf46c2012-08-12 03:16:37 +0000127 setOperationAction(ISD::SHL, VT, Custom);
128 setOperationAction(ISD::SRA, VT, Custom);
129 setOperationAction(ISD::SRL, VT, Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +0000130 }
131
132 // Promote all bit-wise operations.
133 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Craig Topper0faf46c2012-08-12 03:16:37 +0000134 setOperationAction(ISD::AND, VT, Promote);
135 AddPromotedToType (ISD::AND, VT, PromotedBitwiseVT);
136 setOperationAction(ISD::OR, VT, Promote);
137 AddPromotedToType (ISD::OR, VT, PromotedBitwiseVT);
138 setOperationAction(ISD::XOR, VT, Promote);
139 AddPromotedToType (ISD::XOR, VT, PromotedBitwiseVT);
Bob Wilson5bafff32009-06-22 23:27:02 +0000140 }
Bob Wilson16330762009-09-16 00:17:28 +0000141
142 // Neon does not support vector divide/remainder operations.
Craig Topper0faf46c2012-08-12 03:16:37 +0000143 setOperationAction(ISD::SDIV, VT, Expand);
144 setOperationAction(ISD::UDIV, VT, Expand);
145 setOperationAction(ISD::FDIV, VT, Expand);
146 setOperationAction(ISD::SREM, VT, Expand);
147 setOperationAction(ISD::UREM, VT, Expand);
148 setOperationAction(ISD::FREM, VT, Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000149}
150
Craig Topper0faf46c2012-08-12 03:16:37 +0000151void ARMTargetLowering::addDRTypeForNEON(MVT VT) {
Craig Topper420761a2012-04-20 07:30:17 +0000152 addRegisterClass(VT, &ARM::DPRRegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000153 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000154}
155
Craig Topper0faf46c2012-08-12 03:16:37 +0000156void ARMTargetLowering::addQRTypeForNEON(MVT VT) {
Craig Topper420761a2012-04-20 07:30:17 +0000157 addRegisterClass(VT, &ARM::QPRRegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000158 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000159}
160
Chris Lattnerf0144122009-07-28 03:13:23 +0000161static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
162 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000163 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000164
Chris Lattner80ec2792009-08-02 00:34:36 +0000165 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000166}
167
Evan Chenga8e29892007-01-19 07:51:42 +0000168ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000169 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000170 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng31446872010-07-23 22:39:59 +0000171 RegInfo = TM.getRegisterInfo();
Evan Cheng3ef1c872010-09-10 01:29:16 +0000172 Itins = TM.getInstrItineraryData();
Evan Chenga8e29892007-01-19 07:51:42 +0000173
Duncan Sands28b77e92011-09-06 19:07:46 +0000174 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
175
Evan Chengb1df8f22007-04-27 08:15:43 +0000176 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000177 // Uses VFP for Thumb libfuncs if available.
178 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
179 // Single-precision floating-point arithmetic.
180 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
181 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
182 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
183 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000184
Evan Chengb1df8f22007-04-27 08:15:43 +0000185 // Double-precision floating-point arithmetic.
186 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
187 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
188 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
189 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000190
Evan Chengb1df8f22007-04-27 08:15:43 +0000191 // Single-precision comparisons.
192 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
193 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
194 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
195 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
196 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
197 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
198 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
199 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000200
Evan Chengb1df8f22007-04-27 08:15:43 +0000201 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
202 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
203 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
204 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
205 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
206 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
207 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
208 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000209
Evan Chengb1df8f22007-04-27 08:15:43 +0000210 // Double-precision comparisons.
211 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
212 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
213 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
214 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
215 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
216 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
217 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
218 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000219
Evan Chengb1df8f22007-04-27 08:15:43 +0000220 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
221 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
222 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
223 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
224 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
225 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
226 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
227 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000228
Evan Chengb1df8f22007-04-27 08:15:43 +0000229 // Floating-point to integer conversions.
230 // i64 conversions are done via library routines even when generating VFP
231 // instructions, so use the same ones.
232 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
233 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
234 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
235 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000236
Evan Chengb1df8f22007-04-27 08:15:43 +0000237 // Conversions between floating types.
238 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
239 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
240
241 // Integer to floating-point conversions.
242 // i64 conversions are done via library routines even when generating VFP
243 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000244 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
245 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000246 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
247 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
248 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
249 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
250 }
Evan Chenga8e29892007-01-19 07:51:42 +0000251 }
252
Bob Wilson2f954612009-05-22 17:38:41 +0000253 // These libcalls are not available in 32-bit.
254 setLibcallName(RTLIB::SHL_I128, 0);
255 setLibcallName(RTLIB::SRL_I128, 0);
256 setLibcallName(RTLIB::SRA_I128, 0);
257
Evan Cheng07043272012-02-21 20:46:00 +0000258 if (Subtarget->isAAPCS_ABI() && !Subtarget->isTargetDarwin()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000259 // Double-precision floating-point arithmetic helper functions
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000260 // RTABI chapter 4.1.2, Table 2
261 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd");
262 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv");
263 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul");
264 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub");
265 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS);
266 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS);
267 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS);
268 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS);
269
270 // Double-precision floating-point comparison helper functions
271 // RTABI chapter 4.1.2, Table 3
272 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq");
273 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
274 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq");
275 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ);
276 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt");
277 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
278 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple");
279 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
280 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge");
281 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
282 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt");
283 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
284 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun");
285 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
286 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun");
287 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
288 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS);
289 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS);
290 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS);
291 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS);
292 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS);
293 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS);
294 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS);
295 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS);
296
297 // Single-precision floating-point arithmetic helper functions
298 // RTABI chapter 4.1.2, Table 4
299 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd");
300 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv");
301 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul");
302 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub");
303 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS);
304 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS);
305 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS);
306 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS);
307
308 // Single-precision floating-point comparison helper functions
309 // RTABI chapter 4.1.2, Table 5
310 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq");
311 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
312 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq");
313 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ);
314 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt");
315 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
316 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple");
317 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
318 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge");
319 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
320 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt");
321 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
322 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun");
323 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
324 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun");
325 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
326 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS);
327 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS);
328 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS);
329 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS);
330 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS);
331 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS);
332 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS);
333 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS);
334
335 // Floating-point to integer conversions.
336 // RTABI chapter 4.1.2, Table 6
337 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz");
338 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz");
339 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz");
340 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz");
341 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz");
342 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz");
343 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz");
344 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz");
345 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS);
346 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS);
347 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS);
348 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS);
349 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS);
350 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS);
351 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS);
352 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS);
353
354 // Conversions between floating types.
355 // RTABI chapter 4.1.2, Table 7
356 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f");
357 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d");
358 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000359 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000360
361 // Integer to floating-point conversions.
362 // RTABI chapter 4.1.2, Table 8
363 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d");
364 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d");
365 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d");
366 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d");
367 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f");
368 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f");
369 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f");
370 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f");
371 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
372 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
373 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
374 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
375 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
376 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
377 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
378 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
379
380 // Long long helper functions
381 // RTABI chapter 4.2, Table 9
382 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000383 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
384 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
385 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
386 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
387 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
388 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
389 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
390 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
391 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
392
393 // Integer division functions
394 // RTABI chapter 4.3.1
395 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
396 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
397 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000398 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000399 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
400 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
401 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000402 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000403 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
404 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
405 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000406 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000407 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS);
408 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000409 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000410 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
Renato Golin1ec11fb2011-05-22 21:41:23 +0000411
412 // Memory operations
413 // RTABI chapter 4.3.4
414 setLibcallName(RTLIB::MEMCPY, "__aeabi_memcpy");
415 setLibcallName(RTLIB::MEMMOVE, "__aeabi_memmove");
416 setLibcallName(RTLIB::MEMSET, "__aeabi_memset");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000417 setLibcallCallingConv(RTLIB::MEMCPY, CallingConv::ARM_AAPCS);
418 setLibcallCallingConv(RTLIB::MEMMOVE, CallingConv::ARM_AAPCS);
419 setLibcallCallingConv(RTLIB::MEMSET, CallingConv::ARM_AAPCS);
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000420 }
421
Bob Wilson2fef4572011-10-07 16:59:21 +0000422 // Use divmod compiler-rt calls for iOS 5.0 and later.
423 if (Subtarget->getTargetTriple().getOS() == Triple::IOS &&
424 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
425 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
426 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
427 }
428
David Goodwinf1daf7d2009-07-08 23:10:31 +0000429 if (Subtarget->isThumb1Only())
Craig Topper420761a2012-04-20 07:30:17 +0000430 addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000431 else
Craig Topper420761a2012-04-20 07:30:17 +0000432 addRegisterClass(MVT::i32, &ARM::GPRRegClass);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000433 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
434 !Subtarget->isThumb1Only()) {
Craig Topper420761a2012-04-20 07:30:17 +0000435 addRegisterClass(MVT::f32, &ARM::SPRRegClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000436 if (!Subtarget->isFPOnlySP())
Craig Topper420761a2012-04-20 07:30:17 +0000437 addRegisterClass(MVT::f64, &ARM::DPRRegClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000438
Owen Anderson825b72b2009-08-11 20:47:22 +0000439 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000440 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000441
Eli Friedman9f1f26a2011-11-08 01:43:53 +0000442 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
443 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
444 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
445 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
446 setTruncStoreAction((MVT::SimpleValueType)VT,
447 (MVT::SimpleValueType)InnerVT, Expand);
448 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
449 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
450 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
451 }
452
Lang Hames45b5f882012-03-15 18:49:02 +0000453 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
454
Bob Wilson5bafff32009-06-22 23:27:02 +0000455 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000456 addDRTypeForNEON(MVT::v2f32);
457 addDRTypeForNEON(MVT::v8i8);
458 addDRTypeForNEON(MVT::v4i16);
459 addDRTypeForNEON(MVT::v2i32);
460 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000461
Owen Anderson825b72b2009-08-11 20:47:22 +0000462 addQRTypeForNEON(MVT::v4f32);
463 addQRTypeForNEON(MVT::v2f64);
464 addQRTypeForNEON(MVT::v16i8);
465 addQRTypeForNEON(MVT::v8i16);
466 addQRTypeForNEON(MVT::v4i32);
467 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000468
Bob Wilson74dc72e2009-09-15 23:55:57 +0000469 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
470 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000471 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
472 // supported for v4f32.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000473 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
474 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
475 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000476 // FIXME: Code duplication: FDIV and FREM are expanded always, see
477 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000478 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
479 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000480 // FIXME: Create unittest.
481 // In another words, find a way when "copysign" appears in DAG with vector
482 // operands.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000483 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000484 // FIXME: Code duplication: SETCC has custom operation action, see
485 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sands28b77e92011-09-06 19:07:46 +0000486 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000487 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000488 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
489 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
490 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
491 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
492 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
493 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
494 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
495 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
496 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
497 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
498 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
499 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000500 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000501 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
502 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
503 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
504 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
505 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Lang Hamesc0a9f822012-03-29 21:56:11 +0000506
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000507 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
508 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
509 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
510 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
511 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
512 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
513 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
514 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
515 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
516 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Bob Wilson74dc72e2009-09-15 23:55:57 +0000517
Bob Wilson642b3292009-09-16 00:32:15 +0000518 // Neon does not support some operations on v1i64 and v2i64 types.
519 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000520 // Custom handling for some quad-vector types to detect VMULL.
521 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
522 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
523 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begeman7973f352011-02-11 20:53:29 +0000524 // Custom handling for some vector types to avoid expensive expansions
525 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
526 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
527 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
528 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000529 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
530 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000531 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy873fd5f2012-02-20 09:24:05 +0000532 // a destination type that is wider than the source, and nor does
533 // it have a FP_TO_[SU]INT instruction with a narrower destination than
534 // source.
Cameron Zwarich3007d332011-03-29 21:41:55 +0000535 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
536 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy873fd5f2012-02-20 09:24:05 +0000537 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
538 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000539
Bob Wilson1c3ef902011-02-07 17:43:21 +0000540 setTargetDAGCombine(ISD::INTRINSIC_VOID);
541 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000542 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
543 setTargetDAGCombine(ISD::SHL);
544 setTargetDAGCombine(ISD::SRL);
545 setTargetDAGCombine(ISD::SRA);
546 setTargetDAGCombine(ISD::SIGN_EXTEND);
547 setTargetDAGCombine(ISD::ZERO_EXTEND);
548 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000549 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000550 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000551 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000552 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
553 setTargetDAGCombine(ISD::STORE);
Chad Rosieref01edf2011-06-24 19:23:04 +0000554 setTargetDAGCombine(ISD::FP_TO_SINT);
555 setTargetDAGCombine(ISD::FP_TO_UINT);
556 setTargetDAGCombine(ISD::FDIV);
Nadav Rotem004a24b2011-10-15 20:03:12 +0000557
James Molloy873fd5f2012-02-20 09:24:05 +0000558 // It is legal to extload from v4i8 to v4i16 or v4i32.
559 MVT Tys[6] = {MVT::v8i8, MVT::v4i8, MVT::v2i8,
560 MVT::v4i16, MVT::v2i16,
561 MVT::v2i32};
562 for (unsigned i = 0; i < 6; ++i) {
563 setLoadExtAction(ISD::EXTLOAD, Tys[i], Legal);
564 setLoadExtAction(ISD::ZEXTLOAD, Tys[i], Legal);
565 setLoadExtAction(ISD::SEXTLOAD, Tys[i], Legal);
566 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000567 }
568
Arnold Schwaighoferbcc4c1d2012-08-09 15:25:52 +0000569 // ARM and Thumb2 support UMLAL/SMLAL.
570 if (!Subtarget->isThumb1Only())
571 setTargetDAGCombine(ISD::ADDC);
572
573
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000574 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000575
576 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000577 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000578
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000579 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000580 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000581
Evan Chenga8e29892007-01-19 07:51:42 +0000582 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000583 if (!Subtarget->isThumb1Only()) {
584 for (unsigned im = (unsigned)ISD::PRE_INC;
585 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000586 setIndexedLoadAction(im, MVT::i1, Legal);
587 setIndexedLoadAction(im, MVT::i8, Legal);
588 setIndexedLoadAction(im, MVT::i16, Legal);
589 setIndexedLoadAction(im, MVT::i32, Legal);
590 setIndexedStoreAction(im, MVT::i1, Legal);
591 setIndexedStoreAction(im, MVT::i8, Legal);
592 setIndexedStoreAction(im, MVT::i16, Legal);
593 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000594 }
Evan Chenga8e29892007-01-19 07:51:42 +0000595 }
596
597 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000598 setOperationAction(ISD::MUL, MVT::i64, Expand);
599 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000600 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000601 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
602 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000603 }
Jim Grosbacha7603982011-07-01 21:12:19 +0000604 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
605 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopher2cc40132011-04-19 18:49:19 +0000606 setOperationAction(ISD::MULHS, MVT::i32, Expand);
607
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000608 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000609 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000610 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000611 setOperationAction(ISD::SRL, MVT::i64, Custom);
612 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000613
Evan Cheng342e3162011-08-30 01:34:54 +0000614 if (!Subtarget->isThumb1Only()) {
615 // FIXME: We should do this for Thumb1 as well.
616 setOperationAction(ISD::ADDC, MVT::i32, Custom);
617 setOperationAction(ISD::ADDE, MVT::i32, Custom);
618 setOperationAction(ISD::SUBC, MVT::i32, Custom);
619 setOperationAction(ISD::SUBE, MVT::i32, Custom);
620 }
621
Evan Chenga8e29892007-01-19 07:51:42 +0000622 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000623 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000624 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000625 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000626 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000627 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000628
Chandler Carruth63974b22011-12-13 01:56:10 +0000629 // These just redirect to CTTZ and CTLZ on ARM.
630 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
631 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
632
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000633 // Only ARMv6 has BSWAP.
634 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000635 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000636
Evan Chenga8e29892007-01-19 07:51:42 +0000637 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000638 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000639 // v7M has a hardware divider
640 setOperationAction(ISD::SDIV, MVT::i32, Expand);
641 setOperationAction(ISD::UDIV, MVT::i32, Expand);
642 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000643 setOperationAction(ISD::SREM, MVT::i32, Expand);
644 setOperationAction(ISD::UREM, MVT::i32, Expand);
645 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
646 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000647
Owen Anderson825b72b2009-08-11 20:47:22 +0000648 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
649 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
650 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
651 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000652 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000653
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000654 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000655
Evan Chenga8e29892007-01-19 07:51:42 +0000656 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000657 setOperationAction(ISD::VASTART, MVT::Other, Custom);
658 setOperationAction(ISD::VAARG, MVT::Other, Expand);
659 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
660 setOperationAction(ISD::VAEND, MVT::Other, Expand);
661 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
662 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendlingbdf9db62012-02-13 23:47:16 +0000663
664 if (!Subtarget->isTargetDarwin()) {
665 // Non-Darwin platforms may return values in these registers via the
666 // personality function.
667 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
668 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
669 setExceptionPointerRegister(ARM::R0);
670 setExceptionSelectorRegister(ARM::R1);
671 }
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000672
Evan Cheng3a1588a2010-04-15 22:20:34 +0000673 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000674 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
675 // the default expansion.
Eli Friedman4db5aca2011-08-29 18:23:02 +0000676 // FIXME: This should be checking for v6k, not just v6.
Evan Cheng11db0682010-08-11 06:22:01 +0000677 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000678 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000679 // membarrier needs custom lowering; the rest are legal and handled
680 // normally.
681 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000682 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Eli Friedman2bdffe42011-08-31 00:31:29 +0000683 // Custom lowering for 64-bit ops
684 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
685 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
686 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
687 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
688 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
689 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Eli Friedman4d3f3292011-08-31 17:52:22 +0000690 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Eli Friedman26689ac2011-08-03 21:06:02 +0000691 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
692 setInsertFencesForAtomic(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000693 } else {
694 // Set them all for expansion, which will force libcalls.
695 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000696 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000697 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000698 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000699 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000700 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000701 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000702 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000703 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000704 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000705 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000706 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000707 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000708 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedman7cc15662011-09-15 22:18:49 +0000709 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
710 // Unordered/Monotonic case.
711 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
712 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000713 // Since the libcalls include locking, fold in the fences
714 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000715 }
Evan Chenga8e29892007-01-19 07:51:42 +0000716
Evan Cheng416941d2010-11-04 05:19:35 +0000717 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000718
Eli Friedmana2c6f452010-06-26 04:36:50 +0000719 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
720 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000721 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
722 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000723 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000724 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000725
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000726 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
727 !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000728 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
729 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000730 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000731 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
732 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000733
734 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000735 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000736 if (Subtarget->isTargetDarwin()) {
737 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
738 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000739 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000740 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000741
Owen Anderson825b72b2009-08-11 20:47:22 +0000742 setOperationAction(ISD::SETCC, MVT::i32, Expand);
743 setOperationAction(ISD::SETCC, MVT::f32, Expand);
744 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000745 setOperationAction(ISD::SELECT, MVT::i32, Custom);
746 setOperationAction(ISD::SELECT, MVT::f32, Custom);
747 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000748 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
749 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
750 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000751
Owen Anderson825b72b2009-08-11 20:47:22 +0000752 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
753 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
754 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
755 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
756 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000757
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000758 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000759 setOperationAction(ISD::FSIN, MVT::f64, Expand);
760 setOperationAction(ISD::FSIN, MVT::f32, Expand);
761 setOperationAction(ISD::FCOS, MVT::f32, Expand);
762 setOperationAction(ISD::FCOS, MVT::f64, Expand);
763 setOperationAction(ISD::FREM, MVT::f64, Expand);
764 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000765 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
766 !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000767 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
768 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000769 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000770 setOperationAction(ISD::FPOW, MVT::f64, Expand);
771 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000772
Evan Cheng3aef2ff2012-04-10 21:40:28 +0000773 if (!Subtarget->hasVFP4()) {
774 setOperationAction(ISD::FMA, MVT::f64, Expand);
775 setOperationAction(ISD::FMA, MVT::f32, Expand);
776 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000777
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000778 // Various VFP goodness
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000779 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000780 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
781 if (Subtarget->hasVFP2()) {
782 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
783 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
784 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
785 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
786 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000787 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000788 if (!Subtarget->hasFP16()) {
789 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
790 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000791 }
Evan Cheng110cf482008-04-01 01:50:16 +0000792 }
Evan Chenga8e29892007-01-19 07:51:42 +0000793
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000794 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000795 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000796 setTargetDAGCombine(ISD::ADD);
797 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000798 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000799
Evan Chengc892aeb2012-02-23 01:19:06 +0000800 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON()) {
Owen Anderson080c0922010-11-05 19:27:46 +0000801 setTargetDAGCombine(ISD::AND);
Evan Chengc892aeb2012-02-23 01:19:06 +0000802 setTargetDAGCombine(ISD::OR);
803 setTargetDAGCombine(ISD::XOR);
804 }
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000805
Evan Cheng5fb468a2012-02-23 02:58:19 +0000806 if (Subtarget->hasV6Ops())
807 setTargetDAGCombine(ISD::SRL);
808
Evan Chenga8e29892007-01-19 07:51:42 +0000809 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000810
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000811 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
812 !Subtarget->hasVFP2())
Evan Chengf7d87ee2010-05-21 00:43:17 +0000813 setSchedulingPreference(Sched::RegPressure);
814 else
815 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000816
Evan Cheng05219282011-01-06 06:52:41 +0000817 //// temporary - rewrite interface to use type
818 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Lang Hames75757f92011-10-26 20:56:52 +0000819 maxStoresPerMemset = 16;
820 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengf6799392010-06-26 01:52:05 +0000821
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000822 // On ARM arguments smaller than 4 bytes are extended, so all arguments
823 // are at least 4 bytes aligned.
824 setMinStackArgumentAlignment(4);
825
Evan Chengfff606d2010-09-24 19:07:23 +0000826 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000827
Benjamin Krameraaf723d2012-05-05 12:49:14 +0000828 // Prefer likely predicted branches to selects on out-of-order cores.
829 predictableSelectIsExpensive = Subtarget->isCortexA9();
830
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000831 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000832}
833
Andrew Trick32cec0a2011-01-19 02:35:27 +0000834// FIXME: It might make sense to define the representative register class as the
835// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
836// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
837// SPR's representative would be DPR_VFP2. This should work well if register
838// pressure tracking were modified such that a register use would increment the
839// pressure of the register class's representative and all of it's super
840// classes' representatives transitively. We have not implemented this because
841// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000842// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000843// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000844std::pair<const TargetRegisterClass*, uint8_t>
845ARMTargetLowering::findRepresentativeClass(EVT VT) const{
846 const TargetRegisterClass *RRC = 0;
847 uint8_t Cost = 1;
848 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000849 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000850 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000851 // Use DPR as representative register class for all floating point
852 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
853 // the cost is 1 for both f32 and f64.
854 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000855 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topper420761a2012-04-20 07:30:17 +0000856 RRC = &ARM::DPRRegClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000857 // When NEON is used for SP, only half of the register file is available
858 // because operations that define both SP and DP results will be constrained
859 // to the VFP2 class (D0-D15). We currently model this constraint prior to
860 // coalescing by double-counting the SP regs. See the FIXME above.
861 if (Subtarget->useNEONForSinglePrecisionFP())
862 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000863 break;
864 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
865 case MVT::v4f32: case MVT::v2f64:
Craig Topper420761a2012-04-20 07:30:17 +0000866 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000867 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000868 break;
869 case MVT::v4i64:
Craig Topper420761a2012-04-20 07:30:17 +0000870 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000871 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000872 break;
873 case MVT::v8i64:
Craig Topper420761a2012-04-20 07:30:17 +0000874 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000875 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000876 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000877 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000878 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000879}
880
Evan Chenga8e29892007-01-19 07:51:42 +0000881const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
882 switch (Opcode) {
883 default: return 0;
884 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000885 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000886 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000887 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
888 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000889 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000890 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
891 case ARMISD::tCALL: return "ARMISD::tCALL";
892 case ARMISD::BRCOND: return "ARMISD::BRCOND";
893 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000894 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000895 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
896 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
897 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendlingad5c8802012-06-11 08:07:26 +0000898 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwinc0309b42009-06-29 15:33:01 +0000899 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000900 case ARMISD::CMPFP: return "ARMISD::CMPFP";
901 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000902 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000903 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chengc892aeb2012-02-23 01:19:06 +0000904
Evan Chenga8e29892007-01-19 07:51:42 +0000905 case ARMISD::CMOV: return "ARMISD::CMOV";
Evan Chengc892aeb2012-02-23 01:19:06 +0000906 case ARMISD::CAND: return "ARMISD::CAND";
907 case ARMISD::COR: return "ARMISD::COR";
908 case ARMISD::CXOR: return "ARMISD::CXOR";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000909
Jim Grosbach3482c802010-01-18 19:58:49 +0000910 case ARMISD::RBIT: return "ARMISD::RBIT";
911
Bob Wilson76a312b2010-03-19 22:51:32 +0000912 case ARMISD::FTOSI: return "ARMISD::FTOSI";
913 case ARMISD::FTOUI: return "ARMISD::FTOUI";
914 case ARMISD::SITOF: return "ARMISD::SITOF";
915 case ARMISD::UITOF: return "ARMISD::UITOF";
916
Evan Chenga8e29892007-01-19 07:51:42 +0000917 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
918 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
919 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000920
Evan Cheng342e3162011-08-30 01:34:54 +0000921 case ARMISD::ADDC: return "ARMISD::ADDC";
922 case ARMISD::ADDE: return "ARMISD::ADDE";
923 case ARMISD::SUBC: return "ARMISD::SUBC";
924 case ARMISD::SUBE: return "ARMISD::SUBE";
925
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000926 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
927 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000928
Evan Chengc5942082009-10-28 06:55:03 +0000929 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
930 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
931
Dale Johannesen51e28e62010-06-03 21:09:53 +0000932 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000933
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000934 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000935
Evan Cheng86198642009-08-07 00:34:42 +0000936 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
937
Jim Grosbach3728e962009-12-10 00:11:09 +0000938 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000939 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000940
Evan Chengdfed19f2010-11-03 06:34:55 +0000941 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
942
Bob Wilson5bafff32009-06-22 23:27:02 +0000943 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000944 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000945 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000946 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
947 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000948 case ARMISD::VCGEU: return "ARMISD::VCGEU";
949 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000950 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
951 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000952 case ARMISD::VCGTU: return "ARMISD::VCGTU";
953 case ARMISD::VTST: return "ARMISD::VTST";
954
955 case ARMISD::VSHL: return "ARMISD::VSHL";
956 case ARMISD::VSHRs: return "ARMISD::VSHRs";
957 case ARMISD::VSHRu: return "ARMISD::VSHRu";
958 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
959 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
960 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
961 case ARMISD::VSHRN: return "ARMISD::VSHRN";
962 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
963 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
964 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
965 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
966 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
967 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
968 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
969 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
970 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
971 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
972 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
973 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
974 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
975 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000976 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000977 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Chengeaa192a2011-11-15 02:12:34 +0000978 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000979 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000980 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000981 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000982 case ARMISD::VREV64: return "ARMISD::VREV64";
983 case ARMISD::VREV32: return "ARMISD::VREV32";
984 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000985 case ARMISD::VZIP: return "ARMISD::VZIP";
986 case ARMISD::VUZP: return "ARMISD::VUZP";
987 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000988 case ARMISD::VTBL1: return "ARMISD::VTBL1";
989 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000990 case ARMISD::VMULLs: return "ARMISD::VMULLs";
991 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighoferbcc4c1d2012-08-09 15:25:52 +0000992 case ARMISD::UMLAL: return "ARMISD::UMLAL";
993 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000994 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000995 case ARMISD::FMAX: return "ARMISD::FMAX";
996 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000997 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000998 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
999 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00001000 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00001001 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1002 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1003 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +00001004 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1005 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1006 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1007 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1008 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1009 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1010 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1011 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1012 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1013 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1014 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1015 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1016 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1017 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1018 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1019 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1020 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +00001021 }
1022}
1023
Duncan Sands28b77e92011-09-06 19:07:46 +00001024EVT ARMTargetLowering::getSetCCResultType(EVT VT) const {
1025 if (!VT.isVector()) return getPointerTy();
1026 return VT.changeVectorElementTypeToInteger();
1027}
1028
Evan Cheng06b666c2010-05-15 02:18:07 +00001029/// getRegClassFor - Return the register class that should be used for the
1030/// specified value type.
Craig Topper44d23822012-02-22 05:59:10 +00001031const TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
Evan Cheng06b666c2010-05-15 02:18:07 +00001032 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1033 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1034 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +00001035 if (Subtarget->hasNEON()) {
1036 if (VT == MVT::v4i64)
Craig Topper420761a2012-04-20 07:30:17 +00001037 return &ARM::QQPRRegClass;
1038 if (VT == MVT::v8i64)
1039 return &ARM::QQQQPRRegClass;
Evan Cheng4782b1e2010-05-15 02:20:21 +00001040 }
Evan Cheng06b666c2010-05-15 02:18:07 +00001041 return TargetLowering::getRegClassFor(VT);
1042}
1043
Eric Christopherab695882010-07-21 22:26:11 +00001044// Create a fast isel object.
1045FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00001046ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1047 const TargetLibraryInfo *libInfo) const {
1048 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopherab695882010-07-21 22:26:11 +00001049}
1050
Anton Korobeynikovcec36f42010-07-24 21:52:08 +00001051/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1052/// be used for loads / stores from the global.
1053unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1054 return (Subtarget->isThumb1Only() ? 127 : 4095);
1055}
1056
Evan Cheng1cc39842010-05-20 23:26:43 +00001057Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +00001058 unsigned NumVals = N->getNumValues();
1059 if (!NumVals)
1060 return Sched::RegPressure;
1061
1062 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +00001063 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001064 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +00001065 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +00001066 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman692c1d82011-10-24 17:55:11 +00001067 return Sched::ILP;
Evan Cheng1cc39842010-05-20 23:26:43 +00001068 }
Evan Chengc10f5432010-05-28 23:25:23 +00001069
1070 if (!N->isMachineOpcode())
1071 return Sched::RegPressure;
1072
1073 // Load are scheduled for latency even if there instruction itinerary
1074 // is not available.
1075 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +00001076 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +00001077
Evan Chenge837dea2011-06-28 19:10:37 +00001078 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +00001079 return Sched::RegPressure;
1080 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +00001081 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman692c1d82011-10-24 17:55:11 +00001082 return Sched::ILP;
Evan Chengc10f5432010-05-28 23:25:23 +00001083
Evan Cheng1cc39842010-05-20 23:26:43 +00001084 return Sched::RegPressure;
1085}
1086
Evan Chenga8e29892007-01-19 07:51:42 +00001087//===----------------------------------------------------------------------===//
1088// Lowering Code
1089//===----------------------------------------------------------------------===//
1090
Evan Chenga8e29892007-01-19 07:51:42 +00001091/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1092static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1093 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001094 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +00001095 case ISD::SETNE: return ARMCC::NE;
1096 case ISD::SETEQ: return ARMCC::EQ;
1097 case ISD::SETGT: return ARMCC::GT;
1098 case ISD::SETGE: return ARMCC::GE;
1099 case ISD::SETLT: return ARMCC::LT;
1100 case ISD::SETLE: return ARMCC::LE;
1101 case ISD::SETUGT: return ARMCC::HI;
1102 case ISD::SETUGE: return ARMCC::HS;
1103 case ISD::SETULT: return ARMCC::LO;
1104 case ISD::SETULE: return ARMCC::LS;
1105 }
1106}
1107
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001108/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1109static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001110 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001111 CondCode2 = ARMCC::AL;
1112 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001113 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001114 case ISD::SETEQ:
1115 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1116 case ISD::SETGT:
1117 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1118 case ISD::SETGE:
1119 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1120 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001121 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001122 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1123 case ISD::SETO: CondCode = ARMCC::VC; break;
1124 case ISD::SETUO: CondCode = ARMCC::VS; break;
1125 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1126 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1127 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1128 case ISD::SETLT:
1129 case ISD::SETULT: CondCode = ARMCC::LT; break;
1130 case ISD::SETLE:
1131 case ISD::SETULE: CondCode = ARMCC::LE; break;
1132 case ISD::SETNE:
1133 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1134 }
Evan Chenga8e29892007-01-19 07:51:42 +00001135}
1136
Bob Wilson1f595bb2009-04-17 19:07:39 +00001137//===----------------------------------------------------------------------===//
1138// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001139//===----------------------------------------------------------------------===//
1140
1141#include "ARMGenCallingConv.inc"
1142
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001143/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1144/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001145CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001146 bool Return,
1147 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001148 switch (CC) {
1149 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001150 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001151 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001152 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001153 if (!Subtarget->isAAPCS_ABI())
1154 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1155 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1156 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1157 }
1158 // Fallthrough
1159 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001160 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001161 if (!Subtarget->isAAPCS_ABI())
1162 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1163 else if (Subtarget->hasVFP2() &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001164 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1165 !isVarArg)
Evan Cheng76f920d2010-10-22 18:23:05 +00001166 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1167 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1168 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001169 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikovf349cb82012-01-29 09:06:09 +00001170 if (!isVarArg)
1171 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1172 // Fallthrough
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001173 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001174 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001175 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001176 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Eric Christophere94ac882012-08-03 00:05:53 +00001177 case CallingConv::GHC:
1178 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001179 }
1180}
1181
Dan Gohman98ca4f22009-08-05 01:29:28 +00001182/// LowerCallResult - Lower the result values of a call into the
1183/// appropriate copies out of appropriate physical registers.
1184SDValue
1185ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001186 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001187 const SmallVectorImpl<ISD::InputArg> &Ins,
1188 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001189 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001190
Bob Wilson1f595bb2009-04-17 19:07:39 +00001191 // Assign locations to each value returned by this call.
1192 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001193 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1194 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001195 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001196 CCAssignFnForNode(CallConv, /* Return*/ true,
1197 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001198
1199 // Copy all of the result registers out of their specified physreg.
1200 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1201 CCValAssign VA = RVLocs[i];
1202
Bob Wilson80915242009-04-25 00:33:20 +00001203 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001204 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001205 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001206 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001207 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001208 Chain = Lo.getValue(1);
1209 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001210 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001211 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001212 InFlag);
1213 Chain = Hi.getValue(1);
1214 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001215 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001216
Owen Anderson825b72b2009-08-11 20:47:22 +00001217 if (VA.getLocVT() == MVT::v2f64) {
1218 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1219 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1220 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001221
1222 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001223 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001224 Chain = Lo.getValue(1);
1225 InFlag = Lo.getValue(2);
1226 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001227 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001228 Chain = Hi.getValue(1);
1229 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001230 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001231 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1232 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001233 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001234 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001235 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1236 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001237 Chain = Val.getValue(1);
1238 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001239 }
Bob Wilson80915242009-04-25 00:33:20 +00001240
1241 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001242 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001243 case CCValAssign::Full: break;
1244 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001245 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001246 break;
1247 }
1248
Dan Gohman98ca4f22009-08-05 01:29:28 +00001249 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001250 }
1251
Dan Gohman98ca4f22009-08-05 01:29:28 +00001252 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001253}
1254
Bob Wilsondee46d72009-04-17 20:35:10 +00001255/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001256SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001257ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1258 SDValue StackPtr, SDValue Arg,
1259 DebugLoc dl, SelectionDAG &DAG,
1260 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001261 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001262 unsigned LocMemOffset = VA.getLocMemOffset();
1263 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1264 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001265 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001266 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001267 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001268}
1269
Dan Gohman98ca4f22009-08-05 01:29:28 +00001270void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001271 SDValue Chain, SDValue &Arg,
1272 RegsToPassVector &RegsToPass,
1273 CCValAssign &VA, CCValAssign &NextVA,
1274 SDValue &StackPtr,
1275 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001276 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001277
Jim Grosbache5165492009-11-09 00:11:35 +00001278 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001279 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001280 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1281
1282 if (NextVA.isRegLoc())
1283 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1284 else {
1285 assert(NextVA.isMemLoc());
1286 if (StackPtr.getNode() == 0)
1287 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1288
Dan Gohman98ca4f22009-08-05 01:29:28 +00001289 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1290 dl, DAG, NextVA,
1291 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001292 }
1293}
1294
Dan Gohman98ca4f22009-08-05 01:29:28 +00001295/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001296/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1297/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001298SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001299ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00001300 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001301 SelectionDAG &DAG = CLI.DAG;
1302 DebugLoc &dl = CLI.DL;
1303 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
1304 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
1305 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
1306 SDValue Chain = CLI.Chain;
1307 SDValue Callee = CLI.Callee;
1308 bool &isTailCall = CLI.IsTailCall;
1309 CallingConv::ID CallConv = CLI.CallConv;
1310 bool doesNotRet = CLI.DoesNotReturn;
1311 bool isVarArg = CLI.IsVarArg;
1312
Dale Johannesen51e28e62010-06-03 21:09:53 +00001313 MachineFunction &MF = DAG.getMachineFunction();
1314 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1315 bool IsSibCall = false;
Bob Wilson6d2f9ce2011-10-07 17:17:49 +00001316 // Disable tail calls if they're not supported.
1317 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Bob Wilson703af3a2010-08-13 22:43:33 +00001318 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001319 if (isTailCall) {
1320 // Check if it's really possible to do a tail call.
1321 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1322 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001323 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001324 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1325 // detected sibcalls.
1326 if (isTailCall) {
1327 ++NumTailCalls;
1328 IsSibCall = true;
1329 }
1330 }
Evan Chenga8e29892007-01-19 07:51:42 +00001331
Bob Wilson1f595bb2009-04-17 19:07:39 +00001332 // Analyze operands of the call, assigning locations to each operand.
1333 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001334 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1335 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001336 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001337 CCAssignFnForNode(CallConv, /* Return*/ false,
1338 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001339
Bob Wilson1f595bb2009-04-17 19:07:39 +00001340 // Get a count of how many bytes are to be pushed on the stack.
1341 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001342
Dale Johannesen51e28e62010-06-03 21:09:53 +00001343 // For tail calls, memory operands are available in our caller's stack.
1344 if (IsSibCall)
1345 NumBytes = 0;
1346
Evan Chenga8e29892007-01-19 07:51:42 +00001347 // Adjust the stack pointer for the new arguments...
1348 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001349 if (!IsSibCall)
1350 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001351
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001352 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001353
Bob Wilson5bafff32009-06-22 23:27:02 +00001354 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001355 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001356
Bob Wilson1f595bb2009-04-17 19:07:39 +00001357 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001358 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001359 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1360 i != e;
1361 ++i, ++realArgIdx) {
1362 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001363 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001364 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001365 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001366
Bob Wilson1f595bb2009-04-17 19:07:39 +00001367 // Promote the value if needed.
1368 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001369 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001370 case CCValAssign::Full: break;
1371 case CCValAssign::SExt:
1372 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1373 break;
1374 case CCValAssign::ZExt:
1375 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1376 break;
1377 case CCValAssign::AExt:
1378 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1379 break;
1380 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001381 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001382 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001383 }
1384
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001385 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001386 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001387 if (VA.getLocVT() == MVT::v2f64) {
1388 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1389 DAG.getConstant(0, MVT::i32));
1390 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1391 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001392
Dan Gohman98ca4f22009-08-05 01:29:28 +00001393 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001394 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1395
1396 VA = ArgLocs[++i]; // skip ahead to next loc
1397 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001398 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001399 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1400 } else {
1401 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001402
Dan Gohman98ca4f22009-08-05 01:29:28 +00001403 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1404 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001405 }
1406 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001407 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001408 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001409 }
1410 } else if (VA.isRegLoc()) {
1411 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001412 } else if (isByVal) {
1413 assert(VA.isMemLoc());
1414 unsigned offset = 0;
1415
1416 // True if this byval aggregate will be split between registers
1417 // and memory.
1418 if (CCInfo.isFirstByValRegValid()) {
1419 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1420 unsigned int i, j;
1421 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1422 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1423 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1424 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1425 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001426 false, false, false, 0);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001427 MemOpChains.push_back(Load.getValue(1));
1428 RegsToPass.push_back(std::make_pair(j, Load));
1429 }
1430 offset = ARM::R4 - CCInfo.getFirstByValReg();
1431 CCInfo.clearFirstByValReg();
1432 }
1433
Manman Ren763a75d2012-06-01 02:44:42 +00001434 if (Flags.getByValSize() - 4*offset > 0) {
1435 unsigned LocMemOffset = VA.getLocMemOffset();
1436 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1437 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1438 StkPtrOff);
1439 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1440 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1441 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1442 MVT::i32);
Manman Ren68f25572012-06-01 19:33:18 +00001443 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), MVT::i32);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001444
Manman Ren763a75d2012-06-01 02:44:42 +00001445 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Ren68f25572012-06-01 19:33:18 +00001446 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren763a75d2012-06-01 02:44:42 +00001447 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
1448 Ops, array_lengthof(Ops)));
1449 }
Stuart Hastingsc7315872011-04-20 16:47:52 +00001450 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001451 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001452
Dan Gohman98ca4f22009-08-05 01:29:28 +00001453 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1454 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001455 }
Evan Chenga8e29892007-01-19 07:51:42 +00001456 }
1457
1458 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001459 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001460 &MemOpChains[0], MemOpChains.size());
1461
1462 // Build a sequence of copy-to-reg nodes chained together with token chain
1463 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001464 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001465 // Tail call byval lowering might overwrite argument registers so in case of
1466 // tail call optimization the copies to registers are lowered later.
1467 if (!isTailCall)
1468 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1469 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1470 RegsToPass[i].second, InFlag);
1471 InFlag = Chain.getValue(1);
1472 }
Evan Chenga8e29892007-01-19 07:51:42 +00001473
Dale Johannesen51e28e62010-06-03 21:09:53 +00001474 // For tail calls lower the arguments to the 'real' stack slot.
1475 if (isTailCall) {
1476 // Force all the incoming stack arguments to be loaded from the stack
1477 // before any new outgoing arguments are stored to the stack, because the
1478 // outgoing stack slots may alias the incoming argument stack slots, and
1479 // the alias isn't otherwise explicit. This is slightly more conservative
1480 // than necessary, because it means that each store effectively depends
1481 // on every argument instead of just those arguments it would clobber.
1482
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001483 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001484 InFlag = SDValue();
1485 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1486 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1487 RegsToPass[i].second, InFlag);
1488 InFlag = Chain.getValue(1);
1489 }
1490 InFlag =SDValue();
1491 }
1492
Bill Wendling056292f2008-09-16 21:48:12 +00001493 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1494 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1495 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001496 bool isDirect = false;
1497 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001498 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001499 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001500
1501 if (EnableARMLongCalls) {
1502 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1503 && "long-calls with non-static relocation model!");
1504 // Handle a global address or an external symbol. If it's not one of
1505 // those, the target's already in a register, so we don't need to do
1506 // anything extra.
1507 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001508 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001509 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001510 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001511 ARMConstantPoolValue *CPV =
1512 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1513
Jim Grosbache7b52522010-04-14 22:28:31 +00001514 // Get the address of the callee into a register
1515 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1516 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1517 Callee = DAG.getLoad(getPointerTy(), dl,
1518 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001519 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001520 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001521 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1522 const char *Sym = S->getSymbol();
1523
1524 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001525 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001526 ARMConstantPoolValue *CPV =
1527 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1528 ARMPCLabelIndex, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001529 // Get the address of the callee into a register
1530 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1531 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1532 Callee = DAG.getLoad(getPointerTy(), dl,
1533 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001534 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001535 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001536 }
1537 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001538 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001539 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001540 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001541 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001542 getTargetMachine().getRelocationModel() != Reloc::Static;
1543 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001544 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001545 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001546 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001547 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001548 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001549 ARMConstantPoolValue *CPV =
1550 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001551 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001552 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001553 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001554 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001555 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001556 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001557 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001558 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001559 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001560 } else {
1561 // On ELF targets for PIC code, direct calls should go through the PLT
1562 unsigned OpFlags = 0;
1563 if (Subtarget->isTargetELF() &&
1564 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1565 OpFlags = ARMII::MO_PLT;
1566 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1567 }
Bill Wendling056292f2008-09-16 21:48:12 +00001568 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001569 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001570 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001571 getTargetMachine().getRelocationModel() != Reloc::Static;
1572 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001573 // tBX takes a register source operand.
1574 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001575 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001576 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001577 ARMConstantPoolValue *CPV =
1578 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1579 ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001580 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001581 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001582 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001583 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001584 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001585 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001586 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001587 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001588 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001589 } else {
1590 unsigned OpFlags = 0;
1591 // On ELF targets for PIC code, direct calls should go through the PLT
1592 if (Subtarget->isTargetELF() &&
1593 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1594 OpFlags = ARMII::MO_PLT;
1595 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1596 }
Evan Chenga8e29892007-01-19 07:51:42 +00001597 }
1598
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001599 // FIXME: handle tail calls differently.
1600 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001601 if (Subtarget->isThumb()) {
1602 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001603 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001604 else if (doesNotRet && isDirect && !isARMFunc &&
1605 Subtarget->hasRAS() && !Subtarget->isThumb1Only())
1606 // "mov lr, pc; b _foo" to avoid confusing the RSP
1607 CallOpc = ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001608 else
1609 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1610 } else {
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001611 if (!isDirect && !Subtarget->hasV5TOps()) {
1612 CallOpc = ARMISD::CALL_NOLINK;
1613 } else if (doesNotRet && isDirect && Subtarget->hasRAS())
1614 // "mov lr, pc; b _foo" to avoid confusing the RSP
1615 CallOpc = ARMISD::CALL_NOLINK;
1616 else
1617 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001618 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001619
Dan Gohman475871a2008-07-27 21:46:04 +00001620 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001621 Ops.push_back(Chain);
1622 Ops.push_back(Callee);
1623
1624 // Add argument registers to the end of the list so that they are known live
1625 // into the call.
1626 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1627 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1628 RegsToPass[i].second.getValueType()));
1629
Jakob Stoklund Olesenc54f6342012-02-24 01:19:29 +00001630 // Add a register mask operand representing the call-preserved registers.
1631 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
1632 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
1633 assert(Mask && "Missing call preserved mask for calling convention");
1634 Ops.push_back(DAG.getRegisterMask(Mask));
1635
Gabor Greifba36cb52008-08-28 21:40:38 +00001636 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001637 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001638
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001639 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001640 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001641 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001642
Duncan Sands4bdcb612008-07-02 17:40:58 +00001643 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001644 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001645 InFlag = Chain.getValue(1);
1646
Chris Lattnere563bbc2008-10-11 22:08:30 +00001647 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1648 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001649 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001650 InFlag = Chain.getValue(1);
1651
Bob Wilson1f595bb2009-04-17 19:07:39 +00001652 // Handle result values, copying them out of physregs into vregs that we
1653 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001654 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1655 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001656}
1657
Stuart Hastingsf222e592011-02-28 17:17:53 +00001658/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001659/// on the stack. Remember the next parameter register to allocate,
1660/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001661/// this.
1662void
Craig Topperc89c7442012-03-27 07:21:54 +00001663ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
Stuart Hastingsc7315872011-04-20 16:47:52 +00001664 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1665 assert((State->getCallOrPrologue() == Prologue ||
1666 State->getCallOrPrologue() == Call) &&
1667 "unhandled ParmContext");
1668 if ((!State->isFirstByValRegValid()) &&
1669 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1670 State->setFirstByValReg(reg);
1671 // At a call site, a byval parameter that is split between
1672 // registers and memory needs its size truncated here. In a
1673 // function prologue, such byval parameters are reassembled in
1674 // memory, and are not truncated.
1675 if (State->getCallOrPrologue() == Call) {
1676 unsigned excess = 4 * (ARM::R4 - reg);
1677 assert(size >= excess && "expected larger existing stack allocation");
1678 size -= excess;
1679 }
1680 }
1681 // Confiscate any remaining parameter registers to preclude their
1682 // assignment to subsequent parameters.
1683 while (State->AllocateReg(GPRArgRegs, 4))
1684 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001685}
1686
Dale Johannesen51e28e62010-06-03 21:09:53 +00001687/// MatchingStackOffset - Return true if the given stack call argument is
1688/// already available in the same position (relatively) of the caller's
1689/// incoming argument stack.
1690static
1691bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1692 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topperacf20772012-03-25 23:49:58 +00001693 const TargetInstrInfo *TII) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001694 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1695 int FI = INT_MAX;
1696 if (Arg.getOpcode() == ISD::CopyFromReg) {
1697 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001698 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001699 return false;
1700 MachineInstr *Def = MRI->getVRegDef(VR);
1701 if (!Def)
1702 return false;
1703 if (!Flags.isByVal()) {
1704 if (!TII->isLoadFromStackSlot(Def, FI))
1705 return false;
1706 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001707 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001708 }
1709 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1710 if (Flags.isByVal())
1711 // ByVal argument is passed in as a pointer but it's now being
1712 // dereferenced. e.g.
1713 // define @foo(%struct.X* %A) {
1714 // tail call @bar(%struct.X* byval %A)
1715 // }
1716 return false;
1717 SDValue Ptr = Ld->getBasePtr();
1718 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1719 if (!FINode)
1720 return false;
1721 FI = FINode->getIndex();
1722 } else
1723 return false;
1724
1725 assert(FI != INT_MAX);
1726 if (!MFI->isFixedObjectIndex(FI))
1727 return false;
1728 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1729}
1730
1731/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1732/// for tail call optimization. Targets which want to do tail call
1733/// optimization should implement this function.
1734bool
1735ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1736 CallingConv::ID CalleeCC,
1737 bool isVarArg,
1738 bool isCalleeStructRet,
1739 bool isCallerStructRet,
1740 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001741 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001742 const SmallVectorImpl<ISD::InputArg> &Ins,
1743 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001744 const Function *CallerF = DAG.getMachineFunction().getFunction();
1745 CallingConv::ID CallerCC = CallerF->getCallingConv();
1746 bool CCMatch = CallerCC == CalleeCC;
1747
1748 // Look for obvious safe cases to perform tail call optimization that do not
1749 // require ABI changes. This is what gcc calls sibcall.
1750
Jim Grosbach7616b642010-06-16 23:45:49 +00001751 // Do not sibcall optimize vararg calls unless the call site is not passing
1752 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001753 if (isVarArg && !Outs.empty())
1754 return false;
1755
1756 // Also avoid sibcall optimization if either caller or callee uses struct
1757 // return semantics.
1758 if (isCalleeStructRet || isCallerStructRet)
1759 return false;
1760
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001761 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001762 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1763 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1764 // support in the assembler and linker to be used. This would need to be
1765 // fixed to fully support tail calls in Thumb1.
1766 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001767 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1768 // LR. This means if we need to reload LR, it takes an extra instructions,
1769 // which outweighs the value of the tail call; but here we don't know yet
1770 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001771 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001772 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001773
1774 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1775 // but we need to make sure there are enough registers; the only valid
1776 // registers are the 4 used for parameters. We don't currently do this
1777 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001778 if (Subtarget->isThumb1Only())
1779 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001780
Dale Johannesen51e28e62010-06-03 21:09:53 +00001781 // If the calling conventions do not match, then we'd better make sure the
1782 // results are returned in the same way as what the caller expects.
1783 if (!CCMatch) {
1784 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001785 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1786 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001787 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1788
1789 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001790 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1791 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001792 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1793
1794 if (RVLocs1.size() != RVLocs2.size())
1795 return false;
1796 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1797 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1798 return false;
1799 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1800 return false;
1801 if (RVLocs1[i].isRegLoc()) {
1802 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1803 return false;
1804 } else {
1805 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1806 return false;
1807 }
1808 }
1809 }
1810
1811 // If the callee takes no arguments then go on to check the results of the
1812 // call.
1813 if (!Outs.empty()) {
1814 // Check if stack adjustment is needed. For now, do not do this if any
1815 // argument is passed on the stack.
1816 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001817 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1818 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001819 CCInfo.AnalyzeCallOperands(Outs,
1820 CCAssignFnForNode(CalleeCC, false, isVarArg));
1821 if (CCInfo.getNextStackOffset()) {
1822 MachineFunction &MF = DAG.getMachineFunction();
1823
1824 // Check if the arguments are already laid out in the right way as
1825 // the caller's fixed stack objects.
1826 MachineFrameInfo *MFI = MF.getFrameInfo();
1827 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Craig Topperacf20772012-03-25 23:49:58 +00001828 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001829 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1830 i != e;
1831 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001832 CCValAssign &VA = ArgLocs[i];
1833 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001834 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001835 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001836 if (VA.getLocInfo() == CCValAssign::Indirect)
1837 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001838 if (VA.needsCustom()) {
1839 // f64 and vector types are split into multiple registers or
1840 // register/stack-slot combinations. The types will not match
1841 // the registers; give up on memory f64 refs until we figure
1842 // out what to do about this.
1843 if (!VA.isRegLoc())
1844 return false;
1845 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001846 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001847 if (RegVT == MVT::v2f64) {
1848 if (!ArgLocs[++i].isRegLoc())
1849 return false;
1850 if (!ArgLocs[++i].isRegLoc())
1851 return false;
1852 }
1853 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001854 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1855 MFI, MRI, TII))
1856 return false;
1857 }
1858 }
1859 }
1860 }
1861
1862 return true;
1863}
1864
Dan Gohman98ca4f22009-08-05 01:29:28 +00001865SDValue
1866ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001867 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001868 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001869 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001870 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001871
Bob Wilsondee46d72009-04-17 20:35:10 +00001872 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001873 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001874
Bob Wilsondee46d72009-04-17 20:35:10 +00001875 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001876 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1877 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001878
Dan Gohman98ca4f22009-08-05 01:29:28 +00001879 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001880 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1881 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001882
1883 // If this is the first return lowered for this function, add
1884 // the regs to the liveout set for the function.
1885 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1886 for (unsigned i = 0; i != RVLocs.size(); ++i)
1887 if (RVLocs[i].isRegLoc())
1888 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001889 }
1890
Bob Wilson1f595bb2009-04-17 19:07:39 +00001891 SDValue Flag;
1892
1893 // Copy the result values into the output registers.
1894 for (unsigned i = 0, realRVLocIdx = 0;
1895 i != RVLocs.size();
1896 ++i, ++realRVLocIdx) {
1897 CCValAssign &VA = RVLocs[i];
1898 assert(VA.isRegLoc() && "Can only return in registers!");
1899
Dan Gohmanc9403652010-07-07 15:54:55 +00001900 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001901
1902 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001903 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001904 case CCValAssign::Full: break;
1905 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001906 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001907 break;
1908 }
1909
Bob Wilson1f595bb2009-04-17 19:07:39 +00001910 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001911 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001912 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001913 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1914 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001915 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001916 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001917
1918 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1919 Flag = Chain.getValue(1);
1920 VA = RVLocs[++i]; // skip ahead to next loc
1921 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1922 HalfGPRs.getValue(1), Flag);
1923 Flag = Chain.getValue(1);
1924 VA = RVLocs[++i]; // skip ahead to next loc
1925
1926 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001927 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1928 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001929 }
1930 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1931 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001932 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001933 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001934 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001935 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001936 VA = RVLocs[++i]; // skip ahead to next loc
1937 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1938 Flag);
1939 } else
1940 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1941
Bob Wilsondee46d72009-04-17 20:35:10 +00001942 // Guarantee that all emitted copies are
1943 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001944 Flag = Chain.getValue(1);
1945 }
1946
1947 SDValue result;
1948 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001949 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001950 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001951 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001952
1953 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001954}
1955
Evan Chengbf010eb2012-04-10 01:51:00 +00001956bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001957 if (N->getNumValues() != 1)
1958 return false;
1959 if (!N->hasNUsesOfValue(1, 0))
1960 return false;
1961
Evan Chengbf010eb2012-04-10 01:51:00 +00001962 SDValue TCChain = Chain;
1963 SDNode *Copy = *N->use_begin();
1964 if (Copy->getOpcode() == ISD::CopyToReg) {
1965 // If the copy has a glue operand, we conservatively assume it isn't safe to
1966 // perform a tail call.
1967 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1968 return false;
1969 TCChain = Copy->getOperand(0);
1970 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
1971 SDNode *VMov = Copy;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001972 // f64 returned in a pair of GPRs.
Evan Chengbf010eb2012-04-10 01:51:00 +00001973 SmallPtrSet<SDNode*, 2> Copies;
1974 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Cheng3d2125c2010-11-30 23:55:39 +00001975 UI != UE; ++UI) {
1976 if (UI->getOpcode() != ISD::CopyToReg)
1977 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001978 Copies.insert(*UI);
Evan Cheng3d2125c2010-11-30 23:55:39 +00001979 }
Evan Chengbf010eb2012-04-10 01:51:00 +00001980 if (Copies.size() > 2)
1981 return false;
1982
1983 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
1984 UI != UE; ++UI) {
1985 SDValue UseChain = UI->getOperand(0);
1986 if (Copies.count(UseChain.getNode()))
1987 // Second CopyToReg
1988 Copy = *UI;
1989 else
1990 // First CopyToReg
1991 TCChain = UseChain;
1992 }
1993 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001994 // f32 returned in a single GPR.
Evan Chengbf010eb2012-04-10 01:51:00 +00001995 if (!Copy->hasOneUse())
Evan Cheng3d2125c2010-11-30 23:55:39 +00001996 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001997 Copy = *Copy->use_begin();
1998 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Cheng3d2125c2010-11-30 23:55:39 +00001999 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002000 Chain = Copy->getOperand(0);
Evan Cheng3d2125c2010-11-30 23:55:39 +00002001 } else {
2002 return false;
2003 }
2004
Evan Cheng1bf891a2010-12-01 22:59:46 +00002005 bool HasRet = false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002006 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2007 UI != UE; ++UI) {
2008 if (UI->getOpcode() != ARMISD::RET_FLAG)
2009 return false;
2010 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002011 }
2012
Evan Chengbf010eb2012-04-10 01:51:00 +00002013 if (!HasRet)
2014 return false;
2015
2016 Chain = TCChain;
2017 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002018}
2019
Evan Cheng485fafc2011-03-21 01:19:09 +00002020bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Evan Cheng1c80f562012-03-30 01:24:39 +00002021 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Evan Cheng485fafc2011-03-21 01:19:09 +00002022 return false;
2023
2024 if (!CI->isTailCall())
2025 return false;
2026
2027 return !Subtarget->isThumb1Only();
2028}
2029
Bob Wilsonb62d2572009-11-03 00:02:05 +00002030// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2031// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2032// one of the above mentioned nodes. It has to be wrapped because otherwise
2033// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2034// be used to form addressing mode. These wrapped nodes will be selected
2035// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00002036static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002037 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002038 // FIXME there is no actual debug info here
2039 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002040 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00002041 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00002042 if (CP->isMachineConstantPoolEntry())
2043 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2044 CP->getAlignment());
2045 else
2046 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2047 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00002048 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00002049}
2050
Jim Grosbache1102ca2010-07-19 17:20:38 +00002051unsigned ARMTargetLowering::getJumpTableEncoding() const {
2052 return MachineJumpTableInfo::EK_Inline;
2053}
2054
Dan Gohmand858e902010-04-17 15:26:15 +00002055SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2056 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00002057 MachineFunction &MF = DAG.getMachineFunction();
2058 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2059 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00002060 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00002061 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00002062 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00002063 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2064 SDValue CPAddr;
2065 if (RelocM == Reloc::Static) {
2066 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2067 } else {
2068 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002069 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00002070 ARMConstantPoolValue *CPV =
2071 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2072 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson907eebd2009-11-02 20:59:23 +00002073 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2074 }
2075 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2076 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002077 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002078 false, false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00002079 if (RelocM == Reloc::Static)
2080 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00002081 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00002082 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00002083}
2084
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002085// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00002086SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002087ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002088 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002089 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002090 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002091 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00002092 MachineFunction &MF = DAG.getMachineFunction();
2093 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002094 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002095 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002096 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2097 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002098 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002099 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00002100 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002101 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002102 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002103 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002104
Evan Chenge7e0d622009-11-06 22:24:13 +00002105 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002106 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002107
2108 // call __tls_get_addr.
2109 ArgListTy Args;
2110 ArgListEntry Entry;
2111 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002112 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002113 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002114 // FIXME: is there useful debug info available here?
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002115 TargetLowering::CallLoweringInfo CLI(Chain,
2116 (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00002117 false, false, false, false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00002118 0, CallingConv::C, /*isTailCall=*/false,
2119 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002120 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002121 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002122 return CallResult.first;
2123}
2124
2125// Lower ISD::GlobalTLSAddress using the "initial exec" or
2126// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00002127SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002128ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002129 SelectionDAG &DAG,
2130 TLSModel::Model model) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002131 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002132 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002133 SDValue Offset;
2134 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002135 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002136 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002137 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002138
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002139 if (model == TLSModel::InitialExec) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002140 MachineFunction &MF = DAG.getMachineFunction();
2141 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002142 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002143 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002144 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2145 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002146 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2147 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2148 true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002149 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002150 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002151 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002152 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002153 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002154 Chain = Offset.getValue(1);
2155
Evan Chenge7e0d622009-11-06 22:24:13 +00002156 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002157 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002158
Evan Cheng9eda6892009-10-31 03:39:36 +00002159 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002160 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002161 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002162 } else {
2163 // local exec model
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002164 assert(model == TLSModel::LocalExec);
Bill Wendling5bb77992011-10-01 08:00:54 +00002165 ARMConstantPoolValue *CPV =
2166 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002167 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002168 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002169 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002170 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002171 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002172 }
2173
2174 // The address of the thread local variable is the add of the thread
2175 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002176 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002177}
2178
Dan Gohman475871a2008-07-27 21:46:04 +00002179SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002180ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002181 // TODO: implement the "local dynamic" model
2182 assert(Subtarget->isTargetELF() &&
2183 "TLS not implemented for non-ELF targets");
2184 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002185
2186 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2187
2188 switch (model) {
2189 case TLSModel::GeneralDynamic:
2190 case TLSModel::LocalDynamic:
2191 return LowerToTLSGeneralDynamicModel(GA, DAG);
2192 case TLSModel::InitialExec:
2193 case TLSModel::LocalExec:
2194 return LowerToTLSExecModels(GA, DAG, model);
2195 }
Matt Beaumont-Gay39af9442012-05-04 18:34:27 +00002196 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002197}
2198
Dan Gohman475871a2008-07-27 21:46:04 +00002199SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002200 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002201 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002202 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002203 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002204 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2205 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002206 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002207 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002208 ARMConstantPoolConstant::Create(GV,
2209 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002210 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002211 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002212 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002213 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002214 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002215 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002216 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002217 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002218 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002219 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002220 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002221 MachinePointerInfo::getGOT(),
2222 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002223 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002224 }
2225
2226 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloy015cca62011-10-26 08:53:19 +00002227 // pair. This is always cheaper.
2228 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002229 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002230 // FIXME: Once remat is capable of dealing with instructions with register
2231 // operands, expand this into two nodes.
2232 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2233 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002234 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002235 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2236 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2237 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2238 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002239 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002240 }
2241}
2242
Dan Gohman475871a2008-07-27 21:46:04 +00002243SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002244 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002245 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002246 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002247 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002248 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002249 MachineFunction &MF = DAG.getMachineFunction();
2250 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2251
Jakob Stoklund Olesen8f37a242012-01-07 20:49:15 +00002252 // FIXME: Enable this for static codegen when tool issues are fixed. Also
2253 // update ARMFastISel::ARMMaterializeGV.
Evan Chengf31151f2011-10-26 01:17:44 +00002254 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002255 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002256 // FIXME: Once remat is capable of dealing with instructions with register
2257 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002258 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002259 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2260 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2261
Evan Cheng53519f02011-01-21 18:55:51 +00002262 unsigned Wrapper = (RelocM == Reloc::PIC_)
2263 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2264 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002265 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002266 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2267 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002268 MachinePointerInfo::getGOT(),
2269 false, false, false, 0);
Evan Chengfc8475b2011-01-19 02:16:49 +00002270 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002271 }
2272
2273 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002274 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002275 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002276 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002277 } else {
2278 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002279 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2280 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002281 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue,
2282 PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002283 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002284 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002285 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002286
Evan Cheng9eda6892009-10-31 03:39:36 +00002287 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002288 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002289 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002290 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002291
2292 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002293 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002294 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002295 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002296
Evan Cheng63476a82009-09-03 07:04:02 +00002297 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002298 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002299 false, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002300
2301 return Result;
2302}
2303
Dan Gohman475871a2008-07-27 21:46:04 +00002304SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002305 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002306 assert(Subtarget->isTargetELF() &&
2307 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002308 MachineFunction &MF = DAG.getMachineFunction();
2309 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002310 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002311 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002312 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002313 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingfe31e672011-10-01 08:58:29 +00002314 ARMConstantPoolValue *CPV =
2315 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2316 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002317 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002318 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002319 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002320 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002321 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002322 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002323 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002324}
2325
Jim Grosbach0e0da732009-05-12 23:59:14 +00002326SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002327ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2328 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002329 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendlingce370cf2011-10-07 21:25:38 +00002330 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2331 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002332 Op.getOperand(1), Val);
2333}
2334
2335SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002336ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2337 DebugLoc dl = Op.getDebugLoc();
2338 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2339 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2340}
2341
2342SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002343ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002344 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002345 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002346 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002347 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002348 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002349 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002350 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002351 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2352 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002353 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002354 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002355 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002356 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002357 EVT PtrVT = getPointerTy();
2358 DebugLoc dl = Op.getDebugLoc();
2359 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2360 SDValue CPAddr;
2361 unsigned PCAdj = (RelocM != Reloc::PIC_)
2362 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002363 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002364 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2365 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002366 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002367 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002368 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002369 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002370 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002371 false, false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002372
2373 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002374 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002375 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2376 }
2377 return Result;
2378 }
Evan Cheng92e39162011-03-29 23:06:19 +00002379 case Intrinsic::arm_neon_vmulls:
2380 case Intrinsic::arm_neon_vmullu: {
2381 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2382 ? ARMISD::VMULLs : ARMISD::VMULLu;
2383 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2384 Op.getOperand(1), Op.getOperand(2));
2385 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002386 }
2387}
2388
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002389static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002390 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002391 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002392 if (!Subtarget->hasDataBarrier()) {
2393 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2394 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2395 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002396 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002397 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002398 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002399 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002400 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002401
2402 SDValue Op5 = Op.getOperand(5);
2403 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2404 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2405 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2406 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2407
2408 ARM_MB::MemBOpt DMBOpt;
2409 if (isDeviceBarrier)
2410 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2411 else
2412 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2413 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2414 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002415}
2416
Eli Friedman26689ac2011-08-03 21:06:02 +00002417
2418static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2419 const ARMSubtarget *Subtarget) {
2420 // FIXME: handle "fence singlethread" more efficiently.
2421 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002422 if (!Subtarget->hasDataBarrier()) {
2423 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2424 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2425 // here.
2426 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2427 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002428 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002429 DAG.getConstant(0, MVT::i32));
2430 }
2431
Eli Friedman26689ac2011-08-03 21:06:02 +00002432 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002433 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002434}
2435
Evan Chengdfed19f2010-11-03 06:34:55 +00002436static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2437 const ARMSubtarget *Subtarget) {
2438 // ARM pre v5TE and Thumb1 does not have preload instructions.
2439 if (!(Subtarget->isThumb2() ||
2440 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2441 // Just preserve the chain.
2442 return Op.getOperand(0);
2443
2444 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002445 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2446 if (!isRead &&
2447 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2448 // ARMv7 with MP extension has PLDW.
2449 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002450
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002451 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2452 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002453 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002454 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002455 isData = ~isData & 1;
2456 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002457
2458 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002459 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2460 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002461}
2462
Dan Gohman1e93df62010-04-17 14:41:14 +00002463static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2464 MachineFunction &MF = DAG.getMachineFunction();
2465 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2466
Evan Chenga8e29892007-01-19 07:51:42 +00002467 // vastart just stores the address of the VarArgsFrameIndex slot into the
2468 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002469 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002470 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002471 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002472 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002473 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2474 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002475}
2476
Dan Gohman475871a2008-07-27 21:46:04 +00002477SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002478ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2479 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002480 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002481 MachineFunction &MF = DAG.getMachineFunction();
2482 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2483
Craig Topper44d23822012-02-22 05:59:10 +00002484 const TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002485 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002486 RC = &ARM::tGPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002487 else
Craig Topper420761a2012-04-20 07:30:17 +00002488 RC = &ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002489
2490 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002491 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002492 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002493
2494 SDValue ArgValue2;
2495 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002496 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002497 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002498
2499 // Create load node to retrieve arguments from the stack.
2500 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002501 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002502 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002503 false, false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002504 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002505 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002506 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002507 }
2508
Jim Grosbache5165492009-11-09 00:11:35 +00002509 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002510}
2511
Stuart Hastingsc7315872011-04-20 16:47:52 +00002512void
2513ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2514 unsigned &VARegSize, unsigned &VARegSaveSize)
2515 const {
2516 unsigned NumGPRs;
2517 if (CCInfo.isFirstByValRegValid())
2518 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2519 else {
2520 unsigned int firstUnalloced;
2521 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2522 sizeof(GPRArgRegs) /
2523 sizeof(GPRArgRegs[0]));
2524 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2525 }
2526
2527 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2528 VARegSize = NumGPRs * 4;
2529 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2530}
2531
2532// The remaining GPRs hold either the beginning of variable-argument
2533// data, or the beginning of an aggregate passed by value (usuall
2534// byval). Either way, we allocate stack slots adjacent to the data
2535// provided by our caller, and store the unallocated registers there.
2536// If this is a variadic function, the va_list pointer will begin with
2537// these values; otherwise, this reassembles a (byval) structure that
2538// was split between registers and memory.
2539void
2540ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2541 DebugLoc dl, SDValue &Chain,
2542 unsigned ArgOffset) const {
2543 MachineFunction &MF = DAG.getMachineFunction();
2544 MachineFrameInfo *MFI = MF.getFrameInfo();
2545 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2546 unsigned firstRegToSaveIndex;
2547 if (CCInfo.isFirstByValRegValid())
2548 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2549 else {
2550 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2551 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2552 }
2553
2554 unsigned VARegSize, VARegSaveSize;
2555 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2556 if (VARegSaveSize) {
2557 // If this function is vararg, store any remaining integer argument regs
2558 // to their spots on the stack so that they may be loaded by deferencing
2559 // the result of va_next.
2560 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002561 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2562 ArgOffset + VARegSaveSize
2563 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002564 false));
2565 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2566 getPointerTy());
2567
2568 SmallVector<SDValue, 4> MemOps;
2569 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
Craig Topper44d23822012-02-22 05:59:10 +00002570 const TargetRegisterClass *RC;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002571 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002572 RC = &ARM::tGPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002573 else
Craig Topper420761a2012-04-20 07:30:17 +00002574 RC = &ARM::GPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002575
2576 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2577 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2578 SDValue Store =
2579 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002580 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002581 false, false, 0);
2582 MemOps.push_back(Store);
2583 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2584 DAG.getConstant(4, getPointerTy()));
2585 }
2586 if (!MemOps.empty())
2587 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2588 &MemOps[0], MemOps.size());
2589 } else
2590 // This will point to the next argument passed via stack.
2591 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2592}
2593
Bob Wilson5bafff32009-06-22 23:27:02 +00002594SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002595ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002596 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002597 const SmallVectorImpl<ISD::InputArg>
2598 &Ins,
2599 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002600 SmallVectorImpl<SDValue> &InVals)
2601 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002602 MachineFunction &MF = DAG.getMachineFunction();
2603 MachineFrameInfo *MFI = MF.getFrameInfo();
2604
Bob Wilson1f595bb2009-04-17 19:07:39 +00002605 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2606
2607 // Assign locations to all of the incoming arguments.
2608 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002609 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2610 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002611 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002612 CCAssignFnForNode(CallConv, /* Return*/ false,
2613 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002614
2615 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002616 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002617
Stuart Hastingsf222e592011-02-28 17:17:53 +00002618 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002619 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2620 CCValAssign &VA = ArgLocs[i];
2621
Bob Wilsondee46d72009-04-17 20:35:10 +00002622 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002623 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002624 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002625
Bob Wilson1f595bb2009-04-17 19:07:39 +00002626 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002627 // f64 and vector types are split up into multiple registers or
2628 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002629 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002630 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002631 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002632 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002633 SDValue ArgValue2;
2634 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002635 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002636 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2637 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002638 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002639 false, false, false, 0);
Bob Wilson6a234f02010-04-13 22:03:22 +00002640 } else {
2641 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2642 Chain, DAG, dl);
2643 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002644 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2645 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002646 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002647 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002648 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2649 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002650 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002651
Bob Wilson5bafff32009-06-22 23:27:02 +00002652 } else {
Craig Topper44d23822012-02-22 05:59:10 +00002653 const TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002654
Owen Anderson825b72b2009-08-11 20:47:22 +00002655 if (RegVT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002656 RC = &ARM::SPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002657 else if (RegVT == MVT::f64)
Craig Topper420761a2012-04-20 07:30:17 +00002658 RC = &ARM::DPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002659 else if (RegVT == MVT::v2f64)
Craig Topper420761a2012-04-20 07:30:17 +00002660 RC = &ARM::QPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002661 else if (RegVT == MVT::i32)
Craig Topper420761a2012-04-20 07:30:17 +00002662 RC = AFI->isThumb1OnlyFunction() ?
2663 (const TargetRegisterClass*)&ARM::tGPRRegClass :
2664 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002665 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002666 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002667
2668 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002669 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002670 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002671 }
2672
2673 // If this is an 8 or 16-bit value, it is really passed promoted
2674 // to 32 bits. Insert an assert[sz]ext to capture this, then
2675 // truncate to the right size.
2676 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002677 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002678 case CCValAssign::Full: break;
2679 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002680 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002681 break;
2682 case CCValAssign::SExt:
2683 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2684 DAG.getValueType(VA.getValVT()));
2685 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2686 break;
2687 case CCValAssign::ZExt:
2688 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2689 DAG.getValueType(VA.getValVT()));
2690 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2691 break;
2692 }
2693
Dan Gohman98ca4f22009-08-05 01:29:28 +00002694 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002695
2696 } else { // VA.isRegLoc()
2697
2698 // sanity check
2699 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002700 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002701
Stuart Hastingsf222e592011-02-28 17:17:53 +00002702 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002703
Stuart Hastingsf222e592011-02-28 17:17:53 +00002704 // Some Ins[] entries become multiple ArgLoc[] entries.
2705 // Process them only once.
2706 if (index != lastInsIndex)
2707 {
2708 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002709 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002710 // This can be changed with more analysis.
2711 // In case of tail call optimization mark all arguments mutable.
2712 // Since they could be overwritten by lowering of arguments in case of
2713 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002714 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002715 unsigned VARegSize, VARegSaveSize;
2716 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2717 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2718 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002719 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002720 int FI = MFI->CreateFixedObject(Bytes,
2721 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002722 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2723 } else {
2724 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2725 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002726
Stuart Hastingsf222e592011-02-28 17:17:53 +00002727 // Create load nodes to retrieve arguments from the stack.
2728 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2729 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2730 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002731 false, false, false, 0));
Stuart Hastingsf222e592011-02-28 17:17:53 +00002732 }
2733 lastInsIndex = index;
2734 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002735 }
2736 }
2737
2738 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002739 if (isVarArg)
2740 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002741
Dan Gohman98ca4f22009-08-05 01:29:28 +00002742 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002743}
2744
2745/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002746static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002747 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002748 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002749 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002750 // Maybe this has already been legalized into the constant pool?
2751 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002752 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002753 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002754 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002755 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002756 }
2757 }
2758 return false;
2759}
2760
Evan Chenga8e29892007-01-19 07:51:42 +00002761/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2762/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002763SDValue
2764ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002765 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002766 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002767 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002768 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002769 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002770 // Constant does not fit, try adjusting it by one?
2771 switch (CC) {
2772 default: break;
2773 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002774 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002775 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002776 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002777 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002778 }
2779 break;
2780 case ISD::SETULT:
2781 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002782 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002783 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002784 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002785 }
2786 break;
2787 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002788 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002789 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002790 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002791 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002792 }
2793 break;
2794 case ISD::SETULE:
2795 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002796 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002797 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002798 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002799 }
2800 break;
2801 }
2802 }
2803 }
2804
2805 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002806 ARMISD::NodeType CompareType;
2807 switch (CondCode) {
2808 default:
2809 CompareType = ARMISD::CMP;
2810 break;
2811 case ARMCC::EQ:
2812 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002813 // Uses only Z Flag
2814 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002815 break;
2816 }
Evan Cheng218977b2010-07-13 19:27:42 +00002817 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002818 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002819}
2820
2821/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002822SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002823ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002824 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002825 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002826 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002827 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002828 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002829 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2830 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002831}
2832
Bob Wilson79f56c92011-03-08 01:17:20 +00002833/// duplicateCmp - Glue values can have only one use, so this function
2834/// duplicates a comparison node.
2835SDValue
2836ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2837 unsigned Opc = Cmp.getOpcode();
2838 DebugLoc DL = Cmp.getDebugLoc();
2839 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2840 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2841
2842 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2843 Cmp = Cmp.getOperand(0);
2844 Opc = Cmp.getOpcode();
2845 if (Opc == ARMISD::CMPFP)
2846 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2847 else {
2848 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2849 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2850 }
2851 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2852}
2853
Bill Wendlingde2b1512010-08-11 08:43:16 +00002854SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2855 SDValue Cond = Op.getOperand(0);
2856 SDValue SelectTrue = Op.getOperand(1);
2857 SDValue SelectFalse = Op.getOperand(2);
2858 DebugLoc dl = Op.getDebugLoc();
2859
2860 // Convert:
2861 //
2862 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2863 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2864 //
2865 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2866 const ConstantSDNode *CMOVTrue =
2867 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2868 const ConstantSDNode *CMOVFalse =
2869 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2870
2871 if (CMOVTrue && CMOVFalse) {
2872 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2873 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2874
2875 SDValue True;
2876 SDValue False;
2877 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2878 True = SelectTrue;
2879 False = SelectFalse;
2880 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2881 True = SelectFalse;
2882 False = SelectTrue;
2883 }
2884
2885 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002886 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002887 SDValue ARMcc = Cond.getOperand(2);
2888 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002889 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002890 assert(True.getValueType() == VT);
2891 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002892 }
2893 }
2894 }
2895
Dan Gohmandb953892012-02-24 00:09:36 +00002896 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
2897 // undefined bits before doing a full-word comparison with zero.
2898 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
2899 DAG.getConstant(1, Cond.getValueType()));
2900
Bill Wendlingde2b1512010-08-11 08:43:16 +00002901 return DAG.getSelectCC(dl, Cond,
2902 DAG.getConstant(0, Cond.getValueType()),
2903 SelectTrue, SelectFalse, ISD::SETNE);
2904}
2905
Dan Gohmand858e902010-04-17 15:26:15 +00002906SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002907 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002908 SDValue LHS = Op.getOperand(0);
2909 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002910 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002911 SDValue TrueVal = Op.getOperand(2);
2912 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002913 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002914
Owen Anderson825b72b2009-08-11 20:47:22 +00002915 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002916 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002917 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002918 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Jim Grosbachb04546f2011-09-13 20:30:37 +00002919 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002920 }
2921
2922 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002923 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002924
Evan Cheng218977b2010-07-13 19:27:42 +00002925 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2926 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002927 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002928 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002929 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002930 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002931 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002932 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002933 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002934 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002935 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002936 }
2937 return Result;
2938}
2939
Evan Cheng218977b2010-07-13 19:27:42 +00002940/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2941/// to morph to an integer compare sequence.
2942static bool canChangeToInt(SDValue Op, bool &SeenZero,
2943 const ARMSubtarget *Subtarget) {
2944 SDNode *N = Op.getNode();
2945 if (!N->hasOneUse())
2946 // Otherwise it requires moving the value from fp to integer registers.
2947 return false;
2948 if (!N->getNumValues())
2949 return false;
2950 EVT VT = Op.getValueType();
2951 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2952 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2953 // vmrs are very slow, e.g. cortex-a8.
2954 return false;
2955
2956 if (isFloatingPointZero(Op)) {
2957 SeenZero = true;
2958 return true;
2959 }
2960 return ISD::isNormalLoad(N);
2961}
2962
2963static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2964 if (isFloatingPointZero(Op))
2965 return DAG.getConstant(0, MVT::i32);
2966
2967 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2968 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002969 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002970 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002971 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002972
2973 llvm_unreachable("Unknown VFP cmp argument!");
2974}
2975
2976static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2977 SDValue &RetVal1, SDValue &RetVal2) {
2978 if (isFloatingPointZero(Op)) {
2979 RetVal1 = DAG.getConstant(0, MVT::i32);
2980 RetVal2 = DAG.getConstant(0, MVT::i32);
2981 return;
2982 }
2983
2984 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2985 SDValue Ptr = Ld->getBasePtr();
2986 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2987 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002988 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002989 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002990 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002991
2992 EVT PtrType = Ptr.getValueType();
2993 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2994 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2995 PtrType, Ptr, DAG.getConstant(4, PtrType));
2996 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2997 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002998 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002999 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003000 Ld->isInvariant(), NewAlign);
Evan Cheng218977b2010-07-13 19:27:42 +00003001 return;
3002 }
3003
3004 llvm_unreachable("Unknown VFP cmp argument!");
3005}
3006
3007/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3008/// f32 and even f64 comparisons to integer ones.
3009SDValue
3010ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3011 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00003012 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00003013 SDValue LHS = Op.getOperand(2);
3014 SDValue RHS = Op.getOperand(3);
3015 SDValue Dest = Op.getOperand(4);
3016 DebugLoc dl = Op.getDebugLoc();
3017
Evan Chengfc501a32012-03-01 23:27:13 +00003018 bool LHSSeenZero = false;
3019 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3020 bool RHSSeenZero = false;
3021 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3022 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson1b772f92011-03-08 01:17:16 +00003023 // If unsafe fp math optimization is enabled and there are no other uses of
3024 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00003025 // to an integer comparison.
3026 if (CC == ISD::SETOEQ)
3027 CC = ISD::SETEQ;
3028 else if (CC == ISD::SETUNE)
3029 CC = ISD::SETNE;
3030
Evan Chengfc501a32012-03-01 23:27:13 +00003031 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00003032 SDValue ARMcc;
3033 if (LHS.getValueType() == MVT::f32) {
Evan Chengfc501a32012-03-01 23:27:13 +00003034 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3035 bitcastf32Toi32(LHS, DAG), Mask);
3036 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3037 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003038 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3039 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3040 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3041 Chain, Dest, ARMcc, CCR, Cmp);
3042 }
3043
3044 SDValue LHS1, LHS2;
3045 SDValue RHS1, RHS2;
3046 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3047 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengfc501a32012-03-01 23:27:13 +00003048 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3049 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003050 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3051 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003052 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003053 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
3054 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
3055 }
3056
3057 return SDValue();
3058}
3059
3060SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3061 SDValue Chain = Op.getOperand(0);
3062 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3063 SDValue LHS = Op.getOperand(2);
3064 SDValue RHS = Op.getOperand(3);
3065 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00003066 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003067
Owen Anderson825b72b2009-08-11 20:47:22 +00003068 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00003069 SDValue ARMcc;
3070 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003071 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00003072 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00003073 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00003074 }
3075
Owen Anderson825b72b2009-08-11 20:47:22 +00003076 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00003077
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003078 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng218977b2010-07-13 19:27:42 +00003079 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3080 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3081 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3082 if (Result.getNode())
3083 return Result;
3084 }
3085
Evan Chenga8e29892007-01-19 07:51:42 +00003086 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00003087 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003088
Evan Cheng218977b2010-07-13 19:27:42 +00003089 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3090 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003091 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003092 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003093 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00003094 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003095 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00003096 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3097 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00003098 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003099 }
3100 return Res;
3101}
3102
Dan Gohmand858e902010-04-17 15:26:15 +00003103SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00003104 SDValue Chain = Op.getOperand(0);
3105 SDValue Table = Op.getOperand(1);
3106 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003107 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003108
Owen Andersone50ed302009-08-10 22:56:29 +00003109 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00003110 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3111 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00003112 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00003113 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00003114 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00003115 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3116 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00003117 if (Subtarget->isThumb2()) {
3118 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3119 // which does another jump to the destination. This also makes it easier
3120 // to translate it to TBB / TBH later.
3121 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00003122 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00003123 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003124 }
Evan Cheng66ac5312009-07-25 00:33:29 +00003125 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00003126 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003127 MachinePointerInfo::getJumpTable(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003128 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003129 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003130 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00003131 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003132 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00003133 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003134 MachinePointerInfo::getJumpTable(),
3135 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003136 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003137 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003138 }
Evan Chenga8e29892007-01-19 07:51:42 +00003139}
3140
Eli Friedman14e809c2011-11-09 23:36:02 +00003141static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy873fd5f2012-02-20 09:24:05 +00003142 EVT VT = Op.getValueType();
3143 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14e809c2011-11-09 23:36:02 +00003144
James Molloy873fd5f2012-02-20 09:24:05 +00003145 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3146 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3147 return Op;
3148 return DAG.UnrollVectorOp(Op.getNode());
3149 }
3150
3151 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3152 "Invalid type for custom lowering!");
3153 if (VT != MVT::v4i16)
3154 return DAG.UnrollVectorOp(Op.getNode());
3155
3156 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3157 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman14e809c2011-11-09 23:36:02 +00003158}
3159
Bob Wilson76a312b2010-03-19 22:51:32 +00003160static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman14e809c2011-11-09 23:36:02 +00003161 EVT VT = Op.getValueType();
3162 if (VT.isVector())
3163 return LowerVectorFP_TO_INT(Op, DAG);
3164
Bob Wilson76a312b2010-03-19 22:51:32 +00003165 DebugLoc dl = Op.getDebugLoc();
3166 unsigned Opc;
3167
3168 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003169 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003170 case ISD::FP_TO_SINT:
3171 Opc = ARMISD::FTOSI;
3172 break;
3173 case ISD::FP_TO_UINT:
3174 Opc = ARMISD::FTOUI;
3175 break;
3176 }
3177 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003178 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003179}
3180
Cameron Zwarich3007d332011-03-29 21:41:55 +00003181static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3182 EVT VT = Op.getValueType();
3183 DebugLoc dl = Op.getDebugLoc();
3184
Eli Friedman14e809c2011-11-09 23:36:02 +00003185 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3186 if (VT.getVectorElementType() == MVT::f32)
3187 return Op;
3188 return DAG.UnrollVectorOp(Op.getNode());
3189 }
3190
Duncan Sands1f6a3292011-08-12 14:54:45 +00003191 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3192 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003193 if (VT != MVT::v4f32)
3194 return DAG.UnrollVectorOp(Op.getNode());
3195
3196 unsigned CastOpc;
3197 unsigned Opc;
3198 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003199 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003200 case ISD::SINT_TO_FP:
3201 CastOpc = ISD::SIGN_EXTEND;
3202 Opc = ISD::SINT_TO_FP;
3203 break;
3204 case ISD::UINT_TO_FP:
3205 CastOpc = ISD::ZERO_EXTEND;
3206 Opc = ISD::UINT_TO_FP;
3207 break;
3208 }
3209
3210 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3211 return DAG.getNode(Opc, dl, VT, Op);
3212}
3213
Bob Wilson76a312b2010-03-19 22:51:32 +00003214static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3215 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003216 if (VT.isVector())
3217 return LowerVectorINT_TO_FP(Op, DAG);
3218
Bob Wilson76a312b2010-03-19 22:51:32 +00003219 DebugLoc dl = Op.getDebugLoc();
3220 unsigned Opc;
3221
3222 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003223 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003224 case ISD::SINT_TO_FP:
3225 Opc = ARMISD::SITOF;
3226 break;
3227 case ISD::UINT_TO_FP:
3228 Opc = ARMISD::UITOF;
3229 break;
3230 }
3231
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003232 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003233 return DAG.getNode(Opc, dl, VT, Op);
3234}
3235
Evan Cheng515fe3a2010-07-08 02:08:50 +00003236SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003237 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003238 SDValue Tmp0 = Op.getOperand(0);
3239 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003240 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003241 EVT VT = Op.getValueType();
3242 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003243 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3244 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3245 bool UseNEON = !InGPR && Subtarget->hasNEON();
3246
3247 if (UseNEON) {
3248 // Use VBSL to copy the sign bit.
3249 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3250 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3251 DAG.getTargetConstant(EncodedVal, MVT::i32));
3252 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3253 if (VT == MVT::f64)
3254 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3255 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3256 DAG.getConstant(32, MVT::i32));
3257 else /*if (VT == MVT::f32)*/
3258 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3259 if (SrcVT == MVT::f32) {
3260 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3261 if (VT == MVT::f64)
3262 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3263 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3264 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003265 } else if (VT == MVT::f32)
3266 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3267 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3268 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003269 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3270 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3271
3272 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3273 MVT::i32);
3274 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3275 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3276 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003277
Evan Chenge573fb32011-02-23 02:24:55 +00003278 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3279 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3280 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003281 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003282 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3283 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3284 DAG.getConstant(0, MVT::i32));
3285 } else {
3286 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3287 }
3288
3289 return Res;
3290 }
Evan Chengc143dd42011-02-11 02:28:55 +00003291
3292 // Bitcast operand 1 to i32.
3293 if (SrcVT == MVT::f64)
3294 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3295 &Tmp1, 1).getValue(1);
3296 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3297
Evan Chenge573fb32011-02-23 02:24:55 +00003298 // Or in the signbit with integer operations.
3299 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3300 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3301 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3302 if (VT == MVT::f32) {
3303 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3304 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3305 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3306 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003307 }
3308
Evan Chenge573fb32011-02-23 02:24:55 +00003309 // f64: Or the high part with signbit and then combine two parts.
3310 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3311 &Tmp0, 1);
3312 SDValue Lo = Tmp0.getValue(0);
3313 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3314 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3315 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003316}
3317
Evan Cheng2457f2c2010-05-22 01:47:14 +00003318SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3319 MachineFunction &MF = DAG.getMachineFunction();
3320 MachineFrameInfo *MFI = MF.getFrameInfo();
3321 MFI->setReturnAddressIsTaken(true);
3322
3323 EVT VT = Op.getValueType();
3324 DebugLoc dl = Op.getDebugLoc();
3325 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3326 if (Depth) {
3327 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3328 SDValue Offset = DAG.getConstant(4, MVT::i32);
3329 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3330 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003331 MachinePointerInfo(), false, false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003332 }
3333
3334 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003335 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003336 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3337}
3338
Dan Gohmand858e902010-04-17 15:26:15 +00003339SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003340 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3341 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003342
Owen Andersone50ed302009-08-10 22:56:29 +00003343 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003344 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3345 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003346 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003347 ? ARM::R7 : ARM::R11;
3348 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3349 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003350 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3351 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003352 false, false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003353 return FrameAddr;
3354}
3355
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003356/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003357/// expand a bit convert where either the source or destination type is i64 to
3358/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3359/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3360/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003361static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003362 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3363 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003364 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003365
Bob Wilson9f3f0612010-04-17 05:30:19 +00003366 // This function is only supposed to be called for i64 types, either as the
3367 // source or destination of the bit convert.
3368 EVT SrcVT = Op.getValueType();
3369 EVT DstVT = N->getValueType(0);
3370 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003371 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003372
Bob Wilson9f3f0612010-04-17 05:30:19 +00003373 // Turn i64->f64 into VMOVDRR.
3374 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003375 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3376 DAG.getConstant(0, MVT::i32));
3377 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3378 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003379 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003380 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003381 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003382
Jim Grosbache5165492009-11-09 00:11:35 +00003383 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003384 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3385 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3386 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3387 // Merge the pieces into a single i64 value.
3388 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3389 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003390
Bob Wilson9f3f0612010-04-17 05:30:19 +00003391 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003392}
3393
Bob Wilson5bafff32009-06-22 23:27:02 +00003394/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003395/// Zero vectors are used to represent vector negation and in those cases
3396/// will be implemented with the NEON VNEG instruction. However, VNEG does
3397/// not support i64 elements, so sometimes the zero vectors will need to be
3398/// explicitly constructed. Regardless, use a canonical VMOV to create the
3399/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003400static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003401 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003402 // The canonical modified immediate encoding of a zero vector is....0!
3403 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3404 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3405 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003406 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003407}
3408
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003409/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3410/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003411SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3412 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003413 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3414 EVT VT = Op.getValueType();
3415 unsigned VTBits = VT.getSizeInBits();
3416 DebugLoc dl = Op.getDebugLoc();
3417 SDValue ShOpLo = Op.getOperand(0);
3418 SDValue ShOpHi = Op.getOperand(1);
3419 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003420 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003421 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003422
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003423 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3424
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003425 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3426 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3427 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3428 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3429 DAG.getConstant(VTBits, MVT::i32));
3430 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3431 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003432 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003433
3434 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3435 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003436 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003437 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003438 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003439 CCR, Cmp);
3440
3441 SDValue Ops[2] = { Lo, Hi };
3442 return DAG.getMergeValues(Ops, 2, dl);
3443}
3444
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003445/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3446/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003447SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3448 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003449 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3450 EVT VT = Op.getValueType();
3451 unsigned VTBits = VT.getSizeInBits();
3452 DebugLoc dl = Op.getDebugLoc();
3453 SDValue ShOpLo = Op.getOperand(0);
3454 SDValue ShOpHi = Op.getOperand(1);
3455 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003456 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003457
3458 assert(Op.getOpcode() == ISD::SHL_PARTS);
3459 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3460 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3461 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3462 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3463 DAG.getConstant(VTBits, MVT::i32));
3464 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3465 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3466
3467 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3468 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3469 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003470 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003471 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003472 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003473 CCR, Cmp);
3474
3475 SDValue Ops[2] = { Lo, Hi };
3476 return DAG.getMergeValues(Ops, 2, dl);
3477}
3478
Jim Grosbach4725ca72010-09-08 03:54:02 +00003479SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003480 SelectionDAG &DAG) const {
3481 // The rounding mode is in bits 23:22 of the FPSCR.
3482 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3483 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3484 // so that the shift + and get folded into a bitfield extract.
3485 DebugLoc dl = Op.getDebugLoc();
3486 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3487 DAG.getConstant(Intrinsic::arm_get_fpscr,
3488 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003489 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003490 DAG.getConstant(1U << 22, MVT::i32));
3491 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3492 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003493 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003494 DAG.getConstant(3, MVT::i32));
3495}
3496
Jim Grosbach3482c802010-01-18 19:58:49 +00003497static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3498 const ARMSubtarget *ST) {
3499 EVT VT = N->getValueType(0);
3500 DebugLoc dl = N->getDebugLoc();
3501
3502 if (!ST->hasV6T2Ops())
3503 return SDValue();
3504
3505 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3506 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3507}
3508
Bob Wilson5bafff32009-06-22 23:27:02 +00003509static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3510 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003511 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003512 DebugLoc dl = N->getDebugLoc();
3513
Bob Wilsond5448bb2010-11-18 21:16:28 +00003514 if (!VT.isVector())
3515 return SDValue();
3516
Bob Wilson5bafff32009-06-22 23:27:02 +00003517 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003518 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003519
Bob Wilsond5448bb2010-11-18 21:16:28 +00003520 // Left shifts translate directly to the vshiftu intrinsic.
3521 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003522 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003523 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3524 N->getOperand(0), N->getOperand(1));
3525
3526 assert((N->getOpcode() == ISD::SRA ||
3527 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3528
3529 // NEON uses the same intrinsics for both left and right shifts. For
3530 // right shifts, the shift amounts are negative, so negate the vector of
3531 // shift amounts.
3532 EVT ShiftVT = N->getOperand(1).getValueType();
3533 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3534 getZeroVector(ShiftVT, DAG, dl),
3535 N->getOperand(1));
3536 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3537 Intrinsic::arm_neon_vshifts :
3538 Intrinsic::arm_neon_vshiftu);
3539 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3540 DAG.getConstant(vshiftInt, MVT::i32),
3541 N->getOperand(0), NegatedCount);
3542}
3543
3544static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3545 const ARMSubtarget *ST) {
3546 EVT VT = N->getValueType(0);
3547 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003548
Eli Friedmance392eb2009-08-22 03:13:10 +00003549 // We can get here for a node like i32 = ISD::SHL i32, i64
3550 if (VT != MVT::i64)
3551 return SDValue();
3552
3553 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003554 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003555
Chris Lattner27a6c732007-11-24 07:07:01 +00003556 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3557 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003558 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003559 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003560
Chris Lattner27a6c732007-11-24 07:07:01 +00003561 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003562 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003563
Chris Lattner27a6c732007-11-24 07:07:01 +00003564 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003565 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003566 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003567 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003568 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003569
Chris Lattner27a6c732007-11-24 07:07:01 +00003570 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3571 // captures the result into a carry flag.
3572 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003573 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003574
Chris Lattner27a6c732007-11-24 07:07:01 +00003575 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003576 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003577
Chris Lattner27a6c732007-11-24 07:07:01 +00003578 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003579 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003580}
3581
Bob Wilson5bafff32009-06-22 23:27:02 +00003582static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3583 SDValue TmpOp0, TmpOp1;
3584 bool Invert = false;
3585 bool Swap = false;
3586 unsigned Opc = 0;
3587
3588 SDValue Op0 = Op.getOperand(0);
3589 SDValue Op1 = Op.getOperand(1);
3590 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003591 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003592 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3593 DebugLoc dl = Op.getDebugLoc();
3594
3595 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3596 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003597 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003598 case ISD::SETUNE:
3599 case ISD::SETNE: Invert = true; // Fallthrough
3600 case ISD::SETOEQ:
3601 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3602 case ISD::SETOLT:
3603 case ISD::SETLT: Swap = true; // Fallthrough
3604 case ISD::SETOGT:
3605 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3606 case ISD::SETOLE:
3607 case ISD::SETLE: Swap = true; // Fallthrough
3608 case ISD::SETOGE:
3609 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3610 case ISD::SETUGE: Swap = true; // Fallthrough
3611 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3612 case ISD::SETUGT: Swap = true; // Fallthrough
3613 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3614 case ISD::SETUEQ: Invert = true; // Fallthrough
3615 case ISD::SETONE:
3616 // Expand this to (OLT | OGT).
3617 TmpOp0 = Op0;
3618 TmpOp1 = Op1;
3619 Opc = ISD::OR;
3620 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3621 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3622 break;
3623 case ISD::SETUO: Invert = true; // Fallthrough
3624 case ISD::SETO:
3625 // Expand this to (OLT | OGE).
3626 TmpOp0 = Op0;
3627 TmpOp1 = Op1;
3628 Opc = ISD::OR;
3629 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3630 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3631 break;
3632 }
3633 } else {
3634 // Integer comparisons.
3635 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003636 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003637 case ISD::SETNE: Invert = true;
3638 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3639 case ISD::SETLT: Swap = true;
3640 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3641 case ISD::SETLE: Swap = true;
3642 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3643 case ISD::SETULT: Swap = true;
3644 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3645 case ISD::SETULE: Swap = true;
3646 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3647 }
3648
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003649 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003650 if (Opc == ARMISD::VCEQ) {
3651
3652 SDValue AndOp;
3653 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3654 AndOp = Op0;
3655 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3656 AndOp = Op1;
3657
3658 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003659 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003660 AndOp = AndOp.getOperand(0);
3661
3662 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3663 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003664 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3665 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003666 Invert = !Invert;
3667 }
3668 }
3669 }
3670
3671 if (Swap)
3672 std::swap(Op0, Op1);
3673
Owen Andersonc24cb352010-11-08 23:21:22 +00003674 // If one of the operands is a constant vector zero, attempt to fold the
3675 // comparison to a specialized compare-against-zero form.
3676 SDValue SingleOp;
3677 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3678 SingleOp = Op0;
3679 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3680 if (Opc == ARMISD::VCGE)
3681 Opc = ARMISD::VCLEZ;
3682 else if (Opc == ARMISD::VCGT)
3683 Opc = ARMISD::VCLTZ;
3684 SingleOp = Op1;
3685 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003686
Owen Andersonc24cb352010-11-08 23:21:22 +00003687 SDValue Result;
3688 if (SingleOp.getNode()) {
3689 switch (Opc) {
3690 case ARMISD::VCEQ:
3691 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3692 case ARMISD::VCGE:
3693 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3694 case ARMISD::VCLEZ:
3695 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3696 case ARMISD::VCGT:
3697 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3698 case ARMISD::VCLTZ:
3699 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3700 default:
3701 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3702 }
3703 } else {
3704 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3705 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003706
3707 if (Invert)
3708 Result = DAG.getNOT(dl, Result, VT);
3709
3710 return Result;
3711}
3712
Bob Wilsond3c42842010-06-14 22:19:57 +00003713/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3714/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003715/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003716static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3717 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003718 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003719 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003720
Bob Wilson827b2102010-06-15 19:05:35 +00003721 // SplatBitSize is set to the smallest size that splats the vector, so a
3722 // zero vector will always have SplatBitSize == 8. However, NEON modified
3723 // immediate instructions others than VMOV do not support the 8-bit encoding
3724 // of a zero vector, and the default encoding of zero is supposed to be the
3725 // 32-bit version.
3726 if (SplatBits == 0)
3727 SplatBitSize = 32;
3728
Bob Wilson5bafff32009-06-22 23:27:02 +00003729 switch (SplatBitSize) {
3730 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003731 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003732 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003733 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003734 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003735 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003736 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003737 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003738 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003739
3740 case 16:
3741 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003742 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003743 if ((SplatBits & ~0xff) == 0) {
3744 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003745 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003746 Imm = SplatBits;
3747 break;
3748 }
3749 if ((SplatBits & ~0xff00) == 0) {
3750 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003751 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003752 Imm = SplatBits >> 8;
3753 break;
3754 }
3755 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003756
3757 case 32:
3758 // NEON's 32-bit VMOV supports splat values where:
3759 // * only one byte is nonzero, or
3760 // * the least significant byte is 0xff and the second byte is nonzero, or
3761 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003762 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003763 if ((SplatBits & ~0xff) == 0) {
3764 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003765 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003766 Imm = SplatBits;
3767 break;
3768 }
3769 if ((SplatBits & ~0xff00) == 0) {
3770 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003771 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003772 Imm = SplatBits >> 8;
3773 break;
3774 }
3775 if ((SplatBits & ~0xff0000) == 0) {
3776 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003777 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003778 Imm = SplatBits >> 16;
3779 break;
3780 }
3781 if ((SplatBits & ~0xff000000) == 0) {
3782 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003783 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003784 Imm = SplatBits >> 24;
3785 break;
3786 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003787
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003788 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3789 if (type == OtherModImm) return SDValue();
3790
Bob Wilson5bafff32009-06-22 23:27:02 +00003791 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003792 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3793 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003794 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003795 Imm = SplatBits >> 8;
3796 SplatBits |= 0xff;
3797 break;
3798 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003799
3800 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003801 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3802 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003803 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003804 Imm = SplatBits >> 16;
3805 SplatBits |= 0xffff;
3806 break;
3807 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003808
3809 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3810 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3811 // VMOV.I32. A (very) minor optimization would be to replicate the value
3812 // and fall through here to test for a valid 64-bit splat. But, then the
3813 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003814 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003815
3816 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003817 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003818 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003819 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003820 uint64_t BitMask = 0xff;
3821 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003822 unsigned ImmMask = 1;
3823 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003824 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003825 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003826 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003827 Imm |= ImmMask;
3828 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003829 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003830 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003831 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003832 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003833 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003834 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003835 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003836 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003837 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003838 break;
3839 }
3840
Bob Wilson1a913ed2010-06-11 21:34:50 +00003841 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003842 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003843 }
3844
Bob Wilsoncba270d2010-07-13 21:16:48 +00003845 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3846 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003847}
3848
Lang Hamesc0a9f822012-03-29 21:56:11 +00003849SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
3850 const ARMSubtarget *ST) const {
3851 if (!ST->useNEONForSinglePrecisionFP() || !ST->hasVFP3() || ST->hasD16())
3852 return SDValue();
3853
3854 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
3855 assert(Op.getValueType() == MVT::f32 &&
3856 "ConstantFP custom lowering should only occur for f32.");
3857
3858 // Try splatting with a VMOV.f32...
3859 APFloat FPVal = CFP->getValueAPF();
3860 int ImmVal = ARM_AM::getFP32Imm(FPVal);
3861 if (ImmVal != -1) {
3862 DebugLoc DL = Op.getDebugLoc();
3863 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
3864 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
3865 NewVal);
3866 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
3867 DAG.getConstant(0, MVT::i32));
3868 }
3869
3870 // If that fails, try a VMOV.i32
3871 EVT VMovVT;
3872 unsigned iVal = FPVal.bitcastToAPInt().getZExtValue();
3873 SDValue NewVal = isNEONModifiedImm(iVal, 0, 32, DAG, VMovVT, false,
3874 VMOVModImm);
3875 if (NewVal != SDValue()) {
3876 DebugLoc DL = Op.getDebugLoc();
3877 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
3878 NewVal);
3879 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
3880 VecConstant);
3881 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
3882 DAG.getConstant(0, MVT::i32));
3883 }
3884
3885 // Finally, try a VMVN.i32
3886 NewVal = isNEONModifiedImm(~iVal & 0xffffffff, 0, 32, DAG, VMovVT, false,
3887 VMVNModImm);
3888 if (NewVal != SDValue()) {
3889 DebugLoc DL = Op.getDebugLoc();
3890 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
3891 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
3892 VecConstant);
3893 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
3894 DAG.getConstant(0, MVT::i32));
3895 }
3896
3897 return SDValue();
3898}
3899
3900
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003901static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003902 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003903 unsigned NumElts = VT.getVectorNumElements();
3904 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003905
3906 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3907 if (M[0] < 0)
3908 return false;
3909
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003910 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003911
3912 // If this is a VEXT shuffle, the immediate value is the index of the first
3913 // element. The other shuffle indices must be the successive elements after
3914 // the first one.
3915 unsigned ExpectedElt = Imm;
3916 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003917 // Increment the expected index. If it wraps around, it may still be
3918 // a VEXT but the source vectors must be swapped.
3919 ExpectedElt += 1;
3920 if (ExpectedElt == NumElts * 2) {
3921 ExpectedElt = 0;
3922 ReverseVEXT = true;
3923 }
3924
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003925 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003926 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003927 return false;
3928 }
3929
3930 // Adjust the index value if the source operands will be swapped.
3931 if (ReverseVEXT)
3932 Imm -= NumElts;
3933
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003934 return true;
3935}
3936
Bob Wilson8bb9e482009-07-26 00:39:34 +00003937/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3938/// instruction with the specified blocksize. (The order of the elements
3939/// within each block of the vector is reversed.)
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003940static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003941 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3942 "Only possible block sizes for VREV are: 16, 32, 64");
3943
Bob Wilson8bb9e482009-07-26 00:39:34 +00003944 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003945 if (EltSz == 64)
3946 return false;
3947
3948 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003949 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003950 // If the first shuffle index is UNDEF, be optimistic.
3951 if (M[0] < 0)
3952 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003953
3954 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3955 return false;
3956
3957 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003958 if (M[i] < 0) continue; // ignore UNDEF indices
3959 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003960 return false;
3961 }
3962
3963 return true;
3964}
3965
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003966static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003967 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3968 // range, then 0 is placed into the resulting vector. So pretty much any mask
3969 // of 8 elements can work here.
3970 return VT == MVT::v8i8 && M.size() == 8;
3971}
3972
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003973static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003974 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3975 if (EltSz == 64)
3976 return false;
3977
Bob Wilsonc692cb72009-08-21 20:54:19 +00003978 unsigned NumElts = VT.getVectorNumElements();
3979 WhichResult = (M[0] == 0 ? 0 : 1);
3980 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003981 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3982 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003983 return false;
3984 }
3985 return true;
3986}
3987
Bob Wilson324f4f12009-12-03 06:40:55 +00003988/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3989/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3990/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003991static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00003992 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3993 if (EltSz == 64)
3994 return false;
3995
3996 unsigned NumElts = VT.getVectorNumElements();
3997 WhichResult = (M[0] == 0 ? 0 : 1);
3998 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003999 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4000 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00004001 return false;
4002 }
4003 return true;
4004}
4005
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004006static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004007 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4008 if (EltSz == 64)
4009 return false;
4010
Bob Wilsonc692cb72009-08-21 20:54:19 +00004011 unsigned NumElts = VT.getVectorNumElements();
4012 WhichResult = (M[0] == 0 ? 0 : 1);
4013 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004014 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00004015 if ((unsigned) M[i] != 2 * i + WhichResult)
4016 return false;
4017 }
4018
4019 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004020 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004021 return false;
4022
4023 return true;
4024}
4025
Bob Wilson324f4f12009-12-03 06:40:55 +00004026/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
4027/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4028/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004029static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004030 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4031 if (EltSz == 64)
4032 return false;
4033
4034 unsigned Half = VT.getVectorNumElements() / 2;
4035 WhichResult = (M[0] == 0 ? 0 : 1);
4036 for (unsigned j = 0; j != 2; ++j) {
4037 unsigned Idx = WhichResult;
4038 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004039 int MIdx = M[i + j * Half];
4040 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00004041 return false;
4042 Idx += 2;
4043 }
4044 }
4045
4046 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4047 if (VT.is64BitVector() && EltSz == 32)
4048 return false;
4049
4050 return true;
4051}
4052
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004053static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004054 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4055 if (EltSz == 64)
4056 return false;
4057
Bob Wilsonc692cb72009-08-21 20:54:19 +00004058 unsigned NumElts = VT.getVectorNumElements();
4059 WhichResult = (M[0] == 0 ? 0 : 1);
4060 unsigned Idx = WhichResult * NumElts / 2;
4061 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004062 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4063 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00004064 return false;
4065 Idx += 1;
4066 }
4067
4068 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004069 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004070 return false;
4071
4072 return true;
4073}
4074
Bob Wilson324f4f12009-12-03 06:40:55 +00004075/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
4076/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4077/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004078static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004079 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4080 if (EltSz == 64)
4081 return false;
4082
4083 unsigned NumElts = VT.getVectorNumElements();
4084 WhichResult = (M[0] == 0 ? 0 : 1);
4085 unsigned Idx = WhichResult * NumElts / 2;
4086 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004087 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4088 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00004089 return false;
4090 Idx += 1;
4091 }
4092
4093 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4094 if (VT.is64BitVector() && EltSz == 32)
4095 return false;
4096
4097 return true;
4098}
4099
Dale Johannesenf630c712010-07-29 20:10:08 +00004100// If N is an integer constant that can be moved into a register in one
4101// instruction, return an SDValue of such a constant (will become a MOV
4102// instruction). Otherwise return null.
4103static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
4104 const ARMSubtarget *ST, DebugLoc dl) {
4105 uint64_t Val;
4106 if (!isa<ConstantSDNode>(N))
4107 return SDValue();
4108 Val = cast<ConstantSDNode>(N)->getZExtValue();
4109
4110 if (ST->isThumb1Only()) {
4111 if (Val <= 255 || ~Val <= 255)
4112 return DAG.getConstant(Val, MVT::i32);
4113 } else {
4114 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
4115 return DAG.getConstant(Val, MVT::i32);
4116 }
4117 return SDValue();
4118}
4119
Bob Wilson5bafff32009-06-22 23:27:02 +00004120// If this is a case we can't handle, return null and let the default
4121// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00004122SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
4123 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00004124 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00004125 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004126 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004127
4128 APInt SplatBits, SplatUndef;
4129 unsigned SplatBitSize;
4130 bool HasAnyUndefs;
4131 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004132 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00004133 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00004134 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00004135 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00004136 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004137 DAG, VmovVT, VT.is128BitVector(),
4138 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004139 if (Val.getNode()) {
4140 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004141 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004142 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004143
4144 // Try an immediate VMVN.
Eli Friedman8e4d0422011-10-13 22:40:23 +00004145 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004146 Val = isNEONModifiedImm(NegatedImm,
4147 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004148 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004149 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004150 if (Val.getNode()) {
4151 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004152 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004153 }
Evan Chengeaa192a2011-11-15 02:12:34 +00004154
4155 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedman2f21e8c2011-12-15 22:56:53 +00004156 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedmaneffab8f2011-12-09 23:54:42 +00004157 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Chengeaa192a2011-11-15 02:12:34 +00004158 if (ImmVal != -1) {
4159 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4160 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4161 }
4162 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004163 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00004164 }
4165
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004166 // Scan through the operands to see if only one value is used.
4167 unsigned NumElts = VT.getVectorNumElements();
4168 bool isOnlyLowElement = true;
4169 bool usesOnlyOneValue = true;
4170 bool isConstant = true;
4171 SDValue Value;
4172 for (unsigned i = 0; i < NumElts; ++i) {
4173 SDValue V = Op.getOperand(i);
4174 if (V.getOpcode() == ISD::UNDEF)
4175 continue;
4176 if (i > 0)
4177 isOnlyLowElement = false;
4178 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4179 isConstant = false;
4180
4181 if (!Value.getNode())
4182 Value = V;
4183 else if (V != Value)
4184 usesOnlyOneValue = false;
4185 }
4186
4187 if (!Value.getNode())
4188 return DAG.getUNDEF(VT);
4189
4190 if (isOnlyLowElement)
4191 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4192
Dale Johannesenf630c712010-07-29 20:10:08 +00004193 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4194
Dale Johannesen575cd142010-10-19 20:00:17 +00004195 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4196 // i32 and try again.
4197 if (usesOnlyOneValue && EltSize <= 32) {
4198 if (!isConstant)
4199 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4200 if (VT.getVectorElementType().isFloatingPoint()) {
4201 SmallVector<SDValue, 8> Ops;
4202 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004203 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00004204 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00004205 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
4206 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00004207 Val = LowerBUILD_VECTOR(Val, DAG, ST);
4208 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004209 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004210 }
Dale Johannesen575cd142010-10-19 20:00:17 +00004211 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
4212 if (Val.getNode())
4213 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004214 }
4215
4216 // If all elements are constants and the case above didn't get hit, fall back
4217 // to the default expansion, which will generate a load from the constant
4218 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004219 if (isConstant)
4220 return SDValue();
4221
Bob Wilson11a1dff2011-01-07 21:37:30 +00004222 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4223 if (NumElts >= 4) {
4224 SDValue shuffle = ReconstructShuffle(Op, DAG);
4225 if (shuffle != SDValue())
4226 return shuffle;
4227 }
4228
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004229 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004230 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4231 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004232 if (EltSize >= 32) {
4233 // Do the expansion with floating-point types, since that is what the VFP
4234 // registers are defined to use, and since i64 is not legal.
4235 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4236 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004237 SmallVector<SDValue, 8> Ops;
4238 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004239 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004240 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004241 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004242 }
4243
4244 return SDValue();
4245}
4246
Bob Wilson11a1dff2011-01-07 21:37:30 +00004247// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004248// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004249SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4250 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004251 DebugLoc dl = Op.getDebugLoc();
4252 EVT VT = Op.getValueType();
4253 unsigned NumElts = VT.getVectorNumElements();
4254
4255 SmallVector<SDValue, 2> SourceVecs;
4256 SmallVector<unsigned, 2> MinElts;
4257 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004258
Bob Wilson11a1dff2011-01-07 21:37:30 +00004259 for (unsigned i = 0; i < NumElts; ++i) {
4260 SDValue V = Op.getOperand(i);
4261 if (V.getOpcode() == ISD::UNDEF)
4262 continue;
4263 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4264 // A shuffle can only come from building a vector from various
4265 // elements of other vectors.
4266 return SDValue();
Eli Friedman46995fa2011-10-14 23:58:49 +00004267 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
4268 VT.getVectorElementType()) {
4269 // This code doesn't know how to handle shuffles where the vector
4270 // element types do not match (this happens because type legalization
4271 // promotes the return type of EXTRACT_VECTOR_ELT).
4272 // FIXME: It might be appropriate to extend this code to handle
4273 // mismatched types.
4274 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004275 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004276
Bob Wilson11a1dff2011-01-07 21:37:30 +00004277 // Record this extraction against the appropriate vector if possible...
4278 SDValue SourceVec = V.getOperand(0);
Jim Grosbach24220472012-07-25 17:02:47 +00004279 // If the element number isn't a constant, we can't effectively
4280 // analyze what's going on.
4281 if (!isa<ConstantSDNode>(V.getOperand(1)))
4282 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004283 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4284 bool FoundSource = false;
4285 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4286 if (SourceVecs[j] == SourceVec) {
4287 if (MinElts[j] > EltNo)
4288 MinElts[j] = EltNo;
4289 if (MaxElts[j] < EltNo)
4290 MaxElts[j] = EltNo;
4291 FoundSource = true;
4292 break;
4293 }
4294 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004295
Bob Wilson11a1dff2011-01-07 21:37:30 +00004296 // Or record a new source if not...
4297 if (!FoundSource) {
4298 SourceVecs.push_back(SourceVec);
4299 MinElts.push_back(EltNo);
4300 MaxElts.push_back(EltNo);
4301 }
4302 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004303
Bob Wilson11a1dff2011-01-07 21:37:30 +00004304 // Currently only do something sane when at most two source vectors
4305 // involved.
4306 if (SourceVecs.size() > 2)
4307 return SDValue();
4308
4309 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4310 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004311
Bob Wilson11a1dff2011-01-07 21:37:30 +00004312 // This loop extracts the usage patterns of the source vectors
4313 // and prepares appropriate SDValues for a shuffle if possible.
4314 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4315 if (SourceVecs[i].getValueType() == VT) {
4316 // No VEXT necessary
4317 ShuffleSrcs[i] = SourceVecs[i];
4318 VEXTOffsets[i] = 0;
4319 continue;
4320 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4321 // It probably isn't worth padding out a smaller vector just to
4322 // break it down again in a shuffle.
4323 return SDValue();
4324 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004325
Bob Wilson11a1dff2011-01-07 21:37:30 +00004326 // Since only 64-bit and 128-bit vectors are legal on ARM and
4327 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004328 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4329 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004330
Bob Wilson11a1dff2011-01-07 21:37:30 +00004331 if (MaxElts[i] - MinElts[i] >= NumElts) {
4332 // Span too large for a VEXT to cope
4333 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004334 }
4335
Bob Wilson11a1dff2011-01-07 21:37:30 +00004336 if (MinElts[i] >= NumElts) {
4337 // The extraction can just take the second half
4338 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004339 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4340 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004341 DAG.getIntPtrConstant(NumElts));
4342 } else if (MaxElts[i] < NumElts) {
4343 // The extraction can just take the first half
4344 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004345 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4346 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004347 DAG.getIntPtrConstant(0));
4348 } else {
4349 // An actual VEXT is needed
4350 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004351 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4352 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004353 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004354 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4355 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004356 DAG.getIntPtrConstant(NumElts));
4357 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4358 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4359 }
4360 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004361
Bob Wilson11a1dff2011-01-07 21:37:30 +00004362 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004363
Bob Wilson11a1dff2011-01-07 21:37:30 +00004364 for (unsigned i = 0; i < NumElts; ++i) {
4365 SDValue Entry = Op.getOperand(i);
4366 if (Entry.getOpcode() == ISD::UNDEF) {
4367 Mask.push_back(-1);
4368 continue;
4369 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004370
Bob Wilson11a1dff2011-01-07 21:37:30 +00004371 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004372 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4373 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004374 if (ExtractVec == SourceVecs[0]) {
4375 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4376 } else {
4377 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4378 }
4379 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004380
Bob Wilson11a1dff2011-01-07 21:37:30 +00004381 // Final check before we try to produce nonsense...
4382 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004383 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4384 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004385
Bob Wilson11a1dff2011-01-07 21:37:30 +00004386 return SDValue();
4387}
4388
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004389/// isShuffleMaskLegal - Targets can use this to indicate that they only
4390/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4391/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4392/// are assumed to be legal.
4393bool
4394ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4395 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004396 if (VT.getVectorNumElements() == 4 &&
4397 (VT.is128BitVector() || VT.is64BitVector())) {
4398 unsigned PFIndexes[4];
4399 for (unsigned i = 0; i != 4; ++i) {
4400 if (M[i] < 0)
4401 PFIndexes[i] = 8;
4402 else
4403 PFIndexes[i] = M[i];
4404 }
4405
4406 // Compute the index in the perfect shuffle table.
4407 unsigned PFTableIndex =
4408 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4409 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4410 unsigned Cost = (PFEntry >> 30);
4411
4412 if (Cost <= 4)
4413 return true;
4414 }
4415
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004416 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004417 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004418
Bob Wilson53dd2452010-06-07 23:53:38 +00004419 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4420 return (EltSize >= 32 ||
4421 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004422 isVREVMask(M, VT, 64) ||
4423 isVREVMask(M, VT, 32) ||
4424 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004425 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004426 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004427 isVTRNMask(M, VT, WhichResult) ||
4428 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004429 isVZIPMask(M, VT, WhichResult) ||
4430 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4431 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4432 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004433}
4434
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004435/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4436/// the specified operations to build the shuffle.
4437static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4438 SDValue RHS, SelectionDAG &DAG,
4439 DebugLoc dl) {
4440 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4441 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4442 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4443
4444 enum {
4445 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4446 OP_VREV,
4447 OP_VDUP0,
4448 OP_VDUP1,
4449 OP_VDUP2,
4450 OP_VDUP3,
4451 OP_VEXT1,
4452 OP_VEXT2,
4453 OP_VEXT3,
4454 OP_VUZPL, // VUZP, left result
4455 OP_VUZPR, // VUZP, right result
4456 OP_VZIPL, // VZIP, left result
4457 OP_VZIPR, // VZIP, right result
4458 OP_VTRNL, // VTRN, left result
4459 OP_VTRNR // VTRN, right result
4460 };
4461
4462 if (OpNum == OP_COPY) {
4463 if (LHSID == (1*9+2)*9+3) return LHS;
4464 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4465 return RHS;
4466 }
4467
4468 SDValue OpLHS, OpRHS;
4469 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4470 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4471 EVT VT = OpLHS.getValueType();
4472
4473 switch (OpNum) {
4474 default: llvm_unreachable("Unknown shuffle opcode!");
4475 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004476 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004477 if (VT.getVectorElementType() == MVT::i32 ||
4478 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004479 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4480 // vrev <4 x i16> -> VREV32
4481 if (VT.getVectorElementType() == MVT::i16)
4482 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4483 // vrev <4 x i8> -> VREV16
4484 assert(VT.getVectorElementType() == MVT::i8);
4485 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004486 case OP_VDUP0:
4487 case OP_VDUP1:
4488 case OP_VDUP2:
4489 case OP_VDUP3:
4490 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004491 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004492 case OP_VEXT1:
4493 case OP_VEXT2:
4494 case OP_VEXT3:
4495 return DAG.getNode(ARMISD::VEXT, dl, VT,
4496 OpLHS, OpRHS,
4497 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4498 case OP_VUZPL:
4499 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004500 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004501 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4502 case OP_VZIPL:
4503 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004504 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004505 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4506 case OP_VTRNL:
4507 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004508 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4509 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004510 }
4511}
4512
Bill Wendling69a05a72011-03-14 23:02:38 +00004513static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004514 ArrayRef<int> ShuffleMask,
Bill Wendling69a05a72011-03-14 23:02:38 +00004515 SelectionDAG &DAG) {
4516 // Check to see if we can use the VTBL instruction.
4517 SDValue V1 = Op.getOperand(0);
4518 SDValue V2 = Op.getOperand(1);
4519 DebugLoc DL = Op.getDebugLoc();
4520
4521 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004522 for (ArrayRef<int>::iterator
Bill Wendling69a05a72011-03-14 23:02:38 +00004523 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4524 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4525
4526 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4527 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4528 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4529 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004530
Owen Anderson76706012011-04-05 21:48:57 +00004531 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004532 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4533 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004534}
4535
Bob Wilson5bafff32009-06-22 23:27:02 +00004536static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004537 SDValue V1 = Op.getOperand(0);
4538 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004539 DebugLoc dl = Op.getDebugLoc();
4540 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004541 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsond8e17572009-08-12 22:31:50 +00004542
Bob Wilson28865062009-08-13 02:13:04 +00004543 // Convert shuffles that are directly supported on NEON to target-specific
4544 // DAG nodes, instead of keeping them as shuffles and matching them again
4545 // during code selection. This is more efficient and avoids the possibility
4546 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004547 // FIXME: floating-point vectors should be canonicalized to integer vectors
4548 // of the same time so that they get CSEd properly.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004549 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004550
Bob Wilson53dd2452010-06-07 23:53:38 +00004551 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4552 if (EltSize <= 32) {
4553 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4554 int Lane = SVN->getSplatIndex();
4555 // If this is undef splat, generate it via "just" vdup, if possible.
4556 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004557
Dan Gohman65fd6562011-11-03 21:49:52 +00004558 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson53dd2452010-06-07 23:53:38 +00004559 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4560 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4561 }
Dan Gohman65fd6562011-11-03 21:49:52 +00004562 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
4563 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
4564 // reaches it).
4565 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
4566 !isa<ConstantSDNode>(V1.getOperand(0))) {
4567 bool IsScalarToVector = true;
4568 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
4569 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
4570 IsScalarToVector = false;
4571 break;
4572 }
4573 if (IsScalarToVector)
4574 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4575 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004576 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4577 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004578 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004579
4580 bool ReverseVEXT;
4581 unsigned Imm;
4582 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4583 if (ReverseVEXT)
4584 std::swap(V1, V2);
4585 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4586 DAG.getConstant(Imm, MVT::i32));
4587 }
4588
4589 if (isVREVMask(ShuffleMask, VT, 64))
4590 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4591 if (isVREVMask(ShuffleMask, VT, 32))
4592 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4593 if (isVREVMask(ShuffleMask, VT, 16))
4594 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4595
4596 // Check for Neon shuffles that modify both input vectors in place.
4597 // If both results are used, i.e., if there are two shuffles with the same
4598 // source operands and with masks corresponding to both results of one of
4599 // these operations, DAG memoization will ensure that a single node is
4600 // used for both shuffles.
4601 unsigned WhichResult;
4602 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4603 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4604 V1, V2).getValue(WhichResult);
4605 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4606 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4607 V1, V2).getValue(WhichResult);
4608 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4609 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4610 V1, V2).getValue(WhichResult);
4611
4612 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4613 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4614 V1, V1).getValue(WhichResult);
4615 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4616 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4617 V1, V1).getValue(WhichResult);
4618 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4619 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4620 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004621 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004622
Bob Wilsonc692cb72009-08-21 20:54:19 +00004623 // If the shuffle is not directly supported and it has 4 elements, use
4624 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004625 unsigned NumElts = VT.getVectorNumElements();
4626 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004627 unsigned PFIndexes[4];
4628 for (unsigned i = 0; i != 4; ++i) {
4629 if (ShuffleMask[i] < 0)
4630 PFIndexes[i] = 8;
4631 else
4632 PFIndexes[i] = ShuffleMask[i];
4633 }
4634
4635 // Compute the index in the perfect shuffle table.
4636 unsigned PFTableIndex =
4637 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004638 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4639 unsigned Cost = (PFEntry >> 30);
4640
4641 if (Cost <= 4)
4642 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4643 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004644
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004645 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004646 if (EltSize >= 32) {
4647 // Do the expansion with floating-point types, since that is what the VFP
4648 // registers are defined to use, and since i64 is not legal.
4649 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4650 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004651 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4652 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004653 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004654 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004655 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004656 Ops.push_back(DAG.getUNDEF(EltVT));
4657 else
4658 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4659 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4660 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4661 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004662 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004663 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004664 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004665 }
4666
Bill Wendling69a05a72011-03-14 23:02:38 +00004667 if (VT == MVT::v8i8) {
4668 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4669 if (NewOp.getNode())
4670 return NewOp;
4671 }
4672
Bob Wilson22cac0d2009-08-14 05:16:33 +00004673 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004674}
4675
Eli Friedman5c89cb82011-10-24 23:08:52 +00004676static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
4677 // INSERT_VECTOR_ELT is legal only for immediate indexes.
4678 SDValue Lane = Op.getOperand(2);
4679 if (!isa<ConstantSDNode>(Lane))
4680 return SDValue();
4681
4682 return Op;
4683}
4684
Bob Wilson5bafff32009-06-22 23:27:02 +00004685static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004686 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004687 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004688 if (!isa<ConstantSDNode>(Lane))
4689 return SDValue();
4690
4691 SDValue Vec = Op.getOperand(0);
4692 if (Op.getValueType() == MVT::i32 &&
4693 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4694 DebugLoc dl = Op.getDebugLoc();
4695 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4696 }
4697
4698 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004699}
4700
Bob Wilsona6d65862009-08-03 20:36:38 +00004701static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4702 // The only time a CONCAT_VECTORS operation can have legal types is when
4703 // two 64-bit vectors are concatenated to a 128-bit vector.
4704 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4705 "unexpected CONCAT_VECTORS");
4706 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004707 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004708 SDValue Op0 = Op.getOperand(0);
4709 SDValue Op1 = Op.getOperand(1);
4710 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004711 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004712 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004713 DAG.getIntPtrConstant(0));
4714 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004715 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004716 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004717 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004718 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004719}
4720
Bob Wilson626613d2010-11-23 19:38:38 +00004721/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4722/// element has been zero/sign-extended, depending on the isSigned parameter,
4723/// from an integer type half its size.
4724static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4725 bool isSigned) {
4726 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4727 EVT VT = N->getValueType(0);
4728 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4729 SDNode *BVN = N->getOperand(0).getNode();
4730 if (BVN->getValueType(0) != MVT::v4i32 ||
4731 BVN->getOpcode() != ISD::BUILD_VECTOR)
4732 return false;
4733 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4734 unsigned HiElt = 1 - LoElt;
4735 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4736 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4737 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4738 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4739 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4740 return false;
4741 if (isSigned) {
4742 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4743 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4744 return true;
4745 } else {
4746 if (Hi0->isNullValue() && Hi1->isNullValue())
4747 return true;
4748 }
4749 return false;
4750 }
4751
4752 if (N->getOpcode() != ISD::BUILD_VECTOR)
4753 return false;
4754
4755 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4756 SDNode *Elt = N->getOperand(i).getNode();
4757 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4758 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4759 unsigned HalfSize = EltSize / 2;
4760 if (isSigned) {
Bob Wilson9d45de22011-10-18 18:46:49 +00004761 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004762 return false;
4763 } else {
Bob Wilson9d45de22011-10-18 18:46:49 +00004764 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004765 return false;
4766 }
4767 continue;
4768 }
4769 return false;
4770 }
4771
4772 return true;
4773}
4774
4775/// isSignExtended - Check if a node is a vector value that is sign-extended
4776/// or a constant BUILD_VECTOR with sign-extended elements.
4777static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4778 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4779 return true;
4780 if (isExtendedBUILD_VECTOR(N, DAG, true))
4781 return true;
4782 return false;
4783}
4784
4785/// isZeroExtended - Check if a node is a vector value that is zero-extended
4786/// or a constant BUILD_VECTOR with zero-extended elements.
4787static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4788 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4789 return true;
4790 if (isExtendedBUILD_VECTOR(N, DAG, false))
4791 return true;
4792 return false;
4793}
4794
4795/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4796/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004797static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4798 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4799 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004800 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4801 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4802 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004803 LD->isNonTemporal(), LD->isInvariant(),
4804 LD->getAlignment());
Bob Wilson626613d2010-11-23 19:38:38 +00004805 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4806 // have been legalized as a BITCAST from v4i32.
4807 if (N->getOpcode() == ISD::BITCAST) {
4808 SDNode *BVN = N->getOperand(0).getNode();
4809 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4810 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4811 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4812 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4813 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4814 }
4815 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4816 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4817 EVT VT = N->getValueType(0);
4818 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4819 unsigned NumElts = VT.getVectorNumElements();
4820 MVT TruncVT = MVT::getIntegerVT(EltSize);
4821 SmallVector<SDValue, 8> Ops;
4822 for (unsigned i = 0; i != NumElts; ++i) {
4823 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4824 const APInt &CInt = C->getAPIntValue();
Bob Wilsonff73d8f2012-04-30 16:53:34 +00004825 // Element types smaller than 32 bits are not legal, so use i32 elements.
4826 // The values are implicitly truncated so sext vs. zext doesn't matter.
4827 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32));
Bob Wilson626613d2010-11-23 19:38:38 +00004828 }
4829 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4830 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004831}
4832
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004833static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4834 unsigned Opcode = N->getOpcode();
4835 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4836 SDNode *N0 = N->getOperand(0).getNode();
4837 SDNode *N1 = N->getOperand(1).getNode();
4838 return N0->hasOneUse() && N1->hasOneUse() &&
4839 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4840 }
4841 return false;
4842}
4843
4844static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4845 unsigned Opcode = N->getOpcode();
4846 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4847 SDNode *N0 = N->getOperand(0).getNode();
4848 SDNode *N1 = N->getOperand(1).getNode();
4849 return N0->hasOneUse() && N1->hasOneUse() &&
4850 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4851 }
4852 return false;
4853}
4854
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004855static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4856 // Multiplications are only custom-lowered for 128-bit vectors so that
4857 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4858 EVT VT = Op.getValueType();
4859 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4860 SDNode *N0 = Op.getOperand(0).getNode();
4861 SDNode *N1 = Op.getOperand(1).getNode();
4862 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004863 bool isMLA = false;
4864 bool isN0SExt = isSignExtended(N0, DAG);
4865 bool isN1SExt = isSignExtended(N1, DAG);
4866 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004867 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004868 else {
4869 bool isN0ZExt = isZeroExtended(N0, DAG);
4870 bool isN1ZExt = isZeroExtended(N1, DAG);
4871 if (isN0ZExt && isN1ZExt)
4872 NewOpc = ARMISD::VMULLu;
4873 else if (isN1SExt || isN1ZExt) {
4874 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4875 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4876 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4877 NewOpc = ARMISD::VMULLs;
4878 isMLA = true;
4879 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4880 NewOpc = ARMISD::VMULLu;
4881 isMLA = true;
4882 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4883 std::swap(N0, N1);
4884 NewOpc = ARMISD::VMULLu;
4885 isMLA = true;
4886 }
4887 }
4888
4889 if (!NewOpc) {
4890 if (VT == MVT::v2i64)
4891 // Fall through to expand this. It is not legal.
4892 return SDValue();
4893 else
4894 // Other vector multiplications are legal.
4895 return Op;
4896 }
4897 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004898
4899 // Legalize to a VMULL instruction.
4900 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004901 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004902 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004903 if (!isMLA) {
4904 Op0 = SkipExtension(N0, DAG);
4905 assert(Op0.getValueType().is64BitVector() &&
4906 Op1.getValueType().is64BitVector() &&
4907 "unexpected types for extended operands to VMULL");
4908 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4909 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004910
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004911 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4912 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4913 // vmull q0, d4, d6
4914 // vmlal q0, d5, d6
4915 // is faster than
4916 // vaddl q0, d4, d5
4917 // vmovl q1, d6
4918 // vmul q0, q0, q1
4919 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4920 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4921 EVT Op1VT = Op1.getValueType();
4922 return DAG.getNode(N0->getOpcode(), DL, VT,
4923 DAG.getNode(NewOpc, DL, VT,
4924 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4925 DAG.getNode(NewOpc, DL, VT,
4926 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004927}
4928
Owen Anderson76706012011-04-05 21:48:57 +00004929static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004930LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4931 // Convert to float
4932 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4933 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4934 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4935 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4936 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4937 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4938 // Get reciprocal estimate.
4939 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004940 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004941 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4942 // Because char has a smaller range than uchar, we can actually get away
4943 // without any newton steps. This requires that we use a weird bias
4944 // of 0xb000, however (again, this has been exhaustively tested).
4945 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4946 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4947 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4948 Y = DAG.getConstant(0xb000, MVT::i32);
4949 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4950 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4951 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4952 // Convert back to short.
4953 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4954 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4955 return X;
4956}
4957
Owen Anderson76706012011-04-05 21:48:57 +00004958static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004959LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4960 SDValue N2;
4961 // Convert to float.
4962 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4963 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4964 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4965 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4966 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4967 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004968
Nate Begeman7973f352011-02-11 20:53:29 +00004969 // Use reciprocal estimate and one refinement step.
4970 // float4 recip = vrecpeq_f32(yf);
4971 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004972 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004973 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004974 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004975 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4976 N1, N2);
4977 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4978 // Because short has a smaller range than ushort, we can actually get away
4979 // with only a single newton step. This requires that we use a weird bias
4980 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004981 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004982 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4983 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004984 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004985 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4986 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4987 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4988 // Convert back to integer and return.
4989 // return vmovn_s32(vcvt_s32_f32(result));
4990 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4991 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4992 return N0;
4993}
4994
4995static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4996 EVT VT = Op.getValueType();
4997 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4998 "unexpected type for custom-lowering ISD::SDIV");
4999
5000 DebugLoc dl = Op.getDebugLoc();
5001 SDValue N0 = Op.getOperand(0);
5002 SDValue N1 = Op.getOperand(1);
5003 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00005004
Nate Begeman7973f352011-02-11 20:53:29 +00005005 if (VT == MVT::v8i8) {
5006 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
5007 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005008
Nate Begeman7973f352011-02-11 20:53:29 +00005009 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5010 DAG.getIntPtrConstant(4));
5011 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005012 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005013 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5014 DAG.getIntPtrConstant(0));
5015 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5016 DAG.getIntPtrConstant(0));
5017
5018 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
5019 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
5020
5021 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5022 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005023
Nate Begeman7973f352011-02-11 20:53:29 +00005024 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
5025 return N0;
5026 }
5027 return LowerSDIV_v4i16(N0, N1, dl, DAG);
5028}
5029
5030static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
5031 EVT VT = Op.getValueType();
5032 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5033 "unexpected type for custom-lowering ISD::UDIV");
5034
5035 DebugLoc dl = Op.getDebugLoc();
5036 SDValue N0 = Op.getOperand(0);
5037 SDValue N1 = Op.getOperand(1);
5038 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00005039
Nate Begeman7973f352011-02-11 20:53:29 +00005040 if (VT == MVT::v8i8) {
5041 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
5042 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005043
Nate Begeman7973f352011-02-11 20:53:29 +00005044 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5045 DAG.getIntPtrConstant(4));
5046 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005047 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005048 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5049 DAG.getIntPtrConstant(0));
5050 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5051 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00005052
Nate Begeman7973f352011-02-11 20:53:29 +00005053 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
5054 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00005055
Nate Begeman7973f352011-02-11 20:53:29 +00005056 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5057 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005058
5059 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00005060 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
5061 N0);
5062 return N0;
5063 }
Owen Anderson76706012011-04-05 21:48:57 +00005064
Nate Begeman7973f352011-02-11 20:53:29 +00005065 // v4i16 sdiv ... Convert to float.
5066 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
5067 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
5068 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
5069 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
5070 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005071 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00005072
5073 // Use reciprocal estimate and two refinement steps.
5074 // float4 recip = vrecpeq_f32(yf);
5075 // recip *= vrecpsq_f32(yf, recip);
5076 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00005077 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005078 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00005079 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005080 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005081 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005082 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00005083 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005084 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005085 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005086 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5087 // Simply multiplying by the reciprocal estimate can leave us a few ulps
5088 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
5089 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005090 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00005091 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5092 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
5093 N1 = DAG.getConstant(2, MVT::i32);
5094 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5095 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5096 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5097 // Convert back to integer and return.
5098 // return vmovn_u32(vcvt_s32_f32(result));
5099 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5100 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5101 return N0;
5102}
5103
Evan Cheng342e3162011-08-30 01:34:54 +00005104static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
5105 EVT VT = Op.getNode()->getValueType(0);
5106 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
5107
5108 unsigned Opc;
5109 bool ExtraOp = false;
5110 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00005111 default: llvm_unreachable("Invalid code");
Evan Cheng342e3162011-08-30 01:34:54 +00005112 case ISD::ADDC: Opc = ARMISD::ADDC; break;
5113 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
5114 case ISD::SUBC: Opc = ARMISD::SUBC; break;
5115 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
5116 }
5117
5118 if (!ExtraOp)
5119 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5120 Op.getOperand(1));
5121 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5122 Op.getOperand(1), Op.getOperand(2));
5123}
5124
Eli Friedman74bf18c2011-09-15 22:26:18 +00005125static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedman7cc15662011-09-15 22:18:49 +00005126 // Monotonic load/store is legal for all targets
5127 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
5128 return Op;
5129
5130 // Aquire/Release load/store is not legal for targets without a
5131 // dmb or equivalent available.
5132 return SDValue();
5133}
5134
5135
Eli Friedman2bdffe42011-08-31 00:31:29 +00005136static void
Eli Friedman4d3f3292011-08-31 17:52:22 +00005137ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
5138 SelectionDAG &DAG, unsigned NewOp) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005139 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +00005140 assert (Node->getValueType(0) == MVT::i64 &&
5141 "Only know how to expand i64 atomics");
Eli Friedman2bdffe42011-08-31 00:31:29 +00005142
Eli Friedman4d3f3292011-08-31 17:52:22 +00005143 SmallVector<SDValue, 6> Ops;
5144 Ops.push_back(Node->getOperand(0)); // Chain
5145 Ops.push_back(Node->getOperand(1)); // Ptr
5146 // Low part of Val1
5147 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5148 Node->getOperand(2), DAG.getIntPtrConstant(0)));
5149 // High part of Val1
5150 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5151 Node->getOperand(2), DAG.getIntPtrConstant(1)));
Andrew Trick3af7a672011-09-20 03:06:13 +00005152 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) {
Eli Friedman4d3f3292011-08-31 17:52:22 +00005153 // High part of Val1
5154 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5155 Node->getOperand(3), DAG.getIntPtrConstant(0)));
5156 // High part of Val2
5157 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5158 Node->getOperand(3), DAG.getIntPtrConstant(1)));
5159 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005160 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
5161 SDValue Result =
Eli Friedman4d3f3292011-08-31 17:52:22 +00005162 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64,
Eli Friedman2bdffe42011-08-31 00:31:29 +00005163 cast<MemSDNode>(Node)->getMemOperand());
Eli Friedman4d3f3292011-08-31 17:52:22 +00005164 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) };
Eli Friedman2bdffe42011-08-31 00:31:29 +00005165 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
5166 Results.push_back(Result.getValue(2));
5167}
5168
Dan Gohmand858e902010-04-17 15:26:15 +00005169SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005170 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005171 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00005172 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00005173 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005174 case ISD::GlobalAddress:
5175 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
5176 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00005177 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00005178 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00005179 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
5180 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005181 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00005182 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00005183 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00005184 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00005185 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00005186 case ISD::SINT_TO_FP:
5187 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
5188 case ISD::FP_TO_SINT:
5189 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005190 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00005191 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00005192 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005193 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00005194 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00005195 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00005196 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
5197 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00005198 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005199 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00005200 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00005201 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00005202 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00005203 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00005204 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00005205 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Duncan Sands28b77e92011-09-06 19:07:46 +00005206 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hames45b5f882012-03-15 18:49:02 +00005207 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesenf630c712010-07-29 20:10:08 +00005208 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005209 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedman5c89cb82011-10-24 23:08:52 +00005210 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005211 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00005212 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00005213 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005214 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00005215 case ISD::SDIV: return LowerSDIV(Op, DAG);
5216 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00005217 case ISD::ADDC:
5218 case ISD::ADDE:
5219 case ISD::SUBC:
5220 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Eli Friedman7cc15662011-09-15 22:18:49 +00005221 case ISD::ATOMIC_LOAD:
Eli Friedman74bf18c2011-09-15 22:26:18 +00005222 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005223 }
Evan Chenga8e29892007-01-19 07:51:42 +00005224}
5225
Duncan Sands1607f052008-12-01 11:39:25 +00005226/// ReplaceNodeResults - Replace the results of node with an illegal result
5227/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00005228void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
5229 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005230 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00005231 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00005232 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00005233 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005234 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005235 case ISD::BITCAST:
5236 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005237 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005238 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00005239 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00005240 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005241 break;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005242 case ISD::ATOMIC_LOAD_ADD:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005243 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005244 return;
5245 case ISD::ATOMIC_LOAD_AND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005246 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005247 return;
5248 case ISD::ATOMIC_LOAD_NAND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005249 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005250 return;
5251 case ISD::ATOMIC_LOAD_OR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005252 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005253 return;
5254 case ISD::ATOMIC_LOAD_SUB:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005255 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005256 return;
5257 case ISD::ATOMIC_LOAD_XOR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005258 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005259 return;
5260 case ISD::ATOMIC_SWAP:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005261 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005262 return;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005263 case ISD::ATOMIC_CMP_SWAP:
5264 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG);
5265 return;
Duncan Sands1607f052008-12-01 11:39:25 +00005266 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00005267 if (Res.getNode())
5268 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00005269}
Chris Lattner27a6c732007-11-24 07:07:01 +00005270
Evan Chenga8e29892007-01-19 07:51:42 +00005271//===----------------------------------------------------------------------===//
5272// ARM Scheduler Hooks
5273//===----------------------------------------------------------------------===//
5274
5275MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005276ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
5277 MachineBasicBlock *BB,
5278 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005279 unsigned dest = MI->getOperand(0).getReg();
5280 unsigned ptr = MI->getOperand(1).getReg();
5281 unsigned oldval = MI->getOperand(2).getReg();
5282 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005283 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5284 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005285 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005286
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005287 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Craig Topper420761a2012-04-20 07:30:17 +00005288 unsigned scratch = MRI.createVirtualRegister(isThumb2 ?
5289 (const TargetRegisterClass*)&ARM::rGPRRegClass :
5290 (const TargetRegisterClass*)&ARM::GPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005291
5292 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005293 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5294 MRI.constrainRegClass(oldval, &ARM::rGPRRegClass);
5295 MRI.constrainRegClass(newval, &ARM::rGPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005296 }
5297
Jim Grosbach5278eb82009-12-11 01:42:04 +00005298 unsigned ldrOpc, strOpc;
5299 switch (Size) {
5300 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005301 case 1:
5302 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00005303 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005304 break;
5305 case 2:
5306 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5307 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5308 break;
5309 case 4:
5310 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5311 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5312 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00005313 }
5314
5315 MachineFunction *MF = BB->getParent();
5316 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5317 MachineFunction::iterator It = BB;
5318 ++It; // insert the new blocks after the current block
5319
5320 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5321 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5322 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5323 MF->insert(It, loop1MBB);
5324 MF->insert(It, loop2MBB);
5325 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005326
5327 // Transfer the remainder of BB and its successor edges to exitMBB.
5328 exitMBB->splice(exitMBB->begin(), BB,
5329 llvm::next(MachineBasicBlock::iterator(MI)),
5330 BB->end());
5331 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005332
5333 // thisMBB:
5334 // ...
5335 // fallthrough --> loop1MBB
5336 BB->addSuccessor(loop1MBB);
5337
5338 // loop1MBB:
5339 // ldrex dest, [ptr]
5340 // cmp dest, oldval
5341 // bne exitMBB
5342 BB = loop1MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005343 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5344 if (ldrOpc == ARM::t2LDREX)
5345 MIB.addImm(0);
5346 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005347 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005348 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005349 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5350 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005351 BB->addSuccessor(loop2MBB);
5352 BB->addSuccessor(exitMBB);
5353
5354 // loop2MBB:
5355 // strex scratch, newval, [ptr]
5356 // cmp scratch, #0
5357 // bne loop1MBB
5358 BB = loop2MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005359 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr);
5360 if (strOpc == ARM::t2STREX)
5361 MIB.addImm(0);
5362 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005363 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005364 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005365 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5366 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005367 BB->addSuccessor(loop1MBB);
5368 BB->addSuccessor(exitMBB);
5369
5370 // exitMBB:
5371 // ...
5372 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005373
Dan Gohman14152b42010-07-06 20:24:04 +00005374 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005375
Jim Grosbach5278eb82009-12-11 01:42:04 +00005376 return BB;
5377}
5378
5379MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005380ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5381 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005382 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5383 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5384
5385 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005386 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005387 MachineFunction::iterator It = BB;
5388 ++It;
5389
5390 unsigned dest = MI->getOperand(0).getReg();
5391 unsigned ptr = MI->getOperand(1).getReg();
5392 unsigned incr = MI->getOperand(2).getReg();
5393 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005394 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005395
5396 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5397 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005398 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5399 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005400 }
5401
Jim Grosbachc3c23542009-12-14 04:22:04 +00005402 unsigned ldrOpc, strOpc;
5403 switch (Size) {
5404 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005405 case 1:
5406 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005407 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005408 break;
5409 case 2:
5410 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5411 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5412 break;
5413 case 4:
5414 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5415 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5416 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005417 }
5418
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005419 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5420 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5421 MF->insert(It, loopMBB);
5422 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005423
5424 // Transfer the remainder of BB and its successor edges to exitMBB.
5425 exitMBB->splice(exitMBB->begin(), BB,
5426 llvm::next(MachineBasicBlock::iterator(MI)),
5427 BB->end());
5428 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005429
Craig Topper420761a2012-04-20 07:30:17 +00005430 const TargetRegisterClass *TRC = isThumb2 ?
5431 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5432 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005433 unsigned scratch = MRI.createVirtualRegister(TRC);
5434 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005435
5436 // thisMBB:
5437 // ...
5438 // fallthrough --> loopMBB
5439 BB->addSuccessor(loopMBB);
5440
5441 // loopMBB:
5442 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005443 // <binop> scratch2, dest, incr
5444 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005445 // cmp scratch, #0
5446 // bne- loopMBB
5447 // fallthrough --> exitMBB
5448 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005449 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5450 if (ldrOpc == ARM::t2LDREX)
5451 MIB.addImm(0);
5452 AddDefaultPred(MIB);
Jim Grosbachc67b5562009-12-15 00:12:35 +00005453 if (BinOpcode) {
5454 // operand order needs to go the other way for NAND
5455 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5456 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5457 addReg(incr).addReg(dest)).addReg(0);
5458 else
5459 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5460 addReg(dest).addReg(incr)).addReg(0);
5461 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005462
Jim Grosbachb6aed502011-09-09 18:37:27 +00005463 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5464 if (strOpc == ARM::t2STREX)
5465 MIB.addImm(0);
5466 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005467 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005468 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005469 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5470 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005471
5472 BB->addSuccessor(loopMBB);
5473 BB->addSuccessor(exitMBB);
5474
5475 // exitMBB:
5476 // ...
5477 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005478
Dan Gohman14152b42010-07-06 20:24:04 +00005479 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005480
Jim Grosbachc3c23542009-12-14 04:22:04 +00005481 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005482}
5483
Jim Grosbachf7da8822011-04-26 19:44:18 +00005484MachineBasicBlock *
5485ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5486 MachineBasicBlock *BB,
5487 unsigned Size,
5488 bool signExtend,
5489 ARMCC::CondCodes Cond) const {
5490 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5491
5492 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5493 MachineFunction *MF = BB->getParent();
5494 MachineFunction::iterator It = BB;
5495 ++It;
5496
5497 unsigned dest = MI->getOperand(0).getReg();
5498 unsigned ptr = MI->getOperand(1).getReg();
5499 unsigned incr = MI->getOperand(2).getReg();
5500 unsigned oldval = dest;
5501 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005502 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005503
5504 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5505 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005506 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5507 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005508 }
5509
Jim Grosbachf7da8822011-04-26 19:44:18 +00005510 unsigned ldrOpc, strOpc, extendOpc;
5511 switch (Size) {
5512 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5513 case 1:
5514 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5515 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005516 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005517 break;
5518 case 2:
5519 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5520 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005521 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005522 break;
5523 case 4:
5524 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5525 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5526 extendOpc = 0;
5527 break;
5528 }
5529
5530 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5531 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5532 MF->insert(It, loopMBB);
5533 MF->insert(It, exitMBB);
5534
5535 // Transfer the remainder of BB and its successor edges to exitMBB.
5536 exitMBB->splice(exitMBB->begin(), BB,
5537 llvm::next(MachineBasicBlock::iterator(MI)),
5538 BB->end());
5539 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5540
Craig Topper420761a2012-04-20 07:30:17 +00005541 const TargetRegisterClass *TRC = isThumb2 ?
5542 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5543 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005544 unsigned scratch = MRI.createVirtualRegister(TRC);
5545 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005546
5547 // thisMBB:
5548 // ...
5549 // fallthrough --> loopMBB
5550 BB->addSuccessor(loopMBB);
5551
5552 // loopMBB:
5553 // ldrex dest, ptr
5554 // (sign extend dest, if required)
5555 // cmp dest, incr
5556 // cmov.cond scratch2, dest, incr
5557 // strex scratch, scratch2, ptr
5558 // cmp scratch, #0
5559 // bne- loopMBB
5560 // fallthrough --> exitMBB
5561 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005562 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5563 if (ldrOpc == ARM::t2LDREX)
5564 MIB.addImm(0);
5565 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005566
5567 // Sign extend the value, if necessary.
5568 if (signExtend && extendOpc) {
Craig Topper420761a2012-04-20 07:30:17 +00005569 oldval = MRI.createVirtualRegister(&ARM::GPRRegClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005570 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5571 .addReg(dest)
5572 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005573 }
5574
5575 // Build compare and cmov instructions.
5576 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5577 .addReg(oldval).addReg(incr));
5578 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5579 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5580
Jim Grosbachb6aed502011-09-09 18:37:27 +00005581 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5582 if (strOpc == ARM::t2STREX)
5583 MIB.addImm(0);
5584 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005585 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5586 .addReg(scratch).addImm(0));
5587 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5588 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5589
5590 BB->addSuccessor(loopMBB);
5591 BB->addSuccessor(exitMBB);
5592
5593 // exitMBB:
5594 // ...
5595 BB = exitMBB;
5596
5597 MI->eraseFromParent(); // The instruction is gone now.
5598
5599 return BB;
5600}
5601
Eli Friedman2bdffe42011-08-31 00:31:29 +00005602MachineBasicBlock *
5603ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
5604 unsigned Op1, unsigned Op2,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005605 bool NeedsCarry, bool IsCmpxchg) const {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005606 // This also handles ATOMIC_SWAP, indicated by Op1==0.
5607 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5608
5609 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5610 MachineFunction *MF = BB->getParent();
5611 MachineFunction::iterator It = BB;
5612 ++It;
5613
5614 unsigned destlo = MI->getOperand(0).getReg();
5615 unsigned desthi = MI->getOperand(1).getReg();
5616 unsigned ptr = MI->getOperand(2).getReg();
5617 unsigned vallo = MI->getOperand(3).getReg();
5618 unsigned valhi = MI->getOperand(4).getReg();
5619 DebugLoc dl = MI->getDebugLoc();
5620 bool isThumb2 = Subtarget->isThumb2();
5621
5622 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5623 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005624 MRI.constrainRegClass(destlo, &ARM::rGPRRegClass);
5625 MRI.constrainRegClass(desthi, &ARM::rGPRRegClass);
5626 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005627 }
5628
5629 unsigned ldrOpc = isThumb2 ? ARM::t2LDREXD : ARM::LDREXD;
5630 unsigned strOpc = isThumb2 ? ARM::t2STREXD : ARM::STREXD;
5631
5632 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman7df496d2011-09-01 22:27:41 +00005633 MachineBasicBlock *contBB = 0, *cont2BB = 0;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005634 if (IsCmpxchg) {
5635 contBB = MF->CreateMachineBasicBlock(LLVM_BB);
5636 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB);
5637 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005638 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5639 MF->insert(It, loopMBB);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005640 if (IsCmpxchg) {
5641 MF->insert(It, contBB);
5642 MF->insert(It, cont2BB);
5643 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005644 MF->insert(It, exitMBB);
5645
5646 // Transfer the remainder of BB and its successor edges to exitMBB.
5647 exitMBB->splice(exitMBB->begin(), BB,
5648 llvm::next(MachineBasicBlock::iterator(MI)),
5649 BB->end());
5650 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5651
Craig Topper420761a2012-04-20 07:30:17 +00005652 const TargetRegisterClass *TRC = isThumb2 ?
5653 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5654 (const TargetRegisterClass*)&ARM::GPRRegClass;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005655 unsigned storesuccess = MRI.createVirtualRegister(TRC);
5656
5657 // thisMBB:
5658 // ...
5659 // fallthrough --> loopMBB
5660 BB->addSuccessor(loopMBB);
5661
5662 // loopMBB:
5663 // ldrexd r2, r3, ptr
5664 // <binopa> r0, r2, incr
5665 // <binopb> r1, r3, incr
5666 // strexd storesuccess, r0, r1, ptr
5667 // cmp storesuccess, #0
5668 // bne- loopMBB
5669 // fallthrough --> exitMBB
5670 //
5671 // Note that the registers are explicitly specified because there is not any
5672 // way to force the register allocator to allocate a register pair.
5673 //
Andrew Trick3af7a672011-09-20 03:06:13 +00005674 // FIXME: The hardcoded registers are not necessary for Thumb2, but we
Eli Friedman2bdffe42011-08-31 00:31:29 +00005675 // need to properly enforce the restriction that the two output registers
5676 // for ldrexd must be different.
5677 BB = loopMBB;
5678 // Load
5679 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc))
5680 .addReg(ARM::R2, RegState::Define)
5681 .addReg(ARM::R3, RegState::Define).addReg(ptr));
5682 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
5683 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo).addReg(ARM::R2);
5684 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi).addReg(ARM::R3);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005685
5686 if (IsCmpxchg) {
5687 // Add early exit
5688 for (unsigned i = 0; i < 2; i++) {
5689 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr :
5690 ARM::CMPrr))
5691 .addReg(i == 0 ? destlo : desthi)
5692 .addReg(i == 0 ? vallo : valhi));
5693 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5694 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5695 BB->addSuccessor(exitMBB);
5696 BB->addSuccessor(i == 0 ? contBB : cont2BB);
5697 BB = (i == 0 ? contBB : cont2BB);
5698 }
5699
5700 // Copy to physregs for strexd
5701 unsigned setlo = MI->getOperand(5).getReg();
5702 unsigned sethi = MI->getOperand(6).getReg();
5703 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(setlo);
5704 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(sethi);
5705 } else if (Op1) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005706 // Perform binary operation
5707 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), ARM::R0)
5708 .addReg(destlo).addReg(vallo))
5709 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
5710 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), ARM::R1)
5711 .addReg(desthi).addReg(valhi)).addReg(0);
5712 } else {
5713 // Copy to physregs for strexd
5714 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(vallo);
5715 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(valhi);
5716 }
5717
5718 // Store
5719 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), storesuccess)
5720 .addReg(ARM::R0).addReg(ARM::R1).addReg(ptr));
5721 // Cmp+jump
5722 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5723 .addReg(storesuccess).addImm(0));
5724 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5725 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5726
5727 BB->addSuccessor(loopMBB);
5728 BB->addSuccessor(exitMBB);
5729
5730 // exitMBB:
5731 // ...
5732 BB = exitMBB;
5733
5734 MI->eraseFromParent(); // The instruction is gone now.
5735
5736 return BB;
5737}
5738
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005739/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
5740/// registers the function context.
5741void ARMTargetLowering::
5742SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
5743 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005744 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5745 DebugLoc dl = MI->getDebugLoc();
5746 MachineFunction *MF = MBB->getParent();
5747 MachineRegisterInfo *MRI = &MF->getRegInfo();
5748 MachineConstantPool *MCP = MF->getConstantPool();
5749 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5750 const Function *F = MF->getFunction();
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005751
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005752 bool isThumb = Subtarget->isThumb();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005753 bool isThumb2 = Subtarget->isThumb2();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005754
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005755 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005756 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005757 ARMConstantPoolValue *CPV =
5758 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
5759 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
5760
Craig Topper420761a2012-04-20 07:30:17 +00005761 const TargetRegisterClass *TRC = isThumb ?
5762 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5763 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005764
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005765 // Grab constant pool and fixed stack memory operands.
5766 MachineMemOperand *CPMMO =
5767 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
5768 MachineMemOperand::MOLoad, 4, 4);
5769
5770 MachineMemOperand *FIMMOSt =
5771 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
5772 MachineMemOperand::MOStore, 4, 4);
5773
5774 // Load the address of the dispatch MBB into the jump buffer.
5775 if (isThumb2) {
5776 // Incoming value: jbuf
5777 // ldr.n r5, LCPI1_1
5778 // orr r5, r5, #1
5779 // add r5, pc
5780 // str r5, [$jbuf, #+4] ; &jbuf[1]
5781 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5782 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
5783 .addConstantPoolIndex(CPI)
5784 .addMemOperand(CPMMO));
5785 // Set the low bit because of thumb mode.
5786 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5787 AddDefaultCC(
5788 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
5789 .addReg(NewVReg1, RegState::Kill)
5790 .addImm(0x01)));
5791 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5792 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
5793 .addReg(NewVReg2, RegState::Kill)
5794 .addImm(PCLabelId);
5795 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
5796 .addReg(NewVReg3, RegState::Kill)
5797 .addFrameIndex(FI)
5798 .addImm(36) // &jbuf[1] :: pc
5799 .addMemOperand(FIMMOSt));
5800 } else if (isThumb) {
5801 // Incoming value: jbuf
5802 // ldr.n r1, LCPI1_4
5803 // add r1, pc
5804 // mov r2, #1
5805 // orrs r1, r2
5806 // add r2, $jbuf, #+4 ; &jbuf[1]
5807 // str r1, [r2]
5808 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5809 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
5810 .addConstantPoolIndex(CPI)
5811 .addMemOperand(CPMMO));
5812 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5813 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
5814 .addReg(NewVReg1, RegState::Kill)
5815 .addImm(PCLabelId);
5816 // Set the low bit because of thumb mode.
5817 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5818 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
5819 .addReg(ARM::CPSR, RegState::Define)
5820 .addImm(1));
5821 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5822 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
5823 .addReg(ARM::CPSR, RegState::Define)
5824 .addReg(NewVReg2, RegState::Kill)
5825 .addReg(NewVReg3, RegState::Kill));
5826 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5827 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
5828 .addFrameIndex(FI)
5829 .addImm(36)); // &jbuf[1] :: pc
5830 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
5831 .addReg(NewVReg4, RegState::Kill)
5832 .addReg(NewVReg5, RegState::Kill)
5833 .addImm(0)
5834 .addMemOperand(FIMMOSt));
5835 } else {
5836 // Incoming value: jbuf
5837 // ldr r1, LCPI1_1
5838 // add r1, pc, r1
5839 // str r1, [$jbuf, #+4] ; &jbuf[1]
5840 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5841 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
5842 .addConstantPoolIndex(CPI)
5843 .addImm(0)
5844 .addMemOperand(CPMMO));
5845 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5846 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
5847 .addReg(NewVReg1, RegState::Kill)
5848 .addImm(PCLabelId));
5849 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
5850 .addReg(NewVReg2, RegState::Kill)
5851 .addFrameIndex(FI)
5852 .addImm(36) // &jbuf[1] :: pc
5853 .addMemOperand(FIMMOSt));
5854 }
5855}
5856
5857MachineBasicBlock *ARMTargetLowering::
5858EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
5859 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5860 DebugLoc dl = MI->getDebugLoc();
5861 MachineFunction *MF = MBB->getParent();
5862 MachineRegisterInfo *MRI = &MF->getRegInfo();
5863 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5864 MachineFrameInfo *MFI = MF->getFrameInfo();
5865 int FI = MFI->getFunctionContextIndex();
5866
Craig Topper420761a2012-04-20 07:30:17 +00005867 const TargetRegisterClass *TRC = Subtarget->isThumb() ?
5868 (const TargetRegisterClass*)&ARM::tGPRRegClass :
Jakob Stoklund Olesen027c32a2012-05-20 06:38:47 +00005869 (const TargetRegisterClass*)&ARM::GPRnopcRegClass;
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005870
Bill Wendling04f15b42011-10-06 21:29:56 +00005871 // Get a mapping of the call site numbers to all of the landing pads they're
5872 // associated with.
Bill Wendling2a850152011-10-05 00:02:33 +00005873 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
5874 unsigned MaxCSNum = 0;
5875 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbachd4f020a2012-04-06 23:43:50 +00005876 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
5877 ++BB) {
Bill Wendling2a850152011-10-05 00:02:33 +00005878 if (!BB->isLandingPad()) continue;
5879
5880 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
5881 // pad.
5882 for (MachineBasicBlock::iterator
5883 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
5884 if (!II->isEHLabel()) continue;
5885
5886 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendling5cbef192011-10-05 23:28:57 +00005887 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling2a850152011-10-05 00:02:33 +00005888
Bill Wendling5cbef192011-10-05 23:28:57 +00005889 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
5890 for (SmallVectorImpl<unsigned>::iterator
5891 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
5892 CSI != CSE; ++CSI) {
5893 CallSiteNumToLPad[*CSI].push_back(BB);
5894 MaxCSNum = std::max(MaxCSNum, *CSI);
5895 }
Bill Wendling2a850152011-10-05 00:02:33 +00005896 break;
5897 }
5898 }
5899
5900 // Get an ordered list of the machine basic blocks for the jump table.
5901 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling2acf6382011-10-07 23:18:02 +00005902 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling2a850152011-10-05 00:02:33 +00005903 LPadList.reserve(CallSiteNumToLPad.size());
5904 for (unsigned I = 1; I <= MaxCSNum; ++I) {
5905 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
5906 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00005907 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling2a850152011-10-05 00:02:33 +00005908 LPadList.push_back(*II);
Bill Wendling2acf6382011-10-07 23:18:02 +00005909 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
5910 }
Bill Wendling2a850152011-10-05 00:02:33 +00005911 }
5912
Bill Wendling5cbef192011-10-05 23:28:57 +00005913 assert(!LPadList.empty() &&
5914 "No landing pad destinations for the dispatch jump table!");
5915
Bill Wendling04f15b42011-10-06 21:29:56 +00005916 // Create the jump table and associated information.
Bill Wendling2a850152011-10-05 00:02:33 +00005917 MachineJumpTableInfo *JTI =
5918 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
5919 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
5920 unsigned UId = AFI->createJumpTableUId();
5921
Bill Wendling04f15b42011-10-06 21:29:56 +00005922 // Create the MBBs for the dispatch code.
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005923
5924 // Shove the dispatch's address into the return slot in the function context.
5925 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
5926 DispatchBB->setIsLandingPad();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005927
Bill Wendlingbb734682011-10-05 00:39:32 +00005928 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Bill Wendling083a8eb2011-10-06 23:37:36 +00005929 BuildMI(TrapBB, dl, TII->get(Subtarget->isThumb() ? ARM::tTRAP : ARM::TRAP));
Bill Wendlingbb734682011-10-05 00:39:32 +00005930 DispatchBB->addSuccessor(TrapBB);
5931
5932 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
5933 DispatchBB->addSuccessor(DispContBB);
Bill Wendling2a850152011-10-05 00:02:33 +00005934
Bill Wendlinga48ed4f2011-10-17 21:32:56 +00005935 // Insert and MBBs.
Bill Wendling930193c2011-10-06 00:53:33 +00005936 MF->insert(MF->end(), DispatchBB);
5937 MF->insert(MF->end(), DispContBB);
5938 MF->insert(MF->end(), TrapBB);
Bill Wendling930193c2011-10-06 00:53:33 +00005939
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005940 // Insert code into the entry block that creates and registers the function
5941 // context.
5942 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
5943
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005944 MachineMemOperand *FIMMOLd =
Bill Wendling04f15b42011-10-06 21:29:56 +00005945 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendling083a8eb2011-10-06 23:37:36 +00005946 MachineMemOperand::MOLoad |
5947 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling930193c2011-10-06 00:53:33 +00005948
Bob Wilsonf4aea8f2011-12-22 23:39:48 +00005949 if (AFI->isThumb1OnlyFunction())
5950 BuildMI(DispatchBB, dl, TII->get(ARM::tInt_eh_sjlj_dispatchsetup));
5951 else if (!Subtarget->hasVFP2())
5952 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup_nofp));
Lang Hamesc0a9f822012-03-29 21:56:11 +00005953 else
Bob Wilsonf4aea8f2011-12-22 23:39:48 +00005954 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
Bob Wilsoneaab6ef2011-11-16 07:11:57 +00005955
Bill Wendling952cb502011-10-18 22:49:07 +00005956 unsigned NumLPads = LPadList.size();
Bill Wendling95ce2e92011-10-06 22:53:00 +00005957 if (Subtarget->isThumb2()) {
5958 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5959 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
5960 .addFrameIndex(FI)
5961 .addImm(4)
5962 .addMemOperand(FIMMOLd));
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005963
Bill Wendling952cb502011-10-18 22:49:07 +00005964 if (NumLPads < 256) {
5965 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
5966 .addReg(NewVReg1)
5967 .addImm(LPadList.size()));
5968 } else {
5969 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5970 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00005971 .addImm(NumLPads & 0xFFFF));
5972
5973 unsigned VReg2 = VReg1;
5974 if ((NumLPads & 0xFFFF0000) != 0) {
5975 VReg2 = MRI->createVirtualRegister(TRC);
5976 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
5977 .addReg(VReg1)
5978 .addImm(NumLPads >> 16));
5979 }
5980
Bill Wendling952cb502011-10-18 22:49:07 +00005981 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
5982 .addReg(NewVReg1)
5983 .addReg(VReg2));
5984 }
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005985
Bill Wendling95ce2e92011-10-06 22:53:00 +00005986 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
5987 .addMBB(TrapBB)
5988 .addImm(ARMCC::HI)
5989 .addReg(ARM::CPSR);
Bill Wendlingbb734682011-10-05 00:39:32 +00005990
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005991 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5992 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005993 .addJumpTableIndex(MJTI)
5994 .addImm(UId));
Bill Wendling2a850152011-10-05 00:02:33 +00005995
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005996 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005997 AddDefaultCC(
5998 AddDefaultPred(
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005999 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
6000 .addReg(NewVReg3, RegState::Kill)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006001 .addReg(NewVReg1)
6002 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
6003
6004 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006005 .addReg(NewVReg4, RegState::Kill)
Bill Wendling2a850152011-10-05 00:02:33 +00006006 .addReg(NewVReg1)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006007 .addJumpTableIndex(MJTI)
6008 .addImm(UId);
6009 } else if (Subtarget->isThumb()) {
Bill Wendling083a8eb2011-10-06 23:37:36 +00006010 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6011 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
6012 .addFrameIndex(FI)
6013 .addImm(1)
6014 .addMemOperand(FIMMOLd));
Bill Wendlingf1083d42011-10-07 22:08:37 +00006015
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006016 if (NumLPads < 256) {
6017 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
6018 .addReg(NewVReg1)
6019 .addImm(NumLPads));
6020 } else {
6021 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling922ad782011-10-19 09:24:02 +00006022 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6023 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6024
6025 // MachineConstantPool wants an explicit alignment.
6026 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
6027 if (Align == 0)
6028 Align = getTargetData()->getTypeAllocSize(C->getType());
6029 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006030
6031 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6032 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6033 .addReg(VReg1, RegState::Define)
6034 .addConstantPoolIndex(Idx));
6035 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6036 .addReg(NewVReg1)
6037 .addReg(VReg1));
6038 }
6039
Bill Wendling083a8eb2011-10-06 23:37:36 +00006040 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6041 .addMBB(TrapBB)
6042 .addImm(ARMCC::HI)
6043 .addReg(ARM::CPSR);
6044
6045 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6046 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6047 .addReg(ARM::CPSR, RegState::Define)
6048 .addReg(NewVReg1)
6049 .addImm(2));
6050
6051 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling217f0e92011-10-06 23:41:14 +00006052 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendling083a8eb2011-10-06 23:37:36 +00006053 .addJumpTableIndex(MJTI)
6054 .addImm(UId));
6055
6056 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6057 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6058 .addReg(ARM::CPSR, RegState::Define)
6059 .addReg(NewVReg2, RegState::Kill)
6060 .addReg(NewVReg3));
6061
6062 MachineMemOperand *JTMMOLd =
6063 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6064 MachineMemOperand::MOLoad, 4, 4);
6065
6066 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6067 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6068 .addReg(NewVReg4, RegState::Kill)
6069 .addImm(0)
6070 .addMemOperand(JTMMOLd));
6071
6072 unsigned NewVReg6 = MRI->createVirtualRegister(TRC);
6073 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6074 .addReg(ARM::CPSR, RegState::Define)
6075 .addReg(NewVReg5, RegState::Kill)
6076 .addReg(NewVReg3));
6077
6078 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6079 .addReg(NewVReg6, RegState::Kill)
6080 .addJumpTableIndex(MJTI)
6081 .addImm(UId);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006082 } else {
6083 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6084 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6085 .addFrameIndex(FI)
6086 .addImm(4)
6087 .addMemOperand(FIMMOLd));
Bill Wendling564392b2011-10-18 22:11:18 +00006088
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006089 if (NumLPads < 256) {
6090 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6091 .addReg(NewVReg1)
6092 .addImm(NumLPads));
Bill Wendling922ad782011-10-19 09:24:02 +00006093 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006094 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6095 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00006096 .addImm(NumLPads & 0xFFFF));
6097
6098 unsigned VReg2 = VReg1;
6099 if ((NumLPads & 0xFFFF0000) != 0) {
6100 VReg2 = MRI->createVirtualRegister(TRC);
6101 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6102 .addReg(VReg1)
6103 .addImm(NumLPads >> 16));
6104 }
6105
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006106 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6107 .addReg(NewVReg1)
6108 .addReg(VReg2));
Bill Wendling922ad782011-10-19 09:24:02 +00006109 } else {
6110 MachineConstantPool *ConstantPool = MF->getConstantPool();
6111 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6112 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6113
6114 // MachineConstantPool wants an explicit alignment.
6115 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
6116 if (Align == 0)
6117 Align = getTargetData()->getTypeAllocSize(C->getType());
6118 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6119
6120 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6121 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6122 .addReg(VReg1, RegState::Define)
Bill Wendling767f8be2011-10-20 20:37:11 +00006123 .addConstantPoolIndex(Idx)
6124 .addImm(0));
Bill Wendling922ad782011-10-19 09:24:02 +00006125 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6126 .addReg(NewVReg1)
6127 .addReg(VReg1, RegState::Kill));
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006128 }
6129
Bill Wendling95ce2e92011-10-06 22:53:00 +00006130 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6131 .addMBB(TrapBB)
6132 .addImm(ARMCC::HI)
6133 .addReg(ARM::CPSR);
Bill Wendling2a850152011-10-05 00:02:33 +00006134
Bill Wendling564392b2011-10-18 22:11:18 +00006135 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006136 AddDefaultCC(
Bill Wendling564392b2011-10-18 22:11:18 +00006137 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006138 .addReg(NewVReg1)
6139 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling564392b2011-10-18 22:11:18 +00006140 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6141 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006142 .addJumpTableIndex(MJTI)
6143 .addImm(UId));
6144
6145 MachineMemOperand *JTMMOLd =
6146 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6147 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling564392b2011-10-18 22:11:18 +00006148 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006149 AddDefaultPred(
Bill Wendling564392b2011-10-18 22:11:18 +00006150 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6151 .addReg(NewVReg3, RegState::Kill)
6152 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006153 .addImm(0)
6154 .addMemOperand(JTMMOLd));
6155
6156 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
Bill Wendling564392b2011-10-18 22:11:18 +00006157 .addReg(NewVReg5, RegState::Kill)
6158 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006159 .addJumpTableIndex(MJTI)
6160 .addImm(UId);
6161 }
Bill Wendling2a850152011-10-05 00:02:33 +00006162
Bill Wendlingbb734682011-10-05 00:39:32 +00006163 // Add the jump table entries as successors to the MBB.
Bill Wendling2acf6382011-10-07 23:18:02 +00006164 MachineBasicBlock *PrevMBB = 0;
Bill Wendlingbb734682011-10-05 00:39:32 +00006165 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006166 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6167 MachineBasicBlock *CurMBB = *I;
6168 if (PrevMBB != CurMBB)
6169 DispContBB->addSuccessor(CurMBB);
6170 PrevMBB = CurMBB;
6171 }
6172
Bill Wendling24bb9252011-10-17 05:25:09 +00006173 // N.B. the order the invoke BBs are processed in doesn't matter here.
Bill Wendling969c9ef2011-10-14 23:34:37 +00006174 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6175 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
Craig Topper015f2282012-03-04 03:33:22 +00006176 const uint16_t *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006177 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling2acf6382011-10-07 23:18:02 +00006178 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6179 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6180 MachineBasicBlock *BB = *I;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006181
6182 // Remove the landing pad successor from the invoke block and replace it
6183 // with the new dispatch block.
Bill Wendlingde39d862011-10-26 07:16:18 +00006184 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6185 BB->succ_end());
6186 while (!Successors.empty()) {
6187 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling2acf6382011-10-07 23:18:02 +00006188 if (SMBB->isLandingPad()) {
6189 BB->removeSuccessor(SMBB);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006190 MBBLPads.push_back(SMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006191 }
6192 }
6193
6194 BB->addSuccessor(DispatchBB);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006195
6196 // Find the invoke call and mark all of the callee-saved registers as
6197 // 'implicit defined' so that they're spilled. This prevents code from
6198 // moving instructions to before the EH block, where they will never be
6199 // executed.
6200 for (MachineBasicBlock::reverse_iterator
6201 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006202 if (!II->isCall()) continue;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006203
6204 DenseMap<unsigned, bool> DefRegs;
6205 for (MachineInstr::mop_iterator
6206 OI = II->operands_begin(), OE = II->operands_end();
6207 OI != OE; ++OI) {
6208 if (!OI->isReg()) continue;
6209 DefRegs[OI->getReg()] = true;
6210 }
6211
6212 MachineInstrBuilder MIB(&*II);
6213
Bill Wendling5d798592011-10-14 23:55:44 +00006214 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006215 unsigned Reg = SavedRegs[i];
6216 if (Subtarget->isThumb2() &&
Craig Topper420761a2012-04-20 07:30:17 +00006217 !ARM::tGPRRegClass.contains(Reg) &&
6218 !ARM::hGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006219 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006220 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006221 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006222 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006223 continue;
6224 if (!DefRegs[Reg])
6225 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling5d798592011-10-14 23:55:44 +00006226 }
Bill Wendling969c9ef2011-10-14 23:34:37 +00006227
6228 break;
6229 }
Bill Wendling2acf6382011-10-07 23:18:02 +00006230 }
Bill Wendlingbb734682011-10-05 00:39:32 +00006231
Bill Wendlingf7b02072011-10-18 18:30:49 +00006232 // Mark all former landing pads as non-landing pads. The dispatch is the only
6233 // landing pad now.
6234 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6235 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6236 (*I)->setIsLandingPad(false);
6237
Bill Wendlingbb734682011-10-05 00:39:32 +00006238 // The instruction is gone now.
6239 MI->eraseFromParent();
6240
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006241 return MBB;
6242}
6243
Evan Cheng218977b2010-07-13 19:27:42 +00006244static
6245MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6246 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6247 E = MBB->succ_end(); I != E; ++I)
6248 if (*I != Succ)
6249 return *I;
6250 llvm_unreachable("Expecting a BB with two successors!");
6251}
6252
Manman Ren68f25572012-06-01 19:33:18 +00006253MachineBasicBlock *ARMTargetLowering::
6254EmitStructByval(MachineInstr *MI, MachineBasicBlock *BB) const {
6255 // This pseudo instruction has 3 operands: dst, src, size
6256 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
6257 // Otherwise, we will generate unrolled scalar copies.
6258 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6259 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6260 MachineFunction::iterator It = BB;
6261 ++It;
6262
6263 unsigned dest = MI->getOperand(0).getReg();
6264 unsigned src = MI->getOperand(1).getReg();
6265 unsigned SizeVal = MI->getOperand(2).getImm();
6266 unsigned Align = MI->getOperand(3).getImm();
6267 DebugLoc dl = MI->getDebugLoc();
6268
6269 bool isThumb2 = Subtarget->isThumb2();
6270 MachineFunction *MF = BB->getParent();
6271 MachineRegisterInfo &MRI = MF->getRegInfo();
Manman Reneda9fdf2012-06-18 22:23:48 +00006272 unsigned ldrOpc, strOpc, UnitSize = 0;
Manman Ren68f25572012-06-01 19:33:18 +00006273
6274 const TargetRegisterClass *TRC = isThumb2 ?
6275 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6276 (const TargetRegisterClass*)&ARM::GPRRegClass;
Manman Reneda9fdf2012-06-18 22:23:48 +00006277 const TargetRegisterClass *TRC_Vec = 0;
Manman Ren68f25572012-06-01 19:33:18 +00006278
6279 if (Align & 1) {
6280 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6281 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6282 UnitSize = 1;
6283 } else if (Align & 2) {
6284 ldrOpc = isThumb2 ? ARM::t2LDRH_POST : ARM::LDRH_POST;
6285 strOpc = isThumb2 ? ARM::t2STRH_POST : ARM::STRH_POST;
6286 UnitSize = 2;
6287 } else {
Manman Reneda9fdf2012-06-18 22:23:48 +00006288 // Check whether we can use NEON instructions.
6289 if (!MF->getFunction()->hasFnAttr(Attribute::NoImplicitFloat) &&
6290 Subtarget->hasNEON()) {
6291 if ((Align % 16 == 0) && SizeVal >= 16) {
6292 ldrOpc = ARM::VLD1q32wb_fixed;
6293 strOpc = ARM::VST1q32wb_fixed;
6294 UnitSize = 16;
6295 TRC_Vec = (const TargetRegisterClass*)&ARM::DPairRegClass;
6296 }
6297 else if ((Align % 8 == 0) && SizeVal >= 8) {
6298 ldrOpc = ARM::VLD1d32wb_fixed;
6299 strOpc = ARM::VST1d32wb_fixed;
6300 UnitSize = 8;
6301 TRC_Vec = (const TargetRegisterClass*)&ARM::DPRRegClass;
6302 }
6303 }
6304 // Can't use NEON instructions.
6305 if (UnitSize == 0) {
6306 ldrOpc = isThumb2 ? ARM::t2LDR_POST : ARM::LDR_POST_IMM;
6307 strOpc = isThumb2 ? ARM::t2STR_POST : ARM::STR_POST_IMM;
6308 UnitSize = 4;
6309 }
Manman Ren68f25572012-06-01 19:33:18 +00006310 }
Manman Reneda9fdf2012-06-18 22:23:48 +00006311
Manman Ren68f25572012-06-01 19:33:18 +00006312 unsigned BytesLeft = SizeVal % UnitSize;
6313 unsigned LoopSize = SizeVal - BytesLeft;
6314
6315 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
6316 // Use LDR and STR to copy.
6317 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
6318 // [destOut] = STR_POST(scratch, destIn, UnitSize)
6319 unsigned srcIn = src;
6320 unsigned destIn = dest;
6321 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
Manman Reneda9fdf2012-06-18 22:23:48 +00006322 unsigned scratch = MRI.createVirtualRegister(UnitSize >= 8 ? TRC_Vec:TRC);
Manman Ren68f25572012-06-01 19:33:18 +00006323 unsigned srcOut = MRI.createVirtualRegister(TRC);
6324 unsigned destOut = MRI.createVirtualRegister(TRC);
Manman Reneda9fdf2012-06-18 22:23:48 +00006325 if (UnitSize >= 8) {
6326 AddDefaultPred(BuildMI(*BB, MI, dl,
6327 TII->get(ldrOpc), scratch)
6328 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(0));
6329
6330 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6331 .addReg(destIn).addImm(0).addReg(scratch));
6332 } else if (isThumb2) {
Manman Ren68f25572012-06-01 19:33:18 +00006333 AddDefaultPred(BuildMI(*BB, MI, dl,
6334 TII->get(ldrOpc), scratch)
6335 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(UnitSize));
6336
6337 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6338 .addReg(scratch).addReg(destIn)
6339 .addImm(UnitSize));
6340 } else {
6341 AddDefaultPred(BuildMI(*BB, MI, dl,
6342 TII->get(ldrOpc), scratch)
6343 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0)
6344 .addImm(UnitSize));
6345
6346 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6347 .addReg(scratch).addReg(destIn)
6348 .addReg(0).addImm(UnitSize));
6349 }
6350 srcIn = srcOut;
6351 destIn = destOut;
6352 }
6353
6354 // Handle the leftover bytes with LDRB and STRB.
6355 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
6356 // [destOut] = STRB_POST(scratch, destIn, 1)
6357 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6358 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6359 for (unsigned i = 0; i < BytesLeft; i++) {
6360 unsigned scratch = MRI.createVirtualRegister(TRC);
6361 unsigned srcOut = MRI.createVirtualRegister(TRC);
6362 unsigned destOut = MRI.createVirtualRegister(TRC);
6363 if (isThumb2) {
6364 AddDefaultPred(BuildMI(*BB, MI, dl,
6365 TII->get(ldrOpc),scratch)
6366 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6367
6368 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6369 .addReg(scratch).addReg(destIn)
6370 .addReg(0).addImm(1));
6371 } else {
6372 AddDefaultPred(BuildMI(*BB, MI, dl,
6373 TII->get(ldrOpc),scratch)
6374 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6375
6376 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6377 .addReg(scratch).addReg(destIn)
6378 .addReg(0).addImm(1));
6379 }
6380 srcIn = srcOut;
6381 destIn = destOut;
6382 }
6383 MI->eraseFromParent(); // The instruction is gone now.
6384 return BB;
6385 }
6386
6387 // Expand the pseudo op to a loop.
6388 // thisMBB:
6389 // ...
6390 // movw varEnd, # --> with thumb2
6391 // movt varEnd, #
6392 // ldrcp varEnd, idx --> without thumb2
6393 // fallthrough --> loopMBB
6394 // loopMBB:
6395 // PHI varPhi, varEnd, varLoop
6396 // PHI srcPhi, src, srcLoop
6397 // PHI destPhi, dst, destLoop
6398 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6399 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
6400 // subs varLoop, varPhi, #UnitSize
6401 // bne loopMBB
6402 // fallthrough --> exitMBB
6403 // exitMBB:
6404 // epilogue to handle left-over bytes
6405 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6406 // [destOut] = STRB_POST(scratch, destLoop, 1)
6407 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6408 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6409 MF->insert(It, loopMBB);
6410 MF->insert(It, exitMBB);
6411
6412 // Transfer the remainder of BB and its successor edges to exitMBB.
6413 exitMBB->splice(exitMBB->begin(), BB,
6414 llvm::next(MachineBasicBlock::iterator(MI)),
6415 BB->end());
6416 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
6417
6418 // Load an immediate to varEnd.
6419 unsigned varEnd = MRI.createVirtualRegister(TRC);
6420 if (isThumb2) {
6421 unsigned VReg1 = varEnd;
6422 if ((LoopSize & 0xFFFF0000) != 0)
6423 VReg1 = MRI.createVirtualRegister(TRC);
6424 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVi16), VReg1)
6425 .addImm(LoopSize & 0xFFFF));
6426
6427 if ((LoopSize & 0xFFFF0000) != 0)
6428 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVTi16), varEnd)
6429 .addReg(VReg1)
6430 .addImm(LoopSize >> 16));
6431 } else {
6432 MachineConstantPool *ConstantPool = MF->getConstantPool();
6433 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6434 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
6435
6436 // MachineConstantPool wants an explicit alignment.
6437 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
6438 if (Align == 0)
6439 Align = getTargetData()->getTypeAllocSize(C->getType());
6440 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6441
6442 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::LDRcp))
6443 .addReg(varEnd, RegState::Define)
6444 .addConstantPoolIndex(Idx)
6445 .addImm(0));
6446 }
6447 BB->addSuccessor(loopMBB);
6448
6449 // Generate the loop body:
6450 // varPhi = PHI(varLoop, varEnd)
6451 // srcPhi = PHI(srcLoop, src)
6452 // destPhi = PHI(destLoop, dst)
6453 MachineBasicBlock *entryBB = BB;
6454 BB = loopMBB;
6455 unsigned varLoop = MRI.createVirtualRegister(TRC);
6456 unsigned varPhi = MRI.createVirtualRegister(TRC);
6457 unsigned srcLoop = MRI.createVirtualRegister(TRC);
6458 unsigned srcPhi = MRI.createVirtualRegister(TRC);
6459 unsigned destLoop = MRI.createVirtualRegister(TRC);
6460 unsigned destPhi = MRI.createVirtualRegister(TRC);
6461
6462 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
6463 .addReg(varLoop).addMBB(loopMBB)
6464 .addReg(varEnd).addMBB(entryBB);
6465 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
6466 .addReg(srcLoop).addMBB(loopMBB)
6467 .addReg(src).addMBB(entryBB);
6468 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
6469 .addReg(destLoop).addMBB(loopMBB)
6470 .addReg(dest).addMBB(entryBB);
6471
6472 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6473 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
Manman Reneda9fdf2012-06-18 22:23:48 +00006474 unsigned scratch = MRI.createVirtualRegister(UnitSize >= 8 ? TRC_Vec:TRC);
6475 if (UnitSize >= 8) {
6476 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6477 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(0));
6478
6479 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6480 .addReg(destPhi).addImm(0).addReg(scratch));
6481 } else if (isThumb2) {
Manman Ren68f25572012-06-01 19:33:18 +00006482 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6483 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(UnitSize));
6484
6485 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6486 .addReg(scratch).addReg(destPhi)
6487 .addImm(UnitSize));
6488 } else {
6489 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6490 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addReg(0)
6491 .addImm(UnitSize));
6492
6493 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6494 .addReg(scratch).addReg(destPhi)
6495 .addReg(0).addImm(UnitSize));
6496 }
6497
6498 // Decrement loop variable by UnitSize.
6499 MachineInstrBuilder MIB = BuildMI(BB, dl,
6500 TII->get(isThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
6501 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
6502 MIB->getOperand(5).setReg(ARM::CPSR);
6503 MIB->getOperand(5).setIsDef(true);
6504
6505 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6506 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
6507
6508 // loopMBB can loop back to loopMBB or fall through to exitMBB.
6509 BB->addSuccessor(loopMBB);
6510 BB->addSuccessor(exitMBB);
6511
6512 // Add epilogue to handle BytesLeft.
6513 BB = exitMBB;
6514 MachineInstr *StartOfExit = exitMBB->begin();
6515 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6516 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6517
6518 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6519 // [destOut] = STRB_POST(scratch, destLoop, 1)
6520 unsigned srcIn = srcLoop;
6521 unsigned destIn = destLoop;
6522 for (unsigned i = 0; i < BytesLeft; i++) {
6523 unsigned scratch = MRI.createVirtualRegister(TRC);
6524 unsigned srcOut = MRI.createVirtualRegister(TRC);
6525 unsigned destOut = MRI.createVirtualRegister(TRC);
6526 if (isThumb2) {
6527 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
6528 TII->get(ldrOpc),scratch)
6529 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6530
6531 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
6532 .addReg(scratch).addReg(destIn)
6533 .addImm(1));
6534 } else {
6535 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
6536 TII->get(ldrOpc),scratch)
6537 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0).addImm(1));
6538
6539 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
6540 .addReg(scratch).addReg(destIn)
6541 .addReg(0).addImm(1));
6542 }
6543 srcIn = srcOut;
6544 destIn = destOut;
6545 }
6546
6547 MI->eraseFromParent(); // The instruction is gone now.
6548 return BB;
6549}
6550
Jim Grosbache801dc42009-12-12 01:40:06 +00006551MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006552ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00006553 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006554 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00006555 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006556 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00006557 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00006558 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00006559 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00006560 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00006561 }
Jim Grosbachee2c2a42011-09-16 21:55:56 +00006562 // The Thumb2 pre-indexed stores have the same MI operands, they just
6563 // define them differently in the .td files from the isel patterns, so
6564 // they need pseudos.
6565 case ARM::t2STR_preidx:
6566 MI->setDesc(TII->get(ARM::t2STR_PRE));
6567 return BB;
6568 case ARM::t2STRB_preidx:
6569 MI->setDesc(TII->get(ARM::t2STRB_PRE));
6570 return BB;
6571 case ARM::t2STRH_preidx:
6572 MI->setDesc(TII->get(ARM::t2STRH_PRE));
6573 return BB;
6574
Jim Grosbach19dec202011-08-05 20:35:44 +00006575 case ARM::STRi_preidx:
6576 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00006577 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00006578 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
6579 // Decode the offset.
6580 unsigned Offset = MI->getOperand(4).getImm();
6581 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
6582 Offset = ARM_AM::getAM2Offset(Offset);
6583 if (isSub)
6584 Offset = -Offset;
6585
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006586 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00006587 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00006588 .addOperand(MI->getOperand(0)) // Rn_wb
6589 .addOperand(MI->getOperand(1)) // Rt
6590 .addOperand(MI->getOperand(2)) // Rn
6591 .addImm(Offset) // offset (skip GPR==zero_reg)
6592 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006593 .addOperand(MI->getOperand(6))
6594 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00006595 MI->eraseFromParent();
6596 return BB;
6597 }
6598 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00006599 case ARM::STRBr_preidx:
6600 case ARM::STRH_preidx: {
6601 unsigned NewOpc;
6602 switch (MI->getOpcode()) {
6603 default: llvm_unreachable("unexpected opcode!");
6604 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
6605 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
6606 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
6607 }
Jim Grosbach19dec202011-08-05 20:35:44 +00006608 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
6609 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
6610 MIB.addOperand(MI->getOperand(i));
6611 MI->eraseFromParent();
6612 return BB;
6613 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006614 case ARM::ATOMIC_LOAD_ADD_I8:
6615 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6616 case ARM::ATOMIC_LOAD_ADD_I16:
6617 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6618 case ARM::ATOMIC_LOAD_ADD_I32:
6619 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006620
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006621 case ARM::ATOMIC_LOAD_AND_I8:
6622 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6623 case ARM::ATOMIC_LOAD_AND_I16:
6624 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6625 case ARM::ATOMIC_LOAD_AND_I32:
6626 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006627
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006628 case ARM::ATOMIC_LOAD_OR_I8:
6629 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6630 case ARM::ATOMIC_LOAD_OR_I16:
6631 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6632 case ARM::ATOMIC_LOAD_OR_I32:
6633 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006634
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006635 case ARM::ATOMIC_LOAD_XOR_I8:
6636 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6637 case ARM::ATOMIC_LOAD_XOR_I16:
6638 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6639 case ARM::ATOMIC_LOAD_XOR_I32:
6640 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006641
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006642 case ARM::ATOMIC_LOAD_NAND_I8:
6643 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6644 case ARM::ATOMIC_LOAD_NAND_I16:
6645 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6646 case ARM::ATOMIC_LOAD_NAND_I32:
6647 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006648
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006649 case ARM::ATOMIC_LOAD_SUB_I8:
6650 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6651 case ARM::ATOMIC_LOAD_SUB_I16:
6652 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6653 case ARM::ATOMIC_LOAD_SUB_I32:
6654 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006655
Jim Grosbachf7da8822011-04-26 19:44:18 +00006656 case ARM::ATOMIC_LOAD_MIN_I8:
6657 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
6658 case ARM::ATOMIC_LOAD_MIN_I16:
6659 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
6660 case ARM::ATOMIC_LOAD_MIN_I32:
6661 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
6662
6663 case ARM::ATOMIC_LOAD_MAX_I8:
6664 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
6665 case ARM::ATOMIC_LOAD_MAX_I16:
6666 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
6667 case ARM::ATOMIC_LOAD_MAX_I32:
6668 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
6669
6670 case ARM::ATOMIC_LOAD_UMIN_I8:
6671 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
6672 case ARM::ATOMIC_LOAD_UMIN_I16:
6673 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
6674 case ARM::ATOMIC_LOAD_UMIN_I32:
6675 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
6676
6677 case ARM::ATOMIC_LOAD_UMAX_I8:
6678 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
6679 case ARM::ATOMIC_LOAD_UMAX_I16:
6680 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
6681 case ARM::ATOMIC_LOAD_UMAX_I32:
6682 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
6683
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006684 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
6685 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
6686 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00006687
6688 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
6689 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
6690 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006691
Eli Friedman2bdffe42011-08-31 00:31:29 +00006692
6693 case ARM::ATOMADD6432:
6694 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006695 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr,
6696 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006697 case ARM::ATOMSUB6432:
6698 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006699 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6700 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006701 case ARM::ATOMOR6432:
6702 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006703 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006704 case ARM::ATOMXOR6432:
6705 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006706 isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006707 case ARM::ATOMAND6432:
6708 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006709 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006710 case ARM::ATOMSWAP6432:
6711 return EmitAtomicBinary64(MI, BB, 0, 0, false);
Eli Friedman4d3f3292011-08-31 17:52:22 +00006712 case ARM::ATOMCMPXCHG6432:
6713 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
6714 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6715 /*NeedsCarry*/ false, /*IsCmpxchg*/true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006716
Evan Cheng007ea272009-08-12 05:17:19 +00006717 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00006718 // To "insert" a SELECT_CC instruction, we actually have to insert the
6719 // diamond control-flow pattern. The incoming instruction knows the
6720 // destination vreg to set, the condition code register to branch on, the
6721 // true/false values to select between, and a branch opcode to use.
6722 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006723 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00006724 ++It;
6725
6726 // thisMBB:
6727 // ...
6728 // TrueVal = ...
6729 // cmpTY ccX, r1, r2
6730 // bCC copy1MBB
6731 // fallthrough --> copy0MBB
6732 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006733 MachineFunction *F = BB->getParent();
6734 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6735 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00006736 F->insert(It, copy0MBB);
6737 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006738
6739 // Transfer the remainder of BB and its successor edges to sinkMBB.
6740 sinkMBB->splice(sinkMBB->begin(), BB,
6741 llvm::next(MachineBasicBlock::iterator(MI)),
6742 BB->end());
6743 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6744
Dan Gohman258c58c2010-07-06 15:49:48 +00006745 BB->addSuccessor(copy0MBB);
6746 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00006747
Dan Gohman14152b42010-07-06 20:24:04 +00006748 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
6749 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
6750
Evan Chenga8e29892007-01-19 07:51:42 +00006751 // copy0MBB:
6752 // %FalseValue = ...
6753 // # fallthrough to sinkMBB
6754 BB = copy0MBB;
6755
6756 // Update machine-CFG edges
6757 BB->addSuccessor(sinkMBB);
6758
6759 // sinkMBB:
6760 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6761 // ...
6762 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00006763 BuildMI(*BB, BB->begin(), dl,
6764 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00006765 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6766 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6767
Dan Gohman14152b42010-07-06 20:24:04 +00006768 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00006769 return BB;
6770 }
Evan Cheng86198642009-08-07 00:34:42 +00006771
Evan Cheng218977b2010-07-13 19:27:42 +00006772 case ARM::BCCi64:
6773 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00006774 // If there is an unconditional branch to the other successor, remove it.
6775 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00006776
Evan Cheng218977b2010-07-13 19:27:42 +00006777 // Compare both parts that make up the double comparison separately for
6778 // equality.
6779 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
6780
6781 unsigned LHS1 = MI->getOperand(1).getReg();
6782 unsigned LHS2 = MI->getOperand(2).getReg();
6783 if (RHSisZero) {
6784 AddDefaultPred(BuildMI(BB, dl,
6785 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6786 .addReg(LHS1).addImm(0));
6787 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6788 .addReg(LHS2).addImm(0)
6789 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6790 } else {
6791 unsigned RHS1 = MI->getOperand(3).getReg();
6792 unsigned RHS2 = MI->getOperand(4).getReg();
6793 AddDefaultPred(BuildMI(BB, dl,
6794 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6795 .addReg(LHS1).addReg(RHS1));
6796 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6797 .addReg(LHS2).addReg(RHS2)
6798 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6799 }
6800
6801 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
6802 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
6803 if (MI->getOperand(0).getImm() == ARMCC::NE)
6804 std::swap(destMBB, exitMBB);
6805
6806 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6807 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson51f6a7a2011-09-09 21:48:23 +00006808 if (isThumb2)
6809 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
6810 else
6811 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng218977b2010-07-13 19:27:42 +00006812
6813 MI->eraseFromParent(); // The pseudo instruction is gone now.
6814 return BB;
6815 }
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006816
Bill Wendling5bc85282011-10-17 20:37:20 +00006817 case ARM::Int_eh_sjlj_setjmp:
6818 case ARM::Int_eh_sjlj_setjmp_nofp:
6819 case ARM::tInt_eh_sjlj_setjmp:
6820 case ARM::t2Int_eh_sjlj_setjmp:
6821 case ARM::t2Int_eh_sjlj_setjmp_nofp:
6822 EmitSjLjDispatchBlock(MI, BB);
6823 return BB;
6824
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006825 case ARM::ABS:
6826 case ARM::t2ABS: {
6827 // To insert an ABS instruction, we have to insert the
6828 // diamond control-flow pattern. The incoming instruction knows the
6829 // source vreg to test against 0, the destination vreg to set,
6830 // the condition code register to branch on, the
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006831 // true/false values to select between, and a branch opcode to use.
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006832 // It transforms
6833 // V1 = ABS V0
6834 // into
6835 // V2 = MOVS V0
6836 // BCC (branch to SinkBB if V0 >= 0)
6837 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006838 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006839 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6840 MachineFunction::iterator BBI = BB;
6841 ++BBI;
6842 MachineFunction *Fn = BB->getParent();
6843 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6844 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6845 Fn->insert(BBI, RSBBB);
6846 Fn->insert(BBI, SinkBB);
6847
6848 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
6849 unsigned int ABSDstReg = MI->getOperand(0).getReg();
6850 bool isThumb2 = Subtarget->isThumb2();
6851 MachineRegisterInfo &MRI = Fn->getRegInfo();
6852 // In Thumb mode S must not be specified if source register is the SP or
6853 // PC and if destination register is the SP, so restrict register class
Craig Topper420761a2012-04-20 07:30:17 +00006854 unsigned NewRsbDstReg = MRI.createVirtualRegister(isThumb2 ?
6855 (const TargetRegisterClass*)&ARM::rGPRRegClass :
6856 (const TargetRegisterClass*)&ARM::GPRRegClass);
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006857
6858 // Transfer the remainder of BB and its successor edges to sinkMBB.
6859 SinkBB->splice(SinkBB->begin(), BB,
6860 llvm::next(MachineBasicBlock::iterator(MI)),
6861 BB->end());
6862 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
6863
6864 BB->addSuccessor(RSBBB);
6865 BB->addSuccessor(SinkBB);
6866
6867 // fall through to SinkMBB
6868 RSBBB->addSuccessor(SinkBB);
6869
Manman Ren307473d2012-06-15 21:32:12 +00006870 // insert a cmp at the end of BB
Andrew Trick49b446f2012-07-18 18:34:24 +00006871 AddDefaultPred(BuildMI(BB, dl,
Manman Ren307473d2012-06-15 21:32:12 +00006872 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6873 .addReg(ABSSrcReg).addImm(0));
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006874
6875 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006876 BuildMI(BB, dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006877 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
6878 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
6879
6880 // insert rsbri in RSBBB
6881 // Note: BCC and rsbri will be converted into predicated rsbmi
6882 // by if-conversion pass
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006883 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006884 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Manman Ren307473d2012-06-15 21:32:12 +00006885 .addReg(ABSSrcReg, RegState::Kill)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006886 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
6887
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006888 // insert PHI in SinkBB,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006889 // reuse ABSDstReg to not change uses of ABS instruction
6890 BuildMI(*SinkBB, SinkBB->begin(), dl,
6891 TII->get(ARM::PHI), ABSDstReg)
6892 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Ren307473d2012-06-15 21:32:12 +00006893 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006894
6895 // remove ABS instruction
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006896 MI->eraseFromParent();
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006897
6898 // return last added BB
6899 return SinkBB;
6900 }
Manman Ren68f25572012-06-01 19:33:18 +00006901 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren763a75d2012-06-01 02:44:42 +00006902 ++NumLoopByVals;
Manman Ren68f25572012-06-01 19:33:18 +00006903 return EmitStructByval(MI, BB);
Evan Chenga8e29892007-01-19 07:51:42 +00006904 }
6905}
6906
Evan Cheng37fefc22011-08-30 19:09:48 +00006907void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
6908 SDNode *Node) const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006909 if (!MI->hasPostISelHook()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006910 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
6911 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
6912 return;
6913 }
6914
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006915 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick4815d562011-09-20 03:17:40 +00006916 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
6917 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
6918 // operand is still set to noreg. If needed, set the optional operand's
6919 // register to CPSR, and remove the redundant implicit def.
Andrew Trick3be654f2011-09-21 02:20:46 +00006920 //
Andrew Trick90b7b122011-10-18 19:18:52 +00006921 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick4815d562011-09-20 03:17:40 +00006922
Andrew Trick3be654f2011-09-21 02:20:46 +00006923 // Rename pseudo opcodes.
6924 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
6925 if (NewOpc) {
6926 const ARMBaseInstrInfo *TII =
6927 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick90b7b122011-10-18 19:18:52 +00006928 MCID = &TII->get(NewOpc);
6929
6930 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
6931 "converted opcode should be the same except for cc_out");
6932
6933 MI->setDesc(*MCID);
6934
6935 // Add the optional cc_out operand
6936 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick3be654f2011-09-21 02:20:46 +00006937 }
Andrew Trick90b7b122011-10-18 19:18:52 +00006938 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick4815d562011-09-20 03:17:40 +00006939
6940 // Any ARM instruction that sets the 's' bit should specify an optional
6941 // "cc_out" operand in the last operand position.
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006942 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006943 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006944 return;
6945 }
Andrew Trick3be654f2011-09-21 02:20:46 +00006946 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
6947 // since we already have an optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006948 bool definesCPSR = false;
6949 bool deadCPSR = false;
Andrew Trick90b7b122011-10-18 19:18:52 +00006950 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick4815d562011-09-20 03:17:40 +00006951 i != e; ++i) {
6952 const MachineOperand &MO = MI->getOperand(i);
6953 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
6954 definesCPSR = true;
6955 if (MO.isDead())
6956 deadCPSR = true;
6957 MI->RemoveOperand(i);
6958 break;
Evan Cheng37fefc22011-08-30 19:09:48 +00006959 }
6960 }
Andrew Trick4815d562011-09-20 03:17:40 +00006961 if (!definesCPSR) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006962 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006963 return;
6964 }
6965 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick3be654f2011-09-21 02:20:46 +00006966 if (deadCPSR) {
6967 assert(!MI->getOperand(ccOutIdx).getReg() &&
6968 "expect uninitialized optional cc_out operand");
Andrew Trick4815d562011-09-20 03:17:40 +00006969 return;
Andrew Trick3be654f2011-09-21 02:20:46 +00006970 }
Andrew Trick4815d562011-09-20 03:17:40 +00006971
Andrew Trick3be654f2011-09-21 02:20:46 +00006972 // If this instruction was defined with an optional CPSR def and its dag node
6973 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006974 MachineOperand &MO = MI->getOperand(ccOutIdx);
6975 MO.setReg(ARM::CPSR);
6976 MO.setIsDef(true);
Evan Cheng37fefc22011-08-30 19:09:48 +00006977}
6978
Evan Chenga8e29892007-01-19 07:51:42 +00006979//===----------------------------------------------------------------------===//
6980// ARM Optimization Hooks
6981//===----------------------------------------------------------------------===//
6982
Chris Lattnerd1980a52009-03-12 06:52:53 +00006983static
6984SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
6985 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00006986 SelectionDAG &DAG = DCI.DAG;
6987 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00006988 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00006989 unsigned Opc = N->getOpcode();
6990 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
6991 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
6992 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
6993 ISD::CondCode CC = ISD::SETCC_INVALID;
6994
6995 if (isSlctCC) {
6996 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
6997 } else {
6998 SDValue CCOp = Slct.getOperand(0);
6999 if (CCOp.getOpcode() == ISD::SETCC)
7000 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
7001 }
7002
7003 bool DoXform = false;
7004 bool InvCC = false;
7005 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
7006 "Bad input!");
7007
7008 if (LHS.getOpcode() == ISD::Constant &&
7009 cast<ConstantSDNode>(LHS)->isNullValue()) {
7010 DoXform = true;
7011 } else if (CC != ISD::SETCC_INVALID &&
7012 RHS.getOpcode() == ISD::Constant &&
7013 cast<ConstantSDNode>(RHS)->isNullValue()) {
7014 std::swap(LHS, RHS);
7015 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00007016 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00007017 Op0.getOperand(0).getValueType();
7018 bool isInt = OpVT.isInteger();
7019 CC = ISD::getSetCCInverse(CC, isInt);
7020
7021 if (!TLI.isCondCodeLegal(CC, OpVT))
7022 return SDValue(); // Inverse operator isn't legal.
7023
7024 DoXform = true;
7025 InvCC = true;
7026 }
7027
7028 if (DoXform) {
7029 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
7030 if (isSlctCC)
7031 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
7032 Slct.getOperand(0), Slct.getOperand(1), CC);
7033 SDValue CCOp = Slct.getOperand(0);
7034 if (InvCC)
7035 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
7036 CCOp.getOperand(0), CCOp.getOperand(1), CC);
7037 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
7038 CCOp, OtherOp, Result);
7039 }
7040 return SDValue();
7041}
7042
Eric Christopherfa6f5912011-06-29 21:10:36 +00007043// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00007044// (only after legalization).
7045static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
7046 TargetLowering::DAGCombinerInfo &DCI,
7047 const ARMSubtarget *Subtarget) {
7048
7049 // Only perform optimization if after legalize, and if NEON is available. We
7050 // also expected both operands to be BUILD_VECTORs.
7051 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
7052 || N0.getOpcode() != ISD::BUILD_VECTOR
7053 || N1.getOpcode() != ISD::BUILD_VECTOR)
7054 return SDValue();
7055
7056 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
7057 EVT VT = N->getValueType(0);
7058 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
7059 return SDValue();
7060
7061 // Check that the vector operands are of the right form.
7062 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
7063 // operands, where N is the size of the formed vector.
7064 // Each EXTRACT_VECTOR should have the same input vector and odd or even
7065 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00007066
7067 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00007068 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00007069 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00007070 SDValue Vec = N0->getOperand(0)->getOperand(0);
7071 SDNode *V = Vec.getNode();
7072 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00007073
Eric Christopherfa6f5912011-06-29 21:10:36 +00007074 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00007075 // check to see if each of their operands are an EXTRACT_VECTOR with
7076 // the same vector and appropriate index.
7077 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
7078 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
7079 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00007080
Tanya Lattner189531f2011-06-14 23:48:48 +00007081 SDValue ExtVec0 = N0->getOperand(i);
7082 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00007083
Tanya Lattner189531f2011-06-14 23:48:48 +00007084 // First operand is the vector, verify its the same.
7085 if (V != ExtVec0->getOperand(0).getNode() ||
7086 V != ExtVec1->getOperand(0).getNode())
7087 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00007088
Tanya Lattner189531f2011-06-14 23:48:48 +00007089 // Second is the constant, verify its correct.
7090 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
7091 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00007092
Tanya Lattner189531f2011-06-14 23:48:48 +00007093 // For the constant, we want to see all the even or all the odd.
7094 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
7095 || C1->getZExtValue() != nextIndex+1)
7096 return SDValue();
7097
7098 // Increment index.
7099 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00007100 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00007101 return SDValue();
7102 }
7103
7104 // Create VPADDL node.
7105 SelectionDAG &DAG = DCI.DAG;
7106 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00007107
7108 // Build operand list.
7109 SmallVector<SDValue, 8> Ops;
7110 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
7111 TLI.getPointerTy()));
7112
7113 // Input is the vector.
7114 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00007115
Tanya Lattner189531f2011-06-14 23:48:48 +00007116 // Get widened type and narrowed type.
7117 MVT widenType;
7118 unsigned numElem = VT.getVectorNumElements();
7119 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
7120 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
7121 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
7122 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
7123 default:
Craig Topperbc219812012-02-07 02:50:20 +00007124 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattner189531f2011-06-14 23:48:48 +00007125 }
7126
7127 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7128 widenType, &Ops[0], Ops.size());
7129 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
7130}
7131
Arnold Schwaighoferbcc4c1d2012-08-09 15:25:52 +00007132static SDValue findMUL_LOHI(SDValue V) {
7133 if (V->getOpcode() == ISD::UMUL_LOHI ||
7134 V->getOpcode() == ISD::SMUL_LOHI)
7135 return V;
7136 return SDValue();
7137}
7138
7139static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
7140 TargetLowering::DAGCombinerInfo &DCI,
7141 const ARMSubtarget *Subtarget) {
7142
7143 if (Subtarget->isThumb1Only()) return SDValue();
7144
7145 // Only perform the checks after legalize when the pattern is available.
7146 if (DCI.isBeforeLegalize()) return SDValue();
7147
7148 // Look for multiply add opportunities.
7149 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
7150 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
7151 // a glue link from the first add to the second add.
7152 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
7153 // a S/UMLAL instruction.
7154 // loAdd UMUL_LOHI
7155 // \ / :lo \ :hi
7156 // \ / \ [no multiline comment]
7157 // ADDC | hiAdd
7158 // \ :glue / /
7159 // \ / /
7160 // ADDE
7161 //
7162 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
7163 SDValue AddcOp0 = AddcNode->getOperand(0);
7164 SDValue AddcOp1 = AddcNode->getOperand(1);
7165
7166 // Check if the two operands are from the same mul_lohi node.
7167 if (AddcOp0.getNode() == AddcOp1.getNode())
7168 return SDValue();
7169
7170 assert(AddcNode->getNumValues() == 2 &&
7171 AddcNode->getValueType(0) == MVT::i32 &&
7172 AddcNode->getValueType(1) == MVT::Glue &&
7173 "Expect ADDC with two result values: i32, glue");
7174
7175 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
7176 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
7177 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
7178 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
7179 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
7180 return SDValue();
7181
7182 // Look for the glued ADDE.
7183 SDNode* AddeNode = AddcNode->getGluedUser();
7184 if (AddeNode == NULL)
7185 return SDValue();
7186
7187 // Make sure it is really an ADDE.
7188 if (AddeNode->getOpcode() != ISD::ADDE)
7189 return SDValue();
7190
7191 assert(AddeNode->getNumOperands() == 3 &&
7192 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
7193 "ADDE node has the wrong inputs");
7194
7195 // Check for the triangle shape.
7196 SDValue AddeOp0 = AddeNode->getOperand(0);
7197 SDValue AddeOp1 = AddeNode->getOperand(1);
7198
7199 // Make sure that the ADDE operands are not coming from the same node.
7200 if (AddeOp0.getNode() == AddeOp1.getNode())
7201 return SDValue();
7202
7203 // Find the MUL_LOHI node walking up ADDE's operands.
7204 bool IsLeftOperandMUL = false;
7205 SDValue MULOp = findMUL_LOHI(AddeOp0);
7206 if (MULOp == SDValue())
7207 MULOp = findMUL_LOHI(AddeOp1);
7208 else
7209 IsLeftOperandMUL = true;
7210 if (MULOp == SDValue())
7211 return SDValue();
7212
7213 // Figure out the right opcode.
7214 unsigned Opc = MULOp->getOpcode();
7215 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
7216
7217 // Figure out the high and low input values to the MLAL node.
7218 SDValue* HiMul = &MULOp;
7219 SDValue* HiAdd = NULL;
7220 SDValue* LoMul = NULL;
7221 SDValue* LowAdd = NULL;
7222
7223 if (IsLeftOperandMUL)
7224 HiAdd = &AddeOp1;
7225 else
7226 HiAdd = &AddeOp0;
7227
7228
7229 if (AddcOp0->getOpcode() == Opc) {
7230 LoMul = &AddcOp0;
7231 LowAdd = &AddcOp1;
7232 }
7233 if (AddcOp1->getOpcode() == Opc) {
7234 LoMul = &AddcOp1;
7235 LowAdd = &AddcOp0;
7236 }
7237
7238 if (LoMul == NULL)
7239 return SDValue();
7240
7241 if (LoMul->getNode() != HiMul->getNode())
7242 return SDValue();
7243
7244 // Create the merged node.
7245 SelectionDAG &DAG = DCI.DAG;
7246
7247 // Build operand list.
7248 SmallVector<SDValue, 8> Ops;
7249 Ops.push_back(LoMul->getOperand(0));
7250 Ops.push_back(LoMul->getOperand(1));
7251 Ops.push_back(*LowAdd);
7252 Ops.push_back(*HiAdd);
7253
7254 SDValue MLALNode = DAG.getNode(FinalOpc, AddcNode->getDebugLoc(),
7255 DAG.getVTList(MVT::i32, MVT::i32),
7256 &Ops[0], Ops.size());
7257
7258 // Replace the ADDs' nodes uses by the MLA node's values.
7259 SDValue HiMLALResult(MLALNode.getNode(), 1);
7260 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
7261
7262 SDValue LoMLALResult(MLALNode.getNode(), 0);
7263 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
7264
7265 // Return original node to notify the driver to stop replacing.
7266 SDValue resNode(AddcNode, 0);
7267 return resNode;
7268}
7269
7270/// PerformADDCCombine - Target-specific dag combine transform from
7271/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
7272static SDValue PerformADDCCombine(SDNode *N,
7273 TargetLowering::DAGCombinerInfo &DCI,
7274 const ARMSubtarget *Subtarget) {
7275
7276 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
7277
7278}
7279
Bob Wilson3d5792a2010-07-29 20:34:14 +00007280/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
7281/// operands N0 and N1. This is a helper for PerformADDCombine that is
7282/// called with the default operands, and if that fails, with commuted
7283/// operands.
7284static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00007285 TargetLowering::DAGCombinerInfo &DCI,
7286 const ARMSubtarget *Subtarget){
7287
7288 // Attempt to create vpaddl for this add.
7289 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
7290 if (Result.getNode())
7291 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00007292
Chris Lattnerd1980a52009-03-12 06:52:53 +00007293 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
7294 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
7295 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
7296 if (Result.getNode()) return Result;
7297 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00007298 return SDValue();
7299}
7300
Bob Wilson3d5792a2010-07-29 20:34:14 +00007301/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
7302///
7303static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00007304 TargetLowering::DAGCombinerInfo &DCI,
7305 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00007306 SDValue N0 = N->getOperand(0);
7307 SDValue N1 = N->getOperand(1);
7308
7309 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00007310 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00007311 if (Result.getNode())
7312 return Result;
7313
7314 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00007315 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00007316}
7317
Chris Lattnerd1980a52009-03-12 06:52:53 +00007318/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00007319///
Chris Lattnerd1980a52009-03-12 06:52:53 +00007320static SDValue PerformSUBCombine(SDNode *N,
7321 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00007322 SDValue N0 = N->getOperand(0);
7323 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00007324
Chris Lattnerd1980a52009-03-12 06:52:53 +00007325 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
7326 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
7327 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
7328 if (Result.getNode()) return Result;
7329 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00007330
Chris Lattnerd1980a52009-03-12 06:52:53 +00007331 return SDValue();
7332}
7333
Evan Cheng463d3582011-03-31 19:38:48 +00007334/// PerformVMULCombine
7335/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
7336/// special multiplier accumulator forwarding.
7337/// vmul d3, d0, d2
7338/// vmla d3, d1, d2
7339/// is faster than
7340/// vadd d3, d0, d1
7341/// vmul d3, d3, d2
7342static SDValue PerformVMULCombine(SDNode *N,
7343 TargetLowering::DAGCombinerInfo &DCI,
7344 const ARMSubtarget *Subtarget) {
7345 if (!Subtarget->hasVMLxForwarding())
7346 return SDValue();
7347
7348 SelectionDAG &DAG = DCI.DAG;
7349 SDValue N0 = N->getOperand(0);
7350 SDValue N1 = N->getOperand(1);
7351 unsigned Opcode = N0.getOpcode();
7352 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7353 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00007354 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00007355 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7356 Opcode != ISD::FADD && Opcode != ISD::FSUB)
7357 return SDValue();
7358 std::swap(N0, N1);
7359 }
7360
7361 EVT VT = N->getValueType(0);
7362 DebugLoc DL = N->getDebugLoc();
7363 SDValue N00 = N0->getOperand(0);
7364 SDValue N01 = N0->getOperand(1);
7365 return DAG.getNode(Opcode, DL, VT,
7366 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
7367 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
7368}
7369
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007370static SDValue PerformMULCombine(SDNode *N,
7371 TargetLowering::DAGCombinerInfo &DCI,
7372 const ARMSubtarget *Subtarget) {
7373 SelectionDAG &DAG = DCI.DAG;
7374
7375 if (Subtarget->isThumb1Only())
7376 return SDValue();
7377
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007378 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7379 return SDValue();
7380
7381 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00007382 if (VT.is64BitVector() || VT.is128BitVector())
7383 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007384 if (VT != MVT::i32)
7385 return SDValue();
7386
7387 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
7388 if (!C)
7389 return SDValue();
7390
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007391 int64_t MulAmt = C->getSExtValue();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007392 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007393
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007394 ShiftAmt = ShiftAmt & (32 - 1);
7395 SDValue V = N->getOperand(0);
7396 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007397
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007398 SDValue Res;
7399 MulAmt >>= ShiftAmt;
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007400
7401 if (MulAmt >= 0) {
7402 if (isPowerOf2_32(MulAmt - 1)) {
7403 // (mul x, 2^N + 1) => (add (shl x, N), x)
7404 Res = DAG.getNode(ISD::ADD, DL, VT,
7405 V,
7406 DAG.getNode(ISD::SHL, DL, VT,
7407 V,
7408 DAG.getConstant(Log2_32(MulAmt - 1),
7409 MVT::i32)));
7410 } else if (isPowerOf2_32(MulAmt + 1)) {
7411 // (mul x, 2^N - 1) => (sub (shl x, N), x)
7412 Res = DAG.getNode(ISD::SUB, DL, VT,
7413 DAG.getNode(ISD::SHL, DL, VT,
7414 V,
7415 DAG.getConstant(Log2_32(MulAmt + 1),
7416 MVT::i32)),
7417 V);
7418 } else
7419 return SDValue();
7420 } else {
7421 uint64_t MulAmtAbs = -MulAmt;
7422 if (isPowerOf2_32(MulAmtAbs + 1)) {
7423 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
7424 Res = DAG.getNode(ISD::SUB, DL, VT,
7425 V,
7426 DAG.getNode(ISD::SHL, DL, VT,
7427 V,
7428 DAG.getConstant(Log2_32(MulAmtAbs + 1),
7429 MVT::i32)));
7430 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
7431 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
7432 Res = DAG.getNode(ISD::ADD, DL, VT,
7433 V,
7434 DAG.getNode(ISD::SHL, DL, VT,
7435 V,
7436 DAG.getConstant(Log2_32(MulAmtAbs-1),
7437 MVT::i32)));
7438 Res = DAG.getNode(ISD::SUB, DL, VT,
7439 DAG.getConstant(0, MVT::i32),Res);
7440
7441 } else
7442 return SDValue();
7443 }
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007444
7445 if (ShiftAmt != 0)
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007446 Res = DAG.getNode(ISD::SHL, DL, VT,
7447 Res, DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007448
7449 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007450 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007451 return SDValue();
7452}
7453
Evan Chengc892aeb2012-02-23 01:19:06 +00007454static bool isCMOVWithZeroOrAllOnesLHS(SDValue N, bool AllOnes) {
7455 if (N.getOpcode() != ARMISD::CMOV || !N.getNode()->hasOneUse())
7456 return false;
7457
7458 SDValue FalseVal = N.getOperand(0);
7459 ConstantSDNode *C = dyn_cast<ConstantSDNode>(FalseVal);
7460 if (!C)
7461 return false;
7462 if (AllOnes)
7463 return C->isAllOnesValue();
7464 return C->isNullValue();
7465}
7466
7467/// formConditionalOp - Combine an operation with a conditional move operand
7468/// to form a conditional op. e.g. (or x, (cmov 0, y, cond)) => (or.cond x, y)
7469/// (and x, (cmov -1, y, cond)) => (and.cond, x, y)
7470static SDValue formConditionalOp(SDNode *N, SelectionDAG &DAG,
7471 bool Commutable) {
7472 SDValue N0 = N->getOperand(0);
7473 SDValue N1 = N->getOperand(1);
7474
7475 bool isAND = N->getOpcode() == ISD::AND;
7476 bool isCand = isCMOVWithZeroOrAllOnesLHS(N1, isAND);
7477 if (!isCand && Commutable) {
7478 isCand = isCMOVWithZeroOrAllOnesLHS(N0, isAND);
7479 if (isCand)
7480 std::swap(N0, N1);
7481 }
7482 if (!isCand)
7483 return SDValue();
7484
7485 unsigned Opc = 0;
7486 switch (N->getOpcode()) {
7487 default: llvm_unreachable("Unexpected node");
7488 case ISD::AND: Opc = ARMISD::CAND; break;
7489 case ISD::OR: Opc = ARMISD::COR; break;
7490 case ISD::XOR: Opc = ARMISD::CXOR; break;
7491 }
7492 return DAG.getNode(Opc, N->getDebugLoc(), N->getValueType(0), N0,
7493 N1.getOperand(1), N1.getOperand(2), N1.getOperand(3),
7494 N1.getOperand(4));
7495}
7496
Owen Anderson080c0922010-11-05 19:27:46 +00007497static SDValue PerformANDCombine(SDNode *N,
Evan Chengc892aeb2012-02-23 01:19:06 +00007498 TargetLowering::DAGCombinerInfo &DCI,
7499 const ARMSubtarget *Subtarget) {
Owen Anderson76706012011-04-05 21:48:57 +00007500
Owen Anderson080c0922010-11-05 19:27:46 +00007501 // Attempt to use immediate-form VBIC
7502 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
7503 DebugLoc dl = N->getDebugLoc();
7504 EVT VT = N->getValueType(0);
7505 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007506
Tanya Lattner0433b212011-04-07 15:24:20 +00007507 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7508 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00007509
Owen Anderson080c0922010-11-05 19:27:46 +00007510 APInt SplatBits, SplatUndef;
7511 unsigned SplatBitSize;
7512 bool HasAnyUndefs;
7513 if (BVN &&
7514 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7515 if (SplatBitSize <= 64) {
7516 EVT VbicVT;
7517 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
7518 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007519 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00007520 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00007521 if (Val.getNode()) {
7522 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007523 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00007524 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007525 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00007526 }
7527 }
7528 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007529
Evan Chengc892aeb2012-02-23 01:19:06 +00007530 if (!Subtarget->isThumb1Only()) {
7531 // (and x, (cmov -1, y, cond)) => (and.cond x, y)
7532 SDValue CAND = formConditionalOp(N, DAG, true);
7533 if (CAND.getNode())
7534 return CAND;
7535 }
7536
Owen Anderson080c0922010-11-05 19:27:46 +00007537 return SDValue();
7538}
7539
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007540/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
7541static SDValue PerformORCombine(SDNode *N,
7542 TargetLowering::DAGCombinerInfo &DCI,
7543 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00007544 // Attempt to use immediate-form VORR
7545 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
7546 DebugLoc dl = N->getDebugLoc();
7547 EVT VT = N->getValueType(0);
7548 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007549
Tanya Lattner0433b212011-04-07 15:24:20 +00007550 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7551 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00007552
Owen Anderson60f48702010-11-03 23:15:26 +00007553 APInt SplatBits, SplatUndef;
7554 unsigned SplatBitSize;
7555 bool HasAnyUndefs;
7556 if (BVN && Subtarget->hasNEON() &&
7557 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7558 if (SplatBitSize <= 64) {
7559 EVT VorrVT;
7560 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
7561 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00007562 DAG, VorrVT, VT.is128BitVector(),
7563 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00007564 if (Val.getNode()) {
7565 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007566 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00007567 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007568 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00007569 }
7570 }
7571 }
7572
Evan Chengc892aeb2012-02-23 01:19:06 +00007573 if (!Subtarget->isThumb1Only()) {
7574 // (or x, (cmov 0, y, cond)) => (or.cond x, y)
7575 SDValue COR = formConditionalOp(N, DAG, true);
7576 if (COR.getNode())
7577 return COR;
7578 }
7579
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007580 SDValue N0 = N->getOperand(0);
7581 if (N0.getOpcode() != ISD::AND)
7582 return SDValue();
7583 SDValue N1 = N->getOperand(1);
7584
7585 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
7586 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
7587 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
7588 APInt SplatUndef;
7589 unsigned SplatBitSize;
7590 bool HasAnyUndefs;
7591
7592 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
7593 APInt SplatBits0;
7594 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
7595 HasAnyUndefs) && !HasAnyUndefs) {
7596 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
7597 APInt SplatBits1;
7598 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
7599 HasAnyUndefs) && !HasAnyUndefs &&
7600 SplatBits0 == ~SplatBits1) {
7601 // Canonicalize the vector type to make instruction selection simpler.
7602 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
7603 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
7604 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00007605 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007606 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
7607 }
7608 }
7609 }
7610
Jim Grosbach54238562010-07-17 03:30:54 +00007611 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
7612 // reasonable.
7613
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007614 // BFI is only available on V6T2+
7615 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
7616 return SDValue();
7617
Jim Grosbach54238562010-07-17 03:30:54 +00007618 DebugLoc DL = N->getDebugLoc();
7619 // 1) or (and A, mask), val => ARMbfi A, val, mask
7620 // iff (val & mask) == val
7621 //
7622 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
7623 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00007624 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00007625 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00007626 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00007627 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007628
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007629 if (VT != MVT::i32)
7630 return SDValue();
7631
Evan Cheng30fb13f2010-12-13 20:32:54 +00007632 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00007633
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007634 // The value and the mask need to be constants so we can verify this is
7635 // actually a bitfield set. If the mask is 0xffff, we can do better
7636 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00007637 SDValue MaskOp = N0.getOperand(1);
7638 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
7639 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007640 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00007641 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007642 if (Mask == 0xffff)
7643 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007644 SDValue Res;
7645 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00007646 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
7647 if (N1C) {
7648 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00007649 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00007650 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007651
Evan Chenga9688c42010-12-11 04:11:38 +00007652 if (ARM::isBitFieldInvertedMask(Mask)) {
7653 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007654
Evan Cheng30fb13f2010-12-13 20:32:54 +00007655 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00007656 DAG.getConstant(Val, MVT::i32),
7657 DAG.getConstant(Mask, MVT::i32));
7658
7659 // Do not add new nodes to DAG combiner worklist.
7660 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007661 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00007662 }
Jim Grosbach54238562010-07-17 03:30:54 +00007663 } else if (N1.getOpcode() == ISD::AND) {
7664 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00007665 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7666 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00007667 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00007668 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007669
Eric Christopher29aeed12011-03-26 01:21:03 +00007670 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
7671 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00007672 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007673 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007674 // The pack halfword instruction works better for masks that fit it,
7675 // so use that when it's available.
7676 if (Subtarget->hasT2ExtractPack() &&
7677 (Mask == 0xffff || Mask == 0xffff0000))
7678 return SDValue();
7679 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00007680 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00007681 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00007682 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00007683 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00007684 DAG.getConstant(Mask, MVT::i32));
7685 // Do not add new nodes to DAG combiner worklist.
7686 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007687 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007688 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007689 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007690 // The pack halfword instruction works better for masks that fit it,
7691 // so use that when it's available.
7692 if (Subtarget->hasT2ExtractPack() &&
7693 (Mask2 == 0xffff || Mask2 == 0xffff0000))
7694 return SDValue();
7695 // 2b
7696 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007697 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00007698 DAG.getConstant(lsb, MVT::i32));
7699 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00007700 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00007701 // Do not add new nodes to DAG combiner worklist.
7702 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007703 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007704 }
7705 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007706
Evan Cheng30fb13f2010-12-13 20:32:54 +00007707 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
7708 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
7709 ARM::isBitFieldInvertedMask(~Mask)) {
7710 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
7711 // where lsb(mask) == #shamt and masked bits of B are known zero.
7712 SDValue ShAmt = N00.getOperand(1);
7713 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
7714 unsigned LSB = CountTrailingZeros_32(Mask);
7715 if (ShAmtC != LSB)
7716 return SDValue();
7717
7718 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
7719 DAG.getConstant(~Mask, MVT::i32));
7720
7721 // Do not add new nodes to DAG combiner worklist.
7722 DCI.CombineTo(N, Res, false);
7723 }
7724
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007725 return SDValue();
7726}
7727
Evan Chengc892aeb2012-02-23 01:19:06 +00007728static SDValue PerformXORCombine(SDNode *N,
7729 TargetLowering::DAGCombinerInfo &DCI,
7730 const ARMSubtarget *Subtarget) {
7731 EVT VT = N->getValueType(0);
7732 SelectionDAG &DAG = DCI.DAG;
7733
7734 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7735 return SDValue();
7736
7737 if (!Subtarget->isThumb1Only()) {
7738 // (xor x, (cmov 0, y, cond)) => (xor.cond x, y)
7739 SDValue CXOR = formConditionalOp(N, DAG, true);
7740 if (CXOR.getNode())
7741 return CXOR;
7742 }
7743
7744 return SDValue();
7745}
7746
Evan Chengbf188ae2011-06-15 01:12:31 +00007747/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
7748/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00007749static SDValue PerformBFICombine(SDNode *N,
7750 TargetLowering::DAGCombinerInfo &DCI) {
7751 SDValue N1 = N->getOperand(1);
7752 if (N1.getOpcode() == ISD::AND) {
7753 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7754 if (!N11C)
7755 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007756 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
7757 unsigned LSB = CountTrailingZeros_32(~InvMask);
7758 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
7759 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00007760 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007761 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00007762 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
7763 N->getOperand(0), N1.getOperand(0),
7764 N->getOperand(2));
7765 }
7766 return SDValue();
7767}
7768
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007769/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
7770/// ARMISD::VMOVRRD.
7771static SDValue PerformVMOVRRDCombine(SDNode *N,
7772 TargetLowering::DAGCombinerInfo &DCI) {
7773 // vmovrrd(vmovdrr x, y) -> x,y
7774 SDValue InDouble = N->getOperand(0);
7775 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
7776 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00007777
7778 // vmovrrd(load f64) -> (load i32), (load i32)
7779 SDNode *InNode = InDouble.getNode();
7780 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
7781 InNode->getValueType(0) == MVT::f64 &&
7782 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
7783 !cast<LoadSDNode>(InNode)->isVolatile()) {
7784 // TODO: Should this be done for non-FrameIndex operands?
7785 LoadSDNode *LD = cast<LoadSDNode>(InNode);
7786
7787 SelectionDAG &DAG = DCI.DAG;
7788 DebugLoc DL = LD->getDebugLoc();
7789 SDValue BasePtr = LD->getBasePtr();
7790 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
7791 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007792 LD->isNonTemporal(), LD->isInvariant(),
7793 LD->getAlignment());
Cameron Zwarich4071a712011-04-02 02:40:43 +00007794
7795 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7796 DAG.getConstant(4, MVT::i32));
7797 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
7798 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007799 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich4071a712011-04-02 02:40:43 +00007800 std::min(4U, LD->getAlignment() / 2));
7801
7802 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
7803 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
7804 DCI.RemoveFromWorklist(LD);
7805 DAG.DeleteNode(LD);
7806 return Result;
7807 }
7808
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007809 return SDValue();
7810}
7811
7812/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
7813/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
7814static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
7815 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
7816 SDValue Op0 = N->getOperand(0);
7817 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007818 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007819 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007820 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007821 Op1 = Op1.getOperand(0);
7822 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
7823 Op0.getNode() == Op1.getNode() &&
7824 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007825 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007826 N->getValueType(0), Op0.getOperand(0));
7827 return SDValue();
7828}
7829
Bob Wilson31600902010-12-21 06:43:19 +00007830/// PerformSTORECombine - Target-specific dag combine xforms for
7831/// ISD::STORE.
7832static SDValue PerformSTORECombine(SDNode *N,
7833 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson31600902010-12-21 06:43:19 +00007834 StoreSDNode *St = cast<StoreSDNode>(N);
Chad Rosier7f354552012-04-09 20:32:02 +00007835 if (St->isVolatile())
7836 return SDValue();
7837
Andrew Trick49b446f2012-07-18 18:34:24 +00007838 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
Chad Rosier7f354552012-04-09 20:32:02 +00007839 // pack all of the elements in one place. Next, store to memory in fewer
7840 // chunks.
Bob Wilson31600902010-12-21 06:43:19 +00007841 SDValue StVal = St->getValue();
Chad Rosier7f354552012-04-09 20:32:02 +00007842 EVT VT = StVal.getValueType();
7843 if (St->isTruncatingStore() && VT.isVector()) {
7844 SelectionDAG &DAG = DCI.DAG;
7845 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7846 EVT StVT = St->getMemoryVT();
7847 unsigned NumElems = VT.getVectorNumElements();
7848 assert(StVT != VT && "Cannot truncate to the same type");
7849 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
7850 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
7851
7852 // From, To sizes and ElemCount must be pow of two
7853 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
7854
7855 // We are going to use the original vector elt for storing.
7856 // Accumulated smaller vector elements must be a multiple of the store size.
7857 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
7858
7859 unsigned SizeRatio = FromEltSz / ToEltSz;
7860 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
7861
7862 // Create a type on which we perform the shuffle.
7863 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
7864 NumElems*SizeRatio);
7865 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
7866
7867 DebugLoc DL = St->getDebugLoc();
7868 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
7869 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
7870 for (unsigned i = 0; i < NumElems; ++i) ShuffleVec[i] = i * SizeRatio;
7871
7872 // Can't shuffle using an illegal type.
7873 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
7874
7875 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
7876 DAG.getUNDEF(WideVec.getValueType()),
7877 ShuffleVec.data());
7878 // At this point all of the data is stored at the bottom of the
7879 // register. We now need to save it to mem.
7880
7881 // Find the largest store unit
7882 MVT StoreType = MVT::i8;
7883 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
7884 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
7885 MVT Tp = (MVT::SimpleValueType)tp;
7886 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
7887 StoreType = Tp;
7888 }
7889 // Didn't find a legal store type.
7890 if (!TLI.isTypeLegal(StoreType))
7891 return SDValue();
7892
7893 // Bitcast the original vector into a vector of store-size units
7894 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
7895 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
7896 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
7897 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
7898 SmallVector<SDValue, 8> Chains;
7899 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
7900 TLI.getPointerTy());
7901 SDValue BasePtr = St->getBasePtr();
7902
7903 // Perform one or more big stores into memory.
7904 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
7905 for (unsigned I = 0; I < E; I++) {
7906 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
7907 StoreType, ShuffWide,
7908 DAG.getIntPtrConstant(I));
7909 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
7910 St->getPointerInfo(), St->isVolatile(),
7911 St->isNonTemporal(), St->getAlignment());
7912 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
7913 Increment);
7914 Chains.push_back(Ch);
7915 }
7916 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, &Chains[0],
7917 Chains.size());
7918 }
7919
7920 if (!ISD::isNormalStore(St))
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007921 return SDValue();
7922
Chad Rosier96b66d62012-04-09 19:38:15 +00007923 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
7924 // ARM stores of arguments in the same cache line.
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007925 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
Chad Rosier96b66d62012-04-09 19:38:15 +00007926 StVal.getNode()->hasOneUse()) {
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007927 SelectionDAG &DAG = DCI.DAG;
7928 DebugLoc DL = St->getDebugLoc();
7929 SDValue BasePtr = St->getBasePtr();
7930 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
7931 StVal.getNode()->getOperand(0), BasePtr,
7932 St->getPointerInfo(), St->isVolatile(),
7933 St->isNonTemporal(), St->getAlignment());
7934
7935 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7936 DAG.getConstant(4, MVT::i32));
7937 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
7938 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
7939 St->isNonTemporal(),
7940 std::min(4U, St->getAlignment() / 2));
7941 }
7942
7943 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00007944 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
7945 return SDValue();
7946
Chad Rosier96b66d62012-04-09 19:38:15 +00007947 // Bitcast an i64 store extracted from a vector to f64.
7948 // Otherwise, the i64 value will be legalized to a pair of i32 values.
Bob Wilson31600902010-12-21 06:43:19 +00007949 SelectionDAG &DAG = DCI.DAG;
7950 DebugLoc dl = StVal.getDebugLoc();
7951 SDValue IntVec = StVal.getOperand(0);
7952 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7953 IntVec.getValueType().getVectorNumElements());
7954 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
7955 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
7956 Vec, StVal.getOperand(1));
7957 dl = N->getDebugLoc();
7958 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
7959 // Make the DAGCombiner fold the bitcasts.
7960 DCI.AddToWorklist(Vec.getNode());
7961 DCI.AddToWorklist(ExtElt.getNode());
7962 DCI.AddToWorklist(V.getNode());
7963 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
7964 St->getPointerInfo(), St->isVolatile(),
7965 St->isNonTemporal(), St->getAlignment(),
7966 St->getTBAAInfo());
7967}
7968
7969/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
7970/// are normal, non-volatile loads. If so, it is profitable to bitcast an
7971/// i64 vector to have f64 elements, since the value can then be loaded
7972/// directly into a VFP register.
7973static bool hasNormalLoadOperand(SDNode *N) {
7974 unsigned NumElts = N->getValueType(0).getVectorNumElements();
7975 for (unsigned i = 0; i < NumElts; ++i) {
7976 SDNode *Elt = N->getOperand(i).getNode();
7977 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
7978 return true;
7979 }
7980 return false;
7981}
7982
Bob Wilson75f02882010-09-17 22:59:05 +00007983/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
7984/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00007985static SDValue PerformBUILD_VECTORCombine(SDNode *N,
7986 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00007987 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
7988 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
7989 // into a pair of GPRs, which is fine when the value is used as a scalar,
7990 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00007991 SelectionDAG &DAG = DCI.DAG;
7992 if (N->getNumOperands() == 2) {
7993 SDValue RV = PerformVMOVDRRCombine(N, DAG);
7994 if (RV.getNode())
7995 return RV;
7996 }
Bob Wilson75f02882010-09-17 22:59:05 +00007997
Bob Wilson31600902010-12-21 06:43:19 +00007998 // Load i64 elements as f64 values so that type legalization does not split
7999 // them up into i32 values.
8000 EVT VT = N->getValueType(0);
8001 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
8002 return SDValue();
8003 DebugLoc dl = N->getDebugLoc();
8004 SmallVector<SDValue, 8> Ops;
8005 unsigned NumElts = VT.getVectorNumElements();
8006 for (unsigned i = 0; i < NumElts; ++i) {
8007 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
8008 Ops.push_back(V);
8009 // Make the DAGCombiner fold the bitcast.
8010 DCI.AddToWorklist(V.getNode());
8011 }
8012 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
8013 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
8014 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
8015}
8016
8017/// PerformInsertEltCombine - Target-specific dag combine xforms for
8018/// ISD::INSERT_VECTOR_ELT.
8019static SDValue PerformInsertEltCombine(SDNode *N,
8020 TargetLowering::DAGCombinerInfo &DCI) {
8021 // Bitcast an i64 load inserted into a vector to f64.
8022 // Otherwise, the i64 value will be legalized to a pair of i32 values.
8023 EVT VT = N->getValueType(0);
8024 SDNode *Elt = N->getOperand(1).getNode();
8025 if (VT.getVectorElementType() != MVT::i64 ||
8026 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
8027 return SDValue();
8028
8029 SelectionDAG &DAG = DCI.DAG;
8030 DebugLoc dl = N->getDebugLoc();
8031 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8032 VT.getVectorNumElements());
8033 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
8034 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
8035 // Make the DAGCombiner fold the bitcasts.
8036 DCI.AddToWorklist(Vec.getNode());
8037 DCI.AddToWorklist(V.getNode());
8038 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
8039 Vec, V, N->getOperand(2));
8040 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00008041}
8042
Bob Wilsonf20700c2010-10-27 20:38:28 +00008043/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
8044/// ISD::VECTOR_SHUFFLE.
8045static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
8046 // The LLVM shufflevector instruction does not require the shuffle mask
8047 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
8048 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
8049 // operands do not match the mask length, they are extended by concatenating
8050 // them with undef vectors. That is probably the right thing for other
8051 // targets, but for NEON it is better to concatenate two double-register
8052 // size vector operands into a single quad-register size vector. Do that
8053 // transformation here:
8054 // shuffle(concat(v1, undef), concat(v2, undef)) ->
8055 // shuffle(concat(v1, v2), undef)
8056 SDValue Op0 = N->getOperand(0);
8057 SDValue Op1 = N->getOperand(1);
8058 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
8059 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
8060 Op0.getNumOperands() != 2 ||
8061 Op1.getNumOperands() != 2)
8062 return SDValue();
8063 SDValue Concat0Op1 = Op0.getOperand(1);
8064 SDValue Concat1Op1 = Op1.getOperand(1);
8065 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
8066 Concat1Op1.getOpcode() != ISD::UNDEF)
8067 return SDValue();
8068 // Skip the transformation if any of the types are illegal.
8069 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8070 EVT VT = N->getValueType(0);
8071 if (!TLI.isTypeLegal(VT) ||
8072 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
8073 !TLI.isTypeLegal(Concat1Op1.getValueType()))
8074 return SDValue();
8075
8076 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
8077 Op0.getOperand(0), Op1.getOperand(0));
8078 // Translate the shuffle mask.
8079 SmallVector<int, 16> NewMask;
8080 unsigned NumElts = VT.getVectorNumElements();
8081 unsigned HalfElts = NumElts/2;
8082 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8083 for (unsigned n = 0; n < NumElts; ++n) {
8084 int MaskElt = SVN->getMaskElt(n);
8085 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00008086 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00008087 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00008088 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00008089 NewElt = HalfElts + MaskElt - NumElts;
8090 NewMask.push_back(NewElt);
8091 }
8092 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
8093 DAG.getUNDEF(VT), NewMask.data());
8094}
8095
Bob Wilson1c3ef902011-02-07 17:43:21 +00008096/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
8097/// NEON load/store intrinsics to merge base address updates.
8098static SDValue CombineBaseUpdate(SDNode *N,
8099 TargetLowering::DAGCombinerInfo &DCI) {
8100 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8101 return SDValue();
8102
8103 SelectionDAG &DAG = DCI.DAG;
8104 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
8105 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
8106 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
8107 SDValue Addr = N->getOperand(AddrOpIdx);
8108
8109 // Search for a use of the address operand that is an increment.
8110 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
8111 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
8112 SDNode *User = *UI;
8113 if (User->getOpcode() != ISD::ADD ||
8114 UI.getUse().getResNo() != Addr.getResNo())
8115 continue;
8116
8117 // Check that the add is independent of the load/store. Otherwise, folding
8118 // it would create a cycle.
8119 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
8120 continue;
8121
8122 // Find the new opcode for the updating load/store.
8123 bool isLoad = true;
8124 bool isLaneOp = false;
8125 unsigned NewOpc = 0;
8126 unsigned NumVecs = 0;
8127 if (isIntrinsic) {
8128 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
8129 switch (IntNo) {
Craig Topperbc219812012-02-07 02:50:20 +00008130 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00008131 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
8132 NumVecs = 1; break;
8133 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
8134 NumVecs = 2; break;
8135 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
8136 NumVecs = 3; break;
8137 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
8138 NumVecs = 4; break;
8139 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
8140 NumVecs = 2; isLaneOp = true; break;
8141 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
8142 NumVecs = 3; isLaneOp = true; break;
8143 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
8144 NumVecs = 4; isLaneOp = true; break;
8145 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
8146 NumVecs = 1; isLoad = false; break;
8147 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
8148 NumVecs = 2; isLoad = false; break;
8149 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
8150 NumVecs = 3; isLoad = false; break;
8151 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
8152 NumVecs = 4; isLoad = false; break;
8153 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
8154 NumVecs = 2; isLoad = false; isLaneOp = true; break;
8155 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
8156 NumVecs = 3; isLoad = false; isLaneOp = true; break;
8157 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
8158 NumVecs = 4; isLoad = false; isLaneOp = true; break;
8159 }
8160 } else {
8161 isLaneOp = true;
8162 switch (N->getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00008163 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00008164 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
8165 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
8166 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
8167 }
8168 }
8169
8170 // Find the size of memory referenced by the load/store.
8171 EVT VecTy;
8172 if (isLoad)
8173 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00008174 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00008175 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
8176 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
8177 if (isLaneOp)
8178 NumBytes /= VecTy.getVectorNumElements();
8179
8180 // If the increment is a constant, it must match the memory ref size.
8181 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
8182 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
8183 uint64_t IncVal = CInc->getZExtValue();
8184 if (IncVal != NumBytes)
8185 continue;
8186 } else if (NumBytes >= 3 * 16) {
8187 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
8188 // separate instructions that make it harder to use a non-constant update.
8189 continue;
8190 }
8191
8192 // Create the new updating load/store node.
8193 EVT Tys[6];
8194 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
8195 unsigned n;
8196 for (n = 0; n < NumResultVecs; ++n)
8197 Tys[n] = VecTy;
8198 Tys[n++] = MVT::i32;
8199 Tys[n] = MVT::Other;
8200 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
8201 SmallVector<SDValue, 8> Ops;
8202 Ops.push_back(N->getOperand(0)); // incoming chain
8203 Ops.push_back(N->getOperand(AddrOpIdx));
8204 Ops.push_back(Inc);
8205 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
8206 Ops.push_back(N->getOperand(i));
8207 }
8208 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
8209 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
8210 Ops.data(), Ops.size(),
8211 MemInt->getMemoryVT(),
8212 MemInt->getMemOperand());
8213
8214 // Update the uses.
8215 std::vector<SDValue> NewResults;
8216 for (unsigned i = 0; i < NumResultVecs; ++i) {
8217 NewResults.push_back(SDValue(UpdN.getNode(), i));
8218 }
8219 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
8220 DCI.CombineTo(N, NewResults);
8221 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
8222
8223 break;
Owen Anderson76706012011-04-05 21:48:57 +00008224 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00008225 return SDValue();
8226}
8227
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008228/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
8229/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
8230/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
8231/// return true.
8232static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8233 SelectionDAG &DAG = DCI.DAG;
8234 EVT VT = N->getValueType(0);
8235 // vldN-dup instructions only support 64-bit vectors for N > 1.
8236 if (!VT.is64BitVector())
8237 return false;
8238
8239 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
8240 SDNode *VLD = N->getOperand(0).getNode();
8241 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
8242 return false;
8243 unsigned NumVecs = 0;
8244 unsigned NewOpc = 0;
8245 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
8246 if (IntNo == Intrinsic::arm_neon_vld2lane) {
8247 NumVecs = 2;
8248 NewOpc = ARMISD::VLD2DUP;
8249 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
8250 NumVecs = 3;
8251 NewOpc = ARMISD::VLD3DUP;
8252 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
8253 NumVecs = 4;
8254 NewOpc = ARMISD::VLD4DUP;
8255 } else {
8256 return false;
8257 }
8258
8259 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
8260 // numbers match the load.
8261 unsigned VLDLaneNo =
8262 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
8263 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8264 UI != UE; ++UI) {
8265 // Ignore uses of the chain result.
8266 if (UI.getUse().getResNo() == NumVecs)
8267 continue;
8268 SDNode *User = *UI;
8269 if (User->getOpcode() != ARMISD::VDUPLANE ||
8270 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
8271 return false;
8272 }
8273
8274 // Create the vldN-dup node.
8275 EVT Tys[5];
8276 unsigned n;
8277 for (n = 0; n < NumVecs; ++n)
8278 Tys[n] = VT;
8279 Tys[n] = MVT::Other;
8280 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
8281 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
8282 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
8283 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
8284 Ops, 2, VLDMemInt->getMemoryVT(),
8285 VLDMemInt->getMemOperand());
8286
8287 // Update the uses.
8288 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8289 UI != UE; ++UI) {
8290 unsigned ResNo = UI.getUse().getResNo();
8291 // Ignore uses of the chain result.
8292 if (ResNo == NumVecs)
8293 continue;
8294 SDNode *User = *UI;
8295 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
8296 }
8297
8298 // Now the vldN-lane intrinsic is dead except for its chain result.
8299 // Update uses of the chain.
8300 std::vector<SDValue> VLDDupResults;
8301 for (unsigned n = 0; n < NumVecs; ++n)
8302 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
8303 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
8304 DCI.CombineTo(VLD, VLDDupResults);
8305
8306 return true;
8307}
8308
Bob Wilson9e82bf12010-07-14 01:22:12 +00008309/// PerformVDUPLANECombine - Target-specific dag combine xforms for
8310/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008311static SDValue PerformVDUPLANECombine(SDNode *N,
8312 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00008313 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008314
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008315 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
8316 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
8317 if (CombineVLDDUP(N, DCI))
8318 return SDValue(N, 0);
8319
8320 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
8321 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008322 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00008323 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00008324 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00008325 return SDValue();
8326
8327 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
8328 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
8329 // The canonical VMOV for a zero vector uses a 32-bit element size.
8330 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8331 unsigned EltBits;
8332 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
8333 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008334 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008335 if (EltSize > VT.getVectorElementType().getSizeInBits())
8336 return SDValue();
8337
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008338 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008339}
8340
Eric Christopherfa6f5912011-06-29 21:10:36 +00008341// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00008342// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
8343static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
8344{
Chad Rosier118c9a02011-06-28 17:26:57 +00008345 integerPart cN;
8346 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00008347 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
8348 I != E; I++) {
8349 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
8350 if (!C)
8351 return false;
8352
Eric Christopherfa6f5912011-06-29 21:10:36 +00008353 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00008354 APFloat APF = C->getValueAPF();
8355 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
8356 != APFloat::opOK || !isExact)
8357 return false;
8358
8359 c0 = (I == 0) ? cN : c0;
8360 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
8361 return false;
8362 }
8363 C = c0;
8364 return true;
8365}
8366
8367/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
8368/// can replace combinations of VMUL and VCVT (floating-point to integer)
8369/// when the VMUL has a constant operand that is a power of 2.
8370///
8371/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8372/// vmul.f32 d16, d17, d16
8373/// vcvt.s32.f32 d16, d16
8374/// becomes:
8375/// vcvt.s32.f32 d16, d16, #3
8376static SDValue PerformVCVTCombine(SDNode *N,
8377 TargetLowering::DAGCombinerInfo &DCI,
8378 const ARMSubtarget *Subtarget) {
8379 SelectionDAG &DAG = DCI.DAG;
8380 SDValue Op = N->getOperand(0);
8381
8382 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
8383 Op.getOpcode() != ISD::FMUL)
8384 return SDValue();
8385
8386 uint64_t C;
8387 SDValue N0 = Op->getOperand(0);
8388 SDValue ConstVec = Op->getOperand(1);
8389 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
8390
Eric Christopherfa6f5912011-06-29 21:10:36 +00008391 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00008392 !isConstVecPow2(ConstVec, isSigned, C))
8393 return SDValue();
8394
8395 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
8396 Intrinsic::arm_neon_vcvtfp2fxu;
8397 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
8398 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00008399 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00008400 DAG.getConstant(Log2_64(C), MVT::i32));
8401}
8402
8403/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
8404/// can replace combinations of VCVT (integer to floating-point) and VDIV
8405/// when the VDIV has a constant operand that is a power of 2.
8406///
8407/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8408/// vcvt.f32.s32 d16, d16
8409/// vdiv.f32 d16, d17, d16
8410/// becomes:
8411/// vcvt.f32.s32 d16, d16, #3
8412static SDValue PerformVDIVCombine(SDNode *N,
8413 TargetLowering::DAGCombinerInfo &DCI,
8414 const ARMSubtarget *Subtarget) {
8415 SelectionDAG &DAG = DCI.DAG;
8416 SDValue Op = N->getOperand(0);
8417 unsigned OpOpcode = Op.getNode()->getOpcode();
8418
8419 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
8420 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
8421 return SDValue();
8422
8423 uint64_t C;
8424 SDValue ConstVec = N->getOperand(1);
8425 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
8426
8427 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
8428 !isConstVecPow2(ConstVec, isSigned, C))
8429 return SDValue();
8430
Eric Christopherfa6f5912011-06-29 21:10:36 +00008431 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00008432 Intrinsic::arm_neon_vcvtfxu2fp;
8433 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
8434 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00008435 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00008436 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
8437}
8438
8439/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00008440/// operand of a vector shift operation, where all the elements of the
8441/// build_vector must have the same constant integer value.
8442static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
8443 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008444 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00008445 Op = Op.getOperand(0);
8446 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
8447 APInt SplatBits, SplatUndef;
8448 unsigned SplatBitSize;
8449 bool HasAnyUndefs;
8450 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
8451 HasAnyUndefs, ElementBits) ||
8452 SplatBitSize > ElementBits)
8453 return false;
8454 Cnt = SplatBits.getSExtValue();
8455 return true;
8456}
8457
8458/// isVShiftLImm - Check if this is a valid build_vector for the immediate
8459/// operand of a vector shift left operation. That value must be in the range:
8460/// 0 <= Value < ElementBits for a left shift; or
8461/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00008462static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00008463 assert(VT.isVector() && "vector shift count is not a vector type");
8464 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8465 if (! getVShiftImm(Op, ElementBits, Cnt))
8466 return false;
8467 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
8468}
8469
8470/// isVShiftRImm - Check if this is a valid build_vector for the immediate
8471/// operand of a vector shift right operation. For a shift opcode, the value
8472/// is positive, but for an intrinsic the value count must be negative. The
8473/// absolute value must be in the range:
8474/// 1 <= |Value| <= ElementBits for a right shift; or
8475/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00008476static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00008477 int64_t &Cnt) {
8478 assert(VT.isVector() && "vector shift count is not a vector type");
8479 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8480 if (! getVShiftImm(Op, ElementBits, Cnt))
8481 return false;
8482 if (isIntrinsic)
8483 Cnt = -Cnt;
8484 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
8485}
8486
8487/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
8488static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
8489 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
8490 switch (IntNo) {
8491 default:
8492 // Don't do anything for most intrinsics.
8493 break;
8494
8495 // Vector shifts: check for immediate versions and lower them.
8496 // Note: This is done during DAG combining instead of DAG legalizing because
8497 // the build_vectors for 64-bit vector element shift counts are generally
8498 // not legal, and it is hard to see their values after they get legalized to
8499 // loads from a constant pool.
8500 case Intrinsic::arm_neon_vshifts:
8501 case Intrinsic::arm_neon_vshiftu:
8502 case Intrinsic::arm_neon_vshiftls:
8503 case Intrinsic::arm_neon_vshiftlu:
8504 case Intrinsic::arm_neon_vshiftn:
8505 case Intrinsic::arm_neon_vrshifts:
8506 case Intrinsic::arm_neon_vrshiftu:
8507 case Intrinsic::arm_neon_vrshiftn:
8508 case Intrinsic::arm_neon_vqshifts:
8509 case Intrinsic::arm_neon_vqshiftu:
8510 case Intrinsic::arm_neon_vqshiftsu:
8511 case Intrinsic::arm_neon_vqshiftns:
8512 case Intrinsic::arm_neon_vqshiftnu:
8513 case Intrinsic::arm_neon_vqshiftnsu:
8514 case Intrinsic::arm_neon_vqrshiftns:
8515 case Intrinsic::arm_neon_vqrshiftnu:
8516 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00008517 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008518 int64_t Cnt;
8519 unsigned VShiftOpc = 0;
8520
8521 switch (IntNo) {
8522 case Intrinsic::arm_neon_vshifts:
8523 case Intrinsic::arm_neon_vshiftu:
8524 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
8525 VShiftOpc = ARMISD::VSHL;
8526 break;
8527 }
8528 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
8529 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
8530 ARMISD::VSHRs : ARMISD::VSHRu);
8531 break;
8532 }
8533 return SDValue();
8534
8535 case Intrinsic::arm_neon_vshiftls:
8536 case Intrinsic::arm_neon_vshiftlu:
8537 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
8538 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008539 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008540
8541 case Intrinsic::arm_neon_vrshifts:
8542 case Intrinsic::arm_neon_vrshiftu:
8543 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
8544 break;
8545 return SDValue();
8546
8547 case Intrinsic::arm_neon_vqshifts:
8548 case Intrinsic::arm_neon_vqshiftu:
8549 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
8550 break;
8551 return SDValue();
8552
8553 case Intrinsic::arm_neon_vqshiftsu:
8554 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
8555 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008556 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008557
8558 case Intrinsic::arm_neon_vshiftn:
8559 case Intrinsic::arm_neon_vrshiftn:
8560 case Intrinsic::arm_neon_vqshiftns:
8561 case Intrinsic::arm_neon_vqshiftnu:
8562 case Intrinsic::arm_neon_vqshiftnsu:
8563 case Intrinsic::arm_neon_vqrshiftns:
8564 case Intrinsic::arm_neon_vqrshiftnu:
8565 case Intrinsic::arm_neon_vqrshiftnsu:
8566 // Narrowing shifts require an immediate right shift.
8567 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
8568 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00008569 llvm_unreachable("invalid shift count for narrowing vector shift "
8570 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008571
8572 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008573 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00008574 }
8575
8576 switch (IntNo) {
8577 case Intrinsic::arm_neon_vshifts:
8578 case Intrinsic::arm_neon_vshiftu:
8579 // Opcode already set above.
8580 break;
8581 case Intrinsic::arm_neon_vshiftls:
8582 case Intrinsic::arm_neon_vshiftlu:
8583 if (Cnt == VT.getVectorElementType().getSizeInBits())
8584 VShiftOpc = ARMISD::VSHLLi;
8585 else
8586 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
8587 ARMISD::VSHLLs : ARMISD::VSHLLu);
8588 break;
8589 case Intrinsic::arm_neon_vshiftn:
8590 VShiftOpc = ARMISD::VSHRN; break;
8591 case Intrinsic::arm_neon_vrshifts:
8592 VShiftOpc = ARMISD::VRSHRs; break;
8593 case Intrinsic::arm_neon_vrshiftu:
8594 VShiftOpc = ARMISD::VRSHRu; break;
8595 case Intrinsic::arm_neon_vrshiftn:
8596 VShiftOpc = ARMISD::VRSHRN; break;
8597 case Intrinsic::arm_neon_vqshifts:
8598 VShiftOpc = ARMISD::VQSHLs; break;
8599 case Intrinsic::arm_neon_vqshiftu:
8600 VShiftOpc = ARMISD::VQSHLu; break;
8601 case Intrinsic::arm_neon_vqshiftsu:
8602 VShiftOpc = ARMISD::VQSHLsu; break;
8603 case Intrinsic::arm_neon_vqshiftns:
8604 VShiftOpc = ARMISD::VQSHRNs; break;
8605 case Intrinsic::arm_neon_vqshiftnu:
8606 VShiftOpc = ARMISD::VQSHRNu; break;
8607 case Intrinsic::arm_neon_vqshiftnsu:
8608 VShiftOpc = ARMISD::VQSHRNsu; break;
8609 case Intrinsic::arm_neon_vqrshiftns:
8610 VShiftOpc = ARMISD::VQRSHRNs; break;
8611 case Intrinsic::arm_neon_vqrshiftnu:
8612 VShiftOpc = ARMISD::VQRSHRNu; break;
8613 case Intrinsic::arm_neon_vqrshiftnsu:
8614 VShiftOpc = ARMISD::VQRSHRNsu; break;
8615 }
8616
8617 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008618 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008619 }
8620
8621 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00008622 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008623 int64_t Cnt;
8624 unsigned VShiftOpc = 0;
8625
8626 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
8627 VShiftOpc = ARMISD::VSLI;
8628 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
8629 VShiftOpc = ARMISD::VSRI;
8630 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00008631 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008632 }
8633
8634 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
8635 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008636 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008637 }
8638
8639 case Intrinsic::arm_neon_vqrshifts:
8640 case Intrinsic::arm_neon_vqrshiftu:
8641 // No immediate versions of these to check for.
8642 break;
8643 }
8644
8645 return SDValue();
8646}
8647
8648/// PerformShiftCombine - Checks for immediate versions of vector shifts and
8649/// lowers them. As with the vector shift intrinsics, this is done during DAG
8650/// combining instead of DAG legalizing because the build_vectors for 64-bit
8651/// vector element shift counts are generally not legal, and it is hard to see
8652/// their values after they get legalized to loads from a constant pool.
8653static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
8654 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00008655 EVT VT = N->getValueType(0);
Evan Cheng5fb468a2012-02-23 02:58:19 +00008656 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
8657 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
8658 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
8659 SDValue N1 = N->getOperand(1);
8660 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
8661 SDValue N0 = N->getOperand(0);
8662 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
8663 DAG.MaskedValueIsZero(N0.getOperand(0),
8664 APInt::getHighBitsSet(32, 16)))
8665 return DAG.getNode(ISD::ROTR, N->getDebugLoc(), VT, N0, N1);
8666 }
8667 }
Bob Wilson5bafff32009-06-22 23:27:02 +00008668
8669 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00008670 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8671 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00008672 return SDValue();
8673
8674 assert(ST->hasNEON() && "unexpected vector shift");
8675 int64_t Cnt;
8676
8677 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008678 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00008679
8680 case ISD::SHL:
8681 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
8682 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008683 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008684 break;
8685
8686 case ISD::SRA:
8687 case ISD::SRL:
8688 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
8689 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
8690 ARMISD::VSHRs : ARMISD::VSHRu);
8691 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008692 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008693 }
8694 }
8695 return SDValue();
8696}
8697
8698/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
8699/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
8700static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
8701 const ARMSubtarget *ST) {
8702 SDValue N0 = N->getOperand(0);
8703
8704 // Check for sign- and zero-extensions of vector extract operations of 8-
8705 // and 16-bit vector elements. NEON supports these directly. They are
8706 // handled during DAG combining because type legalization will promote them
8707 // to 32-bit types and it is messy to recognize the operations after that.
8708 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
8709 SDValue Vec = N0.getOperand(0);
8710 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00008711 EVT VT = N->getValueType(0);
8712 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008713 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8714
Owen Anderson825b72b2009-08-11 20:47:22 +00008715 if (VT == MVT::i32 &&
8716 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00008717 TLI.isTypeLegal(Vec.getValueType()) &&
8718 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00008719
8720 unsigned Opc = 0;
8721 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008722 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00008723 case ISD::SIGN_EXTEND:
8724 Opc = ARMISD::VGETLANEs;
8725 break;
8726 case ISD::ZERO_EXTEND:
8727 case ISD::ANY_EXTEND:
8728 Opc = ARMISD::VGETLANEu;
8729 break;
8730 }
8731 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
8732 }
8733 }
8734
8735 return SDValue();
8736}
8737
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008738/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
8739/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
8740static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
8741 const ARMSubtarget *ST) {
8742 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00008743 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008744 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
8745 // a NaN; only do the transformation when it matches that behavior.
8746
8747 // For now only do this when using NEON for FP operations; if using VFP, it
8748 // is not obvious that the benefit outweighs the cost of switching to the
8749 // NEON pipeline.
8750 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
8751 N->getValueType(0) != MVT::f32)
8752 return SDValue();
8753
8754 SDValue CondLHS = N->getOperand(0);
8755 SDValue CondRHS = N->getOperand(1);
8756 SDValue LHS = N->getOperand(2);
8757 SDValue RHS = N->getOperand(3);
8758 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
8759
8760 unsigned Opcode = 0;
8761 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00008762 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008763 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00008764 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008765 IsReversed = true ; // x CC y ? y : x
8766 } else {
8767 return SDValue();
8768 }
8769
Bob Wilsone742bb52010-02-24 22:15:53 +00008770 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008771 switch (CC) {
8772 default: break;
8773 case ISD::SETOLT:
8774 case ISD::SETOLE:
8775 case ISD::SETLT:
8776 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008777 case ISD::SETULT:
8778 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00008779 // If LHS is NaN, an ordered comparison will be false and the result will
8780 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
8781 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
8782 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
8783 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8784 break;
8785 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
8786 // will return -0, so vmin can only be used for unsafe math or if one of
8787 // the operands is known to be nonzero.
8788 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008789 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00008790 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8791 break;
8792 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008793 break;
8794
8795 case ISD::SETOGT:
8796 case ISD::SETOGE:
8797 case ISD::SETGT:
8798 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008799 case ISD::SETUGT:
8800 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00008801 // If LHS is NaN, an ordered comparison will be false and the result will
8802 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
8803 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
8804 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
8805 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8806 break;
8807 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
8808 // will return +0, so vmax can only be used for unsafe math or if one of
8809 // the operands is known to be nonzero.
8810 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008811 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00008812 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8813 break;
8814 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008815 break;
8816 }
8817
8818 if (!Opcode)
8819 return SDValue();
8820 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
8821}
8822
Evan Chenge721f5c2011-07-13 00:42:17 +00008823/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
8824SDValue
8825ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
8826 SDValue Cmp = N->getOperand(4);
8827 if (Cmp.getOpcode() != ARMISD::CMPZ)
8828 // Only looking at EQ and NE cases.
8829 return SDValue();
8830
8831 EVT VT = N->getValueType(0);
8832 DebugLoc dl = N->getDebugLoc();
8833 SDValue LHS = Cmp.getOperand(0);
8834 SDValue RHS = Cmp.getOperand(1);
8835 SDValue FalseVal = N->getOperand(0);
8836 SDValue TrueVal = N->getOperand(1);
8837 SDValue ARMcc = N->getOperand(2);
Jim Grosbachb04546f2011-09-13 20:30:37 +00008838 ARMCC::CondCodes CC =
8839 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chenge721f5c2011-07-13 00:42:17 +00008840
8841 // Simplify
8842 // mov r1, r0
8843 // cmp r1, x
8844 // mov r0, y
8845 // moveq r0, x
8846 // to
8847 // cmp r0, x
8848 // movne r0, y
8849 //
8850 // mov r1, r0
8851 // cmp r1, x
8852 // mov r0, x
8853 // movne r0, y
8854 // to
8855 // cmp r0, x
8856 // movne r0, y
8857 /// FIXME: Turn this into a target neutral optimization?
8858 SDValue Res;
Evan Cheng9b88d2d2011-09-28 23:16:31 +00008859 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chenge721f5c2011-07-13 00:42:17 +00008860 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
8861 N->getOperand(3), Cmp);
8862 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
8863 SDValue ARMcc;
8864 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
8865 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
8866 N->getOperand(3), NewCmp);
8867 }
8868
8869 if (Res.getNode()) {
8870 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008871 DAG.ComputeMaskedBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chenge721f5c2011-07-13 00:42:17 +00008872 // Capture demanded bits information that would be otherwise lost.
8873 if (KnownZero == 0xfffffffe)
8874 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8875 DAG.getValueType(MVT::i1));
8876 else if (KnownZero == 0xffffff00)
8877 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8878 DAG.getValueType(MVT::i8));
8879 else if (KnownZero == 0xffff0000)
8880 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8881 DAG.getValueType(MVT::i16));
8882 }
8883
8884 return Res;
8885}
8886
Dan Gohman475871a2008-07-27 21:46:04 +00008887SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008888 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008889 switch (N->getOpcode()) {
8890 default: break;
Arnold Schwaighoferbcc4c1d2012-08-09 15:25:52 +00008891 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattner189531f2011-06-14 23:48:48 +00008892 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008893 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00008894 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008895 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chengc892aeb2012-02-23 01:19:06 +00008896 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
8897 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Cheng0c1aec12010-12-14 03:22:07 +00008898 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00008899 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008900 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00008901 case ISD::STORE: return PerformSTORECombine(N, DCI);
8902 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
8903 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00008904 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008905 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00008906 case ISD::FP_TO_SINT:
8907 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
8908 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008909 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00008910 case ISD::SHL:
8911 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008912 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00008913 case ISD::SIGN_EXTEND:
8914 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008915 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
8916 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00008917 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00008918 case ARMISD::VLD2DUP:
8919 case ARMISD::VLD3DUP:
8920 case ARMISD::VLD4DUP:
8921 return CombineBaseUpdate(N, DCI);
8922 case ISD::INTRINSIC_VOID:
8923 case ISD::INTRINSIC_W_CHAIN:
8924 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
8925 case Intrinsic::arm_neon_vld1:
8926 case Intrinsic::arm_neon_vld2:
8927 case Intrinsic::arm_neon_vld3:
8928 case Intrinsic::arm_neon_vld4:
8929 case Intrinsic::arm_neon_vld2lane:
8930 case Intrinsic::arm_neon_vld3lane:
8931 case Intrinsic::arm_neon_vld4lane:
8932 case Intrinsic::arm_neon_vst1:
8933 case Intrinsic::arm_neon_vst2:
8934 case Intrinsic::arm_neon_vst3:
8935 case Intrinsic::arm_neon_vst4:
8936 case Intrinsic::arm_neon_vst2lane:
8937 case Intrinsic::arm_neon_vst3lane:
8938 case Intrinsic::arm_neon_vst4lane:
8939 return CombineBaseUpdate(N, DCI);
8940 default: break;
8941 }
8942 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008943 }
Dan Gohman475871a2008-07-27 21:46:04 +00008944 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008945}
8946
Evan Cheng31959b12011-02-02 01:06:55 +00008947bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
8948 EVT VT) const {
8949 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
8950}
8951
Bill Wendlingaf566342009-08-15 21:21:19 +00008952bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00008953 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00008954 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00008955
8956 switch (VT.getSimpleVT().SimpleTy) {
8957 default:
8958 return false;
8959 case MVT::i8:
8960 case MVT::i16:
8961 case MVT::i32:
8962 return true;
8963 // FIXME: VLD1 etc with standard alignment is legal.
8964 }
8965}
8966
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008967static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
8968 unsigned AlignCheck) {
8969 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
8970 (DstAlign == 0 || DstAlign % AlignCheck == 0));
8971}
8972
8973EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
8974 unsigned DstAlign, unsigned SrcAlign,
Lang Hamesa1e78882011-11-02 23:37:04 +00008975 bool IsZeroVal,
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008976 bool MemcpyStrSrc,
8977 MachineFunction &MF) const {
8978 const Function *F = MF.getFunction();
8979
8980 // See if we can use NEON instructions for this...
Lang Hamesa1e78882011-11-02 23:37:04 +00008981 if (IsZeroVal &&
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008982 !F->hasFnAttr(Attribute::NoImplicitFloat) &&
8983 Subtarget->hasNEON()) {
8984 if (memOpAlign(SrcAlign, DstAlign, 16) && Size >= 16) {
8985 return MVT::v4i32;
8986 } else if (memOpAlign(SrcAlign, DstAlign, 8) && Size >= 8) {
8987 return MVT::v2i32;
8988 }
8989 }
8990
Lang Hames5207bf22011-11-08 18:56:23 +00008991 // Lowering to i32/i16 if the size permits.
8992 if (Size >= 4) {
8993 return MVT::i32;
8994 } else if (Size >= 2) {
8995 return MVT::i16;
8996 }
8997
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008998 // Let the target-independent logic figure it out.
8999 return MVT::Other;
9000}
9001
Evan Chenge6c835f2009-08-14 20:09:37 +00009002static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
9003 if (V < 0)
9004 return false;
9005
9006 unsigned Scale = 1;
9007 switch (VT.getSimpleVT().SimpleTy) {
9008 default: return false;
9009 case MVT::i1:
9010 case MVT::i8:
9011 // Scale == 1;
9012 break;
9013 case MVT::i16:
9014 // Scale == 2;
9015 Scale = 2;
9016 break;
9017 case MVT::i32:
9018 // Scale == 4;
9019 Scale = 4;
9020 break;
9021 }
9022
9023 if ((V & (Scale - 1)) != 0)
9024 return false;
9025 V /= Scale;
9026 return V == (V & ((1LL << 5) - 1));
9027}
9028
9029static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
9030 const ARMSubtarget *Subtarget) {
9031 bool isNeg = false;
9032 if (V < 0) {
9033 isNeg = true;
9034 V = - V;
9035 }
9036
9037 switch (VT.getSimpleVT().SimpleTy) {
9038 default: return false;
9039 case MVT::i1:
9040 case MVT::i8:
9041 case MVT::i16:
9042 case MVT::i32:
9043 // + imm12 or - imm8
9044 if (isNeg)
9045 return V == (V & ((1LL << 8) - 1));
9046 return V == (V & ((1LL << 12) - 1));
9047 case MVT::f32:
9048 case MVT::f64:
9049 // Same as ARM mode. FIXME: NEON?
9050 if (!Subtarget->hasVFP2())
9051 return false;
9052 if ((V & 3) != 0)
9053 return false;
9054 V >>= 2;
9055 return V == (V & ((1LL << 8) - 1));
9056 }
9057}
9058
Evan Chengb01fad62007-03-12 23:30:29 +00009059/// isLegalAddressImmediate - Return true if the integer value can be used
9060/// as the offset of the target addressing mode for load / store of the
9061/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00009062static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00009063 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00009064 if (V == 0)
9065 return true;
9066
Evan Cheng65011532009-03-09 19:15:00 +00009067 if (!VT.isSimple())
9068 return false;
9069
Evan Chenge6c835f2009-08-14 20:09:37 +00009070 if (Subtarget->isThumb1Only())
9071 return isLegalT1AddressImmediate(V, VT);
9072 else if (Subtarget->isThumb2())
9073 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00009074
Evan Chenge6c835f2009-08-14 20:09:37 +00009075 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00009076 if (V < 0)
9077 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00009078 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00009079 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00009080 case MVT::i1:
9081 case MVT::i8:
9082 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00009083 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00009084 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009085 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00009086 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00009087 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009088 case MVT::f32:
9089 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00009090 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00009091 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00009092 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00009093 return false;
9094 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00009095 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00009096 }
Evan Chenga8e29892007-01-19 07:51:42 +00009097}
9098
Evan Chenge6c835f2009-08-14 20:09:37 +00009099bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
9100 EVT VT) const {
9101 int Scale = AM.Scale;
9102 if (Scale < 0)
9103 return false;
9104
9105 switch (VT.getSimpleVT().SimpleTy) {
9106 default: return false;
9107 case MVT::i1:
9108 case MVT::i8:
9109 case MVT::i16:
9110 case MVT::i32:
9111 if (Scale == 1)
9112 return true;
9113 // r + r << imm
9114 Scale = Scale & ~1;
9115 return Scale == 2 || Scale == 4 || Scale == 8;
9116 case MVT::i64:
9117 // r + r
9118 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
9119 return true;
9120 return false;
9121 case MVT::isVoid:
9122 // Note, we allow "void" uses (basically, uses that aren't loads or
9123 // stores), because arm allows folding a scale into many arithmetic
9124 // operations. This should be made more precise and revisited later.
9125
9126 // Allow r << imm, but the imm has to be a multiple of two.
9127 if (Scale & 1) return false;
9128 return isPowerOf2_32(Scale);
9129 }
9130}
9131
Chris Lattner37caf8c2007-04-09 23:33:39 +00009132/// isLegalAddressingMode - Return true if the addressing mode represented
9133/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009134bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009135 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009136 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00009137 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00009138 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009139
Chris Lattner37caf8c2007-04-09 23:33:39 +00009140 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009141 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009142 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009143
Chris Lattner37caf8c2007-04-09 23:33:39 +00009144 switch (AM.Scale) {
9145 case 0: // no scale reg, must be "r+i" or "r", or "i".
9146 break;
9147 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00009148 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00009149 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00009150 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00009151 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00009152 // ARM doesn't support any R+R*scale+imm addr modes.
9153 if (AM.BaseOffs)
9154 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009155
Bob Wilson2c7dab12009-04-08 17:55:28 +00009156 if (!VT.isSimple())
9157 return false;
9158
Evan Chenge6c835f2009-08-14 20:09:37 +00009159 if (Subtarget->isThumb2())
9160 return isLegalT2ScaledAddressingMode(AM, VT);
9161
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009162 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00009163 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00009164 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00009165 case MVT::i1:
9166 case MVT::i8:
9167 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009168 if (Scale < 0) Scale = -Scale;
9169 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009170 return true;
9171 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00009172 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009173 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00009174 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00009175 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009176 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009177 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00009178 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009179
Owen Anderson825b72b2009-08-11 20:47:22 +00009180 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00009181 // Note, we allow "void" uses (basically, uses that aren't loads or
9182 // stores), because arm allows folding a scale into many arithmetic
9183 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009184
Chris Lattner37caf8c2007-04-09 23:33:39 +00009185 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00009186 if (Scale & 1) return false;
9187 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00009188 }
Evan Chengb01fad62007-03-12 23:30:29 +00009189 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00009190 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00009191}
9192
Evan Cheng77e47512009-11-11 19:05:52 +00009193/// isLegalICmpImmediate - Return true if the specified immediate is legal
9194/// icmp immediate, that is the target has icmp instructions which can compare
9195/// a register against the immediate without having to materialize the
9196/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00009197bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00009198 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng77e47512009-11-11 19:05:52 +00009199 if (!Subtarget->isThumb())
Chandler Carruthba4d4572012-04-06 20:10:52 +00009200 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1;
Evan Cheng77e47512009-11-11 19:05:52 +00009201 if (Subtarget->isThumb2())
Chandler Carruthba4d4572012-04-06 20:10:52 +00009202 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1;
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00009203 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng06b53c02009-11-12 07:13:11 +00009204 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00009205}
9206
Andrew Trick8d8d9612012-07-18 18:34:27 +00009207/// isLegalAddImmediate - Return true if the specified immediate is a legal add
9208/// *or sub* immediate, that is the target has add or sub instructions which can
9209/// add a register with the immediate without having to materialize the
Dan Gohmancca82142011-05-03 00:46:49 +00009210/// immediate into a register.
9211bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Trick8d8d9612012-07-18 18:34:27 +00009212 // Same encoding for add/sub, just flip the sign.
9213 int64_t AbsImm = llvm::abs64(Imm);
9214 if (!Subtarget->isThumb())
9215 return ARM_AM::getSOImmVal(AbsImm) != -1;
9216 if (Subtarget->isThumb2())
9217 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
9218 // Thumb1 only has 8-bit unsigned immediate.
9219 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohmancca82142011-05-03 00:46:49 +00009220}
9221
Owen Andersone50ed302009-08-10 22:56:29 +00009222static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00009223 bool isSEXTLoad, SDValue &Base,
9224 SDValue &Offset, bool &isInc,
9225 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00009226 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9227 return false;
9228
Owen Anderson825b72b2009-08-11 20:47:22 +00009229 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00009230 // AddressingMode 3
9231 Base = Ptr->getOperand(0);
9232 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009233 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00009234 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009235 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00009236 isInc = false;
9237 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9238 return true;
9239 }
9240 }
9241 isInc = (Ptr->getOpcode() == ISD::ADD);
9242 Offset = Ptr->getOperand(1);
9243 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00009244 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00009245 // AddressingMode 2
9246 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009247 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00009248 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009249 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00009250 isInc = false;
9251 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9252 Base = Ptr->getOperand(0);
9253 return true;
9254 }
9255 }
9256
9257 if (Ptr->getOpcode() == ISD::ADD) {
9258 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00009259 ARM_AM::ShiftOpc ShOpcVal=
9260 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00009261 if (ShOpcVal != ARM_AM::no_shift) {
9262 Base = Ptr->getOperand(1);
9263 Offset = Ptr->getOperand(0);
9264 } else {
9265 Base = Ptr->getOperand(0);
9266 Offset = Ptr->getOperand(1);
9267 }
9268 return true;
9269 }
9270
9271 isInc = (Ptr->getOpcode() == ISD::ADD);
9272 Base = Ptr->getOperand(0);
9273 Offset = Ptr->getOperand(1);
9274 return true;
9275 }
9276
Jim Grosbache5165492009-11-09 00:11:35 +00009277 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00009278 return false;
9279}
9280
Owen Andersone50ed302009-08-10 22:56:29 +00009281static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00009282 bool isSEXTLoad, SDValue &Base,
9283 SDValue &Offset, bool &isInc,
9284 SelectionDAG &DAG) {
9285 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9286 return false;
9287
9288 Base = Ptr->getOperand(0);
9289 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
9290 int RHSC = (int)RHS->getZExtValue();
9291 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
9292 assert(Ptr->getOpcode() == ISD::ADD);
9293 isInc = false;
9294 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9295 return true;
9296 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
9297 isInc = Ptr->getOpcode() == ISD::ADD;
9298 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
9299 return true;
9300 }
9301 }
9302
9303 return false;
9304}
9305
Evan Chenga8e29892007-01-19 07:51:42 +00009306/// getPreIndexedAddressParts - returns true by value, base pointer and
9307/// offset pointer and addressing mode by reference if the node's address
9308/// can be legally represented as pre-indexed load / store address.
9309bool
Dan Gohman475871a2008-07-27 21:46:04 +00009310ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
9311 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00009312 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00009313 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009314 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00009315 return false;
9316
Owen Andersone50ed302009-08-10 22:56:29 +00009317 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00009318 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00009319 bool isSEXTLoad = false;
9320 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
9321 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009322 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00009323 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9324 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
9325 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009326 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00009327 } else
9328 return false;
9329
9330 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00009331 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00009332 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00009333 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
9334 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00009335 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00009336 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00009337 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00009338 if (!isLegal)
9339 return false;
9340
9341 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
9342 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00009343}
9344
9345/// getPostIndexedAddressParts - returns true by value, base pointer and
9346/// offset pointer and addressing mode by reference if this node can be
9347/// combined with a load / store to form a post-indexed load / store.
9348bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00009349 SDValue &Base,
9350 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00009351 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00009352 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009353 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00009354 return false;
9355
Owen Andersone50ed302009-08-10 22:56:29 +00009356 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00009357 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00009358 bool isSEXTLoad = false;
9359 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009360 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00009361 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00009362 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9363 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009364 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00009365 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00009366 } else
9367 return false;
9368
9369 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00009370 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00009371 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00009372 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00009373 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00009374 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00009375 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
9376 isInc, DAG);
9377 if (!isLegal)
9378 return false;
9379
Evan Cheng28dad2a2010-05-18 21:31:17 +00009380 if (Ptr != Base) {
9381 // Swap base ptr and offset to catch more post-index load / store when
9382 // it's legal. In Thumb2 mode, offset must be an immediate.
9383 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
9384 !Subtarget->isThumb2())
9385 std::swap(Base, Offset);
9386
9387 // Post-indexed load / store update the base pointer.
9388 if (Ptr != Base)
9389 return false;
9390 }
9391
Evan Chenge88d5ce2009-07-02 07:28:31 +00009392 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
9393 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00009394}
9395
Dan Gohman475871a2008-07-27 21:46:04 +00009396void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00009397 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009398 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00009399 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00009400 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009401 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00009402 switch (Op.getOpcode()) {
9403 default: break;
9404 case ARMISD::CMOV: {
9405 // Bits are known zero/one if known on the LHS and RHS.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009406 DAG.ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00009407 if (KnownZero == 0 && KnownOne == 0) return;
9408
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009409 APInt KnownZeroRHS, KnownOneRHS;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009410 DAG.ComputeMaskedBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00009411 KnownZero &= KnownZeroRHS;
9412 KnownOne &= KnownOneRHS;
9413 return;
9414 }
9415 }
9416}
9417
9418//===----------------------------------------------------------------------===//
9419// ARM Inline Assembly Support
9420//===----------------------------------------------------------------------===//
9421
Evan Cheng55d42002011-01-08 01:24:27 +00009422bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
9423 // Looking for "rev" which is V6+.
9424 if (!Subtarget->hasV6Ops())
9425 return false;
9426
9427 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9428 std::string AsmStr = IA->getAsmString();
9429 SmallVector<StringRef, 4> AsmPieces;
9430 SplitString(AsmStr, AsmPieces, ";\n");
9431
9432 switch (AsmPieces.size()) {
9433 default: return false;
9434 case 1:
9435 AsmStr = AsmPieces[0];
9436 AsmPieces.clear();
9437 SplitString(AsmStr, AsmPieces, " \t,");
9438
9439 // rev $0, $1
9440 if (AsmPieces.size() == 3 &&
9441 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
9442 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009443 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00009444 if (Ty && Ty->getBitWidth() == 32)
9445 return IntrinsicLowering::LowerToByteSwap(CI);
9446 }
9447 break;
9448 }
9449
9450 return false;
9451}
9452
Evan Chenga8e29892007-01-19 07:51:42 +00009453/// getConstraintType - Given a constraint letter, return the type of
9454/// constraint it is for this target.
9455ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00009456ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
9457 if (Constraint.size() == 1) {
9458 switch (Constraint[0]) {
9459 default: break;
9460 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009461 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00009462 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00009463 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009464 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00009465 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00009466 // An address with a single base register. Due to the way we
9467 // currently handle addresses it is the same as an 'r' memory constraint.
9468 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00009469 }
Eric Christopher1312ca82011-06-21 22:10:57 +00009470 } else if (Constraint.size() == 2) {
9471 switch (Constraint[0]) {
9472 default: break;
9473 // All 'U+' constraints are addresses.
9474 case 'U': return C_Memory;
9475 }
Evan Chenga8e29892007-01-19 07:51:42 +00009476 }
Chris Lattner4234f572007-03-25 02:14:49 +00009477 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00009478}
9479
John Thompson44ab89e2010-10-29 17:29:13 +00009480/// Examine constraint type and operand type and determine a weight value.
9481/// This object must already have been set up with the operand type
9482/// and the current alternative constraint selected.
9483TargetLowering::ConstraintWeight
9484ARMTargetLowering::getSingleConstraintMatchWeight(
9485 AsmOperandInfo &info, const char *constraint) const {
9486 ConstraintWeight weight = CW_Invalid;
9487 Value *CallOperandVal = info.CallOperandVal;
9488 // If we don't have a value, we can't do a match,
9489 // but allow it at the lowest weight.
9490 if (CallOperandVal == NULL)
9491 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009492 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00009493 // Look at the constraint type.
9494 switch (*constraint) {
9495 default:
9496 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
9497 break;
9498 case 'l':
9499 if (type->isIntegerTy()) {
9500 if (Subtarget->isThumb())
9501 weight = CW_SpecificReg;
9502 else
9503 weight = CW_Register;
9504 }
9505 break;
9506 case 'w':
9507 if (type->isFloatingPointTy())
9508 weight = CW_Register;
9509 break;
9510 }
9511 return weight;
9512}
9513
Eric Christopher35e6d4d2011-06-30 23:50:52 +00009514typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
9515RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00009516ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00009517 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00009518 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00009519 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00009520 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00009521 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00009522 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +00009523 return RCPair(0U, &ARM::tGPRRegClass);
9524 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopher73744df2011-06-30 23:23:01 +00009525 case 'h': // High regs or no regs.
9526 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +00009527 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopher1070f822011-07-01 00:19:27 +00009528 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009529 case 'r':
Craig Topper420761a2012-04-20 07:30:17 +00009530 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009531 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00009532 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009533 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00009534 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +00009535 return RCPair(0U, &ARM::DPRRegClass);
Evan Chengd831cda2009-12-08 23:06:22 +00009536 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +00009537 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009538 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00009539 case 'x':
9540 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009541 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009542 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +00009543 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009544 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +00009545 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009546 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009547 case 't':
9548 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009549 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009550 break;
Evan Chenga8e29892007-01-19 07:51:42 +00009551 }
9552 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00009553 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topper420761a2012-04-20 07:30:17 +00009554 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00009555
Evan Chenga8e29892007-01-19 07:51:42 +00009556 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
9557}
9558
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009559/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
9560/// vector. If it is invalid, don't add anything to Ops.
9561void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00009562 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009563 std::vector<SDValue>&Ops,
9564 SelectionDAG &DAG) const {
9565 SDValue Result(0, 0);
9566
Eric Christopher100c8332011-06-02 23:16:42 +00009567 // Currently only support length 1 constraints.
9568 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00009569
Eric Christopher100c8332011-06-02 23:16:42 +00009570 char ConstraintLetter = Constraint[0];
9571 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009572 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00009573 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009574 case 'I': case 'J': case 'K': case 'L':
9575 case 'M': case 'N': case 'O':
9576 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
9577 if (!C)
9578 return;
9579
9580 int64_t CVal64 = C->getSExtValue();
9581 int CVal = (int) CVal64;
9582 // None of these constraints allow values larger than 32 bits. Check
9583 // that the value fits in an int.
9584 if (CVal != CVal64)
9585 return;
9586
Eric Christopher100c8332011-06-02 23:16:42 +00009587 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00009588 case 'j':
Andrew Trick3af7a672011-09-20 03:06:13 +00009589 // Constant suitable for movw, must be between 0 and
9590 // 65535.
9591 if (Subtarget->hasV6T2Ops())
9592 if (CVal >= 0 && CVal <= 65535)
9593 break;
9594 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009595 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009596 if (Subtarget->isThumb1Only()) {
9597 // This must be a constant between 0 and 255, for ADD
9598 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009599 if (CVal >= 0 && CVal <= 255)
9600 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009601 } else if (Subtarget->isThumb2()) {
9602 // A constant that can be used as an immediate value in a
9603 // data-processing instruction.
9604 if (ARM_AM::getT2SOImmVal(CVal) != -1)
9605 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009606 } else {
9607 // A constant that can be used as an immediate value in a
9608 // data-processing instruction.
9609 if (ARM_AM::getSOImmVal(CVal) != -1)
9610 break;
9611 }
9612 return;
9613
9614 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009615 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009616 // This must be a constant between -255 and -1, for negated ADD
9617 // immediates. This can be used in GCC with an "n" modifier that
9618 // prints the negated value, for use with SUB instructions. It is
9619 // not useful otherwise but is implemented for compatibility.
9620 if (CVal >= -255 && CVal <= -1)
9621 break;
9622 } else {
9623 // This must be a constant between -4095 and 4095. It is not clear
9624 // what this constraint is intended for. Implemented for
9625 // compatibility with GCC.
9626 if (CVal >= -4095 && CVal <= 4095)
9627 break;
9628 }
9629 return;
9630
9631 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009632 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009633 // A 32-bit value where only one byte has a nonzero value. Exclude
9634 // zero to match GCC. This constraint is used by GCC internally for
9635 // constants that can be loaded with a move/shift combination.
9636 // It is not useful otherwise but is implemented for compatibility.
9637 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
9638 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009639 } else if (Subtarget->isThumb2()) {
9640 // A constant whose bitwise inverse can be used as an immediate
9641 // value in a data-processing instruction. This can be used in GCC
9642 // with a "B" modifier that prints the inverted value, for use with
9643 // BIC and MVN instructions. It is not useful otherwise but is
9644 // implemented for compatibility.
9645 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
9646 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009647 } else {
9648 // A constant whose bitwise inverse can be used as an immediate
9649 // value in a data-processing instruction. This can be used in GCC
9650 // with a "B" modifier that prints the inverted value, for use with
9651 // BIC and MVN instructions. It is not useful otherwise but is
9652 // implemented for compatibility.
9653 if (ARM_AM::getSOImmVal(~CVal) != -1)
9654 break;
9655 }
9656 return;
9657
9658 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009659 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009660 // This must be a constant between -7 and 7,
9661 // for 3-operand ADD/SUB immediate instructions.
9662 if (CVal >= -7 && CVal < 7)
9663 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009664 } else if (Subtarget->isThumb2()) {
9665 // A constant whose negation can be used as an immediate value in a
9666 // data-processing instruction. This can be used in GCC with an "n"
9667 // modifier that prints the negated value, for use with SUB
9668 // instructions. It is not useful otherwise but is implemented for
9669 // compatibility.
9670 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
9671 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009672 } else {
9673 // A constant whose negation can be used as an immediate value in a
9674 // data-processing instruction. This can be used in GCC with an "n"
9675 // modifier that prints the negated value, for use with SUB
9676 // instructions. It is not useful otherwise but is implemented for
9677 // compatibility.
9678 if (ARM_AM::getSOImmVal(-CVal) != -1)
9679 break;
9680 }
9681 return;
9682
9683 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009684 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009685 // This must be a multiple of 4 between 0 and 1020, for
9686 // ADD sp + immediate.
9687 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
9688 break;
9689 } else {
9690 // A power of two or a constant between 0 and 32. This is used in
9691 // GCC for the shift amount on shifted register operands, but it is
9692 // useful in general for any shift amounts.
9693 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
9694 break;
9695 }
9696 return;
9697
9698 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009699 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009700 // This must be a constant between 0 and 31, for shift amounts.
9701 if (CVal >= 0 && CVal <= 31)
9702 break;
9703 }
9704 return;
9705
9706 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009707 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009708 // This must be a multiple of 4 between -508 and 508, for
9709 // ADD/SUB sp = sp + immediate.
9710 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
9711 break;
9712 }
9713 return;
9714 }
9715 Result = DAG.getTargetConstant(CVal, Op.getValueType());
9716 break;
9717 }
9718
9719 if (Result.getNode()) {
9720 Ops.push_back(Result);
9721 return;
9722 }
Dale Johannesen1784d162010-06-25 21:55:36 +00009723 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009724}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00009725
9726bool
9727ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
9728 // The ARM target isn't yet aware of offsets.
9729 return false;
9730}
Evan Cheng39382422009-10-28 01:44:26 +00009731
Jim Grosbach469bbdb2010-07-16 23:05:05 +00009732bool ARM::isBitFieldInvertedMask(unsigned v) {
9733 if (v == 0xffffffff)
9734 return 0;
9735 // there can be 1's on either or both "outsides", all the "inside"
9736 // bits must be 0's
9737 unsigned int lsb = 0, msb = 31;
9738 while (v & (1 << msb)) --msb;
9739 while (v & (1 << lsb)) ++lsb;
9740 for (unsigned int i = lsb; i <= msb; ++i) {
9741 if (v & (1 << i))
9742 return 0;
9743 }
9744 return 1;
9745}
9746
Evan Cheng39382422009-10-28 01:44:26 +00009747/// isFPImmLegal - Returns true if the target can instruction select the
9748/// specified FP immediate natively. If false, the legalizer will
9749/// materialize the FP immediate as a load from a constant pool.
9750bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
9751 if (!Subtarget->hasVFP3())
9752 return false;
9753 if (VT == MVT::f32)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00009754 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00009755 if (VT == MVT::f64)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00009756 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00009757 return false;
9758}
Bob Wilson65ffec42010-09-21 17:56:22 +00009759
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009760/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00009761/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
9762/// specified in the intrinsic calls.
9763bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
9764 const CallInst &I,
9765 unsigned Intrinsic) const {
9766 switch (Intrinsic) {
9767 case Intrinsic::arm_neon_vld1:
9768 case Intrinsic::arm_neon_vld2:
9769 case Intrinsic::arm_neon_vld3:
9770 case Intrinsic::arm_neon_vld4:
9771 case Intrinsic::arm_neon_vld2lane:
9772 case Intrinsic::arm_neon_vld3lane:
9773 case Intrinsic::arm_neon_vld4lane: {
9774 Info.opc = ISD::INTRINSIC_W_CHAIN;
9775 // Conservatively set memVT to the entire set of vectors loaded.
9776 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
9777 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
9778 Info.ptrVal = I.getArgOperand(0);
9779 Info.offset = 0;
9780 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
9781 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
9782 Info.vol = false; // volatile loads with NEON intrinsics not supported
9783 Info.readMem = true;
9784 Info.writeMem = false;
9785 return true;
9786 }
9787 case Intrinsic::arm_neon_vst1:
9788 case Intrinsic::arm_neon_vst2:
9789 case Intrinsic::arm_neon_vst3:
9790 case Intrinsic::arm_neon_vst4:
9791 case Intrinsic::arm_neon_vst2lane:
9792 case Intrinsic::arm_neon_vst3lane:
9793 case Intrinsic::arm_neon_vst4lane: {
9794 Info.opc = ISD::INTRINSIC_VOID;
9795 // Conservatively set memVT to the entire set of vectors stored.
9796 unsigned NumElts = 0;
9797 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009798 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +00009799 if (!ArgTy->isVectorTy())
9800 break;
9801 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
9802 }
9803 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
9804 Info.ptrVal = I.getArgOperand(0);
9805 Info.offset = 0;
9806 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
9807 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
9808 Info.vol = false; // volatile stores with NEON intrinsics not supported
9809 Info.readMem = false;
9810 Info.writeMem = true;
9811 return true;
9812 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009813 case Intrinsic::arm_strexd: {
9814 Info.opc = ISD::INTRINSIC_W_CHAIN;
9815 Info.memVT = MVT::i64;
9816 Info.ptrVal = I.getArgOperand(2);
9817 Info.offset = 0;
9818 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009819 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009820 Info.readMem = false;
9821 Info.writeMem = true;
9822 return true;
9823 }
9824 case Intrinsic::arm_ldrexd: {
9825 Info.opc = ISD::INTRINSIC_W_CHAIN;
9826 Info.memVT = MVT::i64;
9827 Info.ptrVal = I.getArgOperand(0);
9828 Info.offset = 0;
9829 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009830 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009831 Info.readMem = true;
9832 Info.writeMem = false;
9833 return true;
9834 }
Bob Wilson65ffec42010-09-21 17:56:22 +00009835 default:
9836 break;
9837 }
9838
9839 return false;
9840}