blob: fcb4f4fd097bb20df8a11de1e18c7e9dfc246453 [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");
Dale Johannesen51e28e62010-06-03 21:09:53 +000055
Bob Wilson703af3a2010-08-13 22:43:33 +000056// This option should go away when tail calls fully work.
57static cl::opt<bool>
58EnableARMTailCalls("arm-tail-calls", cl::Hidden,
59 cl::desc("Generate tail calls (TEMPORARY OPTION)."),
60 cl::init(false));
61
Eric Christopher836c6242010-12-15 23:47:29 +000062cl::opt<bool>
Jim Grosbache7b52522010-04-14 22:28:31 +000063EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng515fe3a2010-07-08 02:08:50 +000064 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbache7b52522010-04-14 22:28:31 +000065 cl::init(false));
66
Evan Cheng46df4eb2010-06-16 07:35:02 +000067static cl::opt<bool>
68ARMInterworking("arm-interworking", cl::Hidden,
69 cl::desc("Enable / disable ARM interworking (for debugging only)"),
70 cl::init(true));
71
Benjamin Kramer0861f572011-11-26 23:01:57 +000072namespace {
Cameron Zwaricha86686e2011-06-10 20:59:24 +000073 class ARMCCState : public CCState {
74 public:
75 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
76 const TargetMachine &TM, SmallVector<CCValAssign, 16> &locs,
77 LLVMContext &C, ParmContext PC)
78 : CCState(CC, isVarArg, MF, TM, locs, C) {
79 assert(((PC == Call) || (PC == Prologue)) &&
80 "ARMCCState users must specify whether their context is call"
81 "or prologue generation.");
82 CallOrPrologue = PC;
83 }
84 };
85}
86
Stuart Hastingsc7315872011-04-20 16:47:52 +000087// The APCS parameter registers.
Craig Topperc5eaae42012-03-11 07:57:25 +000088static const uint16_t GPRArgRegs[] = {
Stuart Hastingsc7315872011-04-20 16:47:52 +000089 ARM::R0, ARM::R1, ARM::R2, ARM::R3
90};
91
Owen Andersone50ed302009-08-10 22:56:29 +000092void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
93 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000094 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000095 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000096 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
97 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000098
Owen Anderson70671842009-08-10 20:18:46 +000099 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000100 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000101 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000102 }
103
Owen Andersone50ed302009-08-10 22:56:29 +0000104 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000105 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Duncan Sands28b77e92011-09-06 19:07:46 +0000106 setOperationAction(ISD::SETCC, VT.getSimpleVT(), Custom);
Eli Friedman5c89cb82011-10-24 23:08:52 +0000107 setOperationAction(ISD::INSERT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson3468c2e2010-11-03 16:24:50 +0000108 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Eli Friedman14e809c2011-11-09 23:36:02 +0000109 if (ElemTy == MVT::i32) {
110 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Custom);
111 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Custom);
112 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Custom);
113 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Custom);
114 } else {
Bob Wilson0696fdf2009-09-16 20:20:44 +0000115 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
116 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
117 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
118 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
119 }
Owen Anderson70671842009-08-10 20:18:46 +0000120 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
121 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Bob Wilson07f6e802010-06-16 21:34:01 +0000122 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
Bob Wilson5e8b8332011-01-07 04:59:04 +0000123 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Legal);
Bob Wilsond0910c42010-04-06 22:02:24 +0000124 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
125 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Eli Friedman15f58c52011-11-11 03:16:38 +0000126 setOperationAction(ISD::SIGN_EXTEND_INREG, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000127 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000128 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
129 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
130 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +0000131 }
132
133 // Promote all bit-wise operations.
134 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000135 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000136 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
137 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000138 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000139 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000140 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000141 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000142 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000143 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000144 }
Bob Wilson16330762009-09-16 00:17:28 +0000145
146 // Neon does not support vector divide/remainder operations.
147 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
148 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
149 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
150 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
151 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
152 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000153}
154
Owen Andersone50ed302009-08-10 22:56:29 +0000155void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000156 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000157 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000158}
159
Owen Andersone50ed302009-08-10 22:56:29 +0000160void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000161 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000162 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000163}
164
Chris Lattnerf0144122009-07-28 03:13:23 +0000165static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
166 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000167 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000168
Chris Lattner80ec2792009-08-02 00:34:36 +0000169 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000170}
171
Evan Chenga8e29892007-01-19 07:51:42 +0000172ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000173 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000174 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng31446872010-07-23 22:39:59 +0000175 RegInfo = TM.getRegisterInfo();
Evan Cheng3ef1c872010-09-10 01:29:16 +0000176 Itins = TM.getInstrItineraryData();
Evan Chenga8e29892007-01-19 07:51:42 +0000177
Duncan Sands28b77e92011-09-06 19:07:46 +0000178 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
179
Evan Chengb1df8f22007-04-27 08:15:43 +0000180 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000181 // Uses VFP for Thumb libfuncs if available.
182 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
183 // Single-precision floating-point arithmetic.
184 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
185 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
186 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
187 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000188
Evan Chengb1df8f22007-04-27 08:15:43 +0000189 // Double-precision floating-point arithmetic.
190 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
191 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
192 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
193 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000194
Evan Chengb1df8f22007-04-27 08:15:43 +0000195 // Single-precision comparisons.
196 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
197 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
198 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
199 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
200 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
201 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
202 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
203 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000204
Evan Chengb1df8f22007-04-27 08:15:43 +0000205 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
206 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
207 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
208 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
209 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
210 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
211 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
212 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000213
Evan Chengb1df8f22007-04-27 08:15:43 +0000214 // Double-precision comparisons.
215 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
216 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
217 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
218 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
219 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
220 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
221 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
222 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000223
Evan Chengb1df8f22007-04-27 08:15:43 +0000224 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
225 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
226 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
227 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
228 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
229 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
230 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
231 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000232
Evan Chengb1df8f22007-04-27 08:15:43 +0000233 // Floating-point to integer conversions.
234 // i64 conversions are done via library routines even when generating VFP
235 // instructions, so use the same ones.
236 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
237 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
238 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
239 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000240
Evan Chengb1df8f22007-04-27 08:15:43 +0000241 // Conversions between floating types.
242 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
243 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
244
245 // Integer to floating-point conversions.
246 // i64 conversions are done via library routines even when generating VFP
247 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000248 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
249 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000250 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
251 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
252 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
253 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
254 }
Evan Chenga8e29892007-01-19 07:51:42 +0000255 }
256
Bob Wilson2f954612009-05-22 17:38:41 +0000257 // These libcalls are not available in 32-bit.
258 setLibcallName(RTLIB::SHL_I128, 0);
259 setLibcallName(RTLIB::SRL_I128, 0);
260 setLibcallName(RTLIB::SRA_I128, 0);
261
Evan Cheng07043272012-02-21 20:46:00 +0000262 if (Subtarget->isAAPCS_ABI() && !Subtarget->isTargetDarwin()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000263 // Double-precision floating-point arithmetic helper functions
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000264 // RTABI chapter 4.1.2, Table 2
265 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd");
266 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv");
267 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul");
268 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub");
269 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS);
270 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS);
271 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS);
272 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS);
273
274 // Double-precision floating-point comparison helper functions
275 // RTABI chapter 4.1.2, Table 3
276 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq");
277 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
278 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq");
279 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ);
280 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt");
281 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
282 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple");
283 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
284 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge");
285 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
286 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt");
287 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
288 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun");
289 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
290 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun");
291 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
292 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS);
293 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS);
294 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS);
295 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS);
296 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS);
297 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS);
298 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS);
299 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS);
300
301 // Single-precision floating-point arithmetic helper functions
302 // RTABI chapter 4.1.2, Table 4
303 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd");
304 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv");
305 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul");
306 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub");
307 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS);
308 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS);
309 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS);
310 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS);
311
312 // Single-precision floating-point comparison helper functions
313 // RTABI chapter 4.1.2, Table 5
314 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq");
315 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
316 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq");
317 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ);
318 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt");
319 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
320 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple");
321 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
322 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge");
323 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
324 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt");
325 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
326 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun");
327 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
328 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun");
329 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
330 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS);
331 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS);
332 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS);
333 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS);
334 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS);
335 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS);
336 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS);
337 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS);
338
339 // Floating-point to integer conversions.
340 // RTABI chapter 4.1.2, Table 6
341 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz");
342 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz");
343 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz");
344 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz");
345 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz");
346 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz");
347 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz");
348 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz");
349 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS);
350 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS);
351 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS);
352 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS);
353 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS);
354 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS);
355 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS);
356 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS);
357
358 // Conversions between floating types.
359 // RTABI chapter 4.1.2, Table 7
360 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f");
361 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d");
362 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000363 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000364
365 // Integer to floating-point conversions.
366 // RTABI chapter 4.1.2, Table 8
367 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d");
368 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d");
369 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d");
370 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d");
371 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f");
372 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f");
373 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f");
374 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f");
375 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
376 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
377 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
378 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
379 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
380 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
381 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
382 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
383
384 // Long long helper functions
385 // RTABI chapter 4.2, Table 9
386 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000387 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
388 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
389 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
390 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
391 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
392 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
393 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
394 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
395 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
396
397 // Integer division functions
398 // RTABI chapter 4.3.1
399 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
400 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
401 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000402 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000403 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
404 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
405 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000406 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000407 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
408 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
409 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000410 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000411 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS);
412 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000413 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000414 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
Renato Golin1ec11fb2011-05-22 21:41:23 +0000415
416 // Memory operations
417 // RTABI chapter 4.3.4
418 setLibcallName(RTLIB::MEMCPY, "__aeabi_memcpy");
419 setLibcallName(RTLIB::MEMMOVE, "__aeabi_memmove");
420 setLibcallName(RTLIB::MEMSET, "__aeabi_memset");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000421 setLibcallCallingConv(RTLIB::MEMCPY, CallingConv::ARM_AAPCS);
422 setLibcallCallingConv(RTLIB::MEMMOVE, CallingConv::ARM_AAPCS);
423 setLibcallCallingConv(RTLIB::MEMSET, CallingConv::ARM_AAPCS);
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000424 }
425
Bob Wilson2fef4572011-10-07 16:59:21 +0000426 // Use divmod compiler-rt calls for iOS 5.0 and later.
427 if (Subtarget->getTargetTriple().getOS() == Triple::IOS &&
428 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
429 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
430 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
431 }
432
David Goodwinf1daf7d2009-07-08 23:10:31 +0000433 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000434 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000435 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000436 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000437 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
438 !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000439 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000440 if (!Subtarget->isFPOnlySP())
441 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000442
Owen Anderson825b72b2009-08-11 20:47:22 +0000443 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000444 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000445
Eli Friedman9f1f26a2011-11-08 01:43:53 +0000446 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
447 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
448 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
449 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
450 setTruncStoreAction((MVT::SimpleValueType)VT,
451 (MVT::SimpleValueType)InnerVT, Expand);
452 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
453 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
454 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
455 }
456
Lang Hames45b5f882012-03-15 18:49:02 +0000457 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
458
Bob Wilson5bafff32009-06-22 23:27:02 +0000459 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000460 addDRTypeForNEON(MVT::v2f32);
461 addDRTypeForNEON(MVT::v8i8);
462 addDRTypeForNEON(MVT::v4i16);
463 addDRTypeForNEON(MVT::v2i32);
464 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000465
Owen Anderson825b72b2009-08-11 20:47:22 +0000466 addQRTypeForNEON(MVT::v4f32);
467 addQRTypeForNEON(MVT::v2f64);
468 addQRTypeForNEON(MVT::v16i8);
469 addQRTypeForNEON(MVT::v8i16);
470 addQRTypeForNEON(MVT::v4i32);
471 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000472
Bob Wilson74dc72e2009-09-15 23:55:57 +0000473 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
474 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000475 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
476 // supported for v4f32.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000477 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
478 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
479 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000480 // FIXME: Code duplication: FDIV and FREM are expanded always, see
481 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000482 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
483 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000484 // FIXME: Create unittest.
485 // In another words, find a way when "copysign" appears in DAG with vector
486 // operands.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000487 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000488 // FIXME: Code duplication: SETCC has custom operation action, see
489 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sands28b77e92011-09-06 19:07:46 +0000490 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000491 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000492 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
493 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
494 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
495 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
496 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
497 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
498 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
499 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
500 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
501 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
502 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
503 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000504 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000505 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
506 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
507 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
508 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
509 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Lang Hamesc0a9f822012-03-29 21:56:11 +0000510
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000511 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
512 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
513 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
514 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
515 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
516 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
517 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
518 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
519 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
520 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Bob Wilson74dc72e2009-09-15 23:55:57 +0000521
Bob Wilson642b3292009-09-16 00:32:15 +0000522 // Neon does not support some operations on v1i64 and v2i64 types.
523 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000524 // Custom handling for some quad-vector types to detect VMULL.
525 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
526 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
527 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begeman7973f352011-02-11 20:53:29 +0000528 // Custom handling for some vector types to avoid expensive expansions
529 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
530 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
531 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
532 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000533 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
534 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000535 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy873fd5f2012-02-20 09:24:05 +0000536 // a destination type that is wider than the source, and nor does
537 // it have a FP_TO_[SU]INT instruction with a narrower destination than
538 // source.
Cameron Zwarich3007d332011-03-29 21:41:55 +0000539 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
540 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy873fd5f2012-02-20 09:24:05 +0000541 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
542 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000543
Bob Wilson1c3ef902011-02-07 17:43:21 +0000544 setTargetDAGCombine(ISD::INTRINSIC_VOID);
545 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000546 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
547 setTargetDAGCombine(ISD::SHL);
548 setTargetDAGCombine(ISD::SRL);
549 setTargetDAGCombine(ISD::SRA);
550 setTargetDAGCombine(ISD::SIGN_EXTEND);
551 setTargetDAGCombine(ISD::ZERO_EXTEND);
552 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000553 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000554 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000555 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000556 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
557 setTargetDAGCombine(ISD::STORE);
Chad Rosieref01edf2011-06-24 19:23:04 +0000558 setTargetDAGCombine(ISD::FP_TO_SINT);
559 setTargetDAGCombine(ISD::FP_TO_UINT);
560 setTargetDAGCombine(ISD::FDIV);
Nadav Rotem004a24b2011-10-15 20:03:12 +0000561
James Molloy873fd5f2012-02-20 09:24:05 +0000562 // It is legal to extload from v4i8 to v4i16 or v4i32.
563 MVT Tys[6] = {MVT::v8i8, MVT::v4i8, MVT::v2i8,
564 MVT::v4i16, MVT::v2i16,
565 MVT::v2i32};
566 for (unsigned i = 0; i < 6; ++i) {
567 setLoadExtAction(ISD::EXTLOAD, Tys[i], Legal);
568 setLoadExtAction(ISD::ZEXTLOAD, Tys[i], Legal);
569 setLoadExtAction(ISD::SEXTLOAD, Tys[i], Legal);
570 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000571 }
572
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000573 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000574
575 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000576 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000577
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000578 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000579 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000580
Evan Chenga8e29892007-01-19 07:51:42 +0000581 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000582 if (!Subtarget->isThumb1Only()) {
583 for (unsigned im = (unsigned)ISD::PRE_INC;
584 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000585 setIndexedLoadAction(im, MVT::i1, Legal);
586 setIndexedLoadAction(im, MVT::i8, Legal);
587 setIndexedLoadAction(im, MVT::i16, Legal);
588 setIndexedLoadAction(im, MVT::i32, Legal);
589 setIndexedStoreAction(im, MVT::i1, Legal);
590 setIndexedStoreAction(im, MVT::i8, Legal);
591 setIndexedStoreAction(im, MVT::i16, Legal);
592 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000593 }
Evan Chenga8e29892007-01-19 07:51:42 +0000594 }
595
596 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000597 setOperationAction(ISD::MUL, MVT::i64, Expand);
598 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000599 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000600 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
601 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000602 }
Jim Grosbacha7603982011-07-01 21:12:19 +0000603 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
604 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopher2cc40132011-04-19 18:49:19 +0000605 setOperationAction(ISD::MULHS, MVT::i32, Expand);
606
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000607 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000608 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000609 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000610 setOperationAction(ISD::SRL, MVT::i64, Custom);
611 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000612
Evan Cheng342e3162011-08-30 01:34:54 +0000613 if (!Subtarget->isThumb1Only()) {
614 // FIXME: We should do this for Thumb1 as well.
615 setOperationAction(ISD::ADDC, MVT::i32, Custom);
616 setOperationAction(ISD::ADDE, MVT::i32, Custom);
617 setOperationAction(ISD::SUBC, MVT::i32, Custom);
618 setOperationAction(ISD::SUBE, MVT::i32, Custom);
619 }
620
Evan Chenga8e29892007-01-19 07:51:42 +0000621 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000622 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000623 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000624 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000625 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000626 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000627
Chandler Carruth63974b22011-12-13 01:56:10 +0000628 // These just redirect to CTTZ and CTLZ on ARM.
629 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
630 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
631
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000632 // Only ARMv6 has BSWAP.
633 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000634 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000635
Evan Chenga8e29892007-01-19 07:51:42 +0000636 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000637 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000638 // v7M has a hardware divider
639 setOperationAction(ISD::SDIV, MVT::i32, Expand);
640 setOperationAction(ISD::UDIV, MVT::i32, Expand);
641 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000642 setOperationAction(ISD::SREM, MVT::i32, Expand);
643 setOperationAction(ISD::UREM, MVT::i32, Expand);
644 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
645 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000646
Owen Anderson825b72b2009-08-11 20:47:22 +0000647 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
648 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
649 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
650 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000651 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000652
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000653 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000654
Evan Chenga8e29892007-01-19 07:51:42 +0000655 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000656 setOperationAction(ISD::VASTART, MVT::Other, Custom);
657 setOperationAction(ISD::VAARG, MVT::Other, Expand);
658 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
659 setOperationAction(ISD::VAEND, MVT::Other, Expand);
660 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
661 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendlingbdf9db62012-02-13 23:47:16 +0000662
663 if (!Subtarget->isTargetDarwin()) {
664 // Non-Darwin platforms may return values in these registers via the
665 // personality function.
666 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
667 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
668 setExceptionPointerRegister(ARM::R0);
669 setExceptionSelectorRegister(ARM::R1);
670 }
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000671
Evan Cheng3a1588a2010-04-15 22:20:34 +0000672 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000673 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
674 // the default expansion.
Eli Friedman4db5aca2011-08-29 18:23:02 +0000675 // FIXME: This should be checking for v6k, not just v6.
Evan Cheng11db0682010-08-11 06:22:01 +0000676 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000677 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000678 // membarrier needs custom lowering; the rest are legal and handled
679 // normally.
680 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000681 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Eli Friedman2bdffe42011-08-31 00:31:29 +0000682 // Custom lowering for 64-bit ops
683 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
684 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
685 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
686 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
687 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
688 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Eli Friedman4d3f3292011-08-31 17:52:22 +0000689 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Eli Friedman26689ac2011-08-03 21:06:02 +0000690 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
691 setInsertFencesForAtomic(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000692 } else {
693 // Set them all for expansion, which will force libcalls.
694 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000695 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000696 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000697 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000698 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000699 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000700 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000701 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000702 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000703 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000704 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000705 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000706 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000707 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedman7cc15662011-09-15 22:18:49 +0000708 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
709 // Unordered/Monotonic case.
710 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
711 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000712 // Since the libcalls include locking, fold in the fences
713 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000714 }
Evan Chenga8e29892007-01-19 07:51:42 +0000715
Evan Cheng416941d2010-11-04 05:19:35 +0000716 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000717
Eli Friedmana2c6f452010-06-26 04:36:50 +0000718 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
719 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000720 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
721 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000722 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000723 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000724
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000725 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
726 !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000727 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
728 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000729 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000730 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
731 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000732
733 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000734 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000735 if (Subtarget->isTargetDarwin()) {
736 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
737 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000738 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000739 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000740
Owen Anderson825b72b2009-08-11 20:47:22 +0000741 setOperationAction(ISD::SETCC, MVT::i32, Expand);
742 setOperationAction(ISD::SETCC, MVT::f32, Expand);
743 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000744 setOperationAction(ISD::SELECT, MVT::i32, Custom);
745 setOperationAction(ISD::SELECT, MVT::f32, Custom);
746 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000747 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
748 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
749 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000750
Owen Anderson825b72b2009-08-11 20:47:22 +0000751 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
752 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
753 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
754 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
755 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000756
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000757 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000758 setOperationAction(ISD::FSIN, MVT::f64, Expand);
759 setOperationAction(ISD::FSIN, MVT::f32, Expand);
760 setOperationAction(ISD::FCOS, MVT::f32, Expand);
761 setOperationAction(ISD::FCOS, MVT::f64, Expand);
762 setOperationAction(ISD::FREM, MVT::f64, Expand);
763 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000764 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
765 !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000766 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
767 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000768 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000769 setOperationAction(ISD::FPOW, MVT::f64, Expand);
770 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000771
Cameron Zwarich33390842011-07-08 21:39:21 +0000772 setOperationAction(ISD::FMA, MVT::f64, Expand);
773 setOperationAction(ISD::FMA, MVT::f32, Expand);
774
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000775 // Various VFP goodness
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000776 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000777 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
778 if (Subtarget->hasVFP2()) {
779 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
780 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
781 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
782 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
783 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000784 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000785 if (!Subtarget->hasFP16()) {
786 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
787 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000788 }
Evan Cheng110cf482008-04-01 01:50:16 +0000789 }
Evan Chenga8e29892007-01-19 07:51:42 +0000790
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000791 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000792 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000793 setTargetDAGCombine(ISD::ADD);
794 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000795 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000796
Evan Chengc892aeb2012-02-23 01:19:06 +0000797 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON()) {
Owen Anderson080c0922010-11-05 19:27:46 +0000798 setTargetDAGCombine(ISD::AND);
Evan Chengc892aeb2012-02-23 01:19:06 +0000799 setTargetDAGCombine(ISD::OR);
800 setTargetDAGCombine(ISD::XOR);
801 }
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000802
Evan Cheng5fb468a2012-02-23 02:58:19 +0000803 if (Subtarget->hasV6Ops())
804 setTargetDAGCombine(ISD::SRL);
805
Evan Chenga8e29892007-01-19 07:51:42 +0000806 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000807
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000808 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
809 !Subtarget->hasVFP2())
Evan Chengf7d87ee2010-05-21 00:43:17 +0000810 setSchedulingPreference(Sched::RegPressure);
811 else
812 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000813
Evan Cheng05219282011-01-06 06:52:41 +0000814 //// temporary - rewrite interface to use type
815 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Lang Hames75757f92011-10-26 20:56:52 +0000816 maxStoresPerMemset = 16;
817 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengf6799392010-06-26 01:52:05 +0000818
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000819 // On ARM arguments smaller than 4 bytes are extended, so all arguments
820 // are at least 4 bytes aligned.
821 setMinStackArgumentAlignment(4);
822
Evan Chengfff606d2010-09-24 19:07:23 +0000823 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000824
825 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000826}
827
Andrew Trick32cec0a2011-01-19 02:35:27 +0000828// FIXME: It might make sense to define the representative register class as the
829// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
830// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
831// SPR's representative would be DPR_VFP2. This should work well if register
832// pressure tracking were modified such that a register use would increment the
833// pressure of the register class's representative and all of it's super
834// classes' representatives transitively. We have not implemented this because
835// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000836// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000837// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000838std::pair<const TargetRegisterClass*, uint8_t>
839ARMTargetLowering::findRepresentativeClass(EVT VT) const{
840 const TargetRegisterClass *RRC = 0;
841 uint8_t Cost = 1;
842 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000843 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000844 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000845 // Use DPR as representative register class for all floating point
846 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
847 // the cost is 1 for both f32 and f64.
848 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000849 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000850 RRC = ARM::DPRRegisterClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000851 // When NEON is used for SP, only half of the register file is available
852 // because operations that define both SP and DP results will be constrained
853 // to the VFP2 class (D0-D15). We currently model this constraint prior to
854 // coalescing by double-counting the SP regs. See the FIXME above.
855 if (Subtarget->useNEONForSinglePrecisionFP())
856 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000857 break;
858 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
859 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000860 RRC = ARM::DPRRegisterClass;
861 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000862 break;
863 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000864 RRC = ARM::DPRRegisterClass;
865 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000866 break;
867 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000868 RRC = ARM::DPRRegisterClass;
869 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000870 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000871 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000872 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000873}
874
Evan Chenga8e29892007-01-19 07:51:42 +0000875const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
876 switch (Opcode) {
877 default: return 0;
878 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000879 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000880 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000881 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
882 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000883 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000884 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
885 case ARMISD::tCALL: return "ARMISD::tCALL";
886 case ARMISD::BRCOND: return "ARMISD::BRCOND";
887 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000888 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000889 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
890 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
891 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000892 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000893 case ARMISD::CMPFP: return "ARMISD::CMPFP";
894 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000895 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000896 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chengc892aeb2012-02-23 01:19:06 +0000897
Evan Chenga8e29892007-01-19 07:51:42 +0000898 case ARMISD::CMOV: return "ARMISD::CMOV";
Evan Chengc892aeb2012-02-23 01:19:06 +0000899 case ARMISD::CAND: return "ARMISD::CAND";
900 case ARMISD::COR: return "ARMISD::COR";
901 case ARMISD::CXOR: return "ARMISD::CXOR";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000902
Jim Grosbach3482c802010-01-18 19:58:49 +0000903 case ARMISD::RBIT: return "ARMISD::RBIT";
904
Bob Wilson76a312b2010-03-19 22:51:32 +0000905 case ARMISD::FTOSI: return "ARMISD::FTOSI";
906 case ARMISD::FTOUI: return "ARMISD::FTOUI";
907 case ARMISD::SITOF: return "ARMISD::SITOF";
908 case ARMISD::UITOF: return "ARMISD::UITOF";
909
Evan Chenga8e29892007-01-19 07:51:42 +0000910 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
911 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
912 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000913
Evan Cheng342e3162011-08-30 01:34:54 +0000914 case ARMISD::ADDC: return "ARMISD::ADDC";
915 case ARMISD::ADDE: return "ARMISD::ADDE";
916 case ARMISD::SUBC: return "ARMISD::SUBC";
917 case ARMISD::SUBE: return "ARMISD::SUBE";
918
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000919 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
920 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000921
Evan Chengc5942082009-10-28 06:55:03 +0000922 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
923 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
924
Dale Johannesen51e28e62010-06-03 21:09:53 +0000925 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000926
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000927 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000928
Evan Cheng86198642009-08-07 00:34:42 +0000929 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
930
Jim Grosbach3728e962009-12-10 00:11:09 +0000931 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000932 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000933
Evan Chengdfed19f2010-11-03 06:34:55 +0000934 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
935
Bob Wilson5bafff32009-06-22 23:27:02 +0000936 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000937 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000938 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000939 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
940 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000941 case ARMISD::VCGEU: return "ARMISD::VCGEU";
942 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000943 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
944 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000945 case ARMISD::VCGTU: return "ARMISD::VCGTU";
946 case ARMISD::VTST: return "ARMISD::VTST";
947
948 case ARMISD::VSHL: return "ARMISD::VSHL";
949 case ARMISD::VSHRs: return "ARMISD::VSHRs";
950 case ARMISD::VSHRu: return "ARMISD::VSHRu";
951 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
952 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
953 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
954 case ARMISD::VSHRN: return "ARMISD::VSHRN";
955 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
956 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
957 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
958 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
959 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
960 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
961 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
962 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
963 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
964 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
965 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
966 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
967 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
968 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000969 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000970 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Chengeaa192a2011-11-15 02:12:34 +0000971 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000972 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000973 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000974 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000975 case ARMISD::VREV64: return "ARMISD::VREV64";
976 case ARMISD::VREV32: return "ARMISD::VREV32";
977 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000978 case ARMISD::VZIP: return "ARMISD::VZIP";
979 case ARMISD::VUZP: return "ARMISD::VUZP";
980 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000981 case ARMISD::VTBL1: return "ARMISD::VTBL1";
982 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000983 case ARMISD::VMULLs: return "ARMISD::VMULLs";
984 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000985 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000986 case ARMISD::FMAX: return "ARMISD::FMAX";
987 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000988 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000989 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
990 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000991 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000992 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
993 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
994 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +0000995 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
996 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
997 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
998 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
999 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1000 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1001 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1002 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1003 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1004 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1005 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1006 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1007 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1008 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1009 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1010 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1011 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +00001012 }
1013}
1014
Duncan Sands28b77e92011-09-06 19:07:46 +00001015EVT ARMTargetLowering::getSetCCResultType(EVT VT) const {
1016 if (!VT.isVector()) return getPointerTy();
1017 return VT.changeVectorElementTypeToInteger();
1018}
1019
Evan Cheng06b666c2010-05-15 02:18:07 +00001020/// getRegClassFor - Return the register class that should be used for the
1021/// specified value type.
Craig Topper44d23822012-02-22 05:59:10 +00001022const TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
Evan Cheng06b666c2010-05-15 02:18:07 +00001023 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1024 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1025 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +00001026 if (Subtarget->hasNEON()) {
1027 if (VT == MVT::v4i64)
1028 return ARM::QQPRRegisterClass;
1029 else if (VT == MVT::v8i64)
1030 return ARM::QQQQPRRegisterClass;
1031 }
Evan Cheng06b666c2010-05-15 02:18:07 +00001032 return TargetLowering::getRegClassFor(VT);
1033}
1034
Eric Christopherab695882010-07-21 22:26:11 +00001035// Create a fast isel object.
1036FastISel *
1037ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
1038 return ARM::createFastISel(funcInfo);
1039}
1040
Anton Korobeynikovcec36f42010-07-24 21:52:08 +00001041/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1042/// be used for loads / stores from the global.
1043unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1044 return (Subtarget->isThumb1Only() ? 127 : 4095);
1045}
1046
Evan Cheng1cc39842010-05-20 23:26:43 +00001047Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +00001048 unsigned NumVals = N->getNumValues();
1049 if (!NumVals)
1050 return Sched::RegPressure;
1051
1052 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +00001053 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001054 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +00001055 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +00001056 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman692c1d82011-10-24 17:55:11 +00001057 return Sched::ILP;
Evan Cheng1cc39842010-05-20 23:26:43 +00001058 }
Evan Chengc10f5432010-05-28 23:25:23 +00001059
1060 if (!N->isMachineOpcode())
1061 return Sched::RegPressure;
1062
1063 // Load are scheduled for latency even if there instruction itinerary
1064 // is not available.
1065 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +00001066 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +00001067
Evan Chenge837dea2011-06-28 19:10:37 +00001068 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +00001069 return Sched::RegPressure;
1070 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +00001071 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman692c1d82011-10-24 17:55:11 +00001072 return Sched::ILP;
Evan Chengc10f5432010-05-28 23:25:23 +00001073
Evan Cheng1cc39842010-05-20 23:26:43 +00001074 return Sched::RegPressure;
1075}
1076
Evan Chenga8e29892007-01-19 07:51:42 +00001077//===----------------------------------------------------------------------===//
1078// Lowering Code
1079//===----------------------------------------------------------------------===//
1080
Evan Chenga8e29892007-01-19 07:51:42 +00001081/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1082static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1083 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001084 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +00001085 case ISD::SETNE: return ARMCC::NE;
1086 case ISD::SETEQ: return ARMCC::EQ;
1087 case ISD::SETGT: return ARMCC::GT;
1088 case ISD::SETGE: return ARMCC::GE;
1089 case ISD::SETLT: return ARMCC::LT;
1090 case ISD::SETLE: return ARMCC::LE;
1091 case ISD::SETUGT: return ARMCC::HI;
1092 case ISD::SETUGE: return ARMCC::HS;
1093 case ISD::SETULT: return ARMCC::LO;
1094 case ISD::SETULE: return ARMCC::LS;
1095 }
1096}
1097
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001098/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1099static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001100 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001101 CondCode2 = ARMCC::AL;
1102 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001103 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001104 case ISD::SETEQ:
1105 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1106 case ISD::SETGT:
1107 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1108 case ISD::SETGE:
1109 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1110 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001111 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001112 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1113 case ISD::SETO: CondCode = ARMCC::VC; break;
1114 case ISD::SETUO: CondCode = ARMCC::VS; break;
1115 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1116 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1117 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1118 case ISD::SETLT:
1119 case ISD::SETULT: CondCode = ARMCC::LT; break;
1120 case ISD::SETLE:
1121 case ISD::SETULE: CondCode = ARMCC::LE; break;
1122 case ISD::SETNE:
1123 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1124 }
Evan Chenga8e29892007-01-19 07:51:42 +00001125}
1126
Bob Wilson1f595bb2009-04-17 19:07:39 +00001127//===----------------------------------------------------------------------===//
1128// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001129//===----------------------------------------------------------------------===//
1130
1131#include "ARMGenCallingConv.inc"
1132
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001133/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1134/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001135CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001136 bool Return,
1137 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001138 switch (CC) {
1139 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001140 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001141 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001142 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001143 if (!Subtarget->isAAPCS_ABI())
1144 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1145 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1146 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1147 }
1148 // Fallthrough
1149 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001150 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001151 if (!Subtarget->isAAPCS_ABI())
1152 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1153 else if (Subtarget->hasVFP2() &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001154 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1155 !isVarArg)
Evan Cheng76f920d2010-10-22 18:23:05 +00001156 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1157 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1158 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001159 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikovf349cb82012-01-29 09:06:09 +00001160 if (!isVarArg)
1161 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1162 // Fallthrough
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001163 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001164 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001165 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001166 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001167 }
1168}
1169
Dan Gohman98ca4f22009-08-05 01:29:28 +00001170/// LowerCallResult - Lower the result values of a call into the
1171/// appropriate copies out of appropriate physical registers.
1172SDValue
1173ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001174 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001175 const SmallVectorImpl<ISD::InputArg> &Ins,
1176 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001177 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001178
Bob Wilson1f595bb2009-04-17 19:07:39 +00001179 // Assign locations to each value returned by this call.
1180 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001181 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1182 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001183 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001184 CCAssignFnForNode(CallConv, /* Return*/ true,
1185 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001186
1187 // Copy all of the result registers out of their specified physreg.
1188 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1189 CCValAssign VA = RVLocs[i];
1190
Bob Wilson80915242009-04-25 00:33:20 +00001191 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001192 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001193 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001194 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001195 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001196 Chain = Lo.getValue(1);
1197 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001198 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001199 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001200 InFlag);
1201 Chain = Hi.getValue(1);
1202 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001203 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001204
Owen Anderson825b72b2009-08-11 20:47:22 +00001205 if (VA.getLocVT() == MVT::v2f64) {
1206 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1207 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1208 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001209
1210 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001211 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001212 Chain = Lo.getValue(1);
1213 InFlag = Lo.getValue(2);
1214 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001215 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001216 Chain = Hi.getValue(1);
1217 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001218 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001219 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1220 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001221 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001222 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001223 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1224 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001225 Chain = Val.getValue(1);
1226 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001227 }
Bob Wilson80915242009-04-25 00:33:20 +00001228
1229 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001230 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001231 case CCValAssign::Full: break;
1232 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001233 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001234 break;
1235 }
1236
Dan Gohman98ca4f22009-08-05 01:29:28 +00001237 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001238 }
1239
Dan Gohman98ca4f22009-08-05 01:29:28 +00001240 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001241}
1242
Bob Wilsondee46d72009-04-17 20:35:10 +00001243/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001244SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001245ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1246 SDValue StackPtr, SDValue Arg,
1247 DebugLoc dl, SelectionDAG &DAG,
1248 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001249 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001250 unsigned LocMemOffset = VA.getLocMemOffset();
1251 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1252 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001253 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001254 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001255 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001256}
1257
Dan Gohman98ca4f22009-08-05 01:29:28 +00001258void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001259 SDValue Chain, SDValue &Arg,
1260 RegsToPassVector &RegsToPass,
1261 CCValAssign &VA, CCValAssign &NextVA,
1262 SDValue &StackPtr,
1263 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001264 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001265
Jim Grosbache5165492009-11-09 00:11:35 +00001266 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001267 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001268 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1269
1270 if (NextVA.isRegLoc())
1271 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1272 else {
1273 assert(NextVA.isMemLoc());
1274 if (StackPtr.getNode() == 0)
1275 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1276
Dan Gohman98ca4f22009-08-05 01:29:28 +00001277 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1278 dl, DAG, NextVA,
1279 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001280 }
1281}
1282
Dan Gohman98ca4f22009-08-05 01:29:28 +00001283/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001284/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1285/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001286SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001287ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001288 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001289 bool doesNotRet, bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001290 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001291 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001292 const SmallVectorImpl<ISD::InputArg> &Ins,
1293 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001294 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001295 MachineFunction &MF = DAG.getMachineFunction();
1296 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1297 bool IsSibCall = false;
Bob Wilson6d2f9ce2011-10-07 17:17:49 +00001298 // Disable tail calls if they're not supported.
1299 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Bob Wilson703af3a2010-08-13 22:43:33 +00001300 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001301 if (isTailCall) {
1302 // Check if it's really possible to do a tail call.
1303 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1304 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001305 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001306 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1307 // detected sibcalls.
1308 if (isTailCall) {
1309 ++NumTailCalls;
1310 IsSibCall = true;
1311 }
1312 }
Evan Chenga8e29892007-01-19 07:51:42 +00001313
Bob Wilson1f595bb2009-04-17 19:07:39 +00001314 // Analyze operands of the call, assigning locations to each operand.
1315 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001316 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1317 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001318 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001319 CCAssignFnForNode(CallConv, /* Return*/ false,
1320 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001321
Bob Wilson1f595bb2009-04-17 19:07:39 +00001322 // Get a count of how many bytes are to be pushed on the stack.
1323 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001324
Dale Johannesen51e28e62010-06-03 21:09:53 +00001325 // For tail calls, memory operands are available in our caller's stack.
1326 if (IsSibCall)
1327 NumBytes = 0;
1328
Evan Chenga8e29892007-01-19 07:51:42 +00001329 // Adjust the stack pointer for the new arguments...
1330 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001331 if (!IsSibCall)
1332 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001333
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001334 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001335
Bob Wilson5bafff32009-06-22 23:27:02 +00001336 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001337 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001338
Bob Wilson1f595bb2009-04-17 19:07:39 +00001339 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001340 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001341 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1342 i != e;
1343 ++i, ++realArgIdx) {
1344 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001345 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001346 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001347 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001348
Bob Wilson1f595bb2009-04-17 19:07:39 +00001349 // Promote the value if needed.
1350 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001351 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001352 case CCValAssign::Full: break;
1353 case CCValAssign::SExt:
1354 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1355 break;
1356 case CCValAssign::ZExt:
1357 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1358 break;
1359 case CCValAssign::AExt:
1360 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1361 break;
1362 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001363 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001364 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001365 }
1366
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001367 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001368 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001369 if (VA.getLocVT() == MVT::v2f64) {
1370 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1371 DAG.getConstant(0, MVT::i32));
1372 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1373 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001374
Dan Gohman98ca4f22009-08-05 01:29:28 +00001375 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001376 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1377
1378 VA = ArgLocs[++i]; // skip ahead to next loc
1379 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001380 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001381 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1382 } else {
1383 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001384
Dan Gohman98ca4f22009-08-05 01:29:28 +00001385 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1386 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001387 }
1388 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001389 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001390 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001391 }
1392 } else if (VA.isRegLoc()) {
1393 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001394 } else if (isByVal) {
1395 assert(VA.isMemLoc());
1396 unsigned offset = 0;
1397
1398 // True if this byval aggregate will be split between registers
1399 // and memory.
1400 if (CCInfo.isFirstByValRegValid()) {
1401 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1402 unsigned int i, j;
1403 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1404 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1405 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1406 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1407 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001408 false, false, false, 0);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001409 MemOpChains.push_back(Load.getValue(1));
1410 RegsToPass.push_back(std::make_pair(j, Load));
1411 }
1412 offset = ARM::R4 - CCInfo.getFirstByValReg();
1413 CCInfo.clearFirstByValReg();
1414 }
1415
1416 unsigned LocMemOffset = VA.getLocMemOffset();
1417 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1418 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1419 StkPtrOff);
1420 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1421 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1422 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1423 MVT::i32);
1424 MemOpChains.push_back(DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode,
1425 Flags.getByValAlign(),
1426 /*isVolatile=*/false,
Dan Gohman65fd6562011-11-03 21:49:52 +00001427 /*AlwaysInline=*/false,
Stuart Hastingsc7315872011-04-20 16:47:52 +00001428 MachinePointerInfo(0),
1429 MachinePointerInfo(0)));
1430
1431 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001432 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001433
Dan Gohman98ca4f22009-08-05 01:29:28 +00001434 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1435 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001436 }
Evan Chenga8e29892007-01-19 07:51:42 +00001437 }
1438
1439 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001440 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001441 &MemOpChains[0], MemOpChains.size());
1442
1443 // Build a sequence of copy-to-reg nodes chained together with token chain
1444 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001445 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001446 // Tail call byval lowering might overwrite argument registers so in case of
1447 // tail call optimization the copies to registers are lowered later.
1448 if (!isTailCall)
1449 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1450 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1451 RegsToPass[i].second, InFlag);
1452 InFlag = Chain.getValue(1);
1453 }
Evan Chenga8e29892007-01-19 07:51:42 +00001454
Dale Johannesen51e28e62010-06-03 21:09:53 +00001455 // For tail calls lower the arguments to the 'real' stack slot.
1456 if (isTailCall) {
1457 // Force all the incoming stack arguments to be loaded from the stack
1458 // before any new outgoing arguments are stored to the stack, because the
1459 // outgoing stack slots may alias the incoming argument stack slots, and
1460 // the alias isn't otherwise explicit. This is slightly more conservative
1461 // than necessary, because it means that each store effectively depends
1462 // on every argument instead of just those arguments it would clobber.
1463
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001464 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001465 InFlag = SDValue();
1466 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1467 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1468 RegsToPass[i].second, InFlag);
1469 InFlag = Chain.getValue(1);
1470 }
1471 InFlag =SDValue();
1472 }
1473
Bill Wendling056292f2008-09-16 21:48:12 +00001474 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1475 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1476 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001477 bool isDirect = false;
1478 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001479 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001480 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001481
1482 if (EnableARMLongCalls) {
1483 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1484 && "long-calls with non-static relocation model!");
1485 // Handle a global address or an external symbol. If it's not one of
1486 // those, the target's already in a register, so we don't need to do
1487 // anything extra.
1488 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001489 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001490 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001491 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001492 ARMConstantPoolValue *CPV =
1493 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1494
Jim Grosbache7b52522010-04-14 22:28:31 +00001495 // Get the address of the callee into a register
1496 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1497 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1498 Callee = DAG.getLoad(getPointerTy(), dl,
1499 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001500 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001501 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001502 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1503 const char *Sym = S->getSymbol();
1504
1505 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001506 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001507 ARMConstantPoolValue *CPV =
1508 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1509 ARMPCLabelIndex, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001510 // Get the address of the callee into a register
1511 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1512 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1513 Callee = DAG.getLoad(getPointerTy(), dl,
1514 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001515 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001516 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001517 }
1518 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001519 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001520 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001521 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001522 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001523 getTargetMachine().getRelocationModel() != Reloc::Static;
1524 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001525 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001526 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001527 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001528 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001529 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001530 ARMConstantPoolValue *CPV =
1531 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001532 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001533 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001534 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001535 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001536 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001537 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001538 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001539 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001540 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001541 } else {
1542 // On ELF targets for PIC code, direct calls should go through the PLT
1543 unsigned OpFlags = 0;
1544 if (Subtarget->isTargetELF() &&
1545 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1546 OpFlags = ARMII::MO_PLT;
1547 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1548 }
Bill Wendling056292f2008-09-16 21:48:12 +00001549 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001550 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001551 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001552 getTargetMachine().getRelocationModel() != Reloc::Static;
1553 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001554 // tBX takes a register source operand.
1555 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001556 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001557 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001558 ARMConstantPoolValue *CPV =
1559 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1560 ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001561 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001562 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001563 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001564 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001565 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001566 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001567 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001568 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001569 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001570 } else {
1571 unsigned OpFlags = 0;
1572 // On ELF targets for PIC code, direct calls should go through the PLT
1573 if (Subtarget->isTargetELF() &&
1574 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1575 OpFlags = ARMII::MO_PLT;
1576 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1577 }
Evan Chenga8e29892007-01-19 07:51:42 +00001578 }
1579
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001580 // FIXME: handle tail calls differently.
1581 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001582 if (Subtarget->isThumb()) {
1583 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001584 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001585 else if (doesNotRet && isDirect && !isARMFunc &&
1586 Subtarget->hasRAS() && !Subtarget->isThumb1Only())
1587 // "mov lr, pc; b _foo" to avoid confusing the RSP
1588 CallOpc = ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001589 else
1590 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1591 } else {
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001592 if (!isDirect && !Subtarget->hasV5TOps()) {
1593 CallOpc = ARMISD::CALL_NOLINK;
1594 } else if (doesNotRet && isDirect && Subtarget->hasRAS())
1595 // "mov lr, pc; b _foo" to avoid confusing the RSP
1596 CallOpc = ARMISD::CALL_NOLINK;
1597 else
1598 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001599 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001600
Dan Gohman475871a2008-07-27 21:46:04 +00001601 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001602 Ops.push_back(Chain);
1603 Ops.push_back(Callee);
1604
1605 // Add argument registers to the end of the list so that they are known live
1606 // into the call.
1607 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1608 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1609 RegsToPass[i].second.getValueType()));
1610
Jakob Stoklund Olesenc54f6342012-02-24 01:19:29 +00001611 // Add a register mask operand representing the call-preserved registers.
1612 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
1613 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
1614 assert(Mask && "Missing call preserved mask for calling convention");
1615 Ops.push_back(DAG.getRegisterMask(Mask));
1616
Gabor Greifba36cb52008-08-28 21:40:38 +00001617 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001618 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001619
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001620 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001621 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001622 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001623
Duncan Sands4bdcb612008-07-02 17:40:58 +00001624 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001625 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001626 InFlag = Chain.getValue(1);
1627
Chris Lattnere563bbc2008-10-11 22:08:30 +00001628 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1629 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001630 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001631 InFlag = Chain.getValue(1);
1632
Bob Wilson1f595bb2009-04-17 19:07:39 +00001633 // Handle result values, copying them out of physregs into vregs that we
1634 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001635 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1636 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001637}
1638
Stuart Hastingsf222e592011-02-28 17:17:53 +00001639/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001640/// on the stack. Remember the next parameter register to allocate,
1641/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001642/// this.
1643void
Craig Topperc89c7442012-03-27 07:21:54 +00001644ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
Stuart Hastingsc7315872011-04-20 16:47:52 +00001645 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1646 assert((State->getCallOrPrologue() == Prologue ||
1647 State->getCallOrPrologue() == Call) &&
1648 "unhandled ParmContext");
1649 if ((!State->isFirstByValRegValid()) &&
1650 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1651 State->setFirstByValReg(reg);
1652 // At a call site, a byval parameter that is split between
1653 // registers and memory needs its size truncated here. In a
1654 // function prologue, such byval parameters are reassembled in
1655 // memory, and are not truncated.
1656 if (State->getCallOrPrologue() == Call) {
1657 unsigned excess = 4 * (ARM::R4 - reg);
1658 assert(size >= excess && "expected larger existing stack allocation");
1659 size -= excess;
1660 }
1661 }
1662 // Confiscate any remaining parameter registers to preclude their
1663 // assignment to subsequent parameters.
1664 while (State->AllocateReg(GPRArgRegs, 4))
1665 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001666}
1667
Dale Johannesen51e28e62010-06-03 21:09:53 +00001668/// MatchingStackOffset - Return true if the given stack call argument is
1669/// already available in the same position (relatively) of the caller's
1670/// incoming argument stack.
1671static
1672bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1673 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topperacf20772012-03-25 23:49:58 +00001674 const TargetInstrInfo *TII) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001675 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1676 int FI = INT_MAX;
1677 if (Arg.getOpcode() == ISD::CopyFromReg) {
1678 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001679 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001680 return false;
1681 MachineInstr *Def = MRI->getVRegDef(VR);
1682 if (!Def)
1683 return false;
1684 if (!Flags.isByVal()) {
1685 if (!TII->isLoadFromStackSlot(Def, FI))
1686 return false;
1687 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001688 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001689 }
1690 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1691 if (Flags.isByVal())
1692 // ByVal argument is passed in as a pointer but it's now being
1693 // dereferenced. e.g.
1694 // define @foo(%struct.X* %A) {
1695 // tail call @bar(%struct.X* byval %A)
1696 // }
1697 return false;
1698 SDValue Ptr = Ld->getBasePtr();
1699 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1700 if (!FINode)
1701 return false;
1702 FI = FINode->getIndex();
1703 } else
1704 return false;
1705
1706 assert(FI != INT_MAX);
1707 if (!MFI->isFixedObjectIndex(FI))
1708 return false;
1709 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1710}
1711
1712/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1713/// for tail call optimization. Targets which want to do tail call
1714/// optimization should implement this function.
1715bool
1716ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1717 CallingConv::ID CalleeCC,
1718 bool isVarArg,
1719 bool isCalleeStructRet,
1720 bool isCallerStructRet,
1721 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001722 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001723 const SmallVectorImpl<ISD::InputArg> &Ins,
1724 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001725 const Function *CallerF = DAG.getMachineFunction().getFunction();
1726 CallingConv::ID CallerCC = CallerF->getCallingConv();
1727 bool CCMatch = CallerCC == CalleeCC;
1728
1729 // Look for obvious safe cases to perform tail call optimization that do not
1730 // require ABI changes. This is what gcc calls sibcall.
1731
Jim Grosbach7616b642010-06-16 23:45:49 +00001732 // Do not sibcall optimize vararg calls unless the call site is not passing
1733 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001734 if (isVarArg && !Outs.empty())
1735 return false;
1736
1737 // Also avoid sibcall optimization if either caller or callee uses struct
1738 // return semantics.
1739 if (isCalleeStructRet || isCallerStructRet)
1740 return false;
1741
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001742 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001743 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1744 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1745 // support in the assembler and linker to be used. This would need to be
1746 // fixed to fully support tail calls in Thumb1.
1747 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001748 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1749 // LR. This means if we need to reload LR, it takes an extra instructions,
1750 // which outweighs the value of the tail call; but here we don't know yet
1751 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001752 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001753 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001754
1755 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1756 // but we need to make sure there are enough registers; the only valid
1757 // registers are the 4 used for parameters. We don't currently do this
1758 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001759 if (Subtarget->isThumb1Only())
1760 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001761
Dale Johannesen51e28e62010-06-03 21:09:53 +00001762 // If the calling conventions do not match, then we'd better make sure the
1763 // results are returned in the same way as what the caller expects.
1764 if (!CCMatch) {
1765 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001766 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1767 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001768 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1769
1770 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001771 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1772 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001773 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1774
1775 if (RVLocs1.size() != RVLocs2.size())
1776 return false;
1777 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1778 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1779 return false;
1780 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1781 return false;
1782 if (RVLocs1[i].isRegLoc()) {
1783 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1784 return false;
1785 } else {
1786 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1787 return false;
1788 }
1789 }
1790 }
1791
1792 // If the callee takes no arguments then go on to check the results of the
1793 // call.
1794 if (!Outs.empty()) {
1795 // Check if stack adjustment is needed. For now, do not do this if any
1796 // argument is passed on the stack.
1797 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001798 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1799 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001800 CCInfo.AnalyzeCallOperands(Outs,
1801 CCAssignFnForNode(CalleeCC, false, isVarArg));
1802 if (CCInfo.getNextStackOffset()) {
1803 MachineFunction &MF = DAG.getMachineFunction();
1804
1805 // Check if the arguments are already laid out in the right way as
1806 // the caller's fixed stack objects.
1807 MachineFrameInfo *MFI = MF.getFrameInfo();
1808 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Craig Topperacf20772012-03-25 23:49:58 +00001809 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001810 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1811 i != e;
1812 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001813 CCValAssign &VA = ArgLocs[i];
1814 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001815 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001816 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001817 if (VA.getLocInfo() == CCValAssign::Indirect)
1818 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001819 if (VA.needsCustom()) {
1820 // f64 and vector types are split into multiple registers or
1821 // register/stack-slot combinations. The types will not match
1822 // the registers; give up on memory f64 refs until we figure
1823 // out what to do about this.
1824 if (!VA.isRegLoc())
1825 return false;
1826 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001827 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001828 if (RegVT == MVT::v2f64) {
1829 if (!ArgLocs[++i].isRegLoc())
1830 return false;
1831 if (!ArgLocs[++i].isRegLoc())
1832 return false;
1833 }
1834 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001835 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1836 MFI, MRI, TII))
1837 return false;
1838 }
1839 }
1840 }
1841 }
1842
1843 return true;
1844}
1845
Dan Gohman98ca4f22009-08-05 01:29:28 +00001846SDValue
1847ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001848 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001849 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001850 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001851 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001852
Bob Wilsondee46d72009-04-17 20:35:10 +00001853 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001854 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001855
Bob Wilsondee46d72009-04-17 20:35:10 +00001856 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001857 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1858 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001859
Dan Gohman98ca4f22009-08-05 01:29:28 +00001860 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001861 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1862 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001863
1864 // If this is the first return lowered for this function, add
1865 // the regs to the liveout set for the function.
1866 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1867 for (unsigned i = 0; i != RVLocs.size(); ++i)
1868 if (RVLocs[i].isRegLoc())
1869 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001870 }
1871
Bob Wilson1f595bb2009-04-17 19:07:39 +00001872 SDValue Flag;
1873
1874 // Copy the result values into the output registers.
1875 for (unsigned i = 0, realRVLocIdx = 0;
1876 i != RVLocs.size();
1877 ++i, ++realRVLocIdx) {
1878 CCValAssign &VA = RVLocs[i];
1879 assert(VA.isRegLoc() && "Can only return in registers!");
1880
Dan Gohmanc9403652010-07-07 15:54:55 +00001881 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001882
1883 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001884 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001885 case CCValAssign::Full: break;
1886 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001887 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001888 break;
1889 }
1890
Bob Wilson1f595bb2009-04-17 19:07:39 +00001891 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001892 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001893 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001894 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1895 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001896 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001897 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001898
1899 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1900 Flag = Chain.getValue(1);
1901 VA = RVLocs[++i]; // skip ahead to next loc
1902 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1903 HalfGPRs.getValue(1), Flag);
1904 Flag = Chain.getValue(1);
1905 VA = RVLocs[++i]; // skip ahead to next loc
1906
1907 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001908 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1909 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001910 }
1911 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1912 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001913 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001914 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001915 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001916 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001917 VA = RVLocs[++i]; // skip ahead to next loc
1918 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1919 Flag);
1920 } else
1921 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1922
Bob Wilsondee46d72009-04-17 20:35:10 +00001923 // Guarantee that all emitted copies are
1924 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001925 Flag = Chain.getValue(1);
1926 }
1927
1928 SDValue result;
1929 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001930 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001931 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001932 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001933
1934 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001935}
1936
Evan Chengbf010eb2012-04-10 01:51:00 +00001937bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001938 if (N->getNumValues() != 1)
1939 return false;
1940 if (!N->hasNUsesOfValue(1, 0))
1941 return false;
1942
Evan Chengbf010eb2012-04-10 01:51:00 +00001943 SDValue TCChain = Chain;
1944 SDNode *Copy = *N->use_begin();
1945 if (Copy->getOpcode() == ISD::CopyToReg) {
1946 // If the copy has a glue operand, we conservatively assume it isn't safe to
1947 // perform a tail call.
1948 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1949 return false;
1950 TCChain = Copy->getOperand(0);
1951 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
1952 SDNode *VMov = Copy;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001953 // f64 returned in a pair of GPRs.
Evan Chengbf010eb2012-04-10 01:51:00 +00001954 SmallPtrSet<SDNode*, 2> Copies;
1955 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Cheng3d2125c2010-11-30 23:55:39 +00001956 UI != UE; ++UI) {
1957 if (UI->getOpcode() != ISD::CopyToReg)
1958 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001959 Copies.insert(*UI);
Evan Cheng3d2125c2010-11-30 23:55:39 +00001960 }
Evan Chengbf010eb2012-04-10 01:51:00 +00001961 if (Copies.size() > 2)
1962 return false;
1963
1964 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
1965 UI != UE; ++UI) {
1966 SDValue UseChain = UI->getOperand(0);
1967 if (Copies.count(UseChain.getNode()))
1968 // Second CopyToReg
1969 Copy = *UI;
1970 else
1971 // First CopyToReg
1972 TCChain = UseChain;
1973 }
1974 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001975 // f32 returned in a single GPR.
Evan Chengbf010eb2012-04-10 01:51:00 +00001976 if (!Copy->hasOneUse())
Evan Cheng3d2125c2010-11-30 23:55:39 +00001977 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001978 Copy = *Copy->use_begin();
1979 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Cheng3d2125c2010-11-30 23:55:39 +00001980 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001981 Chain = Copy->getOperand(0);
Evan Cheng3d2125c2010-11-30 23:55:39 +00001982 } else {
1983 return false;
1984 }
1985
Evan Cheng1bf891a2010-12-01 22:59:46 +00001986 bool HasRet = false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001987 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1988 UI != UE; ++UI) {
1989 if (UI->getOpcode() != ARMISD::RET_FLAG)
1990 return false;
1991 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001992 }
1993
Evan Chengbf010eb2012-04-10 01:51:00 +00001994 if (!HasRet)
1995 return false;
1996
1997 Chain = TCChain;
1998 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001999}
2000
Evan Cheng485fafc2011-03-21 01:19:09 +00002001bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Evan Cheng1c80f562012-03-30 01:24:39 +00002002 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Evan Cheng485fafc2011-03-21 01:19:09 +00002003 return false;
2004
2005 if (!CI->isTailCall())
2006 return false;
2007
2008 return !Subtarget->isThumb1Only();
2009}
2010
Bob Wilsonb62d2572009-11-03 00:02:05 +00002011// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2012// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2013// one of the above mentioned nodes. It has to be wrapped because otherwise
2014// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2015// be used to form addressing mode. These wrapped nodes will be selected
2016// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00002017static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002018 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002019 // FIXME there is no actual debug info here
2020 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002021 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00002022 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00002023 if (CP->isMachineConstantPoolEntry())
2024 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2025 CP->getAlignment());
2026 else
2027 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2028 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00002029 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00002030}
2031
Jim Grosbache1102ca2010-07-19 17:20:38 +00002032unsigned ARMTargetLowering::getJumpTableEncoding() const {
2033 return MachineJumpTableInfo::EK_Inline;
2034}
2035
Dan Gohmand858e902010-04-17 15:26:15 +00002036SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2037 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00002038 MachineFunction &MF = DAG.getMachineFunction();
2039 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2040 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00002041 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00002042 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00002043 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00002044 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2045 SDValue CPAddr;
2046 if (RelocM == Reloc::Static) {
2047 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2048 } else {
2049 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002050 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00002051 ARMConstantPoolValue *CPV =
2052 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2053 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson907eebd2009-11-02 20:59:23 +00002054 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2055 }
2056 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2057 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002058 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002059 false, false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00002060 if (RelocM == Reloc::Static)
2061 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00002062 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00002063 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00002064}
2065
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002066// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00002067SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002068ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002069 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002070 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002071 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002072 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00002073 MachineFunction &MF = DAG.getMachineFunction();
2074 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002075 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002076 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002077 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2078 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002079 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002080 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00002081 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002082 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002083 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002084 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002085
Evan Chenge7e0d622009-11-06 22:24:13 +00002086 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002087 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002088
2089 // call __tls_get_addr.
2090 ArgListTy Args;
2091 ArgListEntry Entry;
2092 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002093 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002094 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002095 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00002096 std::pair<SDValue, SDValue> CallResult =
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002097 LowerCallTo(Chain, (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00002098 false, false, false, false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00002099 0, CallingConv::C, /*isTailCall=*/false,
2100 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002101 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002102 return CallResult.first;
2103}
2104
2105// Lower ISD::GlobalTLSAddress using the "initial exec" or
2106// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00002107SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002108ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002109 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002110 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002111 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002112 SDValue Offset;
2113 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002114 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002115 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002116 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002117
Chris Lattner4fb63d02009-07-15 04:12:33 +00002118 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002119 MachineFunction &MF = DAG.getMachineFunction();
2120 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002121 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002122 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002123 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2124 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002125 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2126 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2127 true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002128 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002129 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002130 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002131 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002132 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002133 Chain = Offset.getValue(1);
2134
Evan Chenge7e0d622009-11-06 22:24:13 +00002135 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002136 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002137
Evan Cheng9eda6892009-10-31 03:39:36 +00002138 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002139 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002140 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002141 } else {
2142 // local exec model
Bill Wendling5bb77992011-10-01 08:00:54 +00002143 ARMConstantPoolValue *CPV =
2144 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002145 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002146 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002147 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002148 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002149 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002150 }
2151
2152 // The address of the thread local variable is the add of the thread
2153 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002154 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002155}
2156
Dan Gohman475871a2008-07-27 21:46:04 +00002157SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002158ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002159 // TODO: implement the "local dynamic" model
2160 assert(Subtarget->isTargetELF() &&
2161 "TLS not implemented for non-ELF targets");
2162 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2163 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
2164 // otherwise use the "Local Exec" TLS Model
2165 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
2166 return LowerToTLSGeneralDynamicModel(GA, DAG);
2167 else
2168 return LowerToTLSExecModels(GA, DAG);
2169}
2170
Dan Gohman475871a2008-07-27 21:46:04 +00002171SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002172 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002173 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002174 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002175 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002176 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2177 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002178 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002179 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002180 ARMConstantPoolConstant::Create(GV,
2181 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002182 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002183 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002184 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002185 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002186 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002187 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002188 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002189 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002190 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002191 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002192 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002193 MachinePointerInfo::getGOT(),
2194 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002195 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002196 }
2197
2198 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloy015cca62011-10-26 08:53:19 +00002199 // pair. This is always cheaper.
2200 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002201 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002202 // FIXME: Once remat is capable of dealing with instructions with register
2203 // operands, expand this into two nodes.
2204 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2205 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002206 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002207 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2208 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2209 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2210 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002211 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002212 }
2213}
2214
Dan Gohman475871a2008-07-27 21:46:04 +00002215SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002216 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002217 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002218 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002219 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002220 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002221 MachineFunction &MF = DAG.getMachineFunction();
2222 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2223
Jakob Stoklund Olesen8f37a242012-01-07 20:49:15 +00002224 // FIXME: Enable this for static codegen when tool issues are fixed. Also
2225 // update ARMFastISel::ARMMaterializeGV.
Evan Chengf31151f2011-10-26 01:17:44 +00002226 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002227 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002228 // FIXME: Once remat is capable of dealing with instructions with register
2229 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002230 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002231 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2232 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2233
Evan Cheng53519f02011-01-21 18:55:51 +00002234 unsigned Wrapper = (RelocM == Reloc::PIC_)
2235 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2236 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002237 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002238 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2239 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002240 MachinePointerInfo::getGOT(),
2241 false, false, false, 0);
Evan Chengfc8475b2011-01-19 02:16:49 +00002242 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002243 }
2244
2245 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002246 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002247 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002248 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002249 } else {
2250 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002251 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2252 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002253 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue,
2254 PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002255 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002256 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002257 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002258
Evan Cheng9eda6892009-10-31 03:39:36 +00002259 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002260 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002261 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002262 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002263
2264 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002265 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002266 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002267 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002268
Evan Cheng63476a82009-09-03 07:04:02 +00002269 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002270 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002271 false, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002272
2273 return Result;
2274}
2275
Dan Gohman475871a2008-07-27 21:46:04 +00002276SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002277 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002278 assert(Subtarget->isTargetELF() &&
2279 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002280 MachineFunction &MF = DAG.getMachineFunction();
2281 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002282 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002283 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002284 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002285 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingfe31e672011-10-01 08:58:29 +00002286 ARMConstantPoolValue *CPV =
2287 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2288 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002289 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002290 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002291 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002292 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002293 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002294 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002295 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002296}
2297
Jim Grosbach0e0da732009-05-12 23:59:14 +00002298SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002299ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2300 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002301 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendlingce370cf2011-10-07 21:25:38 +00002302 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2303 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002304 Op.getOperand(1), Val);
2305}
2306
2307SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002308ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2309 DebugLoc dl = Op.getDebugLoc();
2310 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2311 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2312}
2313
2314SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002315ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002316 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002317 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002318 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002319 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002320 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002321 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002322 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002323 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2324 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002325 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002326 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002327 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002328 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002329 EVT PtrVT = getPointerTy();
2330 DebugLoc dl = Op.getDebugLoc();
2331 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2332 SDValue CPAddr;
2333 unsigned PCAdj = (RelocM != Reloc::PIC_)
2334 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002335 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002336 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2337 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002338 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002339 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002340 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002341 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002342 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002343 false, false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002344
2345 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002346 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002347 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2348 }
2349 return Result;
2350 }
Evan Cheng92e39162011-03-29 23:06:19 +00002351 case Intrinsic::arm_neon_vmulls:
2352 case Intrinsic::arm_neon_vmullu: {
2353 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2354 ? ARMISD::VMULLs : ARMISD::VMULLu;
2355 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2356 Op.getOperand(1), Op.getOperand(2));
2357 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002358 }
2359}
2360
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002361static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002362 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002363 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002364 if (!Subtarget->hasDataBarrier()) {
2365 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2366 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2367 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002368 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002369 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002370 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002371 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002372 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002373
2374 SDValue Op5 = Op.getOperand(5);
2375 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2376 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2377 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2378 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2379
2380 ARM_MB::MemBOpt DMBOpt;
2381 if (isDeviceBarrier)
2382 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2383 else
2384 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2385 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2386 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002387}
2388
Eli Friedman26689ac2011-08-03 21:06:02 +00002389
2390static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2391 const ARMSubtarget *Subtarget) {
2392 // FIXME: handle "fence singlethread" more efficiently.
2393 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002394 if (!Subtarget->hasDataBarrier()) {
2395 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2396 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2397 // here.
2398 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2399 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002400 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002401 DAG.getConstant(0, MVT::i32));
2402 }
2403
Eli Friedman26689ac2011-08-03 21:06:02 +00002404 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002405 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002406}
2407
Evan Chengdfed19f2010-11-03 06:34:55 +00002408static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2409 const ARMSubtarget *Subtarget) {
2410 // ARM pre v5TE and Thumb1 does not have preload instructions.
2411 if (!(Subtarget->isThumb2() ||
2412 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2413 // Just preserve the chain.
2414 return Op.getOperand(0);
2415
2416 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002417 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2418 if (!isRead &&
2419 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2420 // ARMv7 with MP extension has PLDW.
2421 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002422
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002423 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2424 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002425 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002426 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002427 isData = ~isData & 1;
2428 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002429
2430 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002431 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2432 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002433}
2434
Dan Gohman1e93df62010-04-17 14:41:14 +00002435static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2436 MachineFunction &MF = DAG.getMachineFunction();
2437 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2438
Evan Chenga8e29892007-01-19 07:51:42 +00002439 // vastart just stores the address of the VarArgsFrameIndex slot into the
2440 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002441 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002442 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002443 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002444 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002445 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2446 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002447}
2448
Dan Gohman475871a2008-07-27 21:46:04 +00002449SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002450ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2451 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002452 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002453 MachineFunction &MF = DAG.getMachineFunction();
2454 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2455
Craig Topper44d23822012-02-22 05:59:10 +00002456 const TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002457 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002458 RC = ARM::tGPRRegisterClass;
2459 else
2460 RC = ARM::GPRRegisterClass;
2461
2462 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002463 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002464 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002465
2466 SDValue ArgValue2;
2467 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002468 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002469 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002470
2471 // Create load node to retrieve arguments from the stack.
2472 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002473 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002474 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002475 false, false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002476 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002477 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002478 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002479 }
2480
Jim Grosbache5165492009-11-09 00:11:35 +00002481 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002482}
2483
Stuart Hastingsc7315872011-04-20 16:47:52 +00002484void
2485ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2486 unsigned &VARegSize, unsigned &VARegSaveSize)
2487 const {
2488 unsigned NumGPRs;
2489 if (CCInfo.isFirstByValRegValid())
2490 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2491 else {
2492 unsigned int firstUnalloced;
2493 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2494 sizeof(GPRArgRegs) /
2495 sizeof(GPRArgRegs[0]));
2496 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2497 }
2498
2499 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2500 VARegSize = NumGPRs * 4;
2501 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2502}
2503
2504// The remaining GPRs hold either the beginning of variable-argument
2505// data, or the beginning of an aggregate passed by value (usuall
2506// byval). Either way, we allocate stack slots adjacent to the data
2507// provided by our caller, and store the unallocated registers there.
2508// If this is a variadic function, the va_list pointer will begin with
2509// these values; otherwise, this reassembles a (byval) structure that
2510// was split between registers and memory.
2511void
2512ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2513 DebugLoc dl, SDValue &Chain,
2514 unsigned ArgOffset) const {
2515 MachineFunction &MF = DAG.getMachineFunction();
2516 MachineFrameInfo *MFI = MF.getFrameInfo();
2517 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2518 unsigned firstRegToSaveIndex;
2519 if (CCInfo.isFirstByValRegValid())
2520 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2521 else {
2522 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2523 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2524 }
2525
2526 unsigned VARegSize, VARegSaveSize;
2527 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2528 if (VARegSaveSize) {
2529 // If this function is vararg, store any remaining integer argument regs
2530 // to their spots on the stack so that they may be loaded by deferencing
2531 // the result of va_next.
2532 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002533 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2534 ArgOffset + VARegSaveSize
2535 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002536 false));
2537 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2538 getPointerTy());
2539
2540 SmallVector<SDValue, 4> MemOps;
2541 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
Craig Topper44d23822012-02-22 05:59:10 +00002542 const TargetRegisterClass *RC;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002543 if (AFI->isThumb1OnlyFunction())
2544 RC = ARM::tGPRRegisterClass;
2545 else
2546 RC = ARM::GPRRegisterClass;
2547
2548 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2549 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2550 SDValue Store =
2551 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002552 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002553 false, false, 0);
2554 MemOps.push_back(Store);
2555 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2556 DAG.getConstant(4, getPointerTy()));
2557 }
2558 if (!MemOps.empty())
2559 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2560 &MemOps[0], MemOps.size());
2561 } else
2562 // This will point to the next argument passed via stack.
2563 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2564}
2565
Bob Wilson5bafff32009-06-22 23:27:02 +00002566SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002567ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002568 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002569 const SmallVectorImpl<ISD::InputArg>
2570 &Ins,
2571 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002572 SmallVectorImpl<SDValue> &InVals)
2573 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002574 MachineFunction &MF = DAG.getMachineFunction();
2575 MachineFrameInfo *MFI = MF.getFrameInfo();
2576
Bob Wilson1f595bb2009-04-17 19:07:39 +00002577 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2578
2579 // Assign locations to all of the incoming arguments.
2580 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002581 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2582 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002583 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002584 CCAssignFnForNode(CallConv, /* Return*/ false,
2585 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002586
2587 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002588 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002589
Stuart Hastingsf222e592011-02-28 17:17:53 +00002590 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002591 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2592 CCValAssign &VA = ArgLocs[i];
2593
Bob Wilsondee46d72009-04-17 20:35:10 +00002594 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002595 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002596 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002597
Bob Wilson1f595bb2009-04-17 19:07:39 +00002598 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002599 // f64 and vector types are split up into multiple registers or
2600 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002601 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002602 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002603 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002604 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002605 SDValue ArgValue2;
2606 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002607 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002608 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2609 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002610 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002611 false, false, false, 0);
Bob Wilson6a234f02010-04-13 22:03:22 +00002612 } else {
2613 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2614 Chain, DAG, dl);
2615 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002616 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2617 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002618 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002619 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002620 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2621 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002622 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002623
Bob Wilson5bafff32009-06-22 23:27:02 +00002624 } else {
Craig Topper44d23822012-02-22 05:59:10 +00002625 const TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002626
Owen Anderson825b72b2009-08-11 20:47:22 +00002627 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002628 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002629 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002630 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002631 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002632 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002633 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002634 RC = (AFI->isThumb1OnlyFunction() ?
2635 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002636 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002637 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002638
2639 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002640 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002641 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002642 }
2643
2644 // If this is an 8 or 16-bit value, it is really passed promoted
2645 // to 32 bits. Insert an assert[sz]ext to capture this, then
2646 // truncate to the right size.
2647 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002648 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002649 case CCValAssign::Full: break;
2650 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002651 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002652 break;
2653 case CCValAssign::SExt:
2654 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2655 DAG.getValueType(VA.getValVT()));
2656 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2657 break;
2658 case CCValAssign::ZExt:
2659 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2660 DAG.getValueType(VA.getValVT()));
2661 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2662 break;
2663 }
2664
Dan Gohman98ca4f22009-08-05 01:29:28 +00002665 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002666
2667 } else { // VA.isRegLoc()
2668
2669 // sanity check
2670 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002671 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002672
Stuart Hastingsf222e592011-02-28 17:17:53 +00002673 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002674
Stuart Hastingsf222e592011-02-28 17:17:53 +00002675 // Some Ins[] entries become multiple ArgLoc[] entries.
2676 // Process them only once.
2677 if (index != lastInsIndex)
2678 {
2679 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002680 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002681 // This can be changed with more analysis.
2682 // In case of tail call optimization mark all arguments mutable.
2683 // Since they could be overwritten by lowering of arguments in case of
2684 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002685 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002686 unsigned VARegSize, VARegSaveSize;
2687 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2688 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2689 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002690 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002691 int FI = MFI->CreateFixedObject(Bytes,
2692 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002693 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2694 } else {
2695 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2696 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002697
Stuart Hastingsf222e592011-02-28 17:17:53 +00002698 // Create load nodes to retrieve arguments from the stack.
2699 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2700 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2701 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002702 false, false, false, 0));
Stuart Hastingsf222e592011-02-28 17:17:53 +00002703 }
2704 lastInsIndex = index;
2705 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002706 }
2707 }
2708
2709 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002710 if (isVarArg)
2711 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002712
Dan Gohman98ca4f22009-08-05 01:29:28 +00002713 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002714}
2715
2716/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002717static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002718 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002719 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002720 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002721 // Maybe this has already been legalized into the constant pool?
2722 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002723 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002724 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002725 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002726 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002727 }
2728 }
2729 return false;
2730}
2731
Evan Chenga8e29892007-01-19 07:51:42 +00002732/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2733/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002734SDValue
2735ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002736 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002737 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002738 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002739 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002740 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002741 // Constant does not fit, try adjusting it by one?
2742 switch (CC) {
2743 default: break;
2744 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002745 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002746 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002747 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002748 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002749 }
2750 break;
2751 case ISD::SETULT:
2752 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002753 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002754 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002755 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002756 }
2757 break;
2758 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002759 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002760 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002761 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002762 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002763 }
2764 break;
2765 case ISD::SETULE:
2766 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002767 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002768 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002769 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002770 }
2771 break;
2772 }
2773 }
2774 }
2775
2776 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002777 ARMISD::NodeType CompareType;
2778 switch (CondCode) {
2779 default:
2780 CompareType = ARMISD::CMP;
2781 break;
2782 case ARMCC::EQ:
2783 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002784 // Uses only Z Flag
2785 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002786 break;
2787 }
Evan Cheng218977b2010-07-13 19:27:42 +00002788 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002789 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002790}
2791
2792/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002793SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002794ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002795 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002796 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002797 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002798 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002799 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002800 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2801 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002802}
2803
Bob Wilson79f56c92011-03-08 01:17:20 +00002804/// duplicateCmp - Glue values can have only one use, so this function
2805/// duplicates a comparison node.
2806SDValue
2807ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2808 unsigned Opc = Cmp.getOpcode();
2809 DebugLoc DL = Cmp.getDebugLoc();
2810 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2811 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2812
2813 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2814 Cmp = Cmp.getOperand(0);
2815 Opc = Cmp.getOpcode();
2816 if (Opc == ARMISD::CMPFP)
2817 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2818 else {
2819 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2820 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2821 }
2822 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2823}
2824
Bill Wendlingde2b1512010-08-11 08:43:16 +00002825SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2826 SDValue Cond = Op.getOperand(0);
2827 SDValue SelectTrue = Op.getOperand(1);
2828 SDValue SelectFalse = Op.getOperand(2);
2829 DebugLoc dl = Op.getDebugLoc();
2830
2831 // Convert:
2832 //
2833 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2834 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2835 //
2836 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2837 const ConstantSDNode *CMOVTrue =
2838 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2839 const ConstantSDNode *CMOVFalse =
2840 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2841
2842 if (CMOVTrue && CMOVFalse) {
2843 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2844 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2845
2846 SDValue True;
2847 SDValue False;
2848 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2849 True = SelectTrue;
2850 False = SelectFalse;
2851 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2852 True = SelectFalse;
2853 False = SelectTrue;
2854 }
2855
2856 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002857 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002858 SDValue ARMcc = Cond.getOperand(2);
2859 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002860 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002861 assert(True.getValueType() == VT);
2862 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002863 }
2864 }
2865 }
2866
Dan Gohmandb953892012-02-24 00:09:36 +00002867 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
2868 // undefined bits before doing a full-word comparison with zero.
2869 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
2870 DAG.getConstant(1, Cond.getValueType()));
2871
Bill Wendlingde2b1512010-08-11 08:43:16 +00002872 return DAG.getSelectCC(dl, Cond,
2873 DAG.getConstant(0, Cond.getValueType()),
2874 SelectTrue, SelectFalse, ISD::SETNE);
2875}
2876
Dan Gohmand858e902010-04-17 15:26:15 +00002877SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002878 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002879 SDValue LHS = Op.getOperand(0);
2880 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002881 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002882 SDValue TrueVal = Op.getOperand(2);
2883 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002884 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002885
Owen Anderson825b72b2009-08-11 20:47:22 +00002886 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002887 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002888 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002889 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Jim Grosbachb04546f2011-09-13 20:30:37 +00002890 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002891 }
2892
2893 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002894 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002895
Evan Cheng218977b2010-07-13 19:27:42 +00002896 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2897 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002898 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002899 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002900 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002901 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002902 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002903 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002904 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002905 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002906 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002907 }
2908 return Result;
2909}
2910
Evan Cheng218977b2010-07-13 19:27:42 +00002911/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2912/// to morph to an integer compare sequence.
2913static bool canChangeToInt(SDValue Op, bool &SeenZero,
2914 const ARMSubtarget *Subtarget) {
2915 SDNode *N = Op.getNode();
2916 if (!N->hasOneUse())
2917 // Otherwise it requires moving the value from fp to integer registers.
2918 return false;
2919 if (!N->getNumValues())
2920 return false;
2921 EVT VT = Op.getValueType();
2922 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2923 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2924 // vmrs are very slow, e.g. cortex-a8.
2925 return false;
2926
2927 if (isFloatingPointZero(Op)) {
2928 SeenZero = true;
2929 return true;
2930 }
2931 return ISD::isNormalLoad(N);
2932}
2933
2934static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2935 if (isFloatingPointZero(Op))
2936 return DAG.getConstant(0, MVT::i32);
2937
2938 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2939 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002940 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002941 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002942 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002943
2944 llvm_unreachable("Unknown VFP cmp argument!");
2945}
2946
2947static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2948 SDValue &RetVal1, SDValue &RetVal2) {
2949 if (isFloatingPointZero(Op)) {
2950 RetVal1 = DAG.getConstant(0, MVT::i32);
2951 RetVal2 = DAG.getConstant(0, MVT::i32);
2952 return;
2953 }
2954
2955 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2956 SDValue Ptr = Ld->getBasePtr();
2957 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2958 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002959 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002960 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002961 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002962
2963 EVT PtrType = Ptr.getValueType();
2964 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2965 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2966 PtrType, Ptr, DAG.getConstant(4, PtrType));
2967 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2968 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002969 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002970 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002971 Ld->isInvariant(), NewAlign);
Evan Cheng218977b2010-07-13 19:27:42 +00002972 return;
2973 }
2974
2975 llvm_unreachable("Unknown VFP cmp argument!");
2976}
2977
2978/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2979/// f32 and even f64 comparisons to integer ones.
2980SDValue
2981ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2982 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002983 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002984 SDValue LHS = Op.getOperand(2);
2985 SDValue RHS = Op.getOperand(3);
2986 SDValue Dest = Op.getOperand(4);
2987 DebugLoc dl = Op.getDebugLoc();
2988
Evan Chengfc501a32012-03-01 23:27:13 +00002989 bool LHSSeenZero = false;
2990 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
2991 bool RHSSeenZero = false;
2992 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
2993 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson1b772f92011-03-08 01:17:16 +00002994 // If unsafe fp math optimization is enabled and there are no other uses of
2995 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00002996 // to an integer comparison.
2997 if (CC == ISD::SETOEQ)
2998 CC = ISD::SETEQ;
2999 else if (CC == ISD::SETUNE)
3000 CC = ISD::SETNE;
3001
Evan Chengfc501a32012-03-01 23:27:13 +00003002 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00003003 SDValue ARMcc;
3004 if (LHS.getValueType() == MVT::f32) {
Evan Chengfc501a32012-03-01 23:27:13 +00003005 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3006 bitcastf32Toi32(LHS, DAG), Mask);
3007 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3008 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003009 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3010 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3011 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3012 Chain, Dest, ARMcc, CCR, Cmp);
3013 }
3014
3015 SDValue LHS1, LHS2;
3016 SDValue RHS1, RHS2;
3017 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3018 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengfc501a32012-03-01 23:27:13 +00003019 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3020 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003021 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3022 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003023 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003024 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
3025 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
3026 }
3027
3028 return SDValue();
3029}
3030
3031SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3032 SDValue Chain = Op.getOperand(0);
3033 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3034 SDValue LHS = Op.getOperand(2);
3035 SDValue RHS = Op.getOperand(3);
3036 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00003037 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003038
Owen Anderson825b72b2009-08-11 20:47:22 +00003039 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00003040 SDValue ARMcc;
3041 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003042 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00003043 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00003044 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00003045 }
3046
Owen Anderson825b72b2009-08-11 20:47:22 +00003047 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00003048
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003049 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng218977b2010-07-13 19:27:42 +00003050 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3051 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3052 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3053 if (Result.getNode())
3054 return Result;
3055 }
3056
Evan Chenga8e29892007-01-19 07:51:42 +00003057 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00003058 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003059
Evan Cheng218977b2010-07-13 19:27:42 +00003060 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3061 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003062 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003063 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003064 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00003065 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003066 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00003067 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3068 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00003069 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003070 }
3071 return Res;
3072}
3073
Dan Gohmand858e902010-04-17 15:26:15 +00003074SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00003075 SDValue Chain = Op.getOperand(0);
3076 SDValue Table = Op.getOperand(1);
3077 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003078 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003079
Owen Andersone50ed302009-08-10 22:56:29 +00003080 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00003081 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3082 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00003083 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00003084 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00003085 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00003086 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3087 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00003088 if (Subtarget->isThumb2()) {
3089 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3090 // which does another jump to the destination. This also makes it easier
3091 // to translate it to TBB / TBH later.
3092 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00003093 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00003094 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003095 }
Evan Cheng66ac5312009-07-25 00:33:29 +00003096 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00003097 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003098 MachinePointerInfo::getJumpTable(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003099 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003100 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003101 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00003102 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003103 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00003104 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003105 MachinePointerInfo::getJumpTable(),
3106 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003107 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003108 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003109 }
Evan Chenga8e29892007-01-19 07:51:42 +00003110}
3111
Eli Friedman14e809c2011-11-09 23:36:02 +00003112static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy873fd5f2012-02-20 09:24:05 +00003113 EVT VT = Op.getValueType();
3114 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14e809c2011-11-09 23:36:02 +00003115
James Molloy873fd5f2012-02-20 09:24:05 +00003116 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3117 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3118 return Op;
3119 return DAG.UnrollVectorOp(Op.getNode());
3120 }
3121
3122 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3123 "Invalid type for custom lowering!");
3124 if (VT != MVT::v4i16)
3125 return DAG.UnrollVectorOp(Op.getNode());
3126
3127 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3128 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman14e809c2011-11-09 23:36:02 +00003129}
3130
Bob Wilson76a312b2010-03-19 22:51:32 +00003131static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman14e809c2011-11-09 23:36:02 +00003132 EVT VT = Op.getValueType();
3133 if (VT.isVector())
3134 return LowerVectorFP_TO_INT(Op, DAG);
3135
Bob Wilson76a312b2010-03-19 22:51:32 +00003136 DebugLoc dl = Op.getDebugLoc();
3137 unsigned Opc;
3138
3139 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003140 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003141 case ISD::FP_TO_SINT:
3142 Opc = ARMISD::FTOSI;
3143 break;
3144 case ISD::FP_TO_UINT:
3145 Opc = ARMISD::FTOUI;
3146 break;
3147 }
3148 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003149 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003150}
3151
Cameron Zwarich3007d332011-03-29 21:41:55 +00003152static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3153 EVT VT = Op.getValueType();
3154 DebugLoc dl = Op.getDebugLoc();
3155
Eli Friedman14e809c2011-11-09 23:36:02 +00003156 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3157 if (VT.getVectorElementType() == MVT::f32)
3158 return Op;
3159 return DAG.UnrollVectorOp(Op.getNode());
3160 }
3161
Duncan Sands1f6a3292011-08-12 14:54:45 +00003162 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3163 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003164 if (VT != MVT::v4f32)
3165 return DAG.UnrollVectorOp(Op.getNode());
3166
3167 unsigned CastOpc;
3168 unsigned Opc;
3169 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003170 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003171 case ISD::SINT_TO_FP:
3172 CastOpc = ISD::SIGN_EXTEND;
3173 Opc = ISD::SINT_TO_FP;
3174 break;
3175 case ISD::UINT_TO_FP:
3176 CastOpc = ISD::ZERO_EXTEND;
3177 Opc = ISD::UINT_TO_FP;
3178 break;
3179 }
3180
3181 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3182 return DAG.getNode(Opc, dl, VT, Op);
3183}
3184
Bob Wilson76a312b2010-03-19 22:51:32 +00003185static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3186 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003187 if (VT.isVector())
3188 return LowerVectorINT_TO_FP(Op, DAG);
3189
Bob Wilson76a312b2010-03-19 22:51:32 +00003190 DebugLoc dl = Op.getDebugLoc();
3191 unsigned Opc;
3192
3193 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003194 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003195 case ISD::SINT_TO_FP:
3196 Opc = ARMISD::SITOF;
3197 break;
3198 case ISD::UINT_TO_FP:
3199 Opc = ARMISD::UITOF;
3200 break;
3201 }
3202
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003203 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003204 return DAG.getNode(Opc, dl, VT, Op);
3205}
3206
Evan Cheng515fe3a2010-07-08 02:08:50 +00003207SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003208 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003209 SDValue Tmp0 = Op.getOperand(0);
3210 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003211 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003212 EVT VT = Op.getValueType();
3213 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003214 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3215 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3216 bool UseNEON = !InGPR && Subtarget->hasNEON();
3217
3218 if (UseNEON) {
3219 // Use VBSL to copy the sign bit.
3220 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3221 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3222 DAG.getTargetConstant(EncodedVal, MVT::i32));
3223 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3224 if (VT == MVT::f64)
3225 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3226 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3227 DAG.getConstant(32, MVT::i32));
3228 else /*if (VT == MVT::f32)*/
3229 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3230 if (SrcVT == MVT::f32) {
3231 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3232 if (VT == MVT::f64)
3233 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3234 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3235 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003236 } else if (VT == MVT::f32)
3237 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3238 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3239 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003240 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3241 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3242
3243 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3244 MVT::i32);
3245 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3246 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3247 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003248
Evan Chenge573fb32011-02-23 02:24:55 +00003249 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3250 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3251 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003252 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003253 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3254 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3255 DAG.getConstant(0, MVT::i32));
3256 } else {
3257 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3258 }
3259
3260 return Res;
3261 }
Evan Chengc143dd42011-02-11 02:28:55 +00003262
3263 // Bitcast operand 1 to i32.
3264 if (SrcVT == MVT::f64)
3265 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3266 &Tmp1, 1).getValue(1);
3267 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3268
Evan Chenge573fb32011-02-23 02:24:55 +00003269 // Or in the signbit with integer operations.
3270 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3271 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3272 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3273 if (VT == MVT::f32) {
3274 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3275 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3276 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3277 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003278 }
3279
Evan Chenge573fb32011-02-23 02:24:55 +00003280 // f64: Or the high part with signbit and then combine two parts.
3281 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3282 &Tmp0, 1);
3283 SDValue Lo = Tmp0.getValue(0);
3284 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3285 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3286 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003287}
3288
Evan Cheng2457f2c2010-05-22 01:47:14 +00003289SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3290 MachineFunction &MF = DAG.getMachineFunction();
3291 MachineFrameInfo *MFI = MF.getFrameInfo();
3292 MFI->setReturnAddressIsTaken(true);
3293
3294 EVT VT = Op.getValueType();
3295 DebugLoc dl = Op.getDebugLoc();
3296 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3297 if (Depth) {
3298 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3299 SDValue Offset = DAG.getConstant(4, MVT::i32);
3300 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3301 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003302 MachinePointerInfo(), false, false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003303 }
3304
3305 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003306 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003307 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3308}
3309
Dan Gohmand858e902010-04-17 15:26:15 +00003310SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003311 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3312 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003313
Owen Andersone50ed302009-08-10 22:56:29 +00003314 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003315 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3316 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003317 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003318 ? ARM::R7 : ARM::R11;
3319 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3320 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003321 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3322 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003323 false, false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003324 return FrameAddr;
3325}
3326
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003327/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003328/// expand a bit convert where either the source or destination type is i64 to
3329/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3330/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3331/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003332static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003333 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3334 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003335 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003336
Bob Wilson9f3f0612010-04-17 05:30:19 +00003337 // This function is only supposed to be called for i64 types, either as the
3338 // source or destination of the bit convert.
3339 EVT SrcVT = Op.getValueType();
3340 EVT DstVT = N->getValueType(0);
3341 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003342 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003343
Bob Wilson9f3f0612010-04-17 05:30:19 +00003344 // Turn i64->f64 into VMOVDRR.
3345 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003346 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3347 DAG.getConstant(0, MVT::i32));
3348 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3349 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003350 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003351 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003352 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003353
Jim Grosbache5165492009-11-09 00:11:35 +00003354 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003355 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3356 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3357 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3358 // Merge the pieces into a single i64 value.
3359 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3360 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003361
Bob Wilson9f3f0612010-04-17 05:30:19 +00003362 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003363}
3364
Bob Wilson5bafff32009-06-22 23:27:02 +00003365/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003366/// Zero vectors are used to represent vector negation and in those cases
3367/// will be implemented with the NEON VNEG instruction. However, VNEG does
3368/// not support i64 elements, so sometimes the zero vectors will need to be
3369/// explicitly constructed. Regardless, use a canonical VMOV to create the
3370/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003371static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003372 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003373 // The canonical modified immediate encoding of a zero vector is....0!
3374 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3375 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3376 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003377 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003378}
3379
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003380/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3381/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003382SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3383 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003384 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3385 EVT VT = Op.getValueType();
3386 unsigned VTBits = VT.getSizeInBits();
3387 DebugLoc dl = Op.getDebugLoc();
3388 SDValue ShOpLo = Op.getOperand(0);
3389 SDValue ShOpHi = Op.getOperand(1);
3390 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003391 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003392 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003393
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003394 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3395
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003396 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3397 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3398 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3399 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3400 DAG.getConstant(VTBits, MVT::i32));
3401 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3402 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003403 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003404
3405 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3406 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003407 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003408 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003409 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003410 CCR, Cmp);
3411
3412 SDValue Ops[2] = { Lo, Hi };
3413 return DAG.getMergeValues(Ops, 2, dl);
3414}
3415
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003416/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3417/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003418SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3419 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003420 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3421 EVT VT = Op.getValueType();
3422 unsigned VTBits = VT.getSizeInBits();
3423 DebugLoc dl = Op.getDebugLoc();
3424 SDValue ShOpLo = Op.getOperand(0);
3425 SDValue ShOpHi = Op.getOperand(1);
3426 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003427 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003428
3429 assert(Op.getOpcode() == ISD::SHL_PARTS);
3430 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3431 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3432 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3433 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3434 DAG.getConstant(VTBits, MVT::i32));
3435 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3436 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3437
3438 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3439 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3440 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003441 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003442 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003443 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003444 CCR, Cmp);
3445
3446 SDValue Ops[2] = { Lo, Hi };
3447 return DAG.getMergeValues(Ops, 2, dl);
3448}
3449
Jim Grosbach4725ca72010-09-08 03:54:02 +00003450SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003451 SelectionDAG &DAG) const {
3452 // The rounding mode is in bits 23:22 of the FPSCR.
3453 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3454 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3455 // so that the shift + and get folded into a bitfield extract.
3456 DebugLoc dl = Op.getDebugLoc();
3457 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3458 DAG.getConstant(Intrinsic::arm_get_fpscr,
3459 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003460 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003461 DAG.getConstant(1U << 22, MVT::i32));
3462 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3463 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003464 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003465 DAG.getConstant(3, MVT::i32));
3466}
3467
Jim Grosbach3482c802010-01-18 19:58:49 +00003468static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3469 const ARMSubtarget *ST) {
3470 EVT VT = N->getValueType(0);
3471 DebugLoc dl = N->getDebugLoc();
3472
3473 if (!ST->hasV6T2Ops())
3474 return SDValue();
3475
3476 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3477 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3478}
3479
Bob Wilson5bafff32009-06-22 23:27:02 +00003480static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3481 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003482 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003483 DebugLoc dl = N->getDebugLoc();
3484
Bob Wilsond5448bb2010-11-18 21:16:28 +00003485 if (!VT.isVector())
3486 return SDValue();
3487
Bob Wilson5bafff32009-06-22 23:27:02 +00003488 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003489 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003490
Bob Wilsond5448bb2010-11-18 21:16:28 +00003491 // Left shifts translate directly to the vshiftu intrinsic.
3492 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003493 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003494 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3495 N->getOperand(0), N->getOperand(1));
3496
3497 assert((N->getOpcode() == ISD::SRA ||
3498 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3499
3500 // NEON uses the same intrinsics for both left and right shifts. For
3501 // right shifts, the shift amounts are negative, so negate the vector of
3502 // shift amounts.
3503 EVT ShiftVT = N->getOperand(1).getValueType();
3504 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3505 getZeroVector(ShiftVT, DAG, dl),
3506 N->getOperand(1));
3507 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3508 Intrinsic::arm_neon_vshifts :
3509 Intrinsic::arm_neon_vshiftu);
3510 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3511 DAG.getConstant(vshiftInt, MVT::i32),
3512 N->getOperand(0), NegatedCount);
3513}
3514
3515static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3516 const ARMSubtarget *ST) {
3517 EVT VT = N->getValueType(0);
3518 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003519
Eli Friedmance392eb2009-08-22 03:13:10 +00003520 // We can get here for a node like i32 = ISD::SHL i32, i64
3521 if (VT != MVT::i64)
3522 return SDValue();
3523
3524 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003525 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003526
Chris Lattner27a6c732007-11-24 07:07:01 +00003527 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3528 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003529 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003530 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003531
Chris Lattner27a6c732007-11-24 07:07:01 +00003532 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003533 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003534
Chris Lattner27a6c732007-11-24 07:07:01 +00003535 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003536 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003537 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003538 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003539 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003540
Chris Lattner27a6c732007-11-24 07:07:01 +00003541 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3542 // captures the result into a carry flag.
3543 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003544 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003545
Chris Lattner27a6c732007-11-24 07:07:01 +00003546 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003547 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003548
Chris Lattner27a6c732007-11-24 07:07:01 +00003549 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003550 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003551}
3552
Bob Wilson5bafff32009-06-22 23:27:02 +00003553static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3554 SDValue TmpOp0, TmpOp1;
3555 bool Invert = false;
3556 bool Swap = false;
3557 unsigned Opc = 0;
3558
3559 SDValue Op0 = Op.getOperand(0);
3560 SDValue Op1 = Op.getOperand(1);
3561 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003562 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003563 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3564 DebugLoc dl = Op.getDebugLoc();
3565
3566 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3567 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003568 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003569 case ISD::SETUNE:
3570 case ISD::SETNE: Invert = true; // Fallthrough
3571 case ISD::SETOEQ:
3572 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3573 case ISD::SETOLT:
3574 case ISD::SETLT: Swap = true; // Fallthrough
3575 case ISD::SETOGT:
3576 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3577 case ISD::SETOLE:
3578 case ISD::SETLE: Swap = true; // Fallthrough
3579 case ISD::SETOGE:
3580 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3581 case ISD::SETUGE: Swap = true; // Fallthrough
3582 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3583 case ISD::SETUGT: Swap = true; // Fallthrough
3584 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3585 case ISD::SETUEQ: Invert = true; // Fallthrough
3586 case ISD::SETONE:
3587 // Expand this to (OLT | OGT).
3588 TmpOp0 = Op0;
3589 TmpOp1 = Op1;
3590 Opc = ISD::OR;
3591 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3592 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3593 break;
3594 case ISD::SETUO: Invert = true; // Fallthrough
3595 case ISD::SETO:
3596 // Expand this to (OLT | OGE).
3597 TmpOp0 = Op0;
3598 TmpOp1 = Op1;
3599 Opc = ISD::OR;
3600 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3601 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3602 break;
3603 }
3604 } else {
3605 // Integer comparisons.
3606 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003607 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003608 case ISD::SETNE: Invert = true;
3609 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3610 case ISD::SETLT: Swap = true;
3611 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3612 case ISD::SETLE: Swap = true;
3613 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3614 case ISD::SETULT: Swap = true;
3615 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3616 case ISD::SETULE: Swap = true;
3617 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3618 }
3619
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003620 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003621 if (Opc == ARMISD::VCEQ) {
3622
3623 SDValue AndOp;
3624 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3625 AndOp = Op0;
3626 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3627 AndOp = Op1;
3628
3629 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003630 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003631 AndOp = AndOp.getOperand(0);
3632
3633 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3634 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003635 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3636 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003637 Invert = !Invert;
3638 }
3639 }
3640 }
3641
3642 if (Swap)
3643 std::swap(Op0, Op1);
3644
Owen Andersonc24cb352010-11-08 23:21:22 +00003645 // If one of the operands is a constant vector zero, attempt to fold the
3646 // comparison to a specialized compare-against-zero form.
3647 SDValue SingleOp;
3648 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3649 SingleOp = Op0;
3650 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3651 if (Opc == ARMISD::VCGE)
3652 Opc = ARMISD::VCLEZ;
3653 else if (Opc == ARMISD::VCGT)
3654 Opc = ARMISD::VCLTZ;
3655 SingleOp = Op1;
3656 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003657
Owen Andersonc24cb352010-11-08 23:21:22 +00003658 SDValue Result;
3659 if (SingleOp.getNode()) {
3660 switch (Opc) {
3661 case ARMISD::VCEQ:
3662 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3663 case ARMISD::VCGE:
3664 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3665 case ARMISD::VCLEZ:
3666 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3667 case ARMISD::VCGT:
3668 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3669 case ARMISD::VCLTZ:
3670 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3671 default:
3672 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3673 }
3674 } else {
3675 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3676 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003677
3678 if (Invert)
3679 Result = DAG.getNOT(dl, Result, VT);
3680
3681 return Result;
3682}
3683
Bob Wilsond3c42842010-06-14 22:19:57 +00003684/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3685/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003686/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003687static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3688 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003689 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003690 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003691
Bob Wilson827b2102010-06-15 19:05:35 +00003692 // SplatBitSize is set to the smallest size that splats the vector, so a
3693 // zero vector will always have SplatBitSize == 8. However, NEON modified
3694 // immediate instructions others than VMOV do not support the 8-bit encoding
3695 // of a zero vector, and the default encoding of zero is supposed to be the
3696 // 32-bit version.
3697 if (SplatBits == 0)
3698 SplatBitSize = 32;
3699
Bob Wilson5bafff32009-06-22 23:27:02 +00003700 switch (SplatBitSize) {
3701 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003702 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003703 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003704 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003705 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003706 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003707 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003708 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003709 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003710
3711 case 16:
3712 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003713 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003714 if ((SplatBits & ~0xff) == 0) {
3715 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003716 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003717 Imm = SplatBits;
3718 break;
3719 }
3720 if ((SplatBits & ~0xff00) == 0) {
3721 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003722 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003723 Imm = SplatBits >> 8;
3724 break;
3725 }
3726 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003727
3728 case 32:
3729 // NEON's 32-bit VMOV supports splat values where:
3730 // * only one byte is nonzero, or
3731 // * the least significant byte is 0xff and the second byte is nonzero, or
3732 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003733 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003734 if ((SplatBits & ~0xff) == 0) {
3735 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003736 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003737 Imm = SplatBits;
3738 break;
3739 }
3740 if ((SplatBits & ~0xff00) == 0) {
3741 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003742 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003743 Imm = SplatBits >> 8;
3744 break;
3745 }
3746 if ((SplatBits & ~0xff0000) == 0) {
3747 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003748 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003749 Imm = SplatBits >> 16;
3750 break;
3751 }
3752 if ((SplatBits & ~0xff000000) == 0) {
3753 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003754 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003755 Imm = SplatBits >> 24;
3756 break;
3757 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003758
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003759 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3760 if (type == OtherModImm) return SDValue();
3761
Bob Wilson5bafff32009-06-22 23:27:02 +00003762 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003763 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3764 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003765 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003766 Imm = SplatBits >> 8;
3767 SplatBits |= 0xff;
3768 break;
3769 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003770
3771 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003772 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3773 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003774 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003775 Imm = SplatBits >> 16;
3776 SplatBits |= 0xffff;
3777 break;
3778 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003779
3780 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3781 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3782 // VMOV.I32. A (very) minor optimization would be to replicate the value
3783 // and fall through here to test for a valid 64-bit splat. But, then the
3784 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003785 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003786
3787 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003788 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003789 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003790 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003791 uint64_t BitMask = 0xff;
3792 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003793 unsigned ImmMask = 1;
3794 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003795 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003796 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003797 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003798 Imm |= ImmMask;
3799 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003800 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003801 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003802 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003803 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003804 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003805 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003806 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003807 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003808 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003809 break;
3810 }
3811
Bob Wilson1a913ed2010-06-11 21:34:50 +00003812 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003813 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003814 }
3815
Bob Wilsoncba270d2010-07-13 21:16:48 +00003816 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3817 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003818}
3819
Lang Hamesc0a9f822012-03-29 21:56:11 +00003820SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
3821 const ARMSubtarget *ST) const {
3822 if (!ST->useNEONForSinglePrecisionFP() || !ST->hasVFP3() || ST->hasD16())
3823 return SDValue();
3824
3825 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
3826 assert(Op.getValueType() == MVT::f32 &&
3827 "ConstantFP custom lowering should only occur for f32.");
3828
3829 // Try splatting with a VMOV.f32...
3830 APFloat FPVal = CFP->getValueAPF();
3831 int ImmVal = ARM_AM::getFP32Imm(FPVal);
3832 if (ImmVal != -1) {
3833 DebugLoc DL = Op.getDebugLoc();
3834 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
3835 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
3836 NewVal);
3837 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
3838 DAG.getConstant(0, MVT::i32));
3839 }
3840
3841 // If that fails, try a VMOV.i32
3842 EVT VMovVT;
3843 unsigned iVal = FPVal.bitcastToAPInt().getZExtValue();
3844 SDValue NewVal = isNEONModifiedImm(iVal, 0, 32, DAG, VMovVT, false,
3845 VMOVModImm);
3846 if (NewVal != SDValue()) {
3847 DebugLoc DL = Op.getDebugLoc();
3848 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
3849 NewVal);
3850 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
3851 VecConstant);
3852 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
3853 DAG.getConstant(0, MVT::i32));
3854 }
3855
3856 // Finally, try a VMVN.i32
3857 NewVal = isNEONModifiedImm(~iVal & 0xffffffff, 0, 32, DAG, VMovVT, false,
3858 VMVNModImm);
3859 if (NewVal != SDValue()) {
3860 DebugLoc DL = Op.getDebugLoc();
3861 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
3862 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
3863 VecConstant);
3864 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
3865 DAG.getConstant(0, MVT::i32));
3866 }
3867
3868 return SDValue();
3869}
3870
3871
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003872static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003873 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003874 unsigned NumElts = VT.getVectorNumElements();
3875 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003876
3877 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3878 if (M[0] < 0)
3879 return false;
3880
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003881 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003882
3883 // If this is a VEXT shuffle, the immediate value is the index of the first
3884 // element. The other shuffle indices must be the successive elements after
3885 // the first one.
3886 unsigned ExpectedElt = Imm;
3887 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003888 // Increment the expected index. If it wraps around, it may still be
3889 // a VEXT but the source vectors must be swapped.
3890 ExpectedElt += 1;
3891 if (ExpectedElt == NumElts * 2) {
3892 ExpectedElt = 0;
3893 ReverseVEXT = true;
3894 }
3895
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003896 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003897 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003898 return false;
3899 }
3900
3901 // Adjust the index value if the source operands will be swapped.
3902 if (ReverseVEXT)
3903 Imm -= NumElts;
3904
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003905 return true;
3906}
3907
Bob Wilson8bb9e482009-07-26 00:39:34 +00003908/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3909/// instruction with the specified blocksize. (The order of the elements
3910/// within each block of the vector is reversed.)
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003911static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003912 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3913 "Only possible block sizes for VREV are: 16, 32, 64");
3914
Bob Wilson8bb9e482009-07-26 00:39:34 +00003915 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003916 if (EltSz == 64)
3917 return false;
3918
3919 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003920 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003921 // If the first shuffle index is UNDEF, be optimistic.
3922 if (M[0] < 0)
3923 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003924
3925 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3926 return false;
3927
3928 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003929 if (M[i] < 0) continue; // ignore UNDEF indices
3930 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003931 return false;
3932 }
3933
3934 return true;
3935}
3936
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003937static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003938 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3939 // range, then 0 is placed into the resulting vector. So pretty much any mask
3940 // of 8 elements can work here.
3941 return VT == MVT::v8i8 && M.size() == 8;
3942}
3943
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003944static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003945 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3946 if (EltSz == 64)
3947 return false;
3948
Bob Wilsonc692cb72009-08-21 20:54:19 +00003949 unsigned NumElts = VT.getVectorNumElements();
3950 WhichResult = (M[0] == 0 ? 0 : 1);
3951 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003952 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3953 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003954 return false;
3955 }
3956 return true;
3957}
3958
Bob Wilson324f4f12009-12-03 06:40:55 +00003959/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3960/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3961/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003962static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00003963 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3964 if (EltSz == 64)
3965 return false;
3966
3967 unsigned NumElts = VT.getVectorNumElements();
3968 WhichResult = (M[0] == 0 ? 0 : 1);
3969 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003970 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3971 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003972 return false;
3973 }
3974 return true;
3975}
3976
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003977static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003978 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3979 if (EltSz == 64)
3980 return false;
3981
Bob Wilsonc692cb72009-08-21 20:54:19 +00003982 unsigned NumElts = VT.getVectorNumElements();
3983 WhichResult = (M[0] == 0 ? 0 : 1);
3984 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003985 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003986 if ((unsigned) M[i] != 2 * i + WhichResult)
3987 return false;
3988 }
3989
3990 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003991 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003992 return false;
3993
3994 return true;
3995}
3996
Bob Wilson324f4f12009-12-03 06:40:55 +00003997/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3998/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3999/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004000static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004001 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4002 if (EltSz == 64)
4003 return false;
4004
4005 unsigned Half = VT.getVectorNumElements() / 2;
4006 WhichResult = (M[0] == 0 ? 0 : 1);
4007 for (unsigned j = 0; j != 2; ++j) {
4008 unsigned Idx = WhichResult;
4009 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004010 int MIdx = M[i + j * Half];
4011 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00004012 return false;
4013 Idx += 2;
4014 }
4015 }
4016
4017 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4018 if (VT.is64BitVector() && EltSz == 32)
4019 return false;
4020
4021 return true;
4022}
4023
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004024static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004025 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4026 if (EltSz == 64)
4027 return false;
4028
Bob Wilsonc692cb72009-08-21 20:54:19 +00004029 unsigned NumElts = VT.getVectorNumElements();
4030 WhichResult = (M[0] == 0 ? 0 : 1);
4031 unsigned Idx = WhichResult * NumElts / 2;
4032 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004033 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4034 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00004035 return false;
4036 Idx += 1;
4037 }
4038
4039 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004040 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004041 return false;
4042
4043 return true;
4044}
4045
Bob Wilson324f4f12009-12-03 06:40:55 +00004046/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
4047/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4048/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004049static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004050 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4051 if (EltSz == 64)
4052 return false;
4053
4054 unsigned NumElts = VT.getVectorNumElements();
4055 WhichResult = (M[0] == 0 ? 0 : 1);
4056 unsigned Idx = WhichResult * NumElts / 2;
4057 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004058 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4059 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00004060 return false;
4061 Idx += 1;
4062 }
4063
4064 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4065 if (VT.is64BitVector() && EltSz == 32)
4066 return false;
4067
4068 return true;
4069}
4070
Dale Johannesenf630c712010-07-29 20:10:08 +00004071// If N is an integer constant that can be moved into a register in one
4072// instruction, return an SDValue of such a constant (will become a MOV
4073// instruction). Otherwise return null.
4074static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
4075 const ARMSubtarget *ST, DebugLoc dl) {
4076 uint64_t Val;
4077 if (!isa<ConstantSDNode>(N))
4078 return SDValue();
4079 Val = cast<ConstantSDNode>(N)->getZExtValue();
4080
4081 if (ST->isThumb1Only()) {
4082 if (Val <= 255 || ~Val <= 255)
4083 return DAG.getConstant(Val, MVT::i32);
4084 } else {
4085 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
4086 return DAG.getConstant(Val, MVT::i32);
4087 }
4088 return SDValue();
4089}
4090
Bob Wilson5bafff32009-06-22 23:27:02 +00004091// If this is a case we can't handle, return null and let the default
4092// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00004093SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
4094 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00004095 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00004096 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004097 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004098
4099 APInt SplatBits, SplatUndef;
4100 unsigned SplatBitSize;
4101 bool HasAnyUndefs;
4102 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004103 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00004104 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00004105 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00004106 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00004107 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004108 DAG, VmovVT, VT.is128BitVector(),
4109 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004110 if (Val.getNode()) {
4111 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004112 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004113 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004114
4115 // Try an immediate VMVN.
Eli Friedman8e4d0422011-10-13 22:40:23 +00004116 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004117 Val = isNEONModifiedImm(NegatedImm,
4118 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004119 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004120 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004121 if (Val.getNode()) {
4122 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004123 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004124 }
Evan Chengeaa192a2011-11-15 02:12:34 +00004125
4126 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedman2f21e8c2011-12-15 22:56:53 +00004127 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedmaneffab8f2011-12-09 23:54:42 +00004128 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Chengeaa192a2011-11-15 02:12:34 +00004129 if (ImmVal != -1) {
4130 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4131 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4132 }
4133 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004134 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00004135 }
4136
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004137 // Scan through the operands to see if only one value is used.
4138 unsigned NumElts = VT.getVectorNumElements();
4139 bool isOnlyLowElement = true;
4140 bool usesOnlyOneValue = true;
4141 bool isConstant = true;
4142 SDValue Value;
4143 for (unsigned i = 0; i < NumElts; ++i) {
4144 SDValue V = Op.getOperand(i);
4145 if (V.getOpcode() == ISD::UNDEF)
4146 continue;
4147 if (i > 0)
4148 isOnlyLowElement = false;
4149 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4150 isConstant = false;
4151
4152 if (!Value.getNode())
4153 Value = V;
4154 else if (V != Value)
4155 usesOnlyOneValue = false;
4156 }
4157
4158 if (!Value.getNode())
4159 return DAG.getUNDEF(VT);
4160
4161 if (isOnlyLowElement)
4162 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4163
Dale Johannesenf630c712010-07-29 20:10:08 +00004164 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4165
Dale Johannesen575cd142010-10-19 20:00:17 +00004166 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4167 // i32 and try again.
4168 if (usesOnlyOneValue && EltSize <= 32) {
4169 if (!isConstant)
4170 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4171 if (VT.getVectorElementType().isFloatingPoint()) {
4172 SmallVector<SDValue, 8> Ops;
4173 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004174 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00004175 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00004176 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
4177 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00004178 Val = LowerBUILD_VECTOR(Val, DAG, ST);
4179 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004180 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004181 }
Dale Johannesen575cd142010-10-19 20:00:17 +00004182 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
4183 if (Val.getNode())
4184 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004185 }
4186
4187 // If all elements are constants and the case above didn't get hit, fall back
4188 // to the default expansion, which will generate a load from the constant
4189 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004190 if (isConstant)
4191 return SDValue();
4192
Bob Wilson11a1dff2011-01-07 21:37:30 +00004193 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4194 if (NumElts >= 4) {
4195 SDValue shuffle = ReconstructShuffle(Op, DAG);
4196 if (shuffle != SDValue())
4197 return shuffle;
4198 }
4199
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004200 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004201 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4202 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004203 if (EltSize >= 32) {
4204 // Do the expansion with floating-point types, since that is what the VFP
4205 // registers are defined to use, and since i64 is not legal.
4206 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4207 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004208 SmallVector<SDValue, 8> Ops;
4209 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004210 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004211 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004212 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004213 }
4214
4215 return SDValue();
4216}
4217
Bob Wilson11a1dff2011-01-07 21:37:30 +00004218// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004219// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004220SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4221 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004222 DebugLoc dl = Op.getDebugLoc();
4223 EVT VT = Op.getValueType();
4224 unsigned NumElts = VT.getVectorNumElements();
4225
4226 SmallVector<SDValue, 2> SourceVecs;
4227 SmallVector<unsigned, 2> MinElts;
4228 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004229
Bob Wilson11a1dff2011-01-07 21:37:30 +00004230 for (unsigned i = 0; i < NumElts; ++i) {
4231 SDValue V = Op.getOperand(i);
4232 if (V.getOpcode() == ISD::UNDEF)
4233 continue;
4234 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4235 // A shuffle can only come from building a vector from various
4236 // elements of other vectors.
4237 return SDValue();
Eli Friedman46995fa2011-10-14 23:58:49 +00004238 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
4239 VT.getVectorElementType()) {
4240 // This code doesn't know how to handle shuffles where the vector
4241 // element types do not match (this happens because type legalization
4242 // promotes the return type of EXTRACT_VECTOR_ELT).
4243 // FIXME: It might be appropriate to extend this code to handle
4244 // mismatched types.
4245 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004246 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004247
Bob Wilson11a1dff2011-01-07 21:37:30 +00004248 // Record this extraction against the appropriate vector if possible...
4249 SDValue SourceVec = V.getOperand(0);
4250 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4251 bool FoundSource = false;
4252 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4253 if (SourceVecs[j] == SourceVec) {
4254 if (MinElts[j] > EltNo)
4255 MinElts[j] = EltNo;
4256 if (MaxElts[j] < EltNo)
4257 MaxElts[j] = EltNo;
4258 FoundSource = true;
4259 break;
4260 }
4261 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004262
Bob Wilson11a1dff2011-01-07 21:37:30 +00004263 // Or record a new source if not...
4264 if (!FoundSource) {
4265 SourceVecs.push_back(SourceVec);
4266 MinElts.push_back(EltNo);
4267 MaxElts.push_back(EltNo);
4268 }
4269 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004270
Bob Wilson11a1dff2011-01-07 21:37:30 +00004271 // Currently only do something sane when at most two source vectors
4272 // involved.
4273 if (SourceVecs.size() > 2)
4274 return SDValue();
4275
4276 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4277 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004278
Bob Wilson11a1dff2011-01-07 21:37:30 +00004279 // This loop extracts the usage patterns of the source vectors
4280 // and prepares appropriate SDValues for a shuffle if possible.
4281 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4282 if (SourceVecs[i].getValueType() == VT) {
4283 // No VEXT necessary
4284 ShuffleSrcs[i] = SourceVecs[i];
4285 VEXTOffsets[i] = 0;
4286 continue;
4287 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4288 // It probably isn't worth padding out a smaller vector just to
4289 // break it down again in a shuffle.
4290 return SDValue();
4291 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004292
Bob Wilson11a1dff2011-01-07 21:37:30 +00004293 // Since only 64-bit and 128-bit vectors are legal on ARM and
4294 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004295 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4296 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004297
Bob Wilson11a1dff2011-01-07 21:37:30 +00004298 if (MaxElts[i] - MinElts[i] >= NumElts) {
4299 // Span too large for a VEXT to cope
4300 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004301 }
4302
Bob Wilson11a1dff2011-01-07 21:37:30 +00004303 if (MinElts[i] >= NumElts) {
4304 // The extraction can just take the second half
4305 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004306 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4307 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004308 DAG.getIntPtrConstant(NumElts));
4309 } else if (MaxElts[i] < NumElts) {
4310 // The extraction can just take the first half
4311 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004312 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4313 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004314 DAG.getIntPtrConstant(0));
4315 } else {
4316 // An actual VEXT is needed
4317 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004318 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4319 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004320 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004321 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4322 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004323 DAG.getIntPtrConstant(NumElts));
4324 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4325 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4326 }
4327 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004328
Bob Wilson11a1dff2011-01-07 21:37:30 +00004329 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004330
Bob Wilson11a1dff2011-01-07 21:37:30 +00004331 for (unsigned i = 0; i < NumElts; ++i) {
4332 SDValue Entry = Op.getOperand(i);
4333 if (Entry.getOpcode() == ISD::UNDEF) {
4334 Mask.push_back(-1);
4335 continue;
4336 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004337
Bob Wilson11a1dff2011-01-07 21:37:30 +00004338 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004339 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4340 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004341 if (ExtractVec == SourceVecs[0]) {
4342 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4343 } else {
4344 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4345 }
4346 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004347
Bob Wilson11a1dff2011-01-07 21:37:30 +00004348 // Final check before we try to produce nonsense...
4349 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004350 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4351 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004352
Bob Wilson11a1dff2011-01-07 21:37:30 +00004353 return SDValue();
4354}
4355
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004356/// isShuffleMaskLegal - Targets can use this to indicate that they only
4357/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4358/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4359/// are assumed to be legal.
4360bool
4361ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4362 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004363 if (VT.getVectorNumElements() == 4 &&
4364 (VT.is128BitVector() || VT.is64BitVector())) {
4365 unsigned PFIndexes[4];
4366 for (unsigned i = 0; i != 4; ++i) {
4367 if (M[i] < 0)
4368 PFIndexes[i] = 8;
4369 else
4370 PFIndexes[i] = M[i];
4371 }
4372
4373 // Compute the index in the perfect shuffle table.
4374 unsigned PFTableIndex =
4375 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4376 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4377 unsigned Cost = (PFEntry >> 30);
4378
4379 if (Cost <= 4)
4380 return true;
4381 }
4382
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004383 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004384 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004385
Bob Wilson53dd2452010-06-07 23:53:38 +00004386 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4387 return (EltSize >= 32 ||
4388 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004389 isVREVMask(M, VT, 64) ||
4390 isVREVMask(M, VT, 32) ||
4391 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004392 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004393 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004394 isVTRNMask(M, VT, WhichResult) ||
4395 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004396 isVZIPMask(M, VT, WhichResult) ||
4397 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4398 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4399 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004400}
4401
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004402/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4403/// the specified operations to build the shuffle.
4404static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4405 SDValue RHS, SelectionDAG &DAG,
4406 DebugLoc dl) {
4407 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4408 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4409 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4410
4411 enum {
4412 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4413 OP_VREV,
4414 OP_VDUP0,
4415 OP_VDUP1,
4416 OP_VDUP2,
4417 OP_VDUP3,
4418 OP_VEXT1,
4419 OP_VEXT2,
4420 OP_VEXT3,
4421 OP_VUZPL, // VUZP, left result
4422 OP_VUZPR, // VUZP, right result
4423 OP_VZIPL, // VZIP, left result
4424 OP_VZIPR, // VZIP, right result
4425 OP_VTRNL, // VTRN, left result
4426 OP_VTRNR // VTRN, right result
4427 };
4428
4429 if (OpNum == OP_COPY) {
4430 if (LHSID == (1*9+2)*9+3) return LHS;
4431 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4432 return RHS;
4433 }
4434
4435 SDValue OpLHS, OpRHS;
4436 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4437 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4438 EVT VT = OpLHS.getValueType();
4439
4440 switch (OpNum) {
4441 default: llvm_unreachable("Unknown shuffle opcode!");
4442 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004443 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004444 if (VT.getVectorElementType() == MVT::i32 ||
4445 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004446 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4447 // vrev <4 x i16> -> VREV32
4448 if (VT.getVectorElementType() == MVT::i16)
4449 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4450 // vrev <4 x i8> -> VREV16
4451 assert(VT.getVectorElementType() == MVT::i8);
4452 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004453 case OP_VDUP0:
4454 case OP_VDUP1:
4455 case OP_VDUP2:
4456 case OP_VDUP3:
4457 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004458 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004459 case OP_VEXT1:
4460 case OP_VEXT2:
4461 case OP_VEXT3:
4462 return DAG.getNode(ARMISD::VEXT, dl, VT,
4463 OpLHS, OpRHS,
4464 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4465 case OP_VUZPL:
4466 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004467 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004468 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4469 case OP_VZIPL:
4470 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004471 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004472 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4473 case OP_VTRNL:
4474 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004475 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4476 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004477 }
4478}
4479
Bill Wendling69a05a72011-03-14 23:02:38 +00004480static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004481 ArrayRef<int> ShuffleMask,
Bill Wendling69a05a72011-03-14 23:02:38 +00004482 SelectionDAG &DAG) {
4483 // Check to see if we can use the VTBL instruction.
4484 SDValue V1 = Op.getOperand(0);
4485 SDValue V2 = Op.getOperand(1);
4486 DebugLoc DL = Op.getDebugLoc();
4487
4488 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004489 for (ArrayRef<int>::iterator
Bill Wendling69a05a72011-03-14 23:02:38 +00004490 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4491 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4492
4493 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4494 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4495 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4496 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004497
Owen Anderson76706012011-04-05 21:48:57 +00004498 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004499 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4500 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004501}
4502
Bob Wilson5bafff32009-06-22 23:27:02 +00004503static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004504 SDValue V1 = Op.getOperand(0);
4505 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004506 DebugLoc dl = Op.getDebugLoc();
4507 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004508 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsond8e17572009-08-12 22:31:50 +00004509
Bob Wilson28865062009-08-13 02:13:04 +00004510 // Convert shuffles that are directly supported on NEON to target-specific
4511 // DAG nodes, instead of keeping them as shuffles and matching them again
4512 // during code selection. This is more efficient and avoids the possibility
4513 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004514 // FIXME: floating-point vectors should be canonicalized to integer vectors
4515 // of the same time so that they get CSEd properly.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004516 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004517
Bob Wilson53dd2452010-06-07 23:53:38 +00004518 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4519 if (EltSize <= 32) {
4520 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4521 int Lane = SVN->getSplatIndex();
4522 // If this is undef splat, generate it via "just" vdup, if possible.
4523 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004524
Dan Gohman65fd6562011-11-03 21:49:52 +00004525 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson53dd2452010-06-07 23:53:38 +00004526 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4527 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4528 }
Dan Gohman65fd6562011-11-03 21:49:52 +00004529 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
4530 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
4531 // reaches it).
4532 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
4533 !isa<ConstantSDNode>(V1.getOperand(0))) {
4534 bool IsScalarToVector = true;
4535 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
4536 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
4537 IsScalarToVector = false;
4538 break;
4539 }
4540 if (IsScalarToVector)
4541 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4542 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004543 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4544 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004545 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004546
4547 bool ReverseVEXT;
4548 unsigned Imm;
4549 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4550 if (ReverseVEXT)
4551 std::swap(V1, V2);
4552 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4553 DAG.getConstant(Imm, MVT::i32));
4554 }
4555
4556 if (isVREVMask(ShuffleMask, VT, 64))
4557 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4558 if (isVREVMask(ShuffleMask, VT, 32))
4559 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4560 if (isVREVMask(ShuffleMask, VT, 16))
4561 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4562
4563 // Check for Neon shuffles that modify both input vectors in place.
4564 // If both results are used, i.e., if there are two shuffles with the same
4565 // source operands and with masks corresponding to both results of one of
4566 // these operations, DAG memoization will ensure that a single node is
4567 // used for both shuffles.
4568 unsigned WhichResult;
4569 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4570 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4571 V1, V2).getValue(WhichResult);
4572 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4573 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4574 V1, V2).getValue(WhichResult);
4575 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4576 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4577 V1, V2).getValue(WhichResult);
4578
4579 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4580 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4581 V1, V1).getValue(WhichResult);
4582 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4583 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4584 V1, V1).getValue(WhichResult);
4585 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4586 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4587 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004588 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004589
Bob Wilsonc692cb72009-08-21 20:54:19 +00004590 // If the shuffle is not directly supported and it has 4 elements, use
4591 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004592 unsigned NumElts = VT.getVectorNumElements();
4593 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004594 unsigned PFIndexes[4];
4595 for (unsigned i = 0; i != 4; ++i) {
4596 if (ShuffleMask[i] < 0)
4597 PFIndexes[i] = 8;
4598 else
4599 PFIndexes[i] = ShuffleMask[i];
4600 }
4601
4602 // Compute the index in the perfect shuffle table.
4603 unsigned PFTableIndex =
4604 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004605 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4606 unsigned Cost = (PFEntry >> 30);
4607
4608 if (Cost <= 4)
4609 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4610 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004611
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004612 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004613 if (EltSize >= 32) {
4614 // Do the expansion with floating-point types, since that is what the VFP
4615 // registers are defined to use, and since i64 is not legal.
4616 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4617 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004618 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4619 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004620 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004621 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004622 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004623 Ops.push_back(DAG.getUNDEF(EltVT));
4624 else
4625 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4626 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4627 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4628 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004629 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004630 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004631 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004632 }
4633
Bill Wendling69a05a72011-03-14 23:02:38 +00004634 if (VT == MVT::v8i8) {
4635 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4636 if (NewOp.getNode())
4637 return NewOp;
4638 }
4639
Bob Wilson22cac0d2009-08-14 05:16:33 +00004640 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004641}
4642
Eli Friedman5c89cb82011-10-24 23:08:52 +00004643static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
4644 // INSERT_VECTOR_ELT is legal only for immediate indexes.
4645 SDValue Lane = Op.getOperand(2);
4646 if (!isa<ConstantSDNode>(Lane))
4647 return SDValue();
4648
4649 return Op;
4650}
4651
Bob Wilson5bafff32009-06-22 23:27:02 +00004652static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004653 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004654 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004655 if (!isa<ConstantSDNode>(Lane))
4656 return SDValue();
4657
4658 SDValue Vec = Op.getOperand(0);
4659 if (Op.getValueType() == MVT::i32 &&
4660 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4661 DebugLoc dl = Op.getDebugLoc();
4662 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4663 }
4664
4665 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004666}
4667
Bob Wilsona6d65862009-08-03 20:36:38 +00004668static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4669 // The only time a CONCAT_VECTORS operation can have legal types is when
4670 // two 64-bit vectors are concatenated to a 128-bit vector.
4671 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4672 "unexpected CONCAT_VECTORS");
4673 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004674 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004675 SDValue Op0 = Op.getOperand(0);
4676 SDValue Op1 = Op.getOperand(1);
4677 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004678 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004679 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004680 DAG.getIntPtrConstant(0));
4681 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004682 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004683 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004684 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004685 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004686}
4687
Bob Wilson626613d2010-11-23 19:38:38 +00004688/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4689/// element has been zero/sign-extended, depending on the isSigned parameter,
4690/// from an integer type half its size.
4691static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4692 bool isSigned) {
4693 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4694 EVT VT = N->getValueType(0);
4695 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4696 SDNode *BVN = N->getOperand(0).getNode();
4697 if (BVN->getValueType(0) != MVT::v4i32 ||
4698 BVN->getOpcode() != ISD::BUILD_VECTOR)
4699 return false;
4700 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4701 unsigned HiElt = 1 - LoElt;
4702 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4703 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4704 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4705 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4706 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4707 return false;
4708 if (isSigned) {
4709 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4710 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4711 return true;
4712 } else {
4713 if (Hi0->isNullValue() && Hi1->isNullValue())
4714 return true;
4715 }
4716 return false;
4717 }
4718
4719 if (N->getOpcode() != ISD::BUILD_VECTOR)
4720 return false;
4721
4722 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4723 SDNode *Elt = N->getOperand(i).getNode();
4724 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4725 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4726 unsigned HalfSize = EltSize / 2;
4727 if (isSigned) {
Bob Wilson9d45de22011-10-18 18:46:49 +00004728 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004729 return false;
4730 } else {
Bob Wilson9d45de22011-10-18 18:46:49 +00004731 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004732 return false;
4733 }
4734 continue;
4735 }
4736 return false;
4737 }
4738
4739 return true;
4740}
4741
4742/// isSignExtended - Check if a node is a vector value that is sign-extended
4743/// or a constant BUILD_VECTOR with sign-extended elements.
4744static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4745 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4746 return true;
4747 if (isExtendedBUILD_VECTOR(N, DAG, true))
4748 return true;
4749 return false;
4750}
4751
4752/// isZeroExtended - Check if a node is a vector value that is zero-extended
4753/// or a constant BUILD_VECTOR with zero-extended elements.
4754static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4755 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4756 return true;
4757 if (isExtendedBUILD_VECTOR(N, DAG, false))
4758 return true;
4759 return false;
4760}
4761
4762/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4763/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004764static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4765 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4766 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004767 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4768 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4769 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004770 LD->isNonTemporal(), LD->isInvariant(),
4771 LD->getAlignment());
Bob Wilson626613d2010-11-23 19:38:38 +00004772 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4773 // have been legalized as a BITCAST from v4i32.
4774 if (N->getOpcode() == ISD::BITCAST) {
4775 SDNode *BVN = N->getOperand(0).getNode();
4776 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4777 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4778 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4779 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4780 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4781 }
4782 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4783 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4784 EVT VT = N->getValueType(0);
4785 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4786 unsigned NumElts = VT.getVectorNumElements();
4787 MVT TruncVT = MVT::getIntegerVT(EltSize);
4788 SmallVector<SDValue, 8> Ops;
4789 for (unsigned i = 0; i != NumElts; ++i) {
4790 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4791 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004792 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004793 }
4794 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4795 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004796}
4797
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004798static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4799 unsigned Opcode = N->getOpcode();
4800 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4801 SDNode *N0 = N->getOperand(0).getNode();
4802 SDNode *N1 = N->getOperand(1).getNode();
4803 return N0->hasOneUse() && N1->hasOneUse() &&
4804 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4805 }
4806 return false;
4807}
4808
4809static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4810 unsigned Opcode = N->getOpcode();
4811 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4812 SDNode *N0 = N->getOperand(0).getNode();
4813 SDNode *N1 = N->getOperand(1).getNode();
4814 return N0->hasOneUse() && N1->hasOneUse() &&
4815 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4816 }
4817 return false;
4818}
4819
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004820static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4821 // Multiplications are only custom-lowered for 128-bit vectors so that
4822 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4823 EVT VT = Op.getValueType();
4824 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4825 SDNode *N0 = Op.getOperand(0).getNode();
4826 SDNode *N1 = Op.getOperand(1).getNode();
4827 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004828 bool isMLA = false;
4829 bool isN0SExt = isSignExtended(N0, DAG);
4830 bool isN1SExt = isSignExtended(N1, DAG);
4831 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004832 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004833 else {
4834 bool isN0ZExt = isZeroExtended(N0, DAG);
4835 bool isN1ZExt = isZeroExtended(N1, DAG);
4836 if (isN0ZExt && isN1ZExt)
4837 NewOpc = ARMISD::VMULLu;
4838 else if (isN1SExt || isN1ZExt) {
4839 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4840 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4841 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4842 NewOpc = ARMISD::VMULLs;
4843 isMLA = true;
4844 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4845 NewOpc = ARMISD::VMULLu;
4846 isMLA = true;
4847 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4848 std::swap(N0, N1);
4849 NewOpc = ARMISD::VMULLu;
4850 isMLA = true;
4851 }
4852 }
4853
4854 if (!NewOpc) {
4855 if (VT == MVT::v2i64)
4856 // Fall through to expand this. It is not legal.
4857 return SDValue();
4858 else
4859 // Other vector multiplications are legal.
4860 return Op;
4861 }
4862 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004863
4864 // Legalize to a VMULL instruction.
4865 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004866 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004867 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004868 if (!isMLA) {
4869 Op0 = SkipExtension(N0, DAG);
4870 assert(Op0.getValueType().is64BitVector() &&
4871 Op1.getValueType().is64BitVector() &&
4872 "unexpected types for extended operands to VMULL");
4873 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4874 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004875
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004876 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4877 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4878 // vmull q0, d4, d6
4879 // vmlal q0, d5, d6
4880 // is faster than
4881 // vaddl q0, d4, d5
4882 // vmovl q1, d6
4883 // vmul q0, q0, q1
4884 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4885 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4886 EVT Op1VT = Op1.getValueType();
4887 return DAG.getNode(N0->getOpcode(), DL, VT,
4888 DAG.getNode(NewOpc, DL, VT,
4889 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4890 DAG.getNode(NewOpc, DL, VT,
4891 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004892}
4893
Owen Anderson76706012011-04-05 21:48:57 +00004894static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004895LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4896 // Convert to float
4897 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4898 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4899 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4900 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4901 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4902 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4903 // Get reciprocal estimate.
4904 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004905 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004906 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4907 // Because char has a smaller range than uchar, we can actually get away
4908 // without any newton steps. This requires that we use a weird bias
4909 // of 0xb000, however (again, this has been exhaustively tested).
4910 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4911 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4912 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4913 Y = DAG.getConstant(0xb000, MVT::i32);
4914 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4915 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4916 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4917 // Convert back to short.
4918 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4919 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4920 return X;
4921}
4922
Owen Anderson76706012011-04-05 21:48:57 +00004923static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004924LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4925 SDValue N2;
4926 // Convert to float.
4927 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4928 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4929 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4930 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4931 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4932 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004933
Nate Begeman7973f352011-02-11 20:53:29 +00004934 // Use reciprocal estimate and one refinement step.
4935 // float4 recip = vrecpeq_f32(yf);
4936 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004937 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004938 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004939 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004940 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4941 N1, N2);
4942 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4943 // Because short has a smaller range than ushort, we can actually get away
4944 // with only a single newton step. This requires that we use a weird bias
4945 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004946 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004947 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4948 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004949 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004950 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4951 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4952 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4953 // Convert back to integer and return.
4954 // return vmovn_s32(vcvt_s32_f32(result));
4955 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4956 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4957 return N0;
4958}
4959
4960static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4961 EVT VT = Op.getValueType();
4962 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4963 "unexpected type for custom-lowering ISD::SDIV");
4964
4965 DebugLoc dl = Op.getDebugLoc();
4966 SDValue N0 = Op.getOperand(0);
4967 SDValue N1 = Op.getOperand(1);
4968 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004969
Nate Begeman7973f352011-02-11 20:53:29 +00004970 if (VT == MVT::v8i8) {
4971 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4972 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004973
Nate Begeman7973f352011-02-11 20:53:29 +00004974 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4975 DAG.getIntPtrConstant(4));
4976 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004977 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004978 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4979 DAG.getIntPtrConstant(0));
4980 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4981 DAG.getIntPtrConstant(0));
4982
4983 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4984 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4985
4986 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4987 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004988
Nate Begeman7973f352011-02-11 20:53:29 +00004989 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
4990 return N0;
4991 }
4992 return LowerSDIV_v4i16(N0, N1, dl, DAG);
4993}
4994
4995static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
4996 EVT VT = Op.getValueType();
4997 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4998 "unexpected type for custom-lowering ISD::UDIV");
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::ZERO_EXTEND, dl, MVT::v8i16, N0);
5007 N1 = DAG.getNode(ISD::ZERO_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));
Owen Anderson76706012011-04-05 21:48:57 +00005017
Nate Begeman7973f352011-02-11 20:53:29 +00005018 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
5019 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00005020
Nate Begeman7973f352011-02-11 20:53:29 +00005021 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
5024 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00005025 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
5026 N0);
5027 return N0;
5028 }
Owen Anderson76706012011-04-05 21:48:57 +00005029
Nate Begeman7973f352011-02-11 20:53:29 +00005030 // v4i16 sdiv ... Convert to float.
5031 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
5032 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
5033 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
5034 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
5035 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005036 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00005037
5038 // Use reciprocal estimate and two refinement steps.
5039 // float4 recip = vrecpeq_f32(yf);
5040 // recip *= vrecpsq_f32(yf, recip);
5041 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00005042 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005043 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00005044 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005045 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005046 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005047 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00005048 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005049 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005050 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005051 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5052 // Simply multiplying by the reciprocal estimate can leave us a few ulps
5053 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
5054 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005055 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00005056 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5057 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
5058 N1 = DAG.getConstant(2, MVT::i32);
5059 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5060 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5061 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5062 // Convert back to integer and return.
5063 // return vmovn_u32(vcvt_s32_f32(result));
5064 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5065 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5066 return N0;
5067}
5068
Evan Cheng342e3162011-08-30 01:34:54 +00005069static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
5070 EVT VT = Op.getNode()->getValueType(0);
5071 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
5072
5073 unsigned Opc;
5074 bool ExtraOp = false;
5075 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00005076 default: llvm_unreachable("Invalid code");
Evan Cheng342e3162011-08-30 01:34:54 +00005077 case ISD::ADDC: Opc = ARMISD::ADDC; break;
5078 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
5079 case ISD::SUBC: Opc = ARMISD::SUBC; break;
5080 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
5081 }
5082
5083 if (!ExtraOp)
5084 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5085 Op.getOperand(1));
5086 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5087 Op.getOperand(1), Op.getOperand(2));
5088}
5089
Eli Friedman74bf18c2011-09-15 22:26:18 +00005090static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedman7cc15662011-09-15 22:18:49 +00005091 // Monotonic load/store is legal for all targets
5092 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
5093 return Op;
5094
5095 // Aquire/Release load/store is not legal for targets without a
5096 // dmb or equivalent available.
5097 return SDValue();
5098}
5099
5100
Eli Friedman2bdffe42011-08-31 00:31:29 +00005101static void
Eli Friedman4d3f3292011-08-31 17:52:22 +00005102ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
5103 SelectionDAG &DAG, unsigned NewOp) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005104 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +00005105 assert (Node->getValueType(0) == MVT::i64 &&
5106 "Only know how to expand i64 atomics");
Eli Friedman2bdffe42011-08-31 00:31:29 +00005107
Eli Friedman4d3f3292011-08-31 17:52:22 +00005108 SmallVector<SDValue, 6> Ops;
5109 Ops.push_back(Node->getOperand(0)); // Chain
5110 Ops.push_back(Node->getOperand(1)); // Ptr
5111 // Low part of Val1
5112 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5113 Node->getOperand(2), DAG.getIntPtrConstant(0)));
5114 // High part of Val1
5115 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5116 Node->getOperand(2), DAG.getIntPtrConstant(1)));
Andrew Trick3af7a672011-09-20 03:06:13 +00005117 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) {
Eli Friedman4d3f3292011-08-31 17:52:22 +00005118 // High part of Val1
5119 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5120 Node->getOperand(3), DAG.getIntPtrConstant(0)));
5121 // High part of Val2
5122 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5123 Node->getOperand(3), DAG.getIntPtrConstant(1)));
5124 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005125 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
5126 SDValue Result =
Eli Friedman4d3f3292011-08-31 17:52:22 +00005127 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64,
Eli Friedman2bdffe42011-08-31 00:31:29 +00005128 cast<MemSDNode>(Node)->getMemOperand());
Eli Friedman4d3f3292011-08-31 17:52:22 +00005129 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) };
Eli Friedman2bdffe42011-08-31 00:31:29 +00005130 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
5131 Results.push_back(Result.getValue(2));
5132}
5133
Dan Gohmand858e902010-04-17 15:26:15 +00005134SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005135 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005136 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00005137 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00005138 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005139 case ISD::GlobalAddress:
5140 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
5141 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00005142 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00005143 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00005144 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
5145 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005146 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00005147 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00005148 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00005149 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00005150 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00005151 case ISD::SINT_TO_FP:
5152 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
5153 case ISD::FP_TO_SINT:
5154 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005155 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00005156 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00005157 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005158 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00005159 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00005160 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00005161 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
5162 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00005163 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005164 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00005165 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00005166 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00005167 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00005168 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00005169 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00005170 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Duncan Sands28b77e92011-09-06 19:07:46 +00005171 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hames45b5f882012-03-15 18:49:02 +00005172 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesenf630c712010-07-29 20:10:08 +00005173 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005174 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedman5c89cb82011-10-24 23:08:52 +00005175 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005176 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00005177 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00005178 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005179 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00005180 case ISD::SDIV: return LowerSDIV(Op, DAG);
5181 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00005182 case ISD::ADDC:
5183 case ISD::ADDE:
5184 case ISD::SUBC:
5185 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Eli Friedman7cc15662011-09-15 22:18:49 +00005186 case ISD::ATOMIC_LOAD:
Eli Friedman74bf18c2011-09-15 22:26:18 +00005187 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005188 }
Evan Chenga8e29892007-01-19 07:51:42 +00005189}
5190
Duncan Sands1607f052008-12-01 11:39:25 +00005191/// ReplaceNodeResults - Replace the results of node with an illegal result
5192/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00005193void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
5194 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005195 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00005196 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00005197 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00005198 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005199 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005200 case ISD::BITCAST:
5201 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005202 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005203 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00005204 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00005205 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005206 break;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005207 case ISD::ATOMIC_LOAD_ADD:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005208 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005209 return;
5210 case ISD::ATOMIC_LOAD_AND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005211 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005212 return;
5213 case ISD::ATOMIC_LOAD_NAND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005214 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005215 return;
5216 case ISD::ATOMIC_LOAD_OR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005217 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005218 return;
5219 case ISD::ATOMIC_LOAD_SUB:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005220 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005221 return;
5222 case ISD::ATOMIC_LOAD_XOR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005223 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005224 return;
5225 case ISD::ATOMIC_SWAP:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005226 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005227 return;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005228 case ISD::ATOMIC_CMP_SWAP:
5229 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG);
5230 return;
Duncan Sands1607f052008-12-01 11:39:25 +00005231 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00005232 if (Res.getNode())
5233 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00005234}
Chris Lattner27a6c732007-11-24 07:07:01 +00005235
Evan Chenga8e29892007-01-19 07:51:42 +00005236//===----------------------------------------------------------------------===//
5237// ARM Scheduler Hooks
5238//===----------------------------------------------------------------------===//
5239
5240MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005241ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
5242 MachineBasicBlock *BB,
5243 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005244 unsigned dest = MI->getOperand(0).getReg();
5245 unsigned ptr = MI->getOperand(1).getReg();
5246 unsigned oldval = MI->getOperand(2).getReg();
5247 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005248 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5249 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005250 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005251
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005252 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5253 unsigned scratch =
Cameron Zwarich141ec632011-05-18 02:29:50 +00005254 MRI.createVirtualRegister(isThumb2 ? ARM::rGPRRegisterClass
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005255 : ARM::GPRRegisterClass);
5256
5257 if (isThumb2) {
Cameron Zwarich141ec632011-05-18 02:29:50 +00005258 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5259 MRI.constrainRegClass(oldval, ARM::rGPRRegisterClass);
5260 MRI.constrainRegClass(newval, ARM::rGPRRegisterClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005261 }
5262
Jim Grosbach5278eb82009-12-11 01:42:04 +00005263 unsigned ldrOpc, strOpc;
5264 switch (Size) {
5265 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005266 case 1:
5267 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00005268 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005269 break;
5270 case 2:
5271 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5272 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5273 break;
5274 case 4:
5275 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5276 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5277 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00005278 }
5279
5280 MachineFunction *MF = BB->getParent();
5281 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5282 MachineFunction::iterator It = BB;
5283 ++It; // insert the new blocks after the current block
5284
5285 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5286 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5287 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5288 MF->insert(It, loop1MBB);
5289 MF->insert(It, loop2MBB);
5290 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005291
5292 // Transfer the remainder of BB and its successor edges to exitMBB.
5293 exitMBB->splice(exitMBB->begin(), BB,
5294 llvm::next(MachineBasicBlock::iterator(MI)),
5295 BB->end());
5296 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005297
5298 // thisMBB:
5299 // ...
5300 // fallthrough --> loop1MBB
5301 BB->addSuccessor(loop1MBB);
5302
5303 // loop1MBB:
5304 // ldrex dest, [ptr]
5305 // cmp dest, oldval
5306 // bne exitMBB
5307 BB = loop1MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005308 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5309 if (ldrOpc == ARM::t2LDREX)
5310 MIB.addImm(0);
5311 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005312 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005313 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005314 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5315 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005316 BB->addSuccessor(loop2MBB);
5317 BB->addSuccessor(exitMBB);
5318
5319 // loop2MBB:
5320 // strex scratch, newval, [ptr]
5321 // cmp scratch, #0
5322 // bne loop1MBB
5323 BB = loop2MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005324 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr);
5325 if (strOpc == ARM::t2STREX)
5326 MIB.addImm(0);
5327 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005328 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005329 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005330 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5331 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005332 BB->addSuccessor(loop1MBB);
5333 BB->addSuccessor(exitMBB);
5334
5335 // exitMBB:
5336 // ...
5337 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005338
Dan Gohman14152b42010-07-06 20:24:04 +00005339 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005340
Jim Grosbach5278eb82009-12-11 01:42:04 +00005341 return BB;
5342}
5343
5344MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005345ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5346 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005347 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5348 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5349
5350 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005351 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005352 MachineFunction::iterator It = BB;
5353 ++It;
5354
5355 unsigned dest = MI->getOperand(0).getReg();
5356 unsigned ptr = MI->getOperand(1).getReg();
5357 unsigned incr = MI->getOperand(2).getReg();
5358 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005359 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005360
5361 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5362 if (isThumb2) {
5363 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5364 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5365 }
5366
Jim Grosbachc3c23542009-12-14 04:22:04 +00005367 unsigned ldrOpc, strOpc;
5368 switch (Size) {
5369 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005370 case 1:
5371 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005372 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005373 break;
5374 case 2:
5375 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5376 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5377 break;
5378 case 4:
5379 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5380 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5381 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005382 }
5383
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005384 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5385 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5386 MF->insert(It, loopMBB);
5387 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005388
5389 // Transfer the remainder of BB and its successor edges to exitMBB.
5390 exitMBB->splice(exitMBB->begin(), BB,
5391 llvm::next(MachineBasicBlock::iterator(MI)),
5392 BB->end());
5393 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005394
Craig Topper44d23822012-02-22 05:59:10 +00005395 const TargetRegisterClass *TRC =
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005396 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5397 unsigned scratch = MRI.createVirtualRegister(TRC);
5398 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005399
5400 // thisMBB:
5401 // ...
5402 // fallthrough --> loopMBB
5403 BB->addSuccessor(loopMBB);
5404
5405 // loopMBB:
5406 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005407 // <binop> scratch2, dest, incr
5408 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005409 // cmp scratch, #0
5410 // bne- loopMBB
5411 // fallthrough --> exitMBB
5412 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005413 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5414 if (ldrOpc == ARM::t2LDREX)
5415 MIB.addImm(0);
5416 AddDefaultPred(MIB);
Jim Grosbachc67b5562009-12-15 00:12:35 +00005417 if (BinOpcode) {
5418 // operand order needs to go the other way for NAND
5419 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5420 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5421 addReg(incr).addReg(dest)).addReg(0);
5422 else
5423 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5424 addReg(dest).addReg(incr)).addReg(0);
5425 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005426
Jim Grosbachb6aed502011-09-09 18:37:27 +00005427 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5428 if (strOpc == ARM::t2STREX)
5429 MIB.addImm(0);
5430 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005431 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005432 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005433 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5434 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005435
5436 BB->addSuccessor(loopMBB);
5437 BB->addSuccessor(exitMBB);
5438
5439 // exitMBB:
5440 // ...
5441 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005442
Dan Gohman14152b42010-07-06 20:24:04 +00005443 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005444
Jim Grosbachc3c23542009-12-14 04:22:04 +00005445 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005446}
5447
Jim Grosbachf7da8822011-04-26 19:44:18 +00005448MachineBasicBlock *
5449ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5450 MachineBasicBlock *BB,
5451 unsigned Size,
5452 bool signExtend,
5453 ARMCC::CondCodes Cond) const {
5454 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5455
5456 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5457 MachineFunction *MF = BB->getParent();
5458 MachineFunction::iterator It = BB;
5459 ++It;
5460
5461 unsigned dest = MI->getOperand(0).getReg();
5462 unsigned ptr = MI->getOperand(1).getReg();
5463 unsigned incr = MI->getOperand(2).getReg();
5464 unsigned oldval = dest;
5465 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005466 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005467
5468 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5469 if (isThumb2) {
5470 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5471 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5472 }
5473
Jim Grosbachf7da8822011-04-26 19:44:18 +00005474 unsigned ldrOpc, strOpc, extendOpc;
5475 switch (Size) {
5476 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5477 case 1:
5478 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5479 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005480 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005481 break;
5482 case 2:
5483 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5484 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005485 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005486 break;
5487 case 4:
5488 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5489 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5490 extendOpc = 0;
5491 break;
5492 }
5493
5494 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5495 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5496 MF->insert(It, loopMBB);
5497 MF->insert(It, exitMBB);
5498
5499 // Transfer the remainder of BB and its successor edges to exitMBB.
5500 exitMBB->splice(exitMBB->begin(), BB,
5501 llvm::next(MachineBasicBlock::iterator(MI)),
5502 BB->end());
5503 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5504
Craig Topper44d23822012-02-22 05:59:10 +00005505 const TargetRegisterClass *TRC =
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005506 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5507 unsigned scratch = MRI.createVirtualRegister(TRC);
5508 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005509
5510 // thisMBB:
5511 // ...
5512 // fallthrough --> loopMBB
5513 BB->addSuccessor(loopMBB);
5514
5515 // loopMBB:
5516 // ldrex dest, ptr
5517 // (sign extend dest, if required)
5518 // cmp dest, incr
5519 // cmov.cond scratch2, dest, incr
5520 // strex scratch, scratch2, ptr
5521 // cmp scratch, #0
5522 // bne- loopMBB
5523 // fallthrough --> exitMBB
5524 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005525 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5526 if (ldrOpc == ARM::t2LDREX)
5527 MIB.addImm(0);
5528 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005529
5530 // Sign extend the value, if necessary.
5531 if (signExtend && extendOpc) {
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005532 oldval = MRI.createVirtualRegister(ARM::GPRRegisterClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005533 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5534 .addReg(dest)
5535 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005536 }
5537
5538 // Build compare and cmov instructions.
5539 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5540 .addReg(oldval).addReg(incr));
5541 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5542 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5543
Jim Grosbachb6aed502011-09-09 18:37:27 +00005544 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5545 if (strOpc == ARM::t2STREX)
5546 MIB.addImm(0);
5547 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005548 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5549 .addReg(scratch).addImm(0));
5550 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5551 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5552
5553 BB->addSuccessor(loopMBB);
5554 BB->addSuccessor(exitMBB);
5555
5556 // exitMBB:
5557 // ...
5558 BB = exitMBB;
5559
5560 MI->eraseFromParent(); // The instruction is gone now.
5561
5562 return BB;
5563}
5564
Eli Friedman2bdffe42011-08-31 00:31:29 +00005565MachineBasicBlock *
5566ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
5567 unsigned Op1, unsigned Op2,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005568 bool NeedsCarry, bool IsCmpxchg) const {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005569 // This also handles ATOMIC_SWAP, indicated by Op1==0.
5570 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5571
5572 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5573 MachineFunction *MF = BB->getParent();
5574 MachineFunction::iterator It = BB;
5575 ++It;
5576
5577 unsigned destlo = MI->getOperand(0).getReg();
5578 unsigned desthi = MI->getOperand(1).getReg();
5579 unsigned ptr = MI->getOperand(2).getReg();
5580 unsigned vallo = MI->getOperand(3).getReg();
5581 unsigned valhi = MI->getOperand(4).getReg();
5582 DebugLoc dl = MI->getDebugLoc();
5583 bool isThumb2 = Subtarget->isThumb2();
5584
5585 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5586 if (isThumb2) {
5587 MRI.constrainRegClass(destlo, ARM::rGPRRegisterClass);
5588 MRI.constrainRegClass(desthi, ARM::rGPRRegisterClass);
5589 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5590 }
5591
5592 unsigned ldrOpc = isThumb2 ? ARM::t2LDREXD : ARM::LDREXD;
5593 unsigned strOpc = isThumb2 ? ARM::t2STREXD : ARM::STREXD;
5594
5595 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman7df496d2011-09-01 22:27:41 +00005596 MachineBasicBlock *contBB = 0, *cont2BB = 0;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005597 if (IsCmpxchg) {
5598 contBB = MF->CreateMachineBasicBlock(LLVM_BB);
5599 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB);
5600 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005601 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5602 MF->insert(It, loopMBB);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005603 if (IsCmpxchg) {
5604 MF->insert(It, contBB);
5605 MF->insert(It, cont2BB);
5606 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005607 MF->insert(It, exitMBB);
5608
5609 // Transfer the remainder of BB and its successor edges to exitMBB.
5610 exitMBB->splice(exitMBB->begin(), BB,
5611 llvm::next(MachineBasicBlock::iterator(MI)),
5612 BB->end());
5613 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5614
Craig Topper44d23822012-02-22 05:59:10 +00005615 const TargetRegisterClass *TRC =
Eli Friedman2bdffe42011-08-31 00:31:29 +00005616 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5617 unsigned storesuccess = MRI.createVirtualRegister(TRC);
5618
5619 // thisMBB:
5620 // ...
5621 // fallthrough --> loopMBB
5622 BB->addSuccessor(loopMBB);
5623
5624 // loopMBB:
5625 // ldrexd r2, r3, ptr
5626 // <binopa> r0, r2, incr
5627 // <binopb> r1, r3, incr
5628 // strexd storesuccess, r0, r1, ptr
5629 // cmp storesuccess, #0
5630 // bne- loopMBB
5631 // fallthrough --> exitMBB
5632 //
5633 // Note that the registers are explicitly specified because there is not any
5634 // way to force the register allocator to allocate a register pair.
5635 //
Andrew Trick3af7a672011-09-20 03:06:13 +00005636 // FIXME: The hardcoded registers are not necessary for Thumb2, but we
Eli Friedman2bdffe42011-08-31 00:31:29 +00005637 // need to properly enforce the restriction that the two output registers
5638 // for ldrexd must be different.
5639 BB = loopMBB;
5640 // Load
5641 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc))
5642 .addReg(ARM::R2, RegState::Define)
5643 .addReg(ARM::R3, RegState::Define).addReg(ptr));
5644 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
5645 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo).addReg(ARM::R2);
5646 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi).addReg(ARM::R3);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005647
5648 if (IsCmpxchg) {
5649 // Add early exit
5650 for (unsigned i = 0; i < 2; i++) {
5651 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr :
5652 ARM::CMPrr))
5653 .addReg(i == 0 ? destlo : desthi)
5654 .addReg(i == 0 ? vallo : valhi));
5655 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5656 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5657 BB->addSuccessor(exitMBB);
5658 BB->addSuccessor(i == 0 ? contBB : cont2BB);
5659 BB = (i == 0 ? contBB : cont2BB);
5660 }
5661
5662 // Copy to physregs for strexd
5663 unsigned setlo = MI->getOperand(5).getReg();
5664 unsigned sethi = MI->getOperand(6).getReg();
5665 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(setlo);
5666 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(sethi);
5667 } else if (Op1) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005668 // Perform binary operation
5669 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), ARM::R0)
5670 .addReg(destlo).addReg(vallo))
5671 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
5672 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), ARM::R1)
5673 .addReg(desthi).addReg(valhi)).addReg(0);
5674 } else {
5675 // Copy to physregs for strexd
5676 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(vallo);
5677 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(valhi);
5678 }
5679
5680 // Store
5681 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), storesuccess)
5682 .addReg(ARM::R0).addReg(ARM::R1).addReg(ptr));
5683 // Cmp+jump
5684 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5685 .addReg(storesuccess).addImm(0));
5686 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5687 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5688
5689 BB->addSuccessor(loopMBB);
5690 BB->addSuccessor(exitMBB);
5691
5692 // exitMBB:
5693 // ...
5694 BB = exitMBB;
5695
5696 MI->eraseFromParent(); // The instruction is gone now.
5697
5698 return BB;
5699}
5700
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005701/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
5702/// registers the function context.
5703void ARMTargetLowering::
5704SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
5705 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005706 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5707 DebugLoc dl = MI->getDebugLoc();
5708 MachineFunction *MF = MBB->getParent();
5709 MachineRegisterInfo *MRI = &MF->getRegInfo();
5710 MachineConstantPool *MCP = MF->getConstantPool();
5711 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5712 const Function *F = MF->getFunction();
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005713
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005714 bool isThumb = Subtarget->isThumb();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005715 bool isThumb2 = Subtarget->isThumb2();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005716
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005717 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005718 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005719 ARMConstantPoolValue *CPV =
5720 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
5721 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
5722
5723 const TargetRegisterClass *TRC =
5724 isThumb ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5725
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005726 // Grab constant pool and fixed stack memory operands.
5727 MachineMemOperand *CPMMO =
5728 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
5729 MachineMemOperand::MOLoad, 4, 4);
5730
5731 MachineMemOperand *FIMMOSt =
5732 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
5733 MachineMemOperand::MOStore, 4, 4);
5734
5735 // Load the address of the dispatch MBB into the jump buffer.
5736 if (isThumb2) {
5737 // Incoming value: jbuf
5738 // ldr.n r5, LCPI1_1
5739 // orr r5, r5, #1
5740 // add r5, pc
5741 // str r5, [$jbuf, #+4] ; &jbuf[1]
5742 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5743 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
5744 .addConstantPoolIndex(CPI)
5745 .addMemOperand(CPMMO));
5746 // Set the low bit because of thumb mode.
5747 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5748 AddDefaultCC(
5749 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
5750 .addReg(NewVReg1, RegState::Kill)
5751 .addImm(0x01)));
5752 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5753 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
5754 .addReg(NewVReg2, RegState::Kill)
5755 .addImm(PCLabelId);
5756 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
5757 .addReg(NewVReg3, RegState::Kill)
5758 .addFrameIndex(FI)
5759 .addImm(36) // &jbuf[1] :: pc
5760 .addMemOperand(FIMMOSt));
5761 } else if (isThumb) {
5762 // Incoming value: jbuf
5763 // ldr.n r1, LCPI1_4
5764 // add r1, pc
5765 // mov r2, #1
5766 // orrs r1, r2
5767 // add r2, $jbuf, #+4 ; &jbuf[1]
5768 // str r1, [r2]
5769 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5770 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
5771 .addConstantPoolIndex(CPI)
5772 .addMemOperand(CPMMO));
5773 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5774 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
5775 .addReg(NewVReg1, RegState::Kill)
5776 .addImm(PCLabelId);
5777 // Set the low bit because of thumb mode.
5778 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5779 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
5780 .addReg(ARM::CPSR, RegState::Define)
5781 .addImm(1));
5782 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5783 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
5784 .addReg(ARM::CPSR, RegState::Define)
5785 .addReg(NewVReg2, RegState::Kill)
5786 .addReg(NewVReg3, RegState::Kill));
5787 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5788 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
5789 .addFrameIndex(FI)
5790 .addImm(36)); // &jbuf[1] :: pc
5791 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
5792 .addReg(NewVReg4, RegState::Kill)
5793 .addReg(NewVReg5, RegState::Kill)
5794 .addImm(0)
5795 .addMemOperand(FIMMOSt));
5796 } else {
5797 // Incoming value: jbuf
5798 // ldr r1, LCPI1_1
5799 // add r1, pc, r1
5800 // str r1, [$jbuf, #+4] ; &jbuf[1]
5801 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5802 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
5803 .addConstantPoolIndex(CPI)
5804 .addImm(0)
5805 .addMemOperand(CPMMO));
5806 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5807 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
5808 .addReg(NewVReg1, RegState::Kill)
5809 .addImm(PCLabelId));
5810 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
5811 .addReg(NewVReg2, RegState::Kill)
5812 .addFrameIndex(FI)
5813 .addImm(36) // &jbuf[1] :: pc
5814 .addMemOperand(FIMMOSt));
5815 }
5816}
5817
5818MachineBasicBlock *ARMTargetLowering::
5819EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
5820 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5821 DebugLoc dl = MI->getDebugLoc();
5822 MachineFunction *MF = MBB->getParent();
5823 MachineRegisterInfo *MRI = &MF->getRegInfo();
5824 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5825 MachineFrameInfo *MFI = MF->getFrameInfo();
5826 int FI = MFI->getFunctionContextIndex();
5827
5828 const TargetRegisterClass *TRC =
5829 Subtarget->isThumb() ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5830
Bill Wendling04f15b42011-10-06 21:29:56 +00005831 // Get a mapping of the call site numbers to all of the landing pads they're
5832 // associated with.
Bill Wendling2a850152011-10-05 00:02:33 +00005833 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
5834 unsigned MaxCSNum = 0;
5835 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbachd4f020a2012-04-06 23:43:50 +00005836 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
5837 ++BB) {
Bill Wendling2a850152011-10-05 00:02:33 +00005838 if (!BB->isLandingPad()) continue;
5839
5840 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
5841 // pad.
5842 for (MachineBasicBlock::iterator
5843 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
5844 if (!II->isEHLabel()) continue;
5845
5846 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendling5cbef192011-10-05 23:28:57 +00005847 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling2a850152011-10-05 00:02:33 +00005848
Bill Wendling5cbef192011-10-05 23:28:57 +00005849 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
5850 for (SmallVectorImpl<unsigned>::iterator
5851 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
5852 CSI != CSE; ++CSI) {
5853 CallSiteNumToLPad[*CSI].push_back(BB);
5854 MaxCSNum = std::max(MaxCSNum, *CSI);
5855 }
Bill Wendling2a850152011-10-05 00:02:33 +00005856 break;
5857 }
5858 }
5859
5860 // Get an ordered list of the machine basic blocks for the jump table.
5861 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling2acf6382011-10-07 23:18:02 +00005862 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling2a850152011-10-05 00:02:33 +00005863 LPadList.reserve(CallSiteNumToLPad.size());
5864 for (unsigned I = 1; I <= MaxCSNum; ++I) {
5865 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
5866 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00005867 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling2a850152011-10-05 00:02:33 +00005868 LPadList.push_back(*II);
Bill Wendling2acf6382011-10-07 23:18:02 +00005869 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
5870 }
Bill Wendling2a850152011-10-05 00:02:33 +00005871 }
5872
Bill Wendling5cbef192011-10-05 23:28:57 +00005873 assert(!LPadList.empty() &&
5874 "No landing pad destinations for the dispatch jump table!");
5875
Bill Wendling04f15b42011-10-06 21:29:56 +00005876 // Create the jump table and associated information.
Bill Wendling2a850152011-10-05 00:02:33 +00005877 MachineJumpTableInfo *JTI =
5878 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
5879 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
5880 unsigned UId = AFI->createJumpTableUId();
5881
Bill Wendling04f15b42011-10-06 21:29:56 +00005882 // Create the MBBs for the dispatch code.
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005883
5884 // Shove the dispatch's address into the return slot in the function context.
5885 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
5886 DispatchBB->setIsLandingPad();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005887
Bill Wendlingbb734682011-10-05 00:39:32 +00005888 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Bill Wendling083a8eb2011-10-06 23:37:36 +00005889 BuildMI(TrapBB, dl, TII->get(Subtarget->isThumb() ? ARM::tTRAP : ARM::TRAP));
Bill Wendlingbb734682011-10-05 00:39:32 +00005890 DispatchBB->addSuccessor(TrapBB);
5891
5892 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
5893 DispatchBB->addSuccessor(DispContBB);
Bill Wendling2a850152011-10-05 00:02:33 +00005894
Bill Wendlinga48ed4f2011-10-17 21:32:56 +00005895 // Insert and MBBs.
Bill Wendling930193c2011-10-06 00:53:33 +00005896 MF->insert(MF->end(), DispatchBB);
5897 MF->insert(MF->end(), DispContBB);
5898 MF->insert(MF->end(), TrapBB);
Bill Wendling930193c2011-10-06 00:53:33 +00005899
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005900 // Insert code into the entry block that creates and registers the function
5901 // context.
5902 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
5903
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005904 MachineMemOperand *FIMMOLd =
Bill Wendling04f15b42011-10-06 21:29:56 +00005905 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendling083a8eb2011-10-06 23:37:36 +00005906 MachineMemOperand::MOLoad |
5907 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling930193c2011-10-06 00:53:33 +00005908
Bob Wilsonf4aea8f2011-12-22 23:39:48 +00005909 if (AFI->isThumb1OnlyFunction())
5910 BuildMI(DispatchBB, dl, TII->get(ARM::tInt_eh_sjlj_dispatchsetup));
5911 else if (!Subtarget->hasVFP2())
5912 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup_nofp));
Lang Hamesc0a9f822012-03-29 21:56:11 +00005913 else
Bob Wilsonf4aea8f2011-12-22 23:39:48 +00005914 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
Bob Wilsoneaab6ef2011-11-16 07:11:57 +00005915
Bill Wendling952cb502011-10-18 22:49:07 +00005916 unsigned NumLPads = LPadList.size();
Bill Wendling95ce2e92011-10-06 22:53:00 +00005917 if (Subtarget->isThumb2()) {
5918 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5919 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
5920 .addFrameIndex(FI)
5921 .addImm(4)
5922 .addMemOperand(FIMMOLd));
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005923
Bill Wendling952cb502011-10-18 22:49:07 +00005924 if (NumLPads < 256) {
5925 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
5926 .addReg(NewVReg1)
5927 .addImm(LPadList.size()));
5928 } else {
5929 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5930 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00005931 .addImm(NumLPads & 0xFFFF));
5932
5933 unsigned VReg2 = VReg1;
5934 if ((NumLPads & 0xFFFF0000) != 0) {
5935 VReg2 = MRI->createVirtualRegister(TRC);
5936 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
5937 .addReg(VReg1)
5938 .addImm(NumLPads >> 16));
5939 }
5940
Bill Wendling952cb502011-10-18 22:49:07 +00005941 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
5942 .addReg(NewVReg1)
5943 .addReg(VReg2));
5944 }
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005945
Bill Wendling95ce2e92011-10-06 22:53:00 +00005946 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
5947 .addMBB(TrapBB)
5948 .addImm(ARMCC::HI)
5949 .addReg(ARM::CPSR);
Bill Wendlingbb734682011-10-05 00:39:32 +00005950
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005951 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5952 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005953 .addJumpTableIndex(MJTI)
5954 .addImm(UId));
Bill Wendling2a850152011-10-05 00:02:33 +00005955
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005956 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005957 AddDefaultCC(
5958 AddDefaultPred(
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005959 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
5960 .addReg(NewVReg3, RegState::Kill)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005961 .addReg(NewVReg1)
5962 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
5963
5964 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005965 .addReg(NewVReg4, RegState::Kill)
Bill Wendling2a850152011-10-05 00:02:33 +00005966 .addReg(NewVReg1)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005967 .addJumpTableIndex(MJTI)
5968 .addImm(UId);
5969 } else if (Subtarget->isThumb()) {
Bill Wendling083a8eb2011-10-06 23:37:36 +00005970 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5971 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
5972 .addFrameIndex(FI)
5973 .addImm(1)
5974 .addMemOperand(FIMMOLd));
Bill Wendlingf1083d42011-10-07 22:08:37 +00005975
Bill Wendlinga5871dc2011-10-18 23:11:05 +00005976 if (NumLPads < 256) {
5977 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
5978 .addReg(NewVReg1)
5979 .addImm(NumLPads));
5980 } else {
5981 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling922ad782011-10-19 09:24:02 +00005982 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
5983 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
5984
5985 // MachineConstantPool wants an explicit alignment.
5986 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
5987 if (Align == 0)
5988 Align = getTargetData()->getTypeAllocSize(C->getType());
5989 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendlinga5871dc2011-10-18 23:11:05 +00005990
5991 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5992 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
5993 .addReg(VReg1, RegState::Define)
5994 .addConstantPoolIndex(Idx));
5995 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
5996 .addReg(NewVReg1)
5997 .addReg(VReg1));
5998 }
5999
Bill Wendling083a8eb2011-10-06 23:37:36 +00006000 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6001 .addMBB(TrapBB)
6002 .addImm(ARMCC::HI)
6003 .addReg(ARM::CPSR);
6004
6005 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6006 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6007 .addReg(ARM::CPSR, RegState::Define)
6008 .addReg(NewVReg1)
6009 .addImm(2));
6010
6011 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling217f0e92011-10-06 23:41:14 +00006012 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendling083a8eb2011-10-06 23:37:36 +00006013 .addJumpTableIndex(MJTI)
6014 .addImm(UId));
6015
6016 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6017 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6018 .addReg(ARM::CPSR, RegState::Define)
6019 .addReg(NewVReg2, RegState::Kill)
6020 .addReg(NewVReg3));
6021
6022 MachineMemOperand *JTMMOLd =
6023 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6024 MachineMemOperand::MOLoad, 4, 4);
6025
6026 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6027 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6028 .addReg(NewVReg4, RegState::Kill)
6029 .addImm(0)
6030 .addMemOperand(JTMMOLd));
6031
6032 unsigned NewVReg6 = MRI->createVirtualRegister(TRC);
6033 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6034 .addReg(ARM::CPSR, RegState::Define)
6035 .addReg(NewVReg5, RegState::Kill)
6036 .addReg(NewVReg3));
6037
6038 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6039 .addReg(NewVReg6, RegState::Kill)
6040 .addJumpTableIndex(MJTI)
6041 .addImm(UId);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006042 } else {
6043 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6044 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6045 .addFrameIndex(FI)
6046 .addImm(4)
6047 .addMemOperand(FIMMOLd));
Bill Wendling564392b2011-10-18 22:11:18 +00006048
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006049 if (NumLPads < 256) {
6050 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6051 .addReg(NewVReg1)
6052 .addImm(NumLPads));
Bill Wendling922ad782011-10-19 09:24:02 +00006053 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006054 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6055 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00006056 .addImm(NumLPads & 0xFFFF));
6057
6058 unsigned VReg2 = VReg1;
6059 if ((NumLPads & 0xFFFF0000) != 0) {
6060 VReg2 = MRI->createVirtualRegister(TRC);
6061 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6062 .addReg(VReg1)
6063 .addImm(NumLPads >> 16));
6064 }
6065
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006066 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6067 .addReg(NewVReg1)
6068 .addReg(VReg2));
Bill Wendling922ad782011-10-19 09:24:02 +00006069 } else {
6070 MachineConstantPool *ConstantPool = MF->getConstantPool();
6071 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6072 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6073
6074 // MachineConstantPool wants an explicit alignment.
6075 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
6076 if (Align == 0)
6077 Align = getTargetData()->getTypeAllocSize(C->getType());
6078 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6079
6080 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6081 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6082 .addReg(VReg1, RegState::Define)
Bill Wendling767f8be2011-10-20 20:37:11 +00006083 .addConstantPoolIndex(Idx)
6084 .addImm(0));
Bill Wendling922ad782011-10-19 09:24:02 +00006085 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6086 .addReg(NewVReg1)
6087 .addReg(VReg1, RegState::Kill));
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006088 }
6089
Bill Wendling95ce2e92011-10-06 22:53:00 +00006090 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6091 .addMBB(TrapBB)
6092 .addImm(ARMCC::HI)
6093 .addReg(ARM::CPSR);
Bill Wendling2a850152011-10-05 00:02:33 +00006094
Bill Wendling564392b2011-10-18 22:11:18 +00006095 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006096 AddDefaultCC(
Bill Wendling564392b2011-10-18 22:11:18 +00006097 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006098 .addReg(NewVReg1)
6099 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling564392b2011-10-18 22:11:18 +00006100 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6101 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006102 .addJumpTableIndex(MJTI)
6103 .addImm(UId));
6104
6105 MachineMemOperand *JTMMOLd =
6106 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6107 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling564392b2011-10-18 22:11:18 +00006108 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006109 AddDefaultPred(
Bill Wendling564392b2011-10-18 22:11:18 +00006110 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6111 .addReg(NewVReg3, RegState::Kill)
6112 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006113 .addImm(0)
6114 .addMemOperand(JTMMOLd));
6115
6116 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
Bill Wendling564392b2011-10-18 22:11:18 +00006117 .addReg(NewVReg5, RegState::Kill)
6118 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006119 .addJumpTableIndex(MJTI)
6120 .addImm(UId);
6121 }
Bill Wendling2a850152011-10-05 00:02:33 +00006122
Bill Wendlingbb734682011-10-05 00:39:32 +00006123 // Add the jump table entries as successors to the MBB.
Bill Wendling2acf6382011-10-07 23:18:02 +00006124 MachineBasicBlock *PrevMBB = 0;
Bill Wendlingbb734682011-10-05 00:39:32 +00006125 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006126 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6127 MachineBasicBlock *CurMBB = *I;
6128 if (PrevMBB != CurMBB)
6129 DispContBB->addSuccessor(CurMBB);
6130 PrevMBB = CurMBB;
6131 }
6132
Bill Wendling24bb9252011-10-17 05:25:09 +00006133 // N.B. the order the invoke BBs are processed in doesn't matter here.
Bill Wendling969c9ef2011-10-14 23:34:37 +00006134 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6135 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
Craig Topper015f2282012-03-04 03:33:22 +00006136 const uint16_t *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006137 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling2acf6382011-10-07 23:18:02 +00006138 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6139 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6140 MachineBasicBlock *BB = *I;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006141
6142 // Remove the landing pad successor from the invoke block and replace it
6143 // with the new dispatch block.
Bill Wendlingde39d862011-10-26 07:16:18 +00006144 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6145 BB->succ_end());
6146 while (!Successors.empty()) {
6147 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling2acf6382011-10-07 23:18:02 +00006148 if (SMBB->isLandingPad()) {
6149 BB->removeSuccessor(SMBB);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006150 MBBLPads.push_back(SMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006151 }
6152 }
6153
6154 BB->addSuccessor(DispatchBB);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006155
6156 // Find the invoke call and mark all of the callee-saved registers as
6157 // 'implicit defined' so that they're spilled. This prevents code from
6158 // moving instructions to before the EH block, where they will never be
6159 // executed.
6160 for (MachineBasicBlock::reverse_iterator
6161 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006162 if (!II->isCall()) continue;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006163
6164 DenseMap<unsigned, bool> DefRegs;
6165 for (MachineInstr::mop_iterator
6166 OI = II->operands_begin(), OE = II->operands_end();
6167 OI != OE; ++OI) {
6168 if (!OI->isReg()) continue;
6169 DefRegs[OI->getReg()] = true;
6170 }
6171
6172 MachineInstrBuilder MIB(&*II);
6173
Bill Wendling5d798592011-10-14 23:55:44 +00006174 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006175 unsigned Reg = SavedRegs[i];
6176 if (Subtarget->isThumb2() &&
6177 !ARM::tGPRRegisterClass->contains(Reg) &&
6178 !ARM::hGPRRegisterClass->contains(Reg))
6179 continue;
6180 else if (Subtarget->isThumb1Only() &&
6181 !ARM::tGPRRegisterClass->contains(Reg))
6182 continue;
6183 else if (!Subtarget->isThumb() &&
6184 !ARM::GPRRegisterClass->contains(Reg))
6185 continue;
6186 if (!DefRegs[Reg])
6187 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling5d798592011-10-14 23:55:44 +00006188 }
Bill Wendling969c9ef2011-10-14 23:34:37 +00006189
6190 break;
6191 }
Bill Wendling2acf6382011-10-07 23:18:02 +00006192 }
Bill Wendlingbb734682011-10-05 00:39:32 +00006193
Bill Wendlingf7b02072011-10-18 18:30:49 +00006194 // Mark all former landing pads as non-landing pads. The dispatch is the only
6195 // landing pad now.
6196 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6197 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6198 (*I)->setIsLandingPad(false);
6199
Bill Wendlingbb734682011-10-05 00:39:32 +00006200 // The instruction is gone now.
6201 MI->eraseFromParent();
6202
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006203 return MBB;
6204}
6205
Evan Cheng218977b2010-07-13 19:27:42 +00006206static
6207MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6208 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6209 E = MBB->succ_end(); I != E; ++I)
6210 if (*I != Succ)
6211 return *I;
6212 llvm_unreachable("Expecting a BB with two successors!");
6213}
6214
Jim Grosbache801dc42009-12-12 01:40:06 +00006215MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006216ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00006217 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006218 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00006219 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006220 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00006221 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00006222 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00006223 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00006224 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00006225 }
Jim Grosbachee2c2a42011-09-16 21:55:56 +00006226 // The Thumb2 pre-indexed stores have the same MI operands, they just
6227 // define them differently in the .td files from the isel patterns, so
6228 // they need pseudos.
6229 case ARM::t2STR_preidx:
6230 MI->setDesc(TII->get(ARM::t2STR_PRE));
6231 return BB;
6232 case ARM::t2STRB_preidx:
6233 MI->setDesc(TII->get(ARM::t2STRB_PRE));
6234 return BB;
6235 case ARM::t2STRH_preidx:
6236 MI->setDesc(TII->get(ARM::t2STRH_PRE));
6237 return BB;
6238
Jim Grosbach19dec202011-08-05 20:35:44 +00006239 case ARM::STRi_preidx:
6240 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00006241 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00006242 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
6243 // Decode the offset.
6244 unsigned Offset = MI->getOperand(4).getImm();
6245 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
6246 Offset = ARM_AM::getAM2Offset(Offset);
6247 if (isSub)
6248 Offset = -Offset;
6249
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006250 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00006251 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00006252 .addOperand(MI->getOperand(0)) // Rn_wb
6253 .addOperand(MI->getOperand(1)) // Rt
6254 .addOperand(MI->getOperand(2)) // Rn
6255 .addImm(Offset) // offset (skip GPR==zero_reg)
6256 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006257 .addOperand(MI->getOperand(6))
6258 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00006259 MI->eraseFromParent();
6260 return BB;
6261 }
6262 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00006263 case ARM::STRBr_preidx:
6264 case ARM::STRH_preidx: {
6265 unsigned NewOpc;
6266 switch (MI->getOpcode()) {
6267 default: llvm_unreachable("unexpected opcode!");
6268 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
6269 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
6270 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
6271 }
Jim Grosbach19dec202011-08-05 20:35:44 +00006272 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
6273 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
6274 MIB.addOperand(MI->getOperand(i));
6275 MI->eraseFromParent();
6276 return BB;
6277 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006278 case ARM::ATOMIC_LOAD_ADD_I8:
6279 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6280 case ARM::ATOMIC_LOAD_ADD_I16:
6281 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6282 case ARM::ATOMIC_LOAD_ADD_I32:
6283 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006284
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006285 case ARM::ATOMIC_LOAD_AND_I8:
6286 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6287 case ARM::ATOMIC_LOAD_AND_I16:
6288 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6289 case ARM::ATOMIC_LOAD_AND_I32:
6290 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006291
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006292 case ARM::ATOMIC_LOAD_OR_I8:
6293 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6294 case ARM::ATOMIC_LOAD_OR_I16:
6295 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6296 case ARM::ATOMIC_LOAD_OR_I32:
6297 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006298
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006299 case ARM::ATOMIC_LOAD_XOR_I8:
6300 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6301 case ARM::ATOMIC_LOAD_XOR_I16:
6302 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6303 case ARM::ATOMIC_LOAD_XOR_I32:
6304 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006305
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006306 case ARM::ATOMIC_LOAD_NAND_I8:
6307 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6308 case ARM::ATOMIC_LOAD_NAND_I16:
6309 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6310 case ARM::ATOMIC_LOAD_NAND_I32:
6311 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006312
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006313 case ARM::ATOMIC_LOAD_SUB_I8:
6314 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6315 case ARM::ATOMIC_LOAD_SUB_I16:
6316 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6317 case ARM::ATOMIC_LOAD_SUB_I32:
6318 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006319
Jim Grosbachf7da8822011-04-26 19:44:18 +00006320 case ARM::ATOMIC_LOAD_MIN_I8:
6321 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
6322 case ARM::ATOMIC_LOAD_MIN_I16:
6323 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
6324 case ARM::ATOMIC_LOAD_MIN_I32:
6325 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
6326
6327 case ARM::ATOMIC_LOAD_MAX_I8:
6328 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
6329 case ARM::ATOMIC_LOAD_MAX_I16:
6330 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
6331 case ARM::ATOMIC_LOAD_MAX_I32:
6332 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
6333
6334 case ARM::ATOMIC_LOAD_UMIN_I8:
6335 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
6336 case ARM::ATOMIC_LOAD_UMIN_I16:
6337 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
6338 case ARM::ATOMIC_LOAD_UMIN_I32:
6339 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
6340
6341 case ARM::ATOMIC_LOAD_UMAX_I8:
6342 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
6343 case ARM::ATOMIC_LOAD_UMAX_I16:
6344 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
6345 case ARM::ATOMIC_LOAD_UMAX_I32:
6346 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
6347
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006348 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
6349 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
6350 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00006351
6352 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
6353 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
6354 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006355
Eli Friedman2bdffe42011-08-31 00:31:29 +00006356
6357 case ARM::ATOMADD6432:
6358 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006359 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr,
6360 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006361 case ARM::ATOMSUB6432:
6362 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006363 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6364 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006365 case ARM::ATOMOR6432:
6366 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006367 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006368 case ARM::ATOMXOR6432:
6369 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006370 isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006371 case ARM::ATOMAND6432:
6372 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006373 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006374 case ARM::ATOMSWAP6432:
6375 return EmitAtomicBinary64(MI, BB, 0, 0, false);
Eli Friedman4d3f3292011-08-31 17:52:22 +00006376 case ARM::ATOMCMPXCHG6432:
6377 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
6378 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6379 /*NeedsCarry*/ false, /*IsCmpxchg*/true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006380
Evan Cheng007ea272009-08-12 05:17:19 +00006381 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00006382 // To "insert" a SELECT_CC instruction, we actually have to insert the
6383 // diamond control-flow pattern. The incoming instruction knows the
6384 // destination vreg to set, the condition code register to branch on, the
6385 // true/false values to select between, and a branch opcode to use.
6386 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006387 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00006388 ++It;
6389
6390 // thisMBB:
6391 // ...
6392 // TrueVal = ...
6393 // cmpTY ccX, r1, r2
6394 // bCC copy1MBB
6395 // fallthrough --> copy0MBB
6396 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006397 MachineFunction *F = BB->getParent();
6398 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6399 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00006400 F->insert(It, copy0MBB);
6401 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006402
6403 // Transfer the remainder of BB and its successor edges to sinkMBB.
6404 sinkMBB->splice(sinkMBB->begin(), BB,
6405 llvm::next(MachineBasicBlock::iterator(MI)),
6406 BB->end());
6407 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6408
Dan Gohman258c58c2010-07-06 15:49:48 +00006409 BB->addSuccessor(copy0MBB);
6410 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00006411
Dan Gohman14152b42010-07-06 20:24:04 +00006412 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
6413 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
6414
Evan Chenga8e29892007-01-19 07:51:42 +00006415 // copy0MBB:
6416 // %FalseValue = ...
6417 // # fallthrough to sinkMBB
6418 BB = copy0MBB;
6419
6420 // Update machine-CFG edges
6421 BB->addSuccessor(sinkMBB);
6422
6423 // sinkMBB:
6424 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6425 // ...
6426 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00006427 BuildMI(*BB, BB->begin(), dl,
6428 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00006429 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6430 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6431
Dan Gohman14152b42010-07-06 20:24:04 +00006432 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00006433 return BB;
6434 }
Evan Cheng86198642009-08-07 00:34:42 +00006435
Evan Cheng218977b2010-07-13 19:27:42 +00006436 case ARM::BCCi64:
6437 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00006438 // If there is an unconditional branch to the other successor, remove it.
6439 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00006440
Evan Cheng218977b2010-07-13 19:27:42 +00006441 // Compare both parts that make up the double comparison separately for
6442 // equality.
6443 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
6444
6445 unsigned LHS1 = MI->getOperand(1).getReg();
6446 unsigned LHS2 = MI->getOperand(2).getReg();
6447 if (RHSisZero) {
6448 AddDefaultPred(BuildMI(BB, dl,
6449 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6450 .addReg(LHS1).addImm(0));
6451 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6452 .addReg(LHS2).addImm(0)
6453 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6454 } else {
6455 unsigned RHS1 = MI->getOperand(3).getReg();
6456 unsigned RHS2 = MI->getOperand(4).getReg();
6457 AddDefaultPred(BuildMI(BB, dl,
6458 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6459 .addReg(LHS1).addReg(RHS1));
6460 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6461 .addReg(LHS2).addReg(RHS2)
6462 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6463 }
6464
6465 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
6466 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
6467 if (MI->getOperand(0).getImm() == ARMCC::NE)
6468 std::swap(destMBB, exitMBB);
6469
6470 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6471 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson51f6a7a2011-09-09 21:48:23 +00006472 if (isThumb2)
6473 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
6474 else
6475 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng218977b2010-07-13 19:27:42 +00006476
6477 MI->eraseFromParent(); // The pseudo instruction is gone now.
6478 return BB;
6479 }
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006480
Bill Wendling5bc85282011-10-17 20:37:20 +00006481 case ARM::Int_eh_sjlj_setjmp:
6482 case ARM::Int_eh_sjlj_setjmp_nofp:
6483 case ARM::tInt_eh_sjlj_setjmp:
6484 case ARM::t2Int_eh_sjlj_setjmp:
6485 case ARM::t2Int_eh_sjlj_setjmp_nofp:
6486 EmitSjLjDispatchBlock(MI, BB);
6487 return BB;
6488
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006489 case ARM::ABS:
6490 case ARM::t2ABS: {
6491 // To insert an ABS instruction, we have to insert the
6492 // diamond control-flow pattern. The incoming instruction knows the
6493 // source vreg to test against 0, the destination vreg to set,
6494 // the condition code register to branch on, the
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006495 // true/false values to select between, and a branch opcode to use.
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006496 // It transforms
6497 // V1 = ABS V0
6498 // into
6499 // V2 = MOVS V0
6500 // BCC (branch to SinkBB if V0 >= 0)
6501 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006502 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006503 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6504 MachineFunction::iterator BBI = BB;
6505 ++BBI;
6506 MachineFunction *Fn = BB->getParent();
6507 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6508 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6509 Fn->insert(BBI, RSBBB);
6510 Fn->insert(BBI, SinkBB);
6511
6512 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
6513 unsigned int ABSDstReg = MI->getOperand(0).getReg();
6514 bool isThumb2 = Subtarget->isThumb2();
6515 MachineRegisterInfo &MRI = Fn->getRegInfo();
6516 // In Thumb mode S must not be specified if source register is the SP or
6517 // PC and if destination register is the SP, so restrict register class
6518 unsigned NewMovDstReg = MRI.createVirtualRegister(
6519 isThumb2 ? ARM::rGPRRegisterClass : ARM::GPRRegisterClass);
6520 unsigned NewRsbDstReg = MRI.createVirtualRegister(
6521 isThumb2 ? ARM::rGPRRegisterClass : ARM::GPRRegisterClass);
6522
6523 // Transfer the remainder of BB and its successor edges to sinkMBB.
6524 SinkBB->splice(SinkBB->begin(), BB,
6525 llvm::next(MachineBasicBlock::iterator(MI)),
6526 BB->end());
6527 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
6528
6529 BB->addSuccessor(RSBBB);
6530 BB->addSuccessor(SinkBB);
6531
6532 // fall through to SinkMBB
6533 RSBBB->addSuccessor(SinkBB);
6534
6535 // insert a movs at the end of BB
6536 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVr : ARM::MOVr),
6537 NewMovDstReg)
6538 .addReg(ABSSrcReg, RegState::Kill)
6539 .addImm((unsigned)ARMCC::AL).addReg(0)
6540 .addReg(ARM::CPSR, RegState::Define);
6541
6542 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006543 BuildMI(BB, dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006544 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
6545 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
6546
6547 // insert rsbri in RSBBB
6548 // Note: BCC and rsbri will be converted into predicated rsbmi
6549 // by if-conversion pass
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006550 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006551 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
6552 .addReg(NewMovDstReg, RegState::Kill)
6553 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
6554
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006555 // insert PHI in SinkBB,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006556 // reuse ABSDstReg to not change uses of ABS instruction
6557 BuildMI(*SinkBB, SinkBB->begin(), dl,
6558 TII->get(ARM::PHI), ABSDstReg)
6559 .addReg(NewRsbDstReg).addMBB(RSBBB)
6560 .addReg(NewMovDstReg).addMBB(BB);
6561
6562 // remove ABS instruction
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006563 MI->eraseFromParent();
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006564
6565 // return last added BB
6566 return SinkBB;
6567 }
Evan Chenga8e29892007-01-19 07:51:42 +00006568 }
6569}
6570
Evan Cheng37fefc22011-08-30 19:09:48 +00006571void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
6572 SDNode *Node) const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006573 if (!MI->hasPostISelHook()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006574 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
6575 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
6576 return;
6577 }
6578
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006579 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick4815d562011-09-20 03:17:40 +00006580 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
6581 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
6582 // operand is still set to noreg. If needed, set the optional operand's
6583 // register to CPSR, and remove the redundant implicit def.
Andrew Trick3be654f2011-09-21 02:20:46 +00006584 //
Andrew Trick90b7b122011-10-18 19:18:52 +00006585 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick4815d562011-09-20 03:17:40 +00006586
Andrew Trick3be654f2011-09-21 02:20:46 +00006587 // Rename pseudo opcodes.
6588 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
6589 if (NewOpc) {
6590 const ARMBaseInstrInfo *TII =
6591 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick90b7b122011-10-18 19:18:52 +00006592 MCID = &TII->get(NewOpc);
6593
6594 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
6595 "converted opcode should be the same except for cc_out");
6596
6597 MI->setDesc(*MCID);
6598
6599 // Add the optional cc_out operand
6600 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick3be654f2011-09-21 02:20:46 +00006601 }
Andrew Trick90b7b122011-10-18 19:18:52 +00006602 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick4815d562011-09-20 03:17:40 +00006603
6604 // Any ARM instruction that sets the 's' bit should specify an optional
6605 // "cc_out" operand in the last operand position.
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006606 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006607 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006608 return;
6609 }
Andrew Trick3be654f2011-09-21 02:20:46 +00006610 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
6611 // since we already have an optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006612 bool definesCPSR = false;
6613 bool deadCPSR = false;
Andrew Trick90b7b122011-10-18 19:18:52 +00006614 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick4815d562011-09-20 03:17:40 +00006615 i != e; ++i) {
6616 const MachineOperand &MO = MI->getOperand(i);
6617 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
6618 definesCPSR = true;
6619 if (MO.isDead())
6620 deadCPSR = true;
6621 MI->RemoveOperand(i);
6622 break;
Evan Cheng37fefc22011-08-30 19:09:48 +00006623 }
6624 }
Andrew Trick4815d562011-09-20 03:17:40 +00006625 if (!definesCPSR) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006626 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006627 return;
6628 }
6629 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick3be654f2011-09-21 02:20:46 +00006630 if (deadCPSR) {
6631 assert(!MI->getOperand(ccOutIdx).getReg() &&
6632 "expect uninitialized optional cc_out operand");
Andrew Trick4815d562011-09-20 03:17:40 +00006633 return;
Andrew Trick3be654f2011-09-21 02:20:46 +00006634 }
Andrew Trick4815d562011-09-20 03:17:40 +00006635
Andrew Trick3be654f2011-09-21 02:20:46 +00006636 // If this instruction was defined with an optional CPSR def and its dag node
6637 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006638 MachineOperand &MO = MI->getOperand(ccOutIdx);
6639 MO.setReg(ARM::CPSR);
6640 MO.setIsDef(true);
Evan Cheng37fefc22011-08-30 19:09:48 +00006641}
6642
Evan Chenga8e29892007-01-19 07:51:42 +00006643//===----------------------------------------------------------------------===//
6644// ARM Optimization Hooks
6645//===----------------------------------------------------------------------===//
6646
Chris Lattnerd1980a52009-03-12 06:52:53 +00006647static
6648SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
6649 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00006650 SelectionDAG &DAG = DCI.DAG;
6651 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00006652 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00006653 unsigned Opc = N->getOpcode();
6654 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
6655 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
6656 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
6657 ISD::CondCode CC = ISD::SETCC_INVALID;
6658
6659 if (isSlctCC) {
6660 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
6661 } else {
6662 SDValue CCOp = Slct.getOperand(0);
6663 if (CCOp.getOpcode() == ISD::SETCC)
6664 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
6665 }
6666
6667 bool DoXform = false;
6668 bool InvCC = false;
6669 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
6670 "Bad input!");
6671
6672 if (LHS.getOpcode() == ISD::Constant &&
6673 cast<ConstantSDNode>(LHS)->isNullValue()) {
6674 DoXform = true;
6675 } else if (CC != ISD::SETCC_INVALID &&
6676 RHS.getOpcode() == ISD::Constant &&
6677 cast<ConstantSDNode>(RHS)->isNullValue()) {
6678 std::swap(LHS, RHS);
6679 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006680 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00006681 Op0.getOperand(0).getValueType();
6682 bool isInt = OpVT.isInteger();
6683 CC = ISD::getSetCCInverse(CC, isInt);
6684
6685 if (!TLI.isCondCodeLegal(CC, OpVT))
6686 return SDValue(); // Inverse operator isn't legal.
6687
6688 DoXform = true;
6689 InvCC = true;
6690 }
6691
6692 if (DoXform) {
6693 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
6694 if (isSlctCC)
6695 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
6696 Slct.getOperand(0), Slct.getOperand(1), CC);
6697 SDValue CCOp = Slct.getOperand(0);
6698 if (InvCC)
6699 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
6700 CCOp.getOperand(0), CCOp.getOperand(1), CC);
6701 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
6702 CCOp, OtherOp, Result);
6703 }
6704 return SDValue();
6705}
6706
Eric Christopherfa6f5912011-06-29 21:10:36 +00006707// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00006708// (only after legalization).
6709static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
6710 TargetLowering::DAGCombinerInfo &DCI,
6711 const ARMSubtarget *Subtarget) {
6712
6713 // Only perform optimization if after legalize, and if NEON is available. We
6714 // also expected both operands to be BUILD_VECTORs.
6715 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
6716 || N0.getOpcode() != ISD::BUILD_VECTOR
6717 || N1.getOpcode() != ISD::BUILD_VECTOR)
6718 return SDValue();
6719
6720 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
6721 EVT VT = N->getValueType(0);
6722 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
6723 return SDValue();
6724
6725 // Check that the vector operands are of the right form.
6726 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
6727 // operands, where N is the size of the formed vector.
6728 // Each EXTRACT_VECTOR should have the same input vector and odd or even
6729 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00006730
6731 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00006732 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00006733 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00006734 SDValue Vec = N0->getOperand(0)->getOperand(0);
6735 SDNode *V = Vec.getNode();
6736 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00006737
Eric Christopherfa6f5912011-06-29 21:10:36 +00006738 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00006739 // check to see if each of their operands are an EXTRACT_VECTOR with
6740 // the same vector and appropriate index.
6741 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
6742 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
6743 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00006744
Tanya Lattner189531f2011-06-14 23:48:48 +00006745 SDValue ExtVec0 = N0->getOperand(i);
6746 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00006747
Tanya Lattner189531f2011-06-14 23:48:48 +00006748 // First operand is the vector, verify its the same.
6749 if (V != ExtVec0->getOperand(0).getNode() ||
6750 V != ExtVec1->getOperand(0).getNode())
6751 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00006752
Tanya Lattner189531f2011-06-14 23:48:48 +00006753 // Second is the constant, verify its correct.
6754 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
6755 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00006756
Tanya Lattner189531f2011-06-14 23:48:48 +00006757 // For the constant, we want to see all the even or all the odd.
6758 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
6759 || C1->getZExtValue() != nextIndex+1)
6760 return SDValue();
6761
6762 // Increment index.
6763 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00006764 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00006765 return SDValue();
6766 }
6767
6768 // Create VPADDL node.
6769 SelectionDAG &DAG = DCI.DAG;
6770 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00006771
6772 // Build operand list.
6773 SmallVector<SDValue, 8> Ops;
6774 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
6775 TLI.getPointerTy()));
6776
6777 // Input is the vector.
6778 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00006779
Tanya Lattner189531f2011-06-14 23:48:48 +00006780 // Get widened type and narrowed type.
6781 MVT widenType;
6782 unsigned numElem = VT.getVectorNumElements();
6783 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
6784 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
6785 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
6786 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
6787 default:
Craig Topperbc219812012-02-07 02:50:20 +00006788 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattner189531f2011-06-14 23:48:48 +00006789 }
6790
6791 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
6792 widenType, &Ops[0], Ops.size());
6793 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
6794}
6795
Bob Wilson3d5792a2010-07-29 20:34:14 +00006796/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
6797/// operands N0 and N1. This is a helper for PerformADDCombine that is
6798/// called with the default operands, and if that fails, with commuted
6799/// operands.
6800static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00006801 TargetLowering::DAGCombinerInfo &DCI,
6802 const ARMSubtarget *Subtarget){
6803
6804 // Attempt to create vpaddl for this add.
6805 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
6806 if (Result.getNode())
6807 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00006808
Chris Lattnerd1980a52009-03-12 06:52:53 +00006809 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
6810 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
6811 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
6812 if (Result.getNode()) return Result;
6813 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00006814 return SDValue();
6815}
6816
Bob Wilson3d5792a2010-07-29 20:34:14 +00006817/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
6818///
6819static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00006820 TargetLowering::DAGCombinerInfo &DCI,
6821 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00006822 SDValue N0 = N->getOperand(0);
6823 SDValue N1 = N->getOperand(1);
6824
6825 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00006826 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00006827 if (Result.getNode())
6828 return Result;
6829
6830 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00006831 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00006832}
6833
Chris Lattnerd1980a52009-03-12 06:52:53 +00006834/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00006835///
Chris Lattnerd1980a52009-03-12 06:52:53 +00006836static SDValue PerformSUBCombine(SDNode *N,
6837 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00006838 SDValue N0 = N->getOperand(0);
6839 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00006840
Chris Lattnerd1980a52009-03-12 06:52:53 +00006841 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
6842 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
6843 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
6844 if (Result.getNode()) return Result;
6845 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00006846
Chris Lattnerd1980a52009-03-12 06:52:53 +00006847 return SDValue();
6848}
6849
Evan Cheng463d3582011-03-31 19:38:48 +00006850/// PerformVMULCombine
6851/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
6852/// special multiplier accumulator forwarding.
6853/// vmul d3, d0, d2
6854/// vmla d3, d1, d2
6855/// is faster than
6856/// vadd d3, d0, d1
6857/// vmul d3, d3, d2
6858static SDValue PerformVMULCombine(SDNode *N,
6859 TargetLowering::DAGCombinerInfo &DCI,
6860 const ARMSubtarget *Subtarget) {
6861 if (!Subtarget->hasVMLxForwarding())
6862 return SDValue();
6863
6864 SelectionDAG &DAG = DCI.DAG;
6865 SDValue N0 = N->getOperand(0);
6866 SDValue N1 = N->getOperand(1);
6867 unsigned Opcode = N0.getOpcode();
6868 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
6869 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00006870 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00006871 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
6872 Opcode != ISD::FADD && Opcode != ISD::FSUB)
6873 return SDValue();
6874 std::swap(N0, N1);
6875 }
6876
6877 EVT VT = N->getValueType(0);
6878 DebugLoc DL = N->getDebugLoc();
6879 SDValue N00 = N0->getOperand(0);
6880 SDValue N01 = N0->getOperand(1);
6881 return DAG.getNode(Opcode, DL, VT,
6882 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
6883 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
6884}
6885
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006886static SDValue PerformMULCombine(SDNode *N,
6887 TargetLowering::DAGCombinerInfo &DCI,
6888 const ARMSubtarget *Subtarget) {
6889 SelectionDAG &DAG = DCI.DAG;
6890
6891 if (Subtarget->isThumb1Only())
6892 return SDValue();
6893
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006894 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
6895 return SDValue();
6896
6897 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00006898 if (VT.is64BitVector() || VT.is128BitVector())
6899 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006900 if (VT != MVT::i32)
6901 return SDValue();
6902
6903 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
6904 if (!C)
6905 return SDValue();
6906
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00006907 int64_t MulAmt = C->getSExtValue();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006908 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00006909
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006910 ShiftAmt = ShiftAmt & (32 - 1);
6911 SDValue V = N->getOperand(0);
6912 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006913
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006914 SDValue Res;
6915 MulAmt >>= ShiftAmt;
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00006916
6917 if (MulAmt >= 0) {
6918 if (isPowerOf2_32(MulAmt - 1)) {
6919 // (mul x, 2^N + 1) => (add (shl x, N), x)
6920 Res = DAG.getNode(ISD::ADD, DL, VT,
6921 V,
6922 DAG.getNode(ISD::SHL, DL, VT,
6923 V,
6924 DAG.getConstant(Log2_32(MulAmt - 1),
6925 MVT::i32)));
6926 } else if (isPowerOf2_32(MulAmt + 1)) {
6927 // (mul x, 2^N - 1) => (sub (shl x, N), x)
6928 Res = DAG.getNode(ISD::SUB, DL, VT,
6929 DAG.getNode(ISD::SHL, DL, VT,
6930 V,
6931 DAG.getConstant(Log2_32(MulAmt + 1),
6932 MVT::i32)),
6933 V);
6934 } else
6935 return SDValue();
6936 } else {
6937 uint64_t MulAmtAbs = -MulAmt;
6938 if (isPowerOf2_32(MulAmtAbs + 1)) {
6939 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
6940 Res = DAG.getNode(ISD::SUB, DL, VT,
6941 V,
6942 DAG.getNode(ISD::SHL, DL, VT,
6943 V,
6944 DAG.getConstant(Log2_32(MulAmtAbs + 1),
6945 MVT::i32)));
6946 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
6947 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
6948 Res = DAG.getNode(ISD::ADD, DL, VT,
6949 V,
6950 DAG.getNode(ISD::SHL, DL, VT,
6951 V,
6952 DAG.getConstant(Log2_32(MulAmtAbs-1),
6953 MVT::i32)));
6954 Res = DAG.getNode(ISD::SUB, DL, VT,
6955 DAG.getConstant(0, MVT::i32),Res);
6956
6957 } else
6958 return SDValue();
6959 }
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006960
6961 if (ShiftAmt != 0)
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00006962 Res = DAG.getNode(ISD::SHL, DL, VT,
6963 Res, DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006964
6965 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006966 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006967 return SDValue();
6968}
6969
Evan Chengc892aeb2012-02-23 01:19:06 +00006970static bool isCMOVWithZeroOrAllOnesLHS(SDValue N, bool AllOnes) {
6971 if (N.getOpcode() != ARMISD::CMOV || !N.getNode()->hasOneUse())
6972 return false;
6973
6974 SDValue FalseVal = N.getOperand(0);
6975 ConstantSDNode *C = dyn_cast<ConstantSDNode>(FalseVal);
6976 if (!C)
6977 return false;
6978 if (AllOnes)
6979 return C->isAllOnesValue();
6980 return C->isNullValue();
6981}
6982
6983/// formConditionalOp - Combine an operation with a conditional move operand
6984/// to form a conditional op. e.g. (or x, (cmov 0, y, cond)) => (or.cond x, y)
6985/// (and x, (cmov -1, y, cond)) => (and.cond, x, y)
6986static SDValue formConditionalOp(SDNode *N, SelectionDAG &DAG,
6987 bool Commutable) {
6988 SDValue N0 = N->getOperand(0);
6989 SDValue N1 = N->getOperand(1);
6990
6991 bool isAND = N->getOpcode() == ISD::AND;
6992 bool isCand = isCMOVWithZeroOrAllOnesLHS(N1, isAND);
6993 if (!isCand && Commutable) {
6994 isCand = isCMOVWithZeroOrAllOnesLHS(N0, isAND);
6995 if (isCand)
6996 std::swap(N0, N1);
6997 }
6998 if (!isCand)
6999 return SDValue();
7000
7001 unsigned Opc = 0;
7002 switch (N->getOpcode()) {
7003 default: llvm_unreachable("Unexpected node");
7004 case ISD::AND: Opc = ARMISD::CAND; break;
7005 case ISD::OR: Opc = ARMISD::COR; break;
7006 case ISD::XOR: Opc = ARMISD::CXOR; break;
7007 }
7008 return DAG.getNode(Opc, N->getDebugLoc(), N->getValueType(0), N0,
7009 N1.getOperand(1), N1.getOperand(2), N1.getOperand(3),
7010 N1.getOperand(4));
7011}
7012
Owen Anderson080c0922010-11-05 19:27:46 +00007013static SDValue PerformANDCombine(SDNode *N,
Evan Chengc892aeb2012-02-23 01:19:06 +00007014 TargetLowering::DAGCombinerInfo &DCI,
7015 const ARMSubtarget *Subtarget) {
Owen Anderson76706012011-04-05 21:48:57 +00007016
Owen Anderson080c0922010-11-05 19:27:46 +00007017 // Attempt to use immediate-form VBIC
7018 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
7019 DebugLoc dl = N->getDebugLoc();
7020 EVT VT = N->getValueType(0);
7021 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007022
Tanya Lattner0433b212011-04-07 15:24:20 +00007023 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7024 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00007025
Owen Anderson080c0922010-11-05 19:27:46 +00007026 APInt SplatBits, SplatUndef;
7027 unsigned SplatBitSize;
7028 bool HasAnyUndefs;
7029 if (BVN &&
7030 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7031 if (SplatBitSize <= 64) {
7032 EVT VbicVT;
7033 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
7034 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007035 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00007036 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00007037 if (Val.getNode()) {
7038 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007039 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00007040 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007041 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00007042 }
7043 }
7044 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007045
Evan Chengc892aeb2012-02-23 01:19:06 +00007046 if (!Subtarget->isThumb1Only()) {
7047 // (and x, (cmov -1, y, cond)) => (and.cond x, y)
7048 SDValue CAND = formConditionalOp(N, DAG, true);
7049 if (CAND.getNode())
7050 return CAND;
7051 }
7052
Owen Anderson080c0922010-11-05 19:27:46 +00007053 return SDValue();
7054}
7055
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007056/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
7057static SDValue PerformORCombine(SDNode *N,
7058 TargetLowering::DAGCombinerInfo &DCI,
7059 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00007060 // Attempt to use immediate-form VORR
7061 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
7062 DebugLoc dl = N->getDebugLoc();
7063 EVT VT = N->getValueType(0);
7064 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007065
Tanya Lattner0433b212011-04-07 15:24:20 +00007066 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7067 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00007068
Owen Anderson60f48702010-11-03 23:15:26 +00007069 APInt SplatBits, SplatUndef;
7070 unsigned SplatBitSize;
7071 bool HasAnyUndefs;
7072 if (BVN && Subtarget->hasNEON() &&
7073 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7074 if (SplatBitSize <= 64) {
7075 EVT VorrVT;
7076 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
7077 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00007078 DAG, VorrVT, VT.is128BitVector(),
7079 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00007080 if (Val.getNode()) {
7081 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007082 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00007083 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007084 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00007085 }
7086 }
7087 }
7088
Evan Chengc892aeb2012-02-23 01:19:06 +00007089 if (!Subtarget->isThumb1Only()) {
7090 // (or x, (cmov 0, y, cond)) => (or.cond x, y)
7091 SDValue COR = formConditionalOp(N, DAG, true);
7092 if (COR.getNode())
7093 return COR;
7094 }
7095
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007096 SDValue N0 = N->getOperand(0);
7097 if (N0.getOpcode() != ISD::AND)
7098 return SDValue();
7099 SDValue N1 = N->getOperand(1);
7100
7101 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
7102 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
7103 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
7104 APInt SplatUndef;
7105 unsigned SplatBitSize;
7106 bool HasAnyUndefs;
7107
7108 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
7109 APInt SplatBits0;
7110 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
7111 HasAnyUndefs) && !HasAnyUndefs) {
7112 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
7113 APInt SplatBits1;
7114 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
7115 HasAnyUndefs) && !HasAnyUndefs &&
7116 SplatBits0 == ~SplatBits1) {
7117 // Canonicalize the vector type to make instruction selection simpler.
7118 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
7119 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
7120 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00007121 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007122 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
7123 }
7124 }
7125 }
7126
Jim Grosbach54238562010-07-17 03:30:54 +00007127 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
7128 // reasonable.
7129
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007130 // BFI is only available on V6T2+
7131 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
7132 return SDValue();
7133
Jim Grosbach54238562010-07-17 03:30:54 +00007134 DebugLoc DL = N->getDebugLoc();
7135 // 1) or (and A, mask), val => ARMbfi A, val, mask
7136 // iff (val & mask) == val
7137 //
7138 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
7139 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00007140 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00007141 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00007142 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00007143 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007144
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007145 if (VT != MVT::i32)
7146 return SDValue();
7147
Evan Cheng30fb13f2010-12-13 20:32:54 +00007148 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00007149
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007150 // The value and the mask need to be constants so we can verify this is
7151 // actually a bitfield set. If the mask is 0xffff, we can do better
7152 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00007153 SDValue MaskOp = N0.getOperand(1);
7154 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
7155 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007156 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00007157 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007158 if (Mask == 0xffff)
7159 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007160 SDValue Res;
7161 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00007162 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
7163 if (N1C) {
7164 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00007165 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00007166 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007167
Evan Chenga9688c42010-12-11 04:11:38 +00007168 if (ARM::isBitFieldInvertedMask(Mask)) {
7169 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007170
Evan Cheng30fb13f2010-12-13 20:32:54 +00007171 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00007172 DAG.getConstant(Val, MVT::i32),
7173 DAG.getConstant(Mask, MVT::i32));
7174
7175 // Do not add new nodes to DAG combiner worklist.
7176 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007177 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00007178 }
Jim Grosbach54238562010-07-17 03:30:54 +00007179 } else if (N1.getOpcode() == ISD::AND) {
7180 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00007181 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7182 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00007183 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00007184 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007185
Eric Christopher29aeed12011-03-26 01:21:03 +00007186 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
7187 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00007188 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007189 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007190 // The pack halfword instruction works better for masks that fit it,
7191 // so use that when it's available.
7192 if (Subtarget->hasT2ExtractPack() &&
7193 (Mask == 0xffff || Mask == 0xffff0000))
7194 return SDValue();
7195 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00007196 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00007197 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00007198 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00007199 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00007200 DAG.getConstant(Mask, MVT::i32));
7201 // Do not add new nodes to DAG combiner worklist.
7202 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007203 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007204 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007205 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007206 // The pack halfword instruction works better for masks that fit it,
7207 // so use that when it's available.
7208 if (Subtarget->hasT2ExtractPack() &&
7209 (Mask2 == 0xffff || Mask2 == 0xffff0000))
7210 return SDValue();
7211 // 2b
7212 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007213 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00007214 DAG.getConstant(lsb, MVT::i32));
7215 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00007216 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00007217 // Do not add new nodes to DAG combiner worklist.
7218 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007219 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007220 }
7221 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007222
Evan Cheng30fb13f2010-12-13 20:32:54 +00007223 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
7224 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
7225 ARM::isBitFieldInvertedMask(~Mask)) {
7226 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
7227 // where lsb(mask) == #shamt and masked bits of B are known zero.
7228 SDValue ShAmt = N00.getOperand(1);
7229 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
7230 unsigned LSB = CountTrailingZeros_32(Mask);
7231 if (ShAmtC != LSB)
7232 return SDValue();
7233
7234 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
7235 DAG.getConstant(~Mask, MVT::i32));
7236
7237 // Do not add new nodes to DAG combiner worklist.
7238 DCI.CombineTo(N, Res, false);
7239 }
7240
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007241 return SDValue();
7242}
7243
Evan Chengc892aeb2012-02-23 01:19:06 +00007244static SDValue PerformXORCombine(SDNode *N,
7245 TargetLowering::DAGCombinerInfo &DCI,
7246 const ARMSubtarget *Subtarget) {
7247 EVT VT = N->getValueType(0);
7248 SelectionDAG &DAG = DCI.DAG;
7249
7250 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7251 return SDValue();
7252
7253 if (!Subtarget->isThumb1Only()) {
7254 // (xor x, (cmov 0, y, cond)) => (xor.cond x, y)
7255 SDValue CXOR = formConditionalOp(N, DAG, true);
7256 if (CXOR.getNode())
7257 return CXOR;
7258 }
7259
7260 return SDValue();
7261}
7262
Evan Chengbf188ae2011-06-15 01:12:31 +00007263/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
7264/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00007265static SDValue PerformBFICombine(SDNode *N,
7266 TargetLowering::DAGCombinerInfo &DCI) {
7267 SDValue N1 = N->getOperand(1);
7268 if (N1.getOpcode() == ISD::AND) {
7269 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7270 if (!N11C)
7271 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007272 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
7273 unsigned LSB = CountTrailingZeros_32(~InvMask);
7274 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
7275 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00007276 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007277 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00007278 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
7279 N->getOperand(0), N1.getOperand(0),
7280 N->getOperand(2));
7281 }
7282 return SDValue();
7283}
7284
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007285/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
7286/// ARMISD::VMOVRRD.
7287static SDValue PerformVMOVRRDCombine(SDNode *N,
7288 TargetLowering::DAGCombinerInfo &DCI) {
7289 // vmovrrd(vmovdrr x, y) -> x,y
7290 SDValue InDouble = N->getOperand(0);
7291 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
7292 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00007293
7294 // vmovrrd(load f64) -> (load i32), (load i32)
7295 SDNode *InNode = InDouble.getNode();
7296 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
7297 InNode->getValueType(0) == MVT::f64 &&
7298 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
7299 !cast<LoadSDNode>(InNode)->isVolatile()) {
7300 // TODO: Should this be done for non-FrameIndex operands?
7301 LoadSDNode *LD = cast<LoadSDNode>(InNode);
7302
7303 SelectionDAG &DAG = DCI.DAG;
7304 DebugLoc DL = LD->getDebugLoc();
7305 SDValue BasePtr = LD->getBasePtr();
7306 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
7307 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007308 LD->isNonTemporal(), LD->isInvariant(),
7309 LD->getAlignment());
Cameron Zwarich4071a712011-04-02 02:40:43 +00007310
7311 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7312 DAG.getConstant(4, MVT::i32));
7313 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
7314 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007315 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich4071a712011-04-02 02:40:43 +00007316 std::min(4U, LD->getAlignment() / 2));
7317
7318 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
7319 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
7320 DCI.RemoveFromWorklist(LD);
7321 DAG.DeleteNode(LD);
7322 return Result;
7323 }
7324
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007325 return SDValue();
7326}
7327
7328/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
7329/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
7330static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
7331 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
7332 SDValue Op0 = N->getOperand(0);
7333 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007334 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007335 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007336 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007337 Op1 = Op1.getOperand(0);
7338 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
7339 Op0.getNode() == Op1.getNode() &&
7340 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007341 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007342 N->getValueType(0), Op0.getOperand(0));
7343 return SDValue();
7344}
7345
Bob Wilson31600902010-12-21 06:43:19 +00007346/// PerformSTORECombine - Target-specific dag combine xforms for
7347/// ISD::STORE.
7348static SDValue PerformSTORECombine(SDNode *N,
7349 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson31600902010-12-21 06:43:19 +00007350 StoreSDNode *St = cast<StoreSDNode>(N);
Chad Rosier7f354552012-04-09 20:32:02 +00007351 if (St->isVolatile())
7352 return SDValue();
7353
7354 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
7355 // pack all of the elements in one place. Next, store to memory in fewer
7356 // chunks.
Bob Wilson31600902010-12-21 06:43:19 +00007357 SDValue StVal = St->getValue();
Chad Rosier7f354552012-04-09 20:32:02 +00007358 EVT VT = StVal.getValueType();
7359 if (St->isTruncatingStore() && VT.isVector()) {
7360 SelectionDAG &DAG = DCI.DAG;
7361 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7362 EVT StVT = St->getMemoryVT();
7363 unsigned NumElems = VT.getVectorNumElements();
7364 assert(StVT != VT && "Cannot truncate to the same type");
7365 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
7366 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
7367
7368 // From, To sizes and ElemCount must be pow of two
7369 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
7370
7371 // We are going to use the original vector elt for storing.
7372 // Accumulated smaller vector elements must be a multiple of the store size.
7373 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
7374
7375 unsigned SizeRatio = FromEltSz / ToEltSz;
7376 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
7377
7378 // Create a type on which we perform the shuffle.
7379 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
7380 NumElems*SizeRatio);
7381 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
7382
7383 DebugLoc DL = St->getDebugLoc();
7384 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
7385 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
7386 for (unsigned i = 0; i < NumElems; ++i) ShuffleVec[i] = i * SizeRatio;
7387
7388 // Can't shuffle using an illegal type.
7389 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
7390
7391 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
7392 DAG.getUNDEF(WideVec.getValueType()),
7393 ShuffleVec.data());
7394 // At this point all of the data is stored at the bottom of the
7395 // register. We now need to save it to mem.
7396
7397 // Find the largest store unit
7398 MVT StoreType = MVT::i8;
7399 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
7400 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
7401 MVT Tp = (MVT::SimpleValueType)tp;
7402 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
7403 StoreType = Tp;
7404 }
7405 // Didn't find a legal store type.
7406 if (!TLI.isTypeLegal(StoreType))
7407 return SDValue();
7408
7409 // Bitcast the original vector into a vector of store-size units
7410 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
7411 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
7412 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
7413 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
7414 SmallVector<SDValue, 8> Chains;
7415 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
7416 TLI.getPointerTy());
7417 SDValue BasePtr = St->getBasePtr();
7418
7419 // Perform one or more big stores into memory.
7420 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
7421 for (unsigned I = 0; I < E; I++) {
7422 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
7423 StoreType, ShuffWide,
7424 DAG.getIntPtrConstant(I));
7425 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
7426 St->getPointerInfo(), St->isVolatile(),
7427 St->isNonTemporal(), St->getAlignment());
7428 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
7429 Increment);
7430 Chains.push_back(Ch);
7431 }
7432 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, &Chains[0],
7433 Chains.size());
7434 }
7435
7436 if (!ISD::isNormalStore(St))
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007437 return SDValue();
7438
Chad Rosier96b66d62012-04-09 19:38:15 +00007439 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
7440 // ARM stores of arguments in the same cache line.
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007441 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
Chad Rosier96b66d62012-04-09 19:38:15 +00007442 StVal.getNode()->hasOneUse()) {
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007443 SelectionDAG &DAG = DCI.DAG;
7444 DebugLoc DL = St->getDebugLoc();
7445 SDValue BasePtr = St->getBasePtr();
7446 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
7447 StVal.getNode()->getOperand(0), BasePtr,
7448 St->getPointerInfo(), St->isVolatile(),
7449 St->isNonTemporal(), St->getAlignment());
7450
7451 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7452 DAG.getConstant(4, MVT::i32));
7453 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
7454 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
7455 St->isNonTemporal(),
7456 std::min(4U, St->getAlignment() / 2));
7457 }
7458
7459 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00007460 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
7461 return SDValue();
7462
Chad Rosier96b66d62012-04-09 19:38:15 +00007463 // Bitcast an i64 store extracted from a vector to f64.
7464 // Otherwise, the i64 value will be legalized to a pair of i32 values.
Bob Wilson31600902010-12-21 06:43:19 +00007465 SelectionDAG &DAG = DCI.DAG;
7466 DebugLoc dl = StVal.getDebugLoc();
7467 SDValue IntVec = StVal.getOperand(0);
7468 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7469 IntVec.getValueType().getVectorNumElements());
7470 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
7471 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
7472 Vec, StVal.getOperand(1));
7473 dl = N->getDebugLoc();
7474 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
7475 // Make the DAGCombiner fold the bitcasts.
7476 DCI.AddToWorklist(Vec.getNode());
7477 DCI.AddToWorklist(ExtElt.getNode());
7478 DCI.AddToWorklist(V.getNode());
7479 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
7480 St->getPointerInfo(), St->isVolatile(),
7481 St->isNonTemporal(), St->getAlignment(),
7482 St->getTBAAInfo());
7483}
7484
7485/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
7486/// are normal, non-volatile loads. If so, it is profitable to bitcast an
7487/// i64 vector to have f64 elements, since the value can then be loaded
7488/// directly into a VFP register.
7489static bool hasNormalLoadOperand(SDNode *N) {
7490 unsigned NumElts = N->getValueType(0).getVectorNumElements();
7491 for (unsigned i = 0; i < NumElts; ++i) {
7492 SDNode *Elt = N->getOperand(i).getNode();
7493 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
7494 return true;
7495 }
7496 return false;
7497}
7498
Bob Wilson75f02882010-09-17 22:59:05 +00007499/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
7500/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00007501static SDValue PerformBUILD_VECTORCombine(SDNode *N,
7502 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00007503 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
7504 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
7505 // into a pair of GPRs, which is fine when the value is used as a scalar,
7506 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00007507 SelectionDAG &DAG = DCI.DAG;
7508 if (N->getNumOperands() == 2) {
7509 SDValue RV = PerformVMOVDRRCombine(N, DAG);
7510 if (RV.getNode())
7511 return RV;
7512 }
Bob Wilson75f02882010-09-17 22:59:05 +00007513
Bob Wilson31600902010-12-21 06:43:19 +00007514 // Load i64 elements as f64 values so that type legalization does not split
7515 // them up into i32 values.
7516 EVT VT = N->getValueType(0);
7517 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
7518 return SDValue();
7519 DebugLoc dl = N->getDebugLoc();
7520 SmallVector<SDValue, 8> Ops;
7521 unsigned NumElts = VT.getVectorNumElements();
7522 for (unsigned i = 0; i < NumElts; ++i) {
7523 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
7524 Ops.push_back(V);
7525 // Make the DAGCombiner fold the bitcast.
7526 DCI.AddToWorklist(V.getNode());
7527 }
7528 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
7529 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
7530 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
7531}
7532
7533/// PerformInsertEltCombine - Target-specific dag combine xforms for
7534/// ISD::INSERT_VECTOR_ELT.
7535static SDValue PerformInsertEltCombine(SDNode *N,
7536 TargetLowering::DAGCombinerInfo &DCI) {
7537 // Bitcast an i64 load inserted into a vector to f64.
7538 // Otherwise, the i64 value will be legalized to a pair of i32 values.
7539 EVT VT = N->getValueType(0);
7540 SDNode *Elt = N->getOperand(1).getNode();
7541 if (VT.getVectorElementType() != MVT::i64 ||
7542 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
7543 return SDValue();
7544
7545 SelectionDAG &DAG = DCI.DAG;
7546 DebugLoc dl = N->getDebugLoc();
7547 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7548 VT.getVectorNumElements());
7549 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
7550 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
7551 // Make the DAGCombiner fold the bitcasts.
7552 DCI.AddToWorklist(Vec.getNode());
7553 DCI.AddToWorklist(V.getNode());
7554 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
7555 Vec, V, N->getOperand(2));
7556 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00007557}
7558
Bob Wilsonf20700c2010-10-27 20:38:28 +00007559/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
7560/// ISD::VECTOR_SHUFFLE.
7561static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
7562 // The LLVM shufflevector instruction does not require the shuffle mask
7563 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
7564 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
7565 // operands do not match the mask length, they are extended by concatenating
7566 // them with undef vectors. That is probably the right thing for other
7567 // targets, but for NEON it is better to concatenate two double-register
7568 // size vector operands into a single quad-register size vector. Do that
7569 // transformation here:
7570 // shuffle(concat(v1, undef), concat(v2, undef)) ->
7571 // shuffle(concat(v1, v2), undef)
7572 SDValue Op0 = N->getOperand(0);
7573 SDValue Op1 = N->getOperand(1);
7574 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
7575 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
7576 Op0.getNumOperands() != 2 ||
7577 Op1.getNumOperands() != 2)
7578 return SDValue();
7579 SDValue Concat0Op1 = Op0.getOperand(1);
7580 SDValue Concat1Op1 = Op1.getOperand(1);
7581 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
7582 Concat1Op1.getOpcode() != ISD::UNDEF)
7583 return SDValue();
7584 // Skip the transformation if any of the types are illegal.
7585 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7586 EVT VT = N->getValueType(0);
7587 if (!TLI.isTypeLegal(VT) ||
7588 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
7589 !TLI.isTypeLegal(Concat1Op1.getValueType()))
7590 return SDValue();
7591
7592 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
7593 Op0.getOperand(0), Op1.getOperand(0));
7594 // Translate the shuffle mask.
7595 SmallVector<int, 16> NewMask;
7596 unsigned NumElts = VT.getVectorNumElements();
7597 unsigned HalfElts = NumElts/2;
7598 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
7599 for (unsigned n = 0; n < NumElts; ++n) {
7600 int MaskElt = SVN->getMaskElt(n);
7601 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007602 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00007603 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007604 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00007605 NewElt = HalfElts + MaskElt - NumElts;
7606 NewMask.push_back(NewElt);
7607 }
7608 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
7609 DAG.getUNDEF(VT), NewMask.data());
7610}
7611
Bob Wilson1c3ef902011-02-07 17:43:21 +00007612/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
7613/// NEON load/store intrinsics to merge base address updates.
7614static SDValue CombineBaseUpdate(SDNode *N,
7615 TargetLowering::DAGCombinerInfo &DCI) {
7616 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7617 return SDValue();
7618
7619 SelectionDAG &DAG = DCI.DAG;
7620 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
7621 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
7622 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
7623 SDValue Addr = N->getOperand(AddrOpIdx);
7624
7625 // Search for a use of the address operand that is an increment.
7626 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
7627 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
7628 SDNode *User = *UI;
7629 if (User->getOpcode() != ISD::ADD ||
7630 UI.getUse().getResNo() != Addr.getResNo())
7631 continue;
7632
7633 // Check that the add is independent of the load/store. Otherwise, folding
7634 // it would create a cycle.
7635 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
7636 continue;
7637
7638 // Find the new opcode for the updating load/store.
7639 bool isLoad = true;
7640 bool isLaneOp = false;
7641 unsigned NewOpc = 0;
7642 unsigned NumVecs = 0;
7643 if (isIntrinsic) {
7644 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
7645 switch (IntNo) {
Craig Topperbc219812012-02-07 02:50:20 +00007646 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00007647 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
7648 NumVecs = 1; break;
7649 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
7650 NumVecs = 2; break;
7651 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
7652 NumVecs = 3; break;
7653 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
7654 NumVecs = 4; break;
7655 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
7656 NumVecs = 2; isLaneOp = true; break;
7657 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
7658 NumVecs = 3; isLaneOp = true; break;
7659 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
7660 NumVecs = 4; isLaneOp = true; break;
7661 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
7662 NumVecs = 1; isLoad = false; break;
7663 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
7664 NumVecs = 2; isLoad = false; break;
7665 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
7666 NumVecs = 3; isLoad = false; break;
7667 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
7668 NumVecs = 4; isLoad = false; break;
7669 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
7670 NumVecs = 2; isLoad = false; isLaneOp = true; break;
7671 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
7672 NumVecs = 3; isLoad = false; isLaneOp = true; break;
7673 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
7674 NumVecs = 4; isLoad = false; isLaneOp = true; break;
7675 }
7676 } else {
7677 isLaneOp = true;
7678 switch (N->getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00007679 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00007680 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
7681 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
7682 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
7683 }
7684 }
7685
7686 // Find the size of memory referenced by the load/store.
7687 EVT VecTy;
7688 if (isLoad)
7689 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00007690 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00007691 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
7692 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
7693 if (isLaneOp)
7694 NumBytes /= VecTy.getVectorNumElements();
7695
7696 // If the increment is a constant, it must match the memory ref size.
7697 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
7698 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
7699 uint64_t IncVal = CInc->getZExtValue();
7700 if (IncVal != NumBytes)
7701 continue;
7702 } else if (NumBytes >= 3 * 16) {
7703 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
7704 // separate instructions that make it harder to use a non-constant update.
7705 continue;
7706 }
7707
7708 // Create the new updating load/store node.
7709 EVT Tys[6];
7710 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
7711 unsigned n;
7712 for (n = 0; n < NumResultVecs; ++n)
7713 Tys[n] = VecTy;
7714 Tys[n++] = MVT::i32;
7715 Tys[n] = MVT::Other;
7716 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
7717 SmallVector<SDValue, 8> Ops;
7718 Ops.push_back(N->getOperand(0)); // incoming chain
7719 Ops.push_back(N->getOperand(AddrOpIdx));
7720 Ops.push_back(Inc);
7721 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
7722 Ops.push_back(N->getOperand(i));
7723 }
7724 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
7725 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
7726 Ops.data(), Ops.size(),
7727 MemInt->getMemoryVT(),
7728 MemInt->getMemOperand());
7729
7730 // Update the uses.
7731 std::vector<SDValue> NewResults;
7732 for (unsigned i = 0; i < NumResultVecs; ++i) {
7733 NewResults.push_back(SDValue(UpdN.getNode(), i));
7734 }
7735 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
7736 DCI.CombineTo(N, NewResults);
7737 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
7738
7739 break;
Owen Anderson76706012011-04-05 21:48:57 +00007740 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00007741 return SDValue();
7742}
7743
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007744/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
7745/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
7746/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
7747/// return true.
7748static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
7749 SelectionDAG &DAG = DCI.DAG;
7750 EVT VT = N->getValueType(0);
7751 // vldN-dup instructions only support 64-bit vectors for N > 1.
7752 if (!VT.is64BitVector())
7753 return false;
7754
7755 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
7756 SDNode *VLD = N->getOperand(0).getNode();
7757 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
7758 return false;
7759 unsigned NumVecs = 0;
7760 unsigned NewOpc = 0;
7761 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
7762 if (IntNo == Intrinsic::arm_neon_vld2lane) {
7763 NumVecs = 2;
7764 NewOpc = ARMISD::VLD2DUP;
7765 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
7766 NumVecs = 3;
7767 NewOpc = ARMISD::VLD3DUP;
7768 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
7769 NumVecs = 4;
7770 NewOpc = ARMISD::VLD4DUP;
7771 } else {
7772 return false;
7773 }
7774
7775 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
7776 // numbers match the load.
7777 unsigned VLDLaneNo =
7778 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
7779 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
7780 UI != UE; ++UI) {
7781 // Ignore uses of the chain result.
7782 if (UI.getUse().getResNo() == NumVecs)
7783 continue;
7784 SDNode *User = *UI;
7785 if (User->getOpcode() != ARMISD::VDUPLANE ||
7786 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
7787 return false;
7788 }
7789
7790 // Create the vldN-dup node.
7791 EVT Tys[5];
7792 unsigned n;
7793 for (n = 0; n < NumVecs; ++n)
7794 Tys[n] = VT;
7795 Tys[n] = MVT::Other;
7796 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
7797 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
7798 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
7799 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
7800 Ops, 2, VLDMemInt->getMemoryVT(),
7801 VLDMemInt->getMemOperand());
7802
7803 // Update the uses.
7804 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
7805 UI != UE; ++UI) {
7806 unsigned ResNo = UI.getUse().getResNo();
7807 // Ignore uses of the chain result.
7808 if (ResNo == NumVecs)
7809 continue;
7810 SDNode *User = *UI;
7811 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
7812 }
7813
7814 // Now the vldN-lane intrinsic is dead except for its chain result.
7815 // Update uses of the chain.
7816 std::vector<SDValue> VLDDupResults;
7817 for (unsigned n = 0; n < NumVecs; ++n)
7818 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
7819 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
7820 DCI.CombineTo(VLD, VLDDupResults);
7821
7822 return true;
7823}
7824
Bob Wilson9e82bf12010-07-14 01:22:12 +00007825/// PerformVDUPLANECombine - Target-specific dag combine xforms for
7826/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007827static SDValue PerformVDUPLANECombine(SDNode *N,
7828 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00007829 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007830
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007831 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
7832 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
7833 if (CombineVLDDUP(N, DCI))
7834 return SDValue(N, 0);
7835
7836 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
7837 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007838 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00007839 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00007840 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00007841 return SDValue();
7842
7843 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
7844 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
7845 // The canonical VMOV for a zero vector uses a 32-bit element size.
7846 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7847 unsigned EltBits;
7848 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
7849 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007850 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007851 if (EltSize > VT.getVectorElementType().getSizeInBits())
7852 return SDValue();
7853
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007854 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007855}
7856
Eric Christopherfa6f5912011-06-29 21:10:36 +00007857// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00007858// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
7859static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
7860{
Chad Rosier118c9a02011-06-28 17:26:57 +00007861 integerPart cN;
7862 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00007863 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
7864 I != E; I++) {
7865 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
7866 if (!C)
7867 return false;
7868
Eric Christopherfa6f5912011-06-29 21:10:36 +00007869 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00007870 APFloat APF = C->getValueAPF();
7871 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
7872 != APFloat::opOK || !isExact)
7873 return false;
7874
7875 c0 = (I == 0) ? cN : c0;
7876 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
7877 return false;
7878 }
7879 C = c0;
7880 return true;
7881}
7882
7883/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
7884/// can replace combinations of VMUL and VCVT (floating-point to integer)
7885/// when the VMUL has a constant operand that is a power of 2.
7886///
7887/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
7888/// vmul.f32 d16, d17, d16
7889/// vcvt.s32.f32 d16, d16
7890/// becomes:
7891/// vcvt.s32.f32 d16, d16, #3
7892static SDValue PerformVCVTCombine(SDNode *N,
7893 TargetLowering::DAGCombinerInfo &DCI,
7894 const ARMSubtarget *Subtarget) {
7895 SelectionDAG &DAG = DCI.DAG;
7896 SDValue Op = N->getOperand(0);
7897
7898 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
7899 Op.getOpcode() != ISD::FMUL)
7900 return SDValue();
7901
7902 uint64_t C;
7903 SDValue N0 = Op->getOperand(0);
7904 SDValue ConstVec = Op->getOperand(1);
7905 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
7906
Eric Christopherfa6f5912011-06-29 21:10:36 +00007907 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00007908 !isConstVecPow2(ConstVec, isSigned, C))
7909 return SDValue();
7910
7911 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
7912 Intrinsic::arm_neon_vcvtfp2fxu;
7913 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7914 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00007915 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00007916 DAG.getConstant(Log2_64(C), MVT::i32));
7917}
7918
7919/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
7920/// can replace combinations of VCVT (integer to floating-point) and VDIV
7921/// when the VDIV has a constant operand that is a power of 2.
7922///
7923/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
7924/// vcvt.f32.s32 d16, d16
7925/// vdiv.f32 d16, d17, d16
7926/// becomes:
7927/// vcvt.f32.s32 d16, d16, #3
7928static SDValue PerformVDIVCombine(SDNode *N,
7929 TargetLowering::DAGCombinerInfo &DCI,
7930 const ARMSubtarget *Subtarget) {
7931 SelectionDAG &DAG = DCI.DAG;
7932 SDValue Op = N->getOperand(0);
7933 unsigned OpOpcode = Op.getNode()->getOpcode();
7934
7935 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
7936 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
7937 return SDValue();
7938
7939 uint64_t C;
7940 SDValue ConstVec = N->getOperand(1);
7941 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
7942
7943 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
7944 !isConstVecPow2(ConstVec, isSigned, C))
7945 return SDValue();
7946
Eric Christopherfa6f5912011-06-29 21:10:36 +00007947 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00007948 Intrinsic::arm_neon_vcvtfxu2fp;
7949 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7950 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00007951 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00007952 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
7953}
7954
7955/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00007956/// operand of a vector shift operation, where all the elements of the
7957/// build_vector must have the same constant integer value.
7958static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
7959 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007960 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00007961 Op = Op.getOperand(0);
7962 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
7963 APInt SplatBits, SplatUndef;
7964 unsigned SplatBitSize;
7965 bool HasAnyUndefs;
7966 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
7967 HasAnyUndefs, ElementBits) ||
7968 SplatBitSize > ElementBits)
7969 return false;
7970 Cnt = SplatBits.getSExtValue();
7971 return true;
7972}
7973
7974/// isVShiftLImm - Check if this is a valid build_vector for the immediate
7975/// operand of a vector shift left operation. That value must be in the range:
7976/// 0 <= Value < ElementBits for a left shift; or
7977/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00007978static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00007979 assert(VT.isVector() && "vector shift count is not a vector type");
7980 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
7981 if (! getVShiftImm(Op, ElementBits, Cnt))
7982 return false;
7983 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
7984}
7985
7986/// isVShiftRImm - Check if this is a valid build_vector for the immediate
7987/// operand of a vector shift right operation. For a shift opcode, the value
7988/// is positive, but for an intrinsic the value count must be negative. The
7989/// absolute value must be in the range:
7990/// 1 <= |Value| <= ElementBits for a right shift; or
7991/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00007992static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00007993 int64_t &Cnt) {
7994 assert(VT.isVector() && "vector shift count is not a vector type");
7995 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
7996 if (! getVShiftImm(Op, ElementBits, Cnt))
7997 return false;
7998 if (isIntrinsic)
7999 Cnt = -Cnt;
8000 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
8001}
8002
8003/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
8004static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
8005 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
8006 switch (IntNo) {
8007 default:
8008 // Don't do anything for most intrinsics.
8009 break;
8010
8011 // Vector shifts: check for immediate versions and lower them.
8012 // Note: This is done during DAG combining instead of DAG legalizing because
8013 // the build_vectors for 64-bit vector element shift counts are generally
8014 // not legal, and it is hard to see their values after they get legalized to
8015 // loads from a constant pool.
8016 case Intrinsic::arm_neon_vshifts:
8017 case Intrinsic::arm_neon_vshiftu:
8018 case Intrinsic::arm_neon_vshiftls:
8019 case Intrinsic::arm_neon_vshiftlu:
8020 case Intrinsic::arm_neon_vshiftn:
8021 case Intrinsic::arm_neon_vrshifts:
8022 case Intrinsic::arm_neon_vrshiftu:
8023 case Intrinsic::arm_neon_vrshiftn:
8024 case Intrinsic::arm_neon_vqshifts:
8025 case Intrinsic::arm_neon_vqshiftu:
8026 case Intrinsic::arm_neon_vqshiftsu:
8027 case Intrinsic::arm_neon_vqshiftns:
8028 case Intrinsic::arm_neon_vqshiftnu:
8029 case Intrinsic::arm_neon_vqshiftnsu:
8030 case Intrinsic::arm_neon_vqrshiftns:
8031 case Intrinsic::arm_neon_vqrshiftnu:
8032 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00008033 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008034 int64_t Cnt;
8035 unsigned VShiftOpc = 0;
8036
8037 switch (IntNo) {
8038 case Intrinsic::arm_neon_vshifts:
8039 case Intrinsic::arm_neon_vshiftu:
8040 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
8041 VShiftOpc = ARMISD::VSHL;
8042 break;
8043 }
8044 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
8045 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
8046 ARMISD::VSHRs : ARMISD::VSHRu);
8047 break;
8048 }
8049 return SDValue();
8050
8051 case Intrinsic::arm_neon_vshiftls:
8052 case Intrinsic::arm_neon_vshiftlu:
8053 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
8054 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008055 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008056
8057 case Intrinsic::arm_neon_vrshifts:
8058 case Intrinsic::arm_neon_vrshiftu:
8059 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
8060 break;
8061 return SDValue();
8062
8063 case Intrinsic::arm_neon_vqshifts:
8064 case Intrinsic::arm_neon_vqshiftu:
8065 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
8066 break;
8067 return SDValue();
8068
8069 case Intrinsic::arm_neon_vqshiftsu:
8070 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
8071 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008072 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008073
8074 case Intrinsic::arm_neon_vshiftn:
8075 case Intrinsic::arm_neon_vrshiftn:
8076 case Intrinsic::arm_neon_vqshiftns:
8077 case Intrinsic::arm_neon_vqshiftnu:
8078 case Intrinsic::arm_neon_vqshiftnsu:
8079 case Intrinsic::arm_neon_vqrshiftns:
8080 case Intrinsic::arm_neon_vqrshiftnu:
8081 case Intrinsic::arm_neon_vqrshiftnsu:
8082 // Narrowing shifts require an immediate right shift.
8083 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
8084 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00008085 llvm_unreachable("invalid shift count for narrowing vector shift "
8086 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008087
8088 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008089 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00008090 }
8091
8092 switch (IntNo) {
8093 case Intrinsic::arm_neon_vshifts:
8094 case Intrinsic::arm_neon_vshiftu:
8095 // Opcode already set above.
8096 break;
8097 case Intrinsic::arm_neon_vshiftls:
8098 case Intrinsic::arm_neon_vshiftlu:
8099 if (Cnt == VT.getVectorElementType().getSizeInBits())
8100 VShiftOpc = ARMISD::VSHLLi;
8101 else
8102 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
8103 ARMISD::VSHLLs : ARMISD::VSHLLu);
8104 break;
8105 case Intrinsic::arm_neon_vshiftn:
8106 VShiftOpc = ARMISD::VSHRN; break;
8107 case Intrinsic::arm_neon_vrshifts:
8108 VShiftOpc = ARMISD::VRSHRs; break;
8109 case Intrinsic::arm_neon_vrshiftu:
8110 VShiftOpc = ARMISD::VRSHRu; break;
8111 case Intrinsic::arm_neon_vrshiftn:
8112 VShiftOpc = ARMISD::VRSHRN; break;
8113 case Intrinsic::arm_neon_vqshifts:
8114 VShiftOpc = ARMISD::VQSHLs; break;
8115 case Intrinsic::arm_neon_vqshiftu:
8116 VShiftOpc = ARMISD::VQSHLu; break;
8117 case Intrinsic::arm_neon_vqshiftsu:
8118 VShiftOpc = ARMISD::VQSHLsu; break;
8119 case Intrinsic::arm_neon_vqshiftns:
8120 VShiftOpc = ARMISD::VQSHRNs; break;
8121 case Intrinsic::arm_neon_vqshiftnu:
8122 VShiftOpc = ARMISD::VQSHRNu; break;
8123 case Intrinsic::arm_neon_vqshiftnsu:
8124 VShiftOpc = ARMISD::VQSHRNsu; break;
8125 case Intrinsic::arm_neon_vqrshiftns:
8126 VShiftOpc = ARMISD::VQRSHRNs; break;
8127 case Intrinsic::arm_neon_vqrshiftnu:
8128 VShiftOpc = ARMISD::VQRSHRNu; break;
8129 case Intrinsic::arm_neon_vqrshiftnsu:
8130 VShiftOpc = ARMISD::VQRSHRNsu; break;
8131 }
8132
8133 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008134 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008135 }
8136
8137 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00008138 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008139 int64_t Cnt;
8140 unsigned VShiftOpc = 0;
8141
8142 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
8143 VShiftOpc = ARMISD::VSLI;
8144 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
8145 VShiftOpc = ARMISD::VSRI;
8146 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00008147 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008148 }
8149
8150 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
8151 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008152 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008153 }
8154
8155 case Intrinsic::arm_neon_vqrshifts:
8156 case Intrinsic::arm_neon_vqrshiftu:
8157 // No immediate versions of these to check for.
8158 break;
8159 }
8160
8161 return SDValue();
8162}
8163
8164/// PerformShiftCombine - Checks for immediate versions of vector shifts and
8165/// lowers them. As with the vector shift intrinsics, this is done during DAG
8166/// combining instead of DAG legalizing because the build_vectors for 64-bit
8167/// vector element shift counts are generally not legal, and it is hard to see
8168/// their values after they get legalized to loads from a constant pool.
8169static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
8170 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00008171 EVT VT = N->getValueType(0);
Evan Cheng5fb468a2012-02-23 02:58:19 +00008172 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
8173 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
8174 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
8175 SDValue N1 = N->getOperand(1);
8176 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
8177 SDValue N0 = N->getOperand(0);
8178 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
8179 DAG.MaskedValueIsZero(N0.getOperand(0),
8180 APInt::getHighBitsSet(32, 16)))
8181 return DAG.getNode(ISD::ROTR, N->getDebugLoc(), VT, N0, N1);
8182 }
8183 }
Bob Wilson5bafff32009-06-22 23:27:02 +00008184
8185 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00008186 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8187 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00008188 return SDValue();
8189
8190 assert(ST->hasNEON() && "unexpected vector shift");
8191 int64_t Cnt;
8192
8193 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008194 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00008195
8196 case ISD::SHL:
8197 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
8198 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008199 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008200 break;
8201
8202 case ISD::SRA:
8203 case ISD::SRL:
8204 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
8205 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
8206 ARMISD::VSHRs : ARMISD::VSHRu);
8207 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008208 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008209 }
8210 }
8211 return SDValue();
8212}
8213
8214/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
8215/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
8216static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
8217 const ARMSubtarget *ST) {
8218 SDValue N0 = N->getOperand(0);
8219
8220 // Check for sign- and zero-extensions of vector extract operations of 8-
8221 // and 16-bit vector elements. NEON supports these directly. They are
8222 // handled during DAG combining because type legalization will promote them
8223 // to 32-bit types and it is messy to recognize the operations after that.
8224 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
8225 SDValue Vec = N0.getOperand(0);
8226 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00008227 EVT VT = N->getValueType(0);
8228 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008229 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8230
Owen Anderson825b72b2009-08-11 20:47:22 +00008231 if (VT == MVT::i32 &&
8232 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00008233 TLI.isTypeLegal(Vec.getValueType()) &&
8234 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00008235
8236 unsigned Opc = 0;
8237 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008238 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00008239 case ISD::SIGN_EXTEND:
8240 Opc = ARMISD::VGETLANEs;
8241 break;
8242 case ISD::ZERO_EXTEND:
8243 case ISD::ANY_EXTEND:
8244 Opc = ARMISD::VGETLANEu;
8245 break;
8246 }
8247 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
8248 }
8249 }
8250
8251 return SDValue();
8252}
8253
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008254/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
8255/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
8256static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
8257 const ARMSubtarget *ST) {
8258 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00008259 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008260 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
8261 // a NaN; only do the transformation when it matches that behavior.
8262
8263 // For now only do this when using NEON for FP operations; if using VFP, it
8264 // is not obvious that the benefit outweighs the cost of switching to the
8265 // NEON pipeline.
8266 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
8267 N->getValueType(0) != MVT::f32)
8268 return SDValue();
8269
8270 SDValue CondLHS = N->getOperand(0);
8271 SDValue CondRHS = N->getOperand(1);
8272 SDValue LHS = N->getOperand(2);
8273 SDValue RHS = N->getOperand(3);
8274 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
8275
8276 unsigned Opcode = 0;
8277 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00008278 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008279 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00008280 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008281 IsReversed = true ; // x CC y ? y : x
8282 } else {
8283 return SDValue();
8284 }
8285
Bob Wilsone742bb52010-02-24 22:15:53 +00008286 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008287 switch (CC) {
8288 default: break;
8289 case ISD::SETOLT:
8290 case ISD::SETOLE:
8291 case ISD::SETLT:
8292 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008293 case ISD::SETULT:
8294 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00008295 // If LHS is NaN, an ordered comparison will be false and the result will
8296 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
8297 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
8298 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
8299 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8300 break;
8301 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
8302 // will return -0, so vmin can only be used for unsafe math or if one of
8303 // the operands is known to be nonzero.
8304 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008305 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00008306 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8307 break;
8308 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008309 break;
8310
8311 case ISD::SETOGT:
8312 case ISD::SETOGE:
8313 case ISD::SETGT:
8314 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008315 case ISD::SETUGT:
8316 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00008317 // If LHS is NaN, an ordered comparison will be false and the result will
8318 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
8319 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
8320 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
8321 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8322 break;
8323 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
8324 // will return +0, so vmax can only be used for unsafe math or if one of
8325 // the operands is known to be nonzero.
8326 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008327 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00008328 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8329 break;
8330 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008331 break;
8332 }
8333
8334 if (!Opcode)
8335 return SDValue();
8336 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
8337}
8338
Evan Chenge721f5c2011-07-13 00:42:17 +00008339/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
8340SDValue
8341ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
8342 SDValue Cmp = N->getOperand(4);
8343 if (Cmp.getOpcode() != ARMISD::CMPZ)
8344 // Only looking at EQ and NE cases.
8345 return SDValue();
8346
8347 EVT VT = N->getValueType(0);
8348 DebugLoc dl = N->getDebugLoc();
8349 SDValue LHS = Cmp.getOperand(0);
8350 SDValue RHS = Cmp.getOperand(1);
8351 SDValue FalseVal = N->getOperand(0);
8352 SDValue TrueVal = N->getOperand(1);
8353 SDValue ARMcc = N->getOperand(2);
Jim Grosbachb04546f2011-09-13 20:30:37 +00008354 ARMCC::CondCodes CC =
8355 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chenge721f5c2011-07-13 00:42:17 +00008356
8357 // Simplify
8358 // mov r1, r0
8359 // cmp r1, x
8360 // mov r0, y
8361 // moveq r0, x
8362 // to
8363 // cmp r0, x
8364 // movne r0, y
8365 //
8366 // mov r1, r0
8367 // cmp r1, x
8368 // mov r0, x
8369 // movne r0, y
8370 // to
8371 // cmp r0, x
8372 // movne r0, y
8373 /// FIXME: Turn this into a target neutral optimization?
8374 SDValue Res;
Evan Cheng9b88d2d2011-09-28 23:16:31 +00008375 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chenge721f5c2011-07-13 00:42:17 +00008376 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
8377 N->getOperand(3), Cmp);
8378 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
8379 SDValue ARMcc;
8380 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
8381 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
8382 N->getOperand(3), NewCmp);
8383 }
8384
8385 if (Res.getNode()) {
8386 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008387 DAG.ComputeMaskedBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chenge721f5c2011-07-13 00:42:17 +00008388 // Capture demanded bits information that would be otherwise lost.
8389 if (KnownZero == 0xfffffffe)
8390 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8391 DAG.getValueType(MVT::i1));
8392 else if (KnownZero == 0xffffff00)
8393 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8394 DAG.getValueType(MVT::i8));
8395 else if (KnownZero == 0xffff0000)
8396 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8397 DAG.getValueType(MVT::i16));
8398 }
8399
8400 return Res;
8401}
8402
Dan Gohman475871a2008-07-27 21:46:04 +00008403SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008404 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008405 switch (N->getOpcode()) {
8406 default: break;
Tanya Lattner189531f2011-06-14 23:48:48 +00008407 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008408 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00008409 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008410 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chengc892aeb2012-02-23 01:19:06 +00008411 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
8412 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Cheng0c1aec12010-12-14 03:22:07 +00008413 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00008414 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008415 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00008416 case ISD::STORE: return PerformSTORECombine(N, DCI);
8417 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
8418 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00008419 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008420 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00008421 case ISD::FP_TO_SINT:
8422 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
8423 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008424 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00008425 case ISD::SHL:
8426 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008427 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00008428 case ISD::SIGN_EXTEND:
8429 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008430 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
8431 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00008432 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00008433 case ARMISD::VLD2DUP:
8434 case ARMISD::VLD3DUP:
8435 case ARMISD::VLD4DUP:
8436 return CombineBaseUpdate(N, DCI);
8437 case ISD::INTRINSIC_VOID:
8438 case ISD::INTRINSIC_W_CHAIN:
8439 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
8440 case Intrinsic::arm_neon_vld1:
8441 case Intrinsic::arm_neon_vld2:
8442 case Intrinsic::arm_neon_vld3:
8443 case Intrinsic::arm_neon_vld4:
8444 case Intrinsic::arm_neon_vld2lane:
8445 case Intrinsic::arm_neon_vld3lane:
8446 case Intrinsic::arm_neon_vld4lane:
8447 case Intrinsic::arm_neon_vst1:
8448 case Intrinsic::arm_neon_vst2:
8449 case Intrinsic::arm_neon_vst3:
8450 case Intrinsic::arm_neon_vst4:
8451 case Intrinsic::arm_neon_vst2lane:
8452 case Intrinsic::arm_neon_vst3lane:
8453 case Intrinsic::arm_neon_vst4lane:
8454 return CombineBaseUpdate(N, DCI);
8455 default: break;
8456 }
8457 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008458 }
Dan Gohman475871a2008-07-27 21:46:04 +00008459 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008460}
8461
Evan Cheng31959b12011-02-02 01:06:55 +00008462bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
8463 EVT VT) const {
8464 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
8465}
8466
Bill Wendlingaf566342009-08-15 21:21:19 +00008467bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00008468 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00008469 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00008470
8471 switch (VT.getSimpleVT().SimpleTy) {
8472 default:
8473 return false;
8474 case MVT::i8:
8475 case MVT::i16:
8476 case MVT::i32:
8477 return true;
8478 // FIXME: VLD1 etc with standard alignment is legal.
8479 }
8480}
8481
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008482static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
8483 unsigned AlignCheck) {
8484 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
8485 (DstAlign == 0 || DstAlign % AlignCheck == 0));
8486}
8487
8488EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
8489 unsigned DstAlign, unsigned SrcAlign,
Lang Hamesa1e78882011-11-02 23:37:04 +00008490 bool IsZeroVal,
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008491 bool MemcpyStrSrc,
8492 MachineFunction &MF) const {
8493 const Function *F = MF.getFunction();
8494
8495 // See if we can use NEON instructions for this...
Lang Hamesa1e78882011-11-02 23:37:04 +00008496 if (IsZeroVal &&
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008497 !F->hasFnAttr(Attribute::NoImplicitFloat) &&
8498 Subtarget->hasNEON()) {
8499 if (memOpAlign(SrcAlign, DstAlign, 16) && Size >= 16) {
8500 return MVT::v4i32;
8501 } else if (memOpAlign(SrcAlign, DstAlign, 8) && Size >= 8) {
8502 return MVT::v2i32;
8503 }
8504 }
8505
Lang Hames5207bf22011-11-08 18:56:23 +00008506 // Lowering to i32/i16 if the size permits.
8507 if (Size >= 4) {
8508 return MVT::i32;
8509 } else if (Size >= 2) {
8510 return MVT::i16;
8511 }
8512
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008513 // Let the target-independent logic figure it out.
8514 return MVT::Other;
8515}
8516
Evan Chenge6c835f2009-08-14 20:09:37 +00008517static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
8518 if (V < 0)
8519 return false;
8520
8521 unsigned Scale = 1;
8522 switch (VT.getSimpleVT().SimpleTy) {
8523 default: return false;
8524 case MVT::i1:
8525 case MVT::i8:
8526 // Scale == 1;
8527 break;
8528 case MVT::i16:
8529 // Scale == 2;
8530 Scale = 2;
8531 break;
8532 case MVT::i32:
8533 // Scale == 4;
8534 Scale = 4;
8535 break;
8536 }
8537
8538 if ((V & (Scale - 1)) != 0)
8539 return false;
8540 V /= Scale;
8541 return V == (V & ((1LL << 5) - 1));
8542}
8543
8544static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
8545 const ARMSubtarget *Subtarget) {
8546 bool isNeg = false;
8547 if (V < 0) {
8548 isNeg = true;
8549 V = - V;
8550 }
8551
8552 switch (VT.getSimpleVT().SimpleTy) {
8553 default: return false;
8554 case MVT::i1:
8555 case MVT::i8:
8556 case MVT::i16:
8557 case MVT::i32:
8558 // + imm12 or - imm8
8559 if (isNeg)
8560 return V == (V & ((1LL << 8) - 1));
8561 return V == (V & ((1LL << 12) - 1));
8562 case MVT::f32:
8563 case MVT::f64:
8564 // Same as ARM mode. FIXME: NEON?
8565 if (!Subtarget->hasVFP2())
8566 return false;
8567 if ((V & 3) != 0)
8568 return false;
8569 V >>= 2;
8570 return V == (V & ((1LL << 8) - 1));
8571 }
8572}
8573
Evan Chengb01fad62007-03-12 23:30:29 +00008574/// isLegalAddressImmediate - Return true if the integer value can be used
8575/// as the offset of the target addressing mode for load / store of the
8576/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00008577static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00008578 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00008579 if (V == 0)
8580 return true;
8581
Evan Cheng65011532009-03-09 19:15:00 +00008582 if (!VT.isSimple())
8583 return false;
8584
Evan Chenge6c835f2009-08-14 20:09:37 +00008585 if (Subtarget->isThumb1Only())
8586 return isLegalT1AddressImmediate(V, VT);
8587 else if (Subtarget->isThumb2())
8588 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00008589
Evan Chenge6c835f2009-08-14 20:09:37 +00008590 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00008591 if (V < 0)
8592 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00008593 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00008594 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008595 case MVT::i1:
8596 case MVT::i8:
8597 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00008598 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008599 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008600 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00008601 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008602 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008603 case MVT::f32:
8604 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00008605 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00008606 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00008607 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00008608 return false;
8609 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008610 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00008611 }
Evan Chenga8e29892007-01-19 07:51:42 +00008612}
8613
Evan Chenge6c835f2009-08-14 20:09:37 +00008614bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
8615 EVT VT) const {
8616 int Scale = AM.Scale;
8617 if (Scale < 0)
8618 return false;
8619
8620 switch (VT.getSimpleVT().SimpleTy) {
8621 default: return false;
8622 case MVT::i1:
8623 case MVT::i8:
8624 case MVT::i16:
8625 case MVT::i32:
8626 if (Scale == 1)
8627 return true;
8628 // r + r << imm
8629 Scale = Scale & ~1;
8630 return Scale == 2 || Scale == 4 || Scale == 8;
8631 case MVT::i64:
8632 // r + r
8633 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
8634 return true;
8635 return false;
8636 case MVT::isVoid:
8637 // Note, we allow "void" uses (basically, uses that aren't loads or
8638 // stores), because arm allows folding a scale into many arithmetic
8639 // operations. This should be made more precise and revisited later.
8640
8641 // Allow r << imm, but the imm has to be a multiple of two.
8642 if (Scale & 1) return false;
8643 return isPowerOf2_32(Scale);
8644 }
8645}
8646
Chris Lattner37caf8c2007-04-09 23:33:39 +00008647/// isLegalAddressingMode - Return true if the addressing mode represented
8648/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008649bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008650 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008651 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00008652 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00008653 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008654
Chris Lattner37caf8c2007-04-09 23:33:39 +00008655 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008656 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008657 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008658
Chris Lattner37caf8c2007-04-09 23:33:39 +00008659 switch (AM.Scale) {
8660 case 0: // no scale reg, must be "r+i" or "r", or "i".
8661 break;
8662 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00008663 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00008664 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008665 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00008666 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008667 // ARM doesn't support any R+R*scale+imm addr modes.
8668 if (AM.BaseOffs)
8669 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008670
Bob Wilson2c7dab12009-04-08 17:55:28 +00008671 if (!VT.isSimple())
8672 return false;
8673
Evan Chenge6c835f2009-08-14 20:09:37 +00008674 if (Subtarget->isThumb2())
8675 return isLegalT2ScaledAddressingMode(AM, VT);
8676
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008677 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00008678 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00008679 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008680 case MVT::i1:
8681 case MVT::i8:
8682 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008683 if (Scale < 0) Scale = -Scale;
8684 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008685 return true;
8686 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00008687 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008688 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00008689 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00008690 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008691 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008692 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00008693 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008694
Owen Anderson825b72b2009-08-11 20:47:22 +00008695 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00008696 // Note, we allow "void" uses (basically, uses that aren't loads or
8697 // stores), because arm allows folding a scale into many arithmetic
8698 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008699
Chris Lattner37caf8c2007-04-09 23:33:39 +00008700 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00008701 if (Scale & 1) return false;
8702 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00008703 }
Evan Chengb01fad62007-03-12 23:30:29 +00008704 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00008705 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00008706}
8707
Evan Cheng77e47512009-11-11 19:05:52 +00008708/// isLegalICmpImmediate - Return true if the specified immediate is legal
8709/// icmp immediate, that is the target has icmp instructions which can compare
8710/// a register against the immediate without having to materialize the
8711/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00008712bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00008713 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng77e47512009-11-11 19:05:52 +00008714 if (!Subtarget->isThumb())
Chandler Carruthba4d4572012-04-06 20:10:52 +00008715 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1;
Evan Cheng77e47512009-11-11 19:05:52 +00008716 if (Subtarget->isThumb2())
Chandler Carruthba4d4572012-04-06 20:10:52 +00008717 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1;
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00008718 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng06b53c02009-11-12 07:13:11 +00008719 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00008720}
8721
Dan Gohmancca82142011-05-03 00:46:49 +00008722/// isLegalAddImmediate - Return true if the specified immediate is legal
8723/// add immediate, that is the target has add instructions which can add
8724/// a register with the immediate without having to materialize the
8725/// immediate into a register.
8726bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
8727 return ARM_AM::getSOImmVal(Imm) != -1;
8728}
8729
Owen Andersone50ed302009-08-10 22:56:29 +00008730static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00008731 bool isSEXTLoad, SDValue &Base,
8732 SDValue &Offset, bool &isInc,
8733 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00008734 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
8735 return false;
8736
Owen Anderson825b72b2009-08-11 20:47:22 +00008737 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00008738 // AddressingMode 3
8739 Base = Ptr->getOperand(0);
8740 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008741 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00008742 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008743 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00008744 isInc = false;
8745 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8746 return true;
8747 }
8748 }
8749 isInc = (Ptr->getOpcode() == ISD::ADD);
8750 Offset = Ptr->getOperand(1);
8751 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00008752 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00008753 // AddressingMode 2
8754 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008755 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00008756 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008757 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00008758 isInc = false;
8759 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8760 Base = Ptr->getOperand(0);
8761 return true;
8762 }
8763 }
8764
8765 if (Ptr->getOpcode() == ISD::ADD) {
8766 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00008767 ARM_AM::ShiftOpc ShOpcVal=
8768 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00008769 if (ShOpcVal != ARM_AM::no_shift) {
8770 Base = Ptr->getOperand(1);
8771 Offset = Ptr->getOperand(0);
8772 } else {
8773 Base = Ptr->getOperand(0);
8774 Offset = Ptr->getOperand(1);
8775 }
8776 return true;
8777 }
8778
8779 isInc = (Ptr->getOpcode() == ISD::ADD);
8780 Base = Ptr->getOperand(0);
8781 Offset = Ptr->getOperand(1);
8782 return true;
8783 }
8784
Jim Grosbache5165492009-11-09 00:11:35 +00008785 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00008786 return false;
8787}
8788
Owen Andersone50ed302009-08-10 22:56:29 +00008789static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00008790 bool isSEXTLoad, SDValue &Base,
8791 SDValue &Offset, bool &isInc,
8792 SelectionDAG &DAG) {
8793 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
8794 return false;
8795
8796 Base = Ptr->getOperand(0);
8797 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
8798 int RHSC = (int)RHS->getZExtValue();
8799 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
8800 assert(Ptr->getOpcode() == ISD::ADD);
8801 isInc = false;
8802 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8803 return true;
8804 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
8805 isInc = Ptr->getOpcode() == ISD::ADD;
8806 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
8807 return true;
8808 }
8809 }
8810
8811 return false;
8812}
8813
Evan Chenga8e29892007-01-19 07:51:42 +00008814/// getPreIndexedAddressParts - returns true by value, base pointer and
8815/// offset pointer and addressing mode by reference if the node's address
8816/// can be legally represented as pre-indexed load / store address.
8817bool
Dan Gohman475871a2008-07-27 21:46:04 +00008818ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
8819 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00008820 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00008821 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008822 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00008823 return false;
8824
Owen Andersone50ed302009-08-10 22:56:29 +00008825 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00008826 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00008827 bool isSEXTLoad = false;
8828 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
8829 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008830 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00008831 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
8832 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
8833 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008834 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00008835 } else
8836 return false;
8837
8838 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00008839 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00008840 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00008841 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
8842 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00008843 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00008844 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00008845 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00008846 if (!isLegal)
8847 return false;
8848
8849 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
8850 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00008851}
8852
8853/// getPostIndexedAddressParts - returns true by value, base pointer and
8854/// offset pointer and addressing mode by reference if this node can be
8855/// combined with a load / store to form a post-indexed load / store.
8856bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00008857 SDValue &Base,
8858 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00008859 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00008860 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008861 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00008862 return false;
8863
Owen Andersone50ed302009-08-10 22:56:29 +00008864 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00008865 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00008866 bool isSEXTLoad = false;
8867 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008868 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00008869 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00008870 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
8871 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008872 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00008873 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00008874 } else
8875 return false;
8876
8877 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00008878 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00008879 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00008880 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00008881 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00008882 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00008883 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
8884 isInc, DAG);
8885 if (!isLegal)
8886 return false;
8887
Evan Cheng28dad2a2010-05-18 21:31:17 +00008888 if (Ptr != Base) {
8889 // Swap base ptr and offset to catch more post-index load / store when
8890 // it's legal. In Thumb2 mode, offset must be an immediate.
8891 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
8892 !Subtarget->isThumb2())
8893 std::swap(Base, Offset);
8894
8895 // Post-indexed load / store update the base pointer.
8896 if (Ptr != Base)
8897 return false;
8898 }
8899
Evan Chenge88d5ce2009-07-02 07:28:31 +00008900 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
8901 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00008902}
8903
Dan Gohman475871a2008-07-27 21:46:04 +00008904void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008905 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008906 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008907 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00008908 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008909 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00008910 switch (Op.getOpcode()) {
8911 default: break;
8912 case ARMISD::CMOV: {
8913 // Bits are known zero/one if known on the LHS and RHS.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008914 DAG.ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00008915 if (KnownZero == 0 && KnownOne == 0) return;
8916
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008917 APInt KnownZeroRHS, KnownOneRHS;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008918 DAG.ComputeMaskedBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00008919 KnownZero &= KnownZeroRHS;
8920 KnownOne &= KnownOneRHS;
8921 return;
8922 }
8923 }
8924}
8925
8926//===----------------------------------------------------------------------===//
8927// ARM Inline Assembly Support
8928//===----------------------------------------------------------------------===//
8929
Evan Cheng55d42002011-01-08 01:24:27 +00008930bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
8931 // Looking for "rev" which is V6+.
8932 if (!Subtarget->hasV6Ops())
8933 return false;
8934
8935 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
8936 std::string AsmStr = IA->getAsmString();
8937 SmallVector<StringRef, 4> AsmPieces;
8938 SplitString(AsmStr, AsmPieces, ";\n");
8939
8940 switch (AsmPieces.size()) {
8941 default: return false;
8942 case 1:
8943 AsmStr = AsmPieces[0];
8944 AsmPieces.clear();
8945 SplitString(AsmStr, AsmPieces, " \t,");
8946
8947 // rev $0, $1
8948 if (AsmPieces.size() == 3 &&
8949 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
8950 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008951 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00008952 if (Ty && Ty->getBitWidth() == 32)
8953 return IntrinsicLowering::LowerToByteSwap(CI);
8954 }
8955 break;
8956 }
8957
8958 return false;
8959}
8960
Evan Chenga8e29892007-01-19 07:51:42 +00008961/// getConstraintType - Given a constraint letter, return the type of
8962/// constraint it is for this target.
8963ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00008964ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
8965 if (Constraint.size() == 1) {
8966 switch (Constraint[0]) {
8967 default: break;
8968 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008969 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00008970 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00008971 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008972 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00008973 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00008974 // An address with a single base register. Due to the way we
8975 // currently handle addresses it is the same as an 'r' memory constraint.
8976 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00008977 }
Eric Christopher1312ca82011-06-21 22:10:57 +00008978 } else if (Constraint.size() == 2) {
8979 switch (Constraint[0]) {
8980 default: break;
8981 // All 'U+' constraints are addresses.
8982 case 'U': return C_Memory;
8983 }
Evan Chenga8e29892007-01-19 07:51:42 +00008984 }
Chris Lattner4234f572007-03-25 02:14:49 +00008985 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00008986}
8987
John Thompson44ab89e2010-10-29 17:29:13 +00008988/// Examine constraint type and operand type and determine a weight value.
8989/// This object must already have been set up with the operand type
8990/// and the current alternative constraint selected.
8991TargetLowering::ConstraintWeight
8992ARMTargetLowering::getSingleConstraintMatchWeight(
8993 AsmOperandInfo &info, const char *constraint) const {
8994 ConstraintWeight weight = CW_Invalid;
8995 Value *CallOperandVal = info.CallOperandVal;
8996 // If we don't have a value, we can't do a match,
8997 // but allow it at the lowest weight.
8998 if (CallOperandVal == NULL)
8999 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009000 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00009001 // Look at the constraint type.
9002 switch (*constraint) {
9003 default:
9004 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
9005 break;
9006 case 'l':
9007 if (type->isIntegerTy()) {
9008 if (Subtarget->isThumb())
9009 weight = CW_SpecificReg;
9010 else
9011 weight = CW_Register;
9012 }
9013 break;
9014 case 'w':
9015 if (type->isFloatingPointTy())
9016 weight = CW_Register;
9017 break;
9018 }
9019 return weight;
9020}
9021
Eric Christopher35e6d4d2011-06-30 23:50:52 +00009022typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
9023RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00009024ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00009025 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00009026 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00009027 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00009028 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00009029 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00009030 if (Subtarget->isThumb())
Eric Christopher35e6d4d2011-06-30 23:50:52 +00009031 return RCPair(0U, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +00009032 else
Eric Christopher35e6d4d2011-06-30 23:50:52 +00009033 return RCPair(0U, ARM::GPRRegisterClass);
Eric Christopher73744df2011-06-30 23:23:01 +00009034 case 'h': // High regs or no regs.
9035 if (Subtarget->isThumb())
Andrew Trick3af7a672011-09-20 03:06:13 +00009036 return RCPair(0U, ARM::hGPRRegisterClass);
Eric Christopher1070f822011-07-01 00:19:27 +00009037 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009038 case 'r':
Eric Christopher35e6d4d2011-06-30 23:50:52 +00009039 return RCPair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009040 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00009041 if (VT == MVT::f32)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00009042 return RCPair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00009043 if (VT.getSizeInBits() == 64)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00009044 return RCPair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00009045 if (VT.getSizeInBits() == 128)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00009046 return RCPair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009047 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00009048 case 'x':
9049 if (VT == MVT::f32)
Andrew Trick3af7a672011-09-20 03:06:13 +00009050 return RCPair(0U, ARM::SPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009051 if (VT.getSizeInBits() == 64)
Andrew Trick3af7a672011-09-20 03:06:13 +00009052 return RCPair(0U, ARM::DPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009053 if (VT.getSizeInBits() == 128)
Andrew Trick3af7a672011-09-20 03:06:13 +00009054 return RCPair(0U, ARM::QPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009055 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009056 case 't':
9057 if (VT == MVT::f32)
Andrew Trick3af7a672011-09-20 03:06:13 +00009058 return RCPair(0U, ARM::SPRRegisterClass);
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009059 break;
Evan Chenga8e29892007-01-19 07:51:42 +00009060 }
9061 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00009062 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00009063 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00009064
Evan Chenga8e29892007-01-19 07:51:42 +00009065 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
9066}
9067
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009068/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
9069/// vector. If it is invalid, don't add anything to Ops.
9070void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00009071 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009072 std::vector<SDValue>&Ops,
9073 SelectionDAG &DAG) const {
9074 SDValue Result(0, 0);
9075
Eric Christopher100c8332011-06-02 23:16:42 +00009076 // Currently only support length 1 constraints.
9077 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00009078
Eric Christopher100c8332011-06-02 23:16:42 +00009079 char ConstraintLetter = Constraint[0];
9080 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009081 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00009082 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009083 case 'I': case 'J': case 'K': case 'L':
9084 case 'M': case 'N': case 'O':
9085 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
9086 if (!C)
9087 return;
9088
9089 int64_t CVal64 = C->getSExtValue();
9090 int CVal = (int) CVal64;
9091 // None of these constraints allow values larger than 32 bits. Check
9092 // that the value fits in an int.
9093 if (CVal != CVal64)
9094 return;
9095
Eric Christopher100c8332011-06-02 23:16:42 +00009096 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00009097 case 'j':
Andrew Trick3af7a672011-09-20 03:06:13 +00009098 // Constant suitable for movw, must be between 0 and
9099 // 65535.
9100 if (Subtarget->hasV6T2Ops())
9101 if (CVal >= 0 && CVal <= 65535)
9102 break;
9103 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009104 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009105 if (Subtarget->isThumb1Only()) {
9106 // This must be a constant between 0 and 255, for ADD
9107 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009108 if (CVal >= 0 && CVal <= 255)
9109 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009110 } else if (Subtarget->isThumb2()) {
9111 // A constant that can be used as an immediate value in a
9112 // data-processing instruction.
9113 if (ARM_AM::getT2SOImmVal(CVal) != -1)
9114 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009115 } else {
9116 // A constant that can be used as an immediate value in a
9117 // data-processing instruction.
9118 if (ARM_AM::getSOImmVal(CVal) != -1)
9119 break;
9120 }
9121 return;
9122
9123 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009124 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009125 // This must be a constant between -255 and -1, for negated ADD
9126 // immediates. This can be used in GCC with an "n" modifier that
9127 // prints the negated value, for use with SUB instructions. It is
9128 // not useful otherwise but is implemented for compatibility.
9129 if (CVal >= -255 && CVal <= -1)
9130 break;
9131 } else {
9132 // This must be a constant between -4095 and 4095. It is not clear
9133 // what this constraint is intended for. Implemented for
9134 // compatibility with GCC.
9135 if (CVal >= -4095 && CVal <= 4095)
9136 break;
9137 }
9138 return;
9139
9140 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009141 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009142 // A 32-bit value where only one byte has a nonzero value. Exclude
9143 // zero to match GCC. This constraint is used by GCC internally for
9144 // constants that can be loaded with a move/shift combination.
9145 // It is not useful otherwise but is implemented for compatibility.
9146 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
9147 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009148 } else if (Subtarget->isThumb2()) {
9149 // A constant whose bitwise inverse can be used as an immediate
9150 // value in a data-processing instruction. This can be used in GCC
9151 // with a "B" modifier that prints the inverted value, for use with
9152 // BIC and MVN instructions. It is not useful otherwise but is
9153 // implemented for compatibility.
9154 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
9155 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009156 } else {
9157 // A constant whose bitwise inverse can be used as an immediate
9158 // value in a data-processing instruction. This can be used in GCC
9159 // with a "B" modifier that prints the inverted value, for use with
9160 // BIC and MVN instructions. It is not useful otherwise but is
9161 // implemented for compatibility.
9162 if (ARM_AM::getSOImmVal(~CVal) != -1)
9163 break;
9164 }
9165 return;
9166
9167 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009168 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009169 // This must be a constant between -7 and 7,
9170 // for 3-operand ADD/SUB immediate instructions.
9171 if (CVal >= -7 && CVal < 7)
9172 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009173 } else if (Subtarget->isThumb2()) {
9174 // A constant whose negation can be used as an immediate value in a
9175 // data-processing instruction. This can be used in GCC with an "n"
9176 // modifier that prints the negated value, for use with SUB
9177 // instructions. It is not useful otherwise but is implemented for
9178 // compatibility.
9179 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
9180 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009181 } else {
9182 // A constant whose negation can be used as an immediate value in a
9183 // data-processing instruction. This can be used in GCC with an "n"
9184 // modifier that prints the negated value, for use with SUB
9185 // instructions. It is not useful otherwise but is implemented for
9186 // compatibility.
9187 if (ARM_AM::getSOImmVal(-CVal) != -1)
9188 break;
9189 }
9190 return;
9191
9192 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009193 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009194 // This must be a multiple of 4 between 0 and 1020, for
9195 // ADD sp + immediate.
9196 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
9197 break;
9198 } else {
9199 // A power of two or a constant between 0 and 32. This is used in
9200 // GCC for the shift amount on shifted register operands, but it is
9201 // useful in general for any shift amounts.
9202 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
9203 break;
9204 }
9205 return;
9206
9207 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009208 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009209 // This must be a constant between 0 and 31, for shift amounts.
9210 if (CVal >= 0 && CVal <= 31)
9211 break;
9212 }
9213 return;
9214
9215 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009216 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009217 // This must be a multiple of 4 between -508 and 508, for
9218 // ADD/SUB sp = sp + immediate.
9219 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
9220 break;
9221 }
9222 return;
9223 }
9224 Result = DAG.getTargetConstant(CVal, Op.getValueType());
9225 break;
9226 }
9227
9228 if (Result.getNode()) {
9229 Ops.push_back(Result);
9230 return;
9231 }
Dale Johannesen1784d162010-06-25 21:55:36 +00009232 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009233}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00009234
9235bool
9236ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
9237 // The ARM target isn't yet aware of offsets.
9238 return false;
9239}
Evan Cheng39382422009-10-28 01:44:26 +00009240
Jim Grosbach469bbdb2010-07-16 23:05:05 +00009241bool ARM::isBitFieldInvertedMask(unsigned v) {
9242 if (v == 0xffffffff)
9243 return 0;
9244 // there can be 1's on either or both "outsides", all the "inside"
9245 // bits must be 0's
9246 unsigned int lsb = 0, msb = 31;
9247 while (v & (1 << msb)) --msb;
9248 while (v & (1 << lsb)) ++lsb;
9249 for (unsigned int i = lsb; i <= msb; ++i) {
9250 if (v & (1 << i))
9251 return 0;
9252 }
9253 return 1;
9254}
9255
Evan Cheng39382422009-10-28 01:44:26 +00009256/// isFPImmLegal - Returns true if the target can instruction select the
9257/// specified FP immediate natively. If false, the legalizer will
9258/// materialize the FP immediate as a load from a constant pool.
9259bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
9260 if (!Subtarget->hasVFP3())
9261 return false;
9262 if (VT == MVT::f32)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00009263 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00009264 if (VT == MVT::f64)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00009265 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00009266 return false;
9267}
Bob Wilson65ffec42010-09-21 17:56:22 +00009268
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009269/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00009270/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
9271/// specified in the intrinsic calls.
9272bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
9273 const CallInst &I,
9274 unsigned Intrinsic) const {
9275 switch (Intrinsic) {
9276 case Intrinsic::arm_neon_vld1:
9277 case Intrinsic::arm_neon_vld2:
9278 case Intrinsic::arm_neon_vld3:
9279 case Intrinsic::arm_neon_vld4:
9280 case Intrinsic::arm_neon_vld2lane:
9281 case Intrinsic::arm_neon_vld3lane:
9282 case Intrinsic::arm_neon_vld4lane: {
9283 Info.opc = ISD::INTRINSIC_W_CHAIN;
9284 // Conservatively set memVT to the entire set of vectors loaded.
9285 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
9286 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
9287 Info.ptrVal = I.getArgOperand(0);
9288 Info.offset = 0;
9289 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
9290 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
9291 Info.vol = false; // volatile loads with NEON intrinsics not supported
9292 Info.readMem = true;
9293 Info.writeMem = false;
9294 return true;
9295 }
9296 case Intrinsic::arm_neon_vst1:
9297 case Intrinsic::arm_neon_vst2:
9298 case Intrinsic::arm_neon_vst3:
9299 case Intrinsic::arm_neon_vst4:
9300 case Intrinsic::arm_neon_vst2lane:
9301 case Intrinsic::arm_neon_vst3lane:
9302 case Intrinsic::arm_neon_vst4lane: {
9303 Info.opc = ISD::INTRINSIC_VOID;
9304 // Conservatively set memVT to the entire set of vectors stored.
9305 unsigned NumElts = 0;
9306 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009307 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +00009308 if (!ArgTy->isVectorTy())
9309 break;
9310 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
9311 }
9312 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
9313 Info.ptrVal = I.getArgOperand(0);
9314 Info.offset = 0;
9315 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
9316 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
9317 Info.vol = false; // volatile stores with NEON intrinsics not supported
9318 Info.readMem = false;
9319 Info.writeMem = true;
9320 return true;
9321 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009322 case Intrinsic::arm_strexd: {
9323 Info.opc = ISD::INTRINSIC_W_CHAIN;
9324 Info.memVT = MVT::i64;
9325 Info.ptrVal = I.getArgOperand(2);
9326 Info.offset = 0;
9327 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009328 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009329 Info.readMem = false;
9330 Info.writeMem = true;
9331 return true;
9332 }
9333 case Intrinsic::arm_ldrexd: {
9334 Info.opc = ISD::INTRINSIC_W_CHAIN;
9335 Info.memVT = MVT::i64;
9336 Info.ptrVal = I.getArgOperand(0);
9337 Info.offset = 0;
9338 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009339 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009340 Info.readMem = true;
9341 Info.writeMem = false;
9342 return true;
9343 }
Bob Wilson65ffec42010-09-21 17:56:22 +00009344 default:
9345 break;
9346 }
9347
9348 return false;
9349}