blob: 94ddf861067f741dfac291313a5e157f6b4d4a17 [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"
Evan Chenga8e29892007-01-19 07:51:42 +000016#include "ARM.h"
Eric Christopher6f2ccef2010-09-10 22:42:06 +000017#include "ARMCallingConv.h"
Evan Chenga8e29892007-01-19 07:51:42 +000018#include "ARMConstantPoolValue.h"
19#include "ARMISelLowering.h"
20#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +000021#include "ARMPerfectShuffle.h"
Evan Chenga8e29892007-01-19 07:51:42 +000022#include "ARMRegisterInfo.h"
23#include "ARMSubtarget.h"
24#include "ARMTargetMachine.h"
Chris Lattner80ec2792009-08-02 00:34:36 +000025#include "ARMTargetObjectFile.h"
Evan Chengee04a6d2011-07-20 23:34:39 +000026#include "MCTargetDesc/ARMAddressingModes.h"
Evan Chenga8e29892007-01-19 07:51:42 +000027#include "llvm/CallingConv.h"
28#include "llvm/Constants.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000029#include "llvm/Function.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000030#include "llvm/GlobalValue.h"
Evan Cheng27707472007-03-16 08:43:56 +000031#include "llvm/Instruction.h"
Bob Wilson65ffec42010-09-21 17:56:22 +000032#include "llvm/Instructions.h"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000033#include "llvm/Intrinsics.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000034#include "llvm/Type.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000035#include "llvm/CodeGen/CallingConvLower.h"
Evan Cheng55d42002011-01-08 01:24:27 +000036#include "llvm/CodeGen/IntrinsicLowering.h"
Evan Chenga8e29892007-01-19 07:51:42 +000037#include "llvm/CodeGen/MachineBasicBlock.h"
38#include "llvm/CodeGen/MachineFrameInfo.h"
39#include "llvm/CodeGen/MachineFunction.h"
40#include "llvm/CodeGen/MachineInstrBuilder.h"
Bill Wendling2a850152011-10-05 00:02:33 +000041#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000042#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chenga8e29892007-01-19 07:51:42 +000043#include "llvm/CodeGen/SelectionDAG.h"
Bill Wendling94a1c632010-03-09 02:46:12 +000044#include "llvm/MC/MCSectionMachO.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000045#include "llvm/Target/TargetOptions.h"
Evan Cheng55d42002011-01-08 01:24:27 +000046#include "llvm/ADT/StringExtras.h"
Dale Johannesen51e28e62010-06-03 21:09:53 +000047#include "llvm/ADT/Statistic.h"
Jim Grosbache7b52522010-04-14 22:28:31 +000048#include "llvm/Support/CommandLine.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000049#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000050#include "llvm/Support/MathExtras.h"
Jim Grosbache801dc42009-12-12 01:40:06 +000051#include "llvm/Support/raw_ostream.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000052#include <sstream>
Evan Chenga8e29892007-01-19 07:51:42 +000053using namespace llvm;
54
Dale Johannesen51e28e62010-06-03 21:09:53 +000055STATISTIC(NumTailCalls, "Number of tail calls");
Evan Chengfc8475b2011-01-19 02:16:49 +000056STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Dale Johannesen51e28e62010-06-03 21:09:53 +000057
Bob Wilson703af3a2010-08-13 22:43:33 +000058// This option should go away when tail calls fully work.
59static cl::opt<bool>
60EnableARMTailCalls("arm-tail-calls", cl::Hidden,
61 cl::desc("Generate tail calls (TEMPORARY OPTION)."),
62 cl::init(false));
63
Eric Christopher836c6242010-12-15 23:47:29 +000064cl::opt<bool>
Jim Grosbache7b52522010-04-14 22:28:31 +000065EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng515fe3a2010-07-08 02:08:50 +000066 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbache7b52522010-04-14 22:28:31 +000067 cl::init(false));
68
Evan Cheng46df4eb2010-06-16 07:35:02 +000069static cl::opt<bool>
70ARMInterworking("arm-interworking", cl::Hidden,
71 cl::desc("Enable / disable ARM interworking (for debugging only)"),
72 cl::init(true));
73
Benjamin Kramer0861f572011-11-26 23:01:57 +000074namespace {
Cameron Zwaricha86686e2011-06-10 20:59:24 +000075 class ARMCCState : public CCState {
76 public:
77 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
78 const TargetMachine &TM, SmallVector<CCValAssign, 16> &locs,
79 LLVMContext &C, ParmContext PC)
80 : CCState(CC, isVarArg, MF, TM, locs, C) {
81 assert(((PC == Call) || (PC == Prologue)) &&
82 "ARMCCState users must specify whether their context is call"
83 "or prologue generation.");
84 CallOrPrologue = PC;
85 }
86 };
87}
88
Stuart Hastingsc7315872011-04-20 16:47:52 +000089// The APCS parameter registers.
90static const unsigned GPRArgRegs[] = {
91 ARM::R0, ARM::R1, ARM::R2, ARM::R3
92};
93
Owen Andersone50ed302009-08-10 22:56:29 +000094void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
95 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000096 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000097 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000098 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
99 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000100
Owen Anderson70671842009-08-10 20:18:46 +0000101 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000102 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000103 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000104 }
105
Owen Andersone50ed302009-08-10 22:56:29 +0000106 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000107 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Duncan Sands28b77e92011-09-06 19:07:46 +0000108 setOperationAction(ISD::SETCC, VT.getSimpleVT(), Custom);
Eli Friedman5c89cb82011-10-24 23:08:52 +0000109 setOperationAction(ISD::INSERT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson3468c2e2010-11-03 16:24:50 +0000110 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Eli Friedman14e809c2011-11-09 23:36:02 +0000111 if (ElemTy == MVT::i32) {
112 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Custom);
113 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Custom);
114 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Custom);
115 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Custom);
116 } else {
Bob Wilson0696fdf2009-09-16 20:20:44 +0000117 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
118 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
119 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
120 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
121 }
Owen Anderson70671842009-08-10 20:18:46 +0000122 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
123 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Bob Wilson07f6e802010-06-16 21:34:01 +0000124 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
Bob Wilson5e8b8332011-01-07 04:59:04 +0000125 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Legal);
Bob Wilsond0910c42010-04-06 22:02:24 +0000126 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
127 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Eli Friedman15f58c52011-11-11 03:16:38 +0000128 setOperationAction(ISD::SIGN_EXTEND_INREG, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000129 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000130 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
131 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
132 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +0000133 }
134
135 // Promote all bit-wise operations.
136 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000137 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000138 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
139 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000140 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000141 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000142 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000143 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000144 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000145 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000146 }
Bob Wilson16330762009-09-16 00:17:28 +0000147
148 // Neon does not support vector divide/remainder operations.
149 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
150 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
151 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
152 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
153 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
154 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000155}
156
Owen Andersone50ed302009-08-10 22:56:29 +0000157void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000158 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000159 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000160}
161
Owen Andersone50ed302009-08-10 22:56:29 +0000162void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000163 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000164 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000165}
166
Chris Lattnerf0144122009-07-28 03:13:23 +0000167static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
168 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000169 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000170
Chris Lattner80ec2792009-08-02 00:34:36 +0000171 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000172}
173
Evan Chenga8e29892007-01-19 07:51:42 +0000174ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000175 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000176 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng31446872010-07-23 22:39:59 +0000177 RegInfo = TM.getRegisterInfo();
Evan Cheng3ef1c872010-09-10 01:29:16 +0000178 Itins = TM.getInstrItineraryData();
Evan Chenga8e29892007-01-19 07:51:42 +0000179
Duncan Sands28b77e92011-09-06 19:07:46 +0000180 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
181
Evan Chengb1df8f22007-04-27 08:15:43 +0000182 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000183 // Uses VFP for Thumb libfuncs if available.
184 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
185 // Single-precision floating-point arithmetic.
186 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
187 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
188 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
189 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000190
Evan Chengb1df8f22007-04-27 08:15:43 +0000191 // Double-precision floating-point arithmetic.
192 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
193 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
194 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
195 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000196
Evan Chengb1df8f22007-04-27 08:15:43 +0000197 // Single-precision comparisons.
198 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
199 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
200 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
201 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
202 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
203 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
204 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
205 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000206
Evan Chengb1df8f22007-04-27 08:15:43 +0000207 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
208 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
209 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
210 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
211 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
212 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
213 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
214 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000215
Evan Chengb1df8f22007-04-27 08:15:43 +0000216 // Double-precision comparisons.
217 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
218 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
219 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
220 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
221 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
222 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
223 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
224 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000225
Evan Chengb1df8f22007-04-27 08:15:43 +0000226 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
227 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
228 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
229 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
230 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
231 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
232 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
233 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000234
Evan Chengb1df8f22007-04-27 08:15:43 +0000235 // Floating-point to integer conversions.
236 // i64 conversions are done via library routines even when generating VFP
237 // instructions, so use the same ones.
238 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
239 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
240 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
241 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000242
Evan Chengb1df8f22007-04-27 08:15:43 +0000243 // Conversions between floating types.
244 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
245 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
246
247 // Integer to floating-point conversions.
248 // i64 conversions are done via library routines even when generating VFP
249 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000250 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
251 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000252 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
253 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
254 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
255 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
256 }
Evan Chenga8e29892007-01-19 07:51:42 +0000257 }
258
Bob Wilson2f954612009-05-22 17:38:41 +0000259 // These libcalls are not available in 32-bit.
260 setLibcallName(RTLIB::SHL_I128, 0);
261 setLibcallName(RTLIB::SRL_I128, 0);
262 setLibcallName(RTLIB::SRA_I128, 0);
263
Evan Cheng07043272012-02-21 20:46:00 +0000264 if (Subtarget->isAAPCS_ABI() && !Subtarget->isTargetDarwin()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000265 // Double-precision floating-point arithmetic helper functions
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000266 // RTABI chapter 4.1.2, Table 2
267 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd");
268 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv");
269 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul");
270 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub");
271 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS);
272 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS);
273 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS);
274 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS);
275
276 // Double-precision floating-point comparison helper functions
277 // RTABI chapter 4.1.2, Table 3
278 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq");
279 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
280 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq");
281 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ);
282 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt");
283 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
284 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple");
285 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
286 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge");
287 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
288 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt");
289 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
290 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun");
291 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
292 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun");
293 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
294 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS);
295 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS);
296 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS);
297 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS);
298 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS);
299 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS);
300 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS);
301 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS);
302
303 // Single-precision floating-point arithmetic helper functions
304 // RTABI chapter 4.1.2, Table 4
305 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd");
306 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv");
307 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul");
308 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub");
309 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS);
310 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS);
311 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS);
312 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS);
313
314 // Single-precision floating-point comparison helper functions
315 // RTABI chapter 4.1.2, Table 5
316 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq");
317 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
318 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq");
319 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ);
320 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt");
321 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
322 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple");
323 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
324 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge");
325 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
326 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt");
327 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
328 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun");
329 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
330 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun");
331 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
332 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS);
333 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS);
334 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS);
335 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS);
336 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS);
337 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS);
338 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS);
339 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS);
340
341 // Floating-point to integer conversions.
342 // RTABI chapter 4.1.2, Table 6
343 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz");
344 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz");
345 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz");
346 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz");
347 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz");
348 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz");
349 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz");
350 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz");
351 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS);
352 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS);
353 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS);
354 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS);
355 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS);
356 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS);
357 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS);
358 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS);
359
360 // Conversions between floating types.
361 // RTABI chapter 4.1.2, Table 7
362 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f");
363 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d");
364 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000365 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000366
367 // Integer to floating-point conversions.
368 // RTABI chapter 4.1.2, Table 8
369 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d");
370 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d");
371 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d");
372 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d");
373 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f");
374 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f");
375 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f");
376 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f");
377 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
378 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
379 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
380 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
381 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
382 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
383 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
384 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
385
386 // Long long helper functions
387 // RTABI chapter 4.2, Table 9
388 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000389 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
390 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
391 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
392 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
393 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
394 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
395 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
396 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
397 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
398
399 // Integer division functions
400 // RTABI chapter 4.3.1
401 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
402 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
403 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000404 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000405 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
406 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
407 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000408 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000409 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
410 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
411 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000412 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000413 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS);
414 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000415 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000416 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
Renato Golin1ec11fb2011-05-22 21:41:23 +0000417
418 // Memory operations
419 // RTABI chapter 4.3.4
420 setLibcallName(RTLIB::MEMCPY, "__aeabi_memcpy");
421 setLibcallName(RTLIB::MEMMOVE, "__aeabi_memmove");
422 setLibcallName(RTLIB::MEMSET, "__aeabi_memset");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000423 setLibcallCallingConv(RTLIB::MEMCPY, CallingConv::ARM_AAPCS);
424 setLibcallCallingConv(RTLIB::MEMMOVE, CallingConv::ARM_AAPCS);
425 setLibcallCallingConv(RTLIB::MEMSET, CallingConv::ARM_AAPCS);
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000426 }
427
Bob Wilson2fef4572011-10-07 16:59:21 +0000428 // Use divmod compiler-rt calls for iOS 5.0 and later.
429 if (Subtarget->getTargetTriple().getOS() == Triple::IOS &&
430 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
431 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
432 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
433 }
434
David Goodwinf1daf7d2009-07-08 23:10:31 +0000435 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000436 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000437 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000438 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000439 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
440 !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000441 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000442 if (!Subtarget->isFPOnlySP())
443 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000444
Owen Anderson825b72b2009-08-11 20:47:22 +0000445 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000446 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000447
Eli Friedman9f1f26a2011-11-08 01:43:53 +0000448 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
449 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
450 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
451 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
452 setTruncStoreAction((MVT::SimpleValueType)VT,
453 (MVT::SimpleValueType)InnerVT, Expand);
454 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
455 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
456 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
457 }
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);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000510
511 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 Cheng0c439eb2010-01-27 00:07:07 +00001289 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;
1585 else
1586 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1587 } else {
1588 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001589 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1590 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001591 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001592
Dan Gohman475871a2008-07-27 21:46:04 +00001593 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001594 Ops.push_back(Chain);
1595 Ops.push_back(Callee);
1596
1597 // Add argument registers to the end of the list so that they are known live
1598 // into the call.
1599 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1600 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1601 RegsToPass[i].second.getValueType()));
1602
Gabor Greifba36cb52008-08-28 21:40:38 +00001603 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001604 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001605
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001606 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001607 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001608 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001609
Duncan Sands4bdcb612008-07-02 17:40:58 +00001610 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001611 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001612 InFlag = Chain.getValue(1);
1613
Chris Lattnere563bbc2008-10-11 22:08:30 +00001614 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1615 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001616 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001617 InFlag = Chain.getValue(1);
1618
Bob Wilson1f595bb2009-04-17 19:07:39 +00001619 // Handle result values, copying them out of physregs into vregs that we
1620 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001621 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1622 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001623}
1624
Stuart Hastingsf222e592011-02-28 17:17:53 +00001625/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001626/// on the stack. Remember the next parameter register to allocate,
1627/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001628/// this.
1629void
Stuart Hastingsc7315872011-04-20 16:47:52 +00001630llvm::ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
1631 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1632 assert((State->getCallOrPrologue() == Prologue ||
1633 State->getCallOrPrologue() == Call) &&
1634 "unhandled ParmContext");
1635 if ((!State->isFirstByValRegValid()) &&
1636 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1637 State->setFirstByValReg(reg);
1638 // At a call site, a byval parameter that is split between
1639 // registers and memory needs its size truncated here. In a
1640 // function prologue, such byval parameters are reassembled in
1641 // memory, and are not truncated.
1642 if (State->getCallOrPrologue() == Call) {
1643 unsigned excess = 4 * (ARM::R4 - reg);
1644 assert(size >= excess && "expected larger existing stack allocation");
1645 size -= excess;
1646 }
1647 }
1648 // Confiscate any remaining parameter registers to preclude their
1649 // assignment to subsequent parameters.
1650 while (State->AllocateReg(GPRArgRegs, 4))
1651 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001652}
1653
Dale Johannesen51e28e62010-06-03 21:09:53 +00001654/// MatchingStackOffset - Return true if the given stack call argument is
1655/// already available in the same position (relatively) of the caller's
1656/// incoming argument stack.
1657static
1658bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1659 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1660 const ARMInstrInfo *TII) {
1661 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1662 int FI = INT_MAX;
1663 if (Arg.getOpcode() == ISD::CopyFromReg) {
1664 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001665 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001666 return false;
1667 MachineInstr *Def = MRI->getVRegDef(VR);
1668 if (!Def)
1669 return false;
1670 if (!Flags.isByVal()) {
1671 if (!TII->isLoadFromStackSlot(Def, FI))
1672 return false;
1673 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001674 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001675 }
1676 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1677 if (Flags.isByVal())
1678 // ByVal argument is passed in as a pointer but it's now being
1679 // dereferenced. e.g.
1680 // define @foo(%struct.X* %A) {
1681 // tail call @bar(%struct.X* byval %A)
1682 // }
1683 return false;
1684 SDValue Ptr = Ld->getBasePtr();
1685 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1686 if (!FINode)
1687 return false;
1688 FI = FINode->getIndex();
1689 } else
1690 return false;
1691
1692 assert(FI != INT_MAX);
1693 if (!MFI->isFixedObjectIndex(FI))
1694 return false;
1695 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1696}
1697
1698/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1699/// for tail call optimization. Targets which want to do tail call
1700/// optimization should implement this function.
1701bool
1702ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1703 CallingConv::ID CalleeCC,
1704 bool isVarArg,
1705 bool isCalleeStructRet,
1706 bool isCallerStructRet,
1707 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001708 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001709 const SmallVectorImpl<ISD::InputArg> &Ins,
1710 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001711 const Function *CallerF = DAG.getMachineFunction().getFunction();
1712 CallingConv::ID CallerCC = CallerF->getCallingConv();
1713 bool CCMatch = CallerCC == CalleeCC;
1714
1715 // Look for obvious safe cases to perform tail call optimization that do not
1716 // require ABI changes. This is what gcc calls sibcall.
1717
Jim Grosbach7616b642010-06-16 23:45:49 +00001718 // Do not sibcall optimize vararg calls unless the call site is not passing
1719 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001720 if (isVarArg && !Outs.empty())
1721 return false;
1722
1723 // Also avoid sibcall optimization if either caller or callee uses struct
1724 // return semantics.
1725 if (isCalleeStructRet || isCallerStructRet)
1726 return false;
1727
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001728 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001729 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1730 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1731 // support in the assembler and linker to be used. This would need to be
1732 // fixed to fully support tail calls in Thumb1.
1733 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001734 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1735 // LR. This means if we need to reload LR, it takes an extra instructions,
1736 // which outweighs the value of the tail call; but here we don't know yet
1737 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001738 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001739 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001740
1741 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1742 // but we need to make sure there are enough registers; the only valid
1743 // registers are the 4 used for parameters. We don't currently do this
1744 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001745 if (Subtarget->isThumb1Only())
1746 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001747
Dale Johannesen51e28e62010-06-03 21:09:53 +00001748 // If the calling conventions do not match, then we'd better make sure the
1749 // results are returned in the same way as what the caller expects.
1750 if (!CCMatch) {
1751 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001752 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1753 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001754 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1755
1756 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001757 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1758 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001759 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1760
1761 if (RVLocs1.size() != RVLocs2.size())
1762 return false;
1763 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1764 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1765 return false;
1766 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1767 return false;
1768 if (RVLocs1[i].isRegLoc()) {
1769 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1770 return false;
1771 } else {
1772 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1773 return false;
1774 }
1775 }
1776 }
1777
1778 // If the callee takes no arguments then go on to check the results of the
1779 // call.
1780 if (!Outs.empty()) {
1781 // Check if stack adjustment is needed. For now, do not do this if any
1782 // argument is passed on the stack.
1783 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001784 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1785 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001786 CCInfo.AnalyzeCallOperands(Outs,
1787 CCAssignFnForNode(CalleeCC, false, isVarArg));
1788 if (CCInfo.getNextStackOffset()) {
1789 MachineFunction &MF = DAG.getMachineFunction();
1790
1791 // Check if the arguments are already laid out in the right way as
1792 // the caller's fixed stack objects.
1793 MachineFrameInfo *MFI = MF.getFrameInfo();
1794 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1795 const ARMInstrInfo *TII =
1796 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001797 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1798 i != e;
1799 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001800 CCValAssign &VA = ArgLocs[i];
1801 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001802 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001803 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001804 if (VA.getLocInfo() == CCValAssign::Indirect)
1805 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001806 if (VA.needsCustom()) {
1807 // f64 and vector types are split into multiple registers or
1808 // register/stack-slot combinations. The types will not match
1809 // the registers; give up on memory f64 refs until we figure
1810 // out what to do about this.
1811 if (!VA.isRegLoc())
1812 return false;
1813 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001814 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001815 if (RegVT == MVT::v2f64) {
1816 if (!ArgLocs[++i].isRegLoc())
1817 return false;
1818 if (!ArgLocs[++i].isRegLoc())
1819 return false;
1820 }
1821 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001822 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1823 MFI, MRI, TII))
1824 return false;
1825 }
1826 }
1827 }
1828 }
1829
1830 return true;
1831}
1832
Dan Gohman98ca4f22009-08-05 01:29:28 +00001833SDValue
1834ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001835 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001836 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001837 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001838 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001839
Bob Wilsondee46d72009-04-17 20:35:10 +00001840 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001841 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001842
Bob Wilsondee46d72009-04-17 20:35:10 +00001843 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001844 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1845 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001846
Dan Gohman98ca4f22009-08-05 01:29:28 +00001847 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001848 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1849 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001850
1851 // If this is the first return lowered for this function, add
1852 // the regs to the liveout set for the function.
1853 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1854 for (unsigned i = 0; i != RVLocs.size(); ++i)
1855 if (RVLocs[i].isRegLoc())
1856 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001857 }
1858
Bob Wilson1f595bb2009-04-17 19:07:39 +00001859 SDValue Flag;
1860
1861 // Copy the result values into the output registers.
1862 for (unsigned i = 0, realRVLocIdx = 0;
1863 i != RVLocs.size();
1864 ++i, ++realRVLocIdx) {
1865 CCValAssign &VA = RVLocs[i];
1866 assert(VA.isRegLoc() && "Can only return in registers!");
1867
Dan Gohmanc9403652010-07-07 15:54:55 +00001868 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001869
1870 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001871 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001872 case CCValAssign::Full: break;
1873 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001874 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001875 break;
1876 }
1877
Bob Wilson1f595bb2009-04-17 19:07:39 +00001878 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001879 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001880 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001881 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1882 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001883 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001884 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001885
1886 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1887 Flag = Chain.getValue(1);
1888 VA = RVLocs[++i]; // skip ahead to next loc
1889 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1890 HalfGPRs.getValue(1), Flag);
1891 Flag = Chain.getValue(1);
1892 VA = RVLocs[++i]; // skip ahead to next loc
1893
1894 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001895 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1896 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001897 }
1898 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1899 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001900 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001901 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001902 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001903 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001904 VA = RVLocs[++i]; // skip ahead to next loc
1905 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1906 Flag);
1907 } else
1908 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1909
Bob Wilsondee46d72009-04-17 20:35:10 +00001910 // Guarantee that all emitted copies are
1911 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001912 Flag = Chain.getValue(1);
1913 }
1914
1915 SDValue result;
1916 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001917 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001918 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001919 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001920
1921 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001922}
1923
Evan Cheng3d2125c2010-11-30 23:55:39 +00001924bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1925 if (N->getNumValues() != 1)
1926 return false;
1927 if (!N->hasNUsesOfValue(1, 0))
1928 return false;
1929
1930 unsigned NumCopies = 0;
Jason W Kim1de886c2012-02-10 16:07:59 +00001931 SDNode* Copies[2] = { 0, 0 };
Evan Cheng3d2125c2010-11-30 23:55:39 +00001932 SDNode *Use = *N->use_begin();
1933 if (Use->getOpcode() == ISD::CopyToReg) {
1934 Copies[NumCopies++] = Use;
1935 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1936 // f64 returned in a pair of GPRs.
1937 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1938 UI != UE; ++UI) {
1939 if (UI->getOpcode() != ISD::CopyToReg)
1940 return false;
1941 Copies[UI.getUse().getResNo()] = *UI;
1942 ++NumCopies;
1943 }
1944 } else if (Use->getOpcode() == ISD::BITCAST) {
1945 // f32 returned in a single GPR.
1946 if (!Use->hasNUsesOfValue(1, 0))
1947 return false;
1948 Use = *Use->use_begin();
1949 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1950 return false;
1951 Copies[NumCopies++] = Use;
1952 } else {
1953 return false;
1954 }
1955
1956 if (NumCopies != 1 && NumCopies != 2)
1957 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001958
1959 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001960 for (unsigned i = 0; i < NumCopies; ++i) {
1961 SDNode *Copy = Copies[i];
1962 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1963 UI != UE; ++UI) {
1964 if (UI->getOpcode() == ISD::CopyToReg) {
1965 SDNode *Use = *UI;
Jason W Kim1de886c2012-02-10 16:07:59 +00001966 if (Use == Copies[0] || ((NumCopies == 2) && (Use == Copies[1])))
Evan Cheng3d2125c2010-11-30 23:55:39 +00001967 continue;
1968 return false;
1969 }
1970 if (UI->getOpcode() != ARMISD::RET_FLAG)
1971 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001972 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001973 }
1974 }
1975
Evan Cheng1bf891a2010-12-01 22:59:46 +00001976 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001977}
1978
Evan Cheng485fafc2011-03-21 01:19:09 +00001979bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1980 if (!EnableARMTailCalls)
1981 return false;
1982
1983 if (!CI->isTailCall())
1984 return false;
1985
1986 return !Subtarget->isThumb1Only();
1987}
1988
Bob Wilsonb62d2572009-11-03 00:02:05 +00001989// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1990// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1991// one of the above mentioned nodes. It has to be wrapped because otherwise
1992// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1993// be used to form addressing mode. These wrapped nodes will be selected
1994// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001995static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001996 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001997 // FIXME there is no actual debug info here
1998 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001999 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00002000 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00002001 if (CP->isMachineConstantPoolEntry())
2002 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2003 CP->getAlignment());
2004 else
2005 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2006 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00002007 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00002008}
2009
Jim Grosbache1102ca2010-07-19 17:20:38 +00002010unsigned ARMTargetLowering::getJumpTableEncoding() const {
2011 return MachineJumpTableInfo::EK_Inline;
2012}
2013
Dan Gohmand858e902010-04-17 15:26:15 +00002014SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2015 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00002016 MachineFunction &MF = DAG.getMachineFunction();
2017 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2018 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00002019 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00002020 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00002021 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00002022 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2023 SDValue CPAddr;
2024 if (RelocM == Reloc::Static) {
2025 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2026 } else {
2027 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002028 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00002029 ARMConstantPoolValue *CPV =
2030 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2031 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson907eebd2009-11-02 20:59:23 +00002032 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2033 }
2034 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2035 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002036 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002037 false, false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00002038 if (RelocM == Reloc::Static)
2039 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00002040 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00002041 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00002042}
2043
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002044// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00002045SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002046ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002047 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002048 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002049 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002050 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00002051 MachineFunction &MF = DAG.getMachineFunction();
2052 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002053 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002054 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002055 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2056 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002057 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002058 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00002059 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002060 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002061 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002062 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002063
Evan Chenge7e0d622009-11-06 22:24:13 +00002064 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002065 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002066
2067 // call __tls_get_addr.
2068 ArgListTy Args;
2069 ArgListEntry Entry;
2070 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002071 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002072 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002073 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00002074 std::pair<SDValue, SDValue> CallResult =
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002075 LowerCallTo(Chain, (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00002076 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002077 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002078 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002079 return CallResult.first;
2080}
2081
2082// Lower ISD::GlobalTLSAddress using the "initial exec" or
2083// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00002084SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002085ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002086 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002087 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002088 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002089 SDValue Offset;
2090 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002091 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002092 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002093 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002094
Chris Lattner4fb63d02009-07-15 04:12:33 +00002095 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002096 MachineFunction &MF = DAG.getMachineFunction();
2097 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002098 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002099 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002100 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2101 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002102 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2103 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2104 true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002105 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002106 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002107 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002108 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002109 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002110 Chain = Offset.getValue(1);
2111
Evan Chenge7e0d622009-11-06 22:24:13 +00002112 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002113 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002114
Evan Cheng9eda6892009-10-31 03:39:36 +00002115 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002116 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002117 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002118 } else {
2119 // local exec model
Bill Wendling5bb77992011-10-01 08:00:54 +00002120 ARMConstantPoolValue *CPV =
2121 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002122 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002123 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002124 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002125 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002126 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002127 }
2128
2129 // The address of the thread local variable is the add of the thread
2130 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002131 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002132}
2133
Dan Gohman475871a2008-07-27 21:46:04 +00002134SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002135ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002136 // TODO: implement the "local dynamic" model
2137 assert(Subtarget->isTargetELF() &&
2138 "TLS not implemented for non-ELF targets");
2139 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2140 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
2141 // otherwise use the "Local Exec" TLS Model
2142 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
2143 return LowerToTLSGeneralDynamicModel(GA, DAG);
2144 else
2145 return LowerToTLSExecModels(GA, DAG);
2146}
2147
Dan Gohman475871a2008-07-27 21:46:04 +00002148SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002149 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002150 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002151 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002152 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002153 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2154 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002155 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002156 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002157 ARMConstantPoolConstant::Create(GV,
2158 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002159 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002160 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002161 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002162 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002163 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002164 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002165 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002166 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002167 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002168 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002169 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002170 MachinePointerInfo::getGOT(),
2171 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002172 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002173 }
2174
2175 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloy015cca62011-10-26 08:53:19 +00002176 // pair. This is always cheaper.
2177 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002178 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002179 // FIXME: Once remat is capable of dealing with instructions with register
2180 // operands, expand this into two nodes.
2181 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2182 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002183 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002184 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2185 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2186 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2187 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002188 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002189 }
2190}
2191
Dan Gohman475871a2008-07-27 21:46:04 +00002192SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002193 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002194 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002195 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002196 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002197 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002198 MachineFunction &MF = DAG.getMachineFunction();
2199 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2200
Jakob Stoklund Olesen8f37a242012-01-07 20:49:15 +00002201 // FIXME: Enable this for static codegen when tool issues are fixed. Also
2202 // update ARMFastISel::ARMMaterializeGV.
Evan Chengf31151f2011-10-26 01:17:44 +00002203 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002204 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002205 // FIXME: Once remat is capable of dealing with instructions with register
2206 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002207 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002208 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2209 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2210
Evan Cheng53519f02011-01-21 18:55:51 +00002211 unsigned Wrapper = (RelocM == Reloc::PIC_)
2212 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2213 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002214 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002215 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2216 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002217 MachinePointerInfo::getGOT(),
2218 false, false, false, 0);
Evan Chengfc8475b2011-01-19 02:16:49 +00002219 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002220 }
2221
2222 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002223 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002224 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002225 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002226 } else {
2227 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002228 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2229 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002230 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue,
2231 PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002232 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002233 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002234 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002235
Evan Cheng9eda6892009-10-31 03:39:36 +00002236 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002237 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002238 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002239 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002240
2241 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002242 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002243 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002244 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002245
Evan Cheng63476a82009-09-03 07:04:02 +00002246 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002247 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002248 false, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002249
2250 return Result;
2251}
2252
Dan Gohman475871a2008-07-27 21:46:04 +00002253SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002254 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002255 assert(Subtarget->isTargetELF() &&
2256 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002257 MachineFunction &MF = DAG.getMachineFunction();
2258 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002259 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002260 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002261 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002262 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingfe31e672011-10-01 08:58:29 +00002263 ARMConstantPoolValue *CPV =
2264 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2265 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002266 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002267 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002268 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002269 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002270 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002271 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002272 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002273}
2274
Jim Grosbach0e0da732009-05-12 23:59:14 +00002275SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002276ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2277 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002278 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendlingce370cf2011-10-07 21:25:38 +00002279 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2280 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002281 Op.getOperand(1), Val);
2282}
2283
2284SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002285ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2286 DebugLoc dl = Op.getDebugLoc();
2287 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2288 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2289}
2290
2291SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002292ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002293 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002294 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002295 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002296 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002297 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002298 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002299 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002300 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2301 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002302 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002303 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002304 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002305 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002306 EVT PtrVT = getPointerTy();
2307 DebugLoc dl = Op.getDebugLoc();
2308 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2309 SDValue CPAddr;
2310 unsigned PCAdj = (RelocM != Reloc::PIC_)
2311 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002312 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002313 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2314 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002315 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002316 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002317 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002318 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002319 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002320 false, false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002321
2322 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002323 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002324 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2325 }
2326 return Result;
2327 }
Evan Cheng92e39162011-03-29 23:06:19 +00002328 case Intrinsic::arm_neon_vmulls:
2329 case Intrinsic::arm_neon_vmullu: {
2330 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2331 ? ARMISD::VMULLs : ARMISD::VMULLu;
2332 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2333 Op.getOperand(1), Op.getOperand(2));
2334 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002335 }
2336}
2337
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002338static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002339 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002340 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002341 if (!Subtarget->hasDataBarrier()) {
2342 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2343 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2344 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002345 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002346 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002347 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002348 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002349 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002350
2351 SDValue Op5 = Op.getOperand(5);
2352 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2353 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2354 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2355 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2356
2357 ARM_MB::MemBOpt DMBOpt;
2358 if (isDeviceBarrier)
2359 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2360 else
2361 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2362 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2363 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002364}
2365
Eli Friedman26689ac2011-08-03 21:06:02 +00002366
2367static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2368 const ARMSubtarget *Subtarget) {
2369 // FIXME: handle "fence singlethread" more efficiently.
2370 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002371 if (!Subtarget->hasDataBarrier()) {
2372 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2373 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2374 // here.
2375 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2376 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002377 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002378 DAG.getConstant(0, MVT::i32));
2379 }
2380
Eli Friedman26689ac2011-08-03 21:06:02 +00002381 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002382 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002383}
2384
Evan Chengdfed19f2010-11-03 06:34:55 +00002385static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2386 const ARMSubtarget *Subtarget) {
2387 // ARM pre v5TE and Thumb1 does not have preload instructions.
2388 if (!(Subtarget->isThumb2() ||
2389 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2390 // Just preserve the chain.
2391 return Op.getOperand(0);
2392
2393 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002394 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2395 if (!isRead &&
2396 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2397 // ARMv7 with MP extension has PLDW.
2398 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002399
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002400 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2401 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002402 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002403 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002404 isData = ~isData & 1;
2405 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002406
2407 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002408 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2409 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002410}
2411
Dan Gohman1e93df62010-04-17 14:41:14 +00002412static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2413 MachineFunction &MF = DAG.getMachineFunction();
2414 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2415
Evan Chenga8e29892007-01-19 07:51:42 +00002416 // vastart just stores the address of the VarArgsFrameIndex slot into the
2417 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002418 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002419 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002420 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002421 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002422 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2423 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002424}
2425
Dan Gohman475871a2008-07-27 21:46:04 +00002426SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002427ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2428 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002429 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002430 MachineFunction &MF = DAG.getMachineFunction();
2431 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2432
Craig Topper44d23822012-02-22 05:59:10 +00002433 const TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002434 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002435 RC = ARM::tGPRRegisterClass;
2436 else
2437 RC = ARM::GPRRegisterClass;
2438
2439 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002440 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002441 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002442
2443 SDValue ArgValue2;
2444 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002445 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002446 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002447
2448 // Create load node to retrieve arguments from the stack.
2449 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002450 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002451 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002452 false, false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002453 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002454 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002455 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002456 }
2457
Jim Grosbache5165492009-11-09 00:11:35 +00002458 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002459}
2460
Stuart Hastingsc7315872011-04-20 16:47:52 +00002461void
2462ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2463 unsigned &VARegSize, unsigned &VARegSaveSize)
2464 const {
2465 unsigned NumGPRs;
2466 if (CCInfo.isFirstByValRegValid())
2467 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2468 else {
2469 unsigned int firstUnalloced;
2470 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2471 sizeof(GPRArgRegs) /
2472 sizeof(GPRArgRegs[0]));
2473 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2474 }
2475
2476 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2477 VARegSize = NumGPRs * 4;
2478 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2479}
2480
2481// The remaining GPRs hold either the beginning of variable-argument
2482// data, or the beginning of an aggregate passed by value (usuall
2483// byval). Either way, we allocate stack slots adjacent to the data
2484// provided by our caller, and store the unallocated registers there.
2485// If this is a variadic function, the va_list pointer will begin with
2486// these values; otherwise, this reassembles a (byval) structure that
2487// was split between registers and memory.
2488void
2489ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2490 DebugLoc dl, SDValue &Chain,
2491 unsigned ArgOffset) const {
2492 MachineFunction &MF = DAG.getMachineFunction();
2493 MachineFrameInfo *MFI = MF.getFrameInfo();
2494 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2495 unsigned firstRegToSaveIndex;
2496 if (CCInfo.isFirstByValRegValid())
2497 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2498 else {
2499 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2500 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2501 }
2502
2503 unsigned VARegSize, VARegSaveSize;
2504 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2505 if (VARegSaveSize) {
2506 // If this function is vararg, store any remaining integer argument regs
2507 // to their spots on the stack so that they may be loaded by deferencing
2508 // the result of va_next.
2509 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002510 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2511 ArgOffset + VARegSaveSize
2512 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002513 false));
2514 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2515 getPointerTy());
2516
2517 SmallVector<SDValue, 4> MemOps;
2518 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
Craig Topper44d23822012-02-22 05:59:10 +00002519 const TargetRegisterClass *RC;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002520 if (AFI->isThumb1OnlyFunction())
2521 RC = ARM::tGPRRegisterClass;
2522 else
2523 RC = ARM::GPRRegisterClass;
2524
2525 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2526 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2527 SDValue Store =
2528 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002529 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002530 false, false, 0);
2531 MemOps.push_back(Store);
2532 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2533 DAG.getConstant(4, getPointerTy()));
2534 }
2535 if (!MemOps.empty())
2536 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2537 &MemOps[0], MemOps.size());
2538 } else
2539 // This will point to the next argument passed via stack.
2540 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2541}
2542
Bob Wilson5bafff32009-06-22 23:27:02 +00002543SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002544ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002545 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002546 const SmallVectorImpl<ISD::InputArg>
2547 &Ins,
2548 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002549 SmallVectorImpl<SDValue> &InVals)
2550 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002551 MachineFunction &MF = DAG.getMachineFunction();
2552 MachineFrameInfo *MFI = MF.getFrameInfo();
2553
Bob Wilson1f595bb2009-04-17 19:07:39 +00002554 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2555
2556 // Assign locations to all of the incoming arguments.
2557 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002558 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2559 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002560 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002561 CCAssignFnForNode(CallConv, /* Return*/ false,
2562 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002563
2564 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002565 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002566
Stuart Hastingsf222e592011-02-28 17:17:53 +00002567 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002568 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2569 CCValAssign &VA = ArgLocs[i];
2570
Bob Wilsondee46d72009-04-17 20:35:10 +00002571 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002572 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002573 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002574
Bob Wilson1f595bb2009-04-17 19:07:39 +00002575 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002576 // f64 and vector types are split up into multiple registers or
2577 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002578 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002579 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002580 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002581 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002582 SDValue ArgValue2;
2583 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002584 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002585 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2586 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002587 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002588 false, false, false, 0);
Bob Wilson6a234f02010-04-13 22:03:22 +00002589 } else {
2590 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2591 Chain, DAG, dl);
2592 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002593 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2594 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002595 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002596 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002597 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2598 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002599 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002600
Bob Wilson5bafff32009-06-22 23:27:02 +00002601 } else {
Craig Topper44d23822012-02-22 05:59:10 +00002602 const TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002603
Owen Anderson825b72b2009-08-11 20:47:22 +00002604 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002605 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002606 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002607 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002608 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002609 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002610 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002611 RC = (AFI->isThumb1OnlyFunction() ?
2612 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002613 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002614 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002615
2616 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002617 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002618 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002619 }
2620
2621 // If this is an 8 or 16-bit value, it is really passed promoted
2622 // to 32 bits. Insert an assert[sz]ext to capture this, then
2623 // truncate to the right size.
2624 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002625 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002626 case CCValAssign::Full: break;
2627 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002628 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002629 break;
2630 case CCValAssign::SExt:
2631 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2632 DAG.getValueType(VA.getValVT()));
2633 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2634 break;
2635 case CCValAssign::ZExt:
2636 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2637 DAG.getValueType(VA.getValVT()));
2638 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2639 break;
2640 }
2641
Dan Gohman98ca4f22009-08-05 01:29:28 +00002642 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002643
2644 } else { // VA.isRegLoc()
2645
2646 // sanity check
2647 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002648 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002649
Stuart Hastingsf222e592011-02-28 17:17:53 +00002650 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002651
Stuart Hastingsf222e592011-02-28 17:17:53 +00002652 // Some Ins[] entries become multiple ArgLoc[] entries.
2653 // Process them only once.
2654 if (index != lastInsIndex)
2655 {
2656 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002657 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002658 // This can be changed with more analysis.
2659 // In case of tail call optimization mark all arguments mutable.
2660 // Since they could be overwritten by lowering of arguments in case of
2661 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002662 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002663 unsigned VARegSize, VARegSaveSize;
2664 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2665 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2666 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002667 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002668 int FI = MFI->CreateFixedObject(Bytes,
2669 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002670 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2671 } else {
2672 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2673 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002674
Stuart Hastingsf222e592011-02-28 17:17:53 +00002675 // Create load nodes to retrieve arguments from the stack.
2676 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2677 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2678 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002679 false, false, false, 0));
Stuart Hastingsf222e592011-02-28 17:17:53 +00002680 }
2681 lastInsIndex = index;
2682 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002683 }
2684 }
2685
2686 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002687 if (isVarArg)
2688 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002689
Dan Gohman98ca4f22009-08-05 01:29:28 +00002690 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002691}
2692
2693/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002694static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002695 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002696 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002697 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002698 // Maybe this has already been legalized into the constant pool?
2699 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002700 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002701 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002702 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002703 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002704 }
2705 }
2706 return false;
2707}
2708
Evan Chenga8e29892007-01-19 07:51:42 +00002709/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2710/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002711SDValue
2712ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002713 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002714 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002715 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002716 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002717 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002718 // Constant does not fit, try adjusting it by one?
2719 switch (CC) {
2720 default: break;
2721 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002722 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002723 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002724 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002725 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002726 }
2727 break;
2728 case ISD::SETULT:
2729 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002730 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002731 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002732 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002733 }
2734 break;
2735 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002736 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002737 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002738 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002739 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002740 }
2741 break;
2742 case ISD::SETULE:
2743 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002744 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002745 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002746 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002747 }
2748 break;
2749 }
2750 }
2751 }
2752
2753 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002754 ARMISD::NodeType CompareType;
2755 switch (CondCode) {
2756 default:
2757 CompareType = ARMISD::CMP;
2758 break;
2759 case ARMCC::EQ:
2760 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002761 // Uses only Z Flag
2762 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002763 break;
2764 }
Evan Cheng218977b2010-07-13 19:27:42 +00002765 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002766 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002767}
2768
2769/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002770SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002771ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002772 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002773 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002774 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002775 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002776 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002777 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2778 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002779}
2780
Bob Wilson79f56c92011-03-08 01:17:20 +00002781/// duplicateCmp - Glue values can have only one use, so this function
2782/// duplicates a comparison node.
2783SDValue
2784ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2785 unsigned Opc = Cmp.getOpcode();
2786 DebugLoc DL = Cmp.getDebugLoc();
2787 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2788 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2789
2790 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2791 Cmp = Cmp.getOperand(0);
2792 Opc = Cmp.getOpcode();
2793 if (Opc == ARMISD::CMPFP)
2794 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2795 else {
2796 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2797 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2798 }
2799 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2800}
2801
Bill Wendlingde2b1512010-08-11 08:43:16 +00002802SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2803 SDValue Cond = Op.getOperand(0);
2804 SDValue SelectTrue = Op.getOperand(1);
2805 SDValue SelectFalse = Op.getOperand(2);
2806 DebugLoc dl = Op.getDebugLoc();
2807
2808 // Convert:
2809 //
2810 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2811 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2812 //
2813 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2814 const ConstantSDNode *CMOVTrue =
2815 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2816 const ConstantSDNode *CMOVFalse =
2817 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2818
2819 if (CMOVTrue && CMOVFalse) {
2820 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2821 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2822
2823 SDValue True;
2824 SDValue False;
2825 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2826 True = SelectTrue;
2827 False = SelectFalse;
2828 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2829 True = SelectFalse;
2830 False = SelectTrue;
2831 }
2832
2833 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002834 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002835 SDValue ARMcc = Cond.getOperand(2);
2836 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002837 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002838 assert(True.getValueType() == VT);
2839 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002840 }
2841 }
2842 }
2843
Dan Gohmandb953892012-02-24 00:09:36 +00002844 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
2845 // undefined bits before doing a full-word comparison with zero.
2846 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
2847 DAG.getConstant(1, Cond.getValueType()));
2848
Bill Wendlingde2b1512010-08-11 08:43:16 +00002849 return DAG.getSelectCC(dl, Cond,
2850 DAG.getConstant(0, Cond.getValueType()),
2851 SelectTrue, SelectFalse, ISD::SETNE);
2852}
2853
Dan Gohmand858e902010-04-17 15:26:15 +00002854SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002855 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002856 SDValue LHS = Op.getOperand(0);
2857 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002858 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002859 SDValue TrueVal = Op.getOperand(2);
2860 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002861 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002862
Owen Anderson825b72b2009-08-11 20:47:22 +00002863 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002864 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002865 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002866 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Jim Grosbachb04546f2011-09-13 20:30:37 +00002867 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002868 }
2869
2870 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002871 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002872
Evan Cheng218977b2010-07-13 19:27:42 +00002873 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2874 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002875 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002876 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002877 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002878 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002879 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002880 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002881 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002882 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002883 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002884 }
2885 return Result;
2886}
2887
Evan Cheng218977b2010-07-13 19:27:42 +00002888/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2889/// to morph to an integer compare sequence.
2890static bool canChangeToInt(SDValue Op, bool &SeenZero,
2891 const ARMSubtarget *Subtarget) {
2892 SDNode *N = Op.getNode();
2893 if (!N->hasOneUse())
2894 // Otherwise it requires moving the value from fp to integer registers.
2895 return false;
2896 if (!N->getNumValues())
2897 return false;
2898 EVT VT = Op.getValueType();
2899 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2900 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2901 // vmrs are very slow, e.g. cortex-a8.
2902 return false;
2903
2904 if (isFloatingPointZero(Op)) {
2905 SeenZero = true;
2906 return true;
2907 }
2908 return ISD::isNormalLoad(N);
2909}
2910
2911static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2912 if (isFloatingPointZero(Op))
2913 return DAG.getConstant(0, MVT::i32);
2914
2915 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2916 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002917 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002918 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002919 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002920
2921 llvm_unreachable("Unknown VFP cmp argument!");
2922}
2923
2924static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2925 SDValue &RetVal1, SDValue &RetVal2) {
2926 if (isFloatingPointZero(Op)) {
2927 RetVal1 = DAG.getConstant(0, MVT::i32);
2928 RetVal2 = DAG.getConstant(0, MVT::i32);
2929 return;
2930 }
2931
2932 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2933 SDValue Ptr = Ld->getBasePtr();
2934 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2935 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002936 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002937 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002938 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002939
2940 EVT PtrType = Ptr.getValueType();
2941 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2942 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2943 PtrType, Ptr, DAG.getConstant(4, PtrType));
2944 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2945 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002946 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002947 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002948 Ld->isInvariant(), NewAlign);
Evan Cheng218977b2010-07-13 19:27:42 +00002949 return;
2950 }
2951
2952 llvm_unreachable("Unknown VFP cmp argument!");
2953}
2954
2955/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2956/// f32 and even f64 comparisons to integer ones.
2957SDValue
2958ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2959 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002960 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002961 SDValue LHS = Op.getOperand(2);
2962 SDValue RHS = Op.getOperand(3);
2963 SDValue Dest = Op.getOperand(4);
2964 DebugLoc dl = Op.getDebugLoc();
2965
2966 bool SeenZero = false;
2967 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2968 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002969 // If one of the operand is zero, it's safe to ignore the NaN case since
2970 // we only care about equality comparisons.
2971 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Bob Wilson1b772f92011-03-08 01:17:16 +00002972 // If unsafe fp math optimization is enabled and there are no other uses of
2973 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00002974 // to an integer comparison.
2975 if (CC == ISD::SETOEQ)
2976 CC = ISD::SETEQ;
2977 else if (CC == ISD::SETUNE)
2978 CC = ISD::SETNE;
2979
2980 SDValue ARMcc;
2981 if (LHS.getValueType() == MVT::f32) {
2982 LHS = bitcastf32Toi32(LHS, DAG);
2983 RHS = bitcastf32Toi32(RHS, DAG);
2984 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2985 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2986 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2987 Chain, Dest, ARMcc, CCR, Cmp);
2988 }
2989
2990 SDValue LHS1, LHS2;
2991 SDValue RHS1, RHS2;
2992 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2993 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2994 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2995 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002996 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002997 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2998 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2999 }
3000
3001 return SDValue();
3002}
3003
3004SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3005 SDValue Chain = Op.getOperand(0);
3006 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3007 SDValue LHS = Op.getOperand(2);
3008 SDValue RHS = Op.getOperand(3);
3009 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00003010 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003011
Owen Anderson825b72b2009-08-11 20:47:22 +00003012 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00003013 SDValue ARMcc;
3014 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003015 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00003016 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00003017 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00003018 }
3019
Owen Anderson825b72b2009-08-11 20:47:22 +00003020 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00003021
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003022 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng218977b2010-07-13 19:27:42 +00003023 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3024 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3025 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3026 if (Result.getNode())
3027 return Result;
3028 }
3029
Evan Chenga8e29892007-01-19 07:51:42 +00003030 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00003031 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003032
Evan Cheng218977b2010-07-13 19:27:42 +00003033 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3034 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003035 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003036 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003037 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00003038 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003039 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00003040 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3041 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00003042 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003043 }
3044 return Res;
3045}
3046
Dan Gohmand858e902010-04-17 15:26:15 +00003047SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00003048 SDValue Chain = Op.getOperand(0);
3049 SDValue Table = Op.getOperand(1);
3050 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003051 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003052
Owen Andersone50ed302009-08-10 22:56:29 +00003053 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00003054 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3055 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00003056 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00003057 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00003058 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00003059 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3060 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00003061 if (Subtarget->isThumb2()) {
3062 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3063 // which does another jump to the destination. This also makes it easier
3064 // to translate it to TBB / TBH later.
3065 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00003066 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00003067 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003068 }
Evan Cheng66ac5312009-07-25 00:33:29 +00003069 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00003070 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003071 MachinePointerInfo::getJumpTable(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003072 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003073 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003074 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00003075 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003076 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00003077 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003078 MachinePointerInfo::getJumpTable(),
3079 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003080 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003081 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003082 }
Evan Chenga8e29892007-01-19 07:51:42 +00003083}
3084
Eli Friedman14e809c2011-11-09 23:36:02 +00003085static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy873fd5f2012-02-20 09:24:05 +00003086 EVT VT = Op.getValueType();
3087 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14e809c2011-11-09 23:36:02 +00003088
James Molloy873fd5f2012-02-20 09:24:05 +00003089 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3090 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3091 return Op;
3092 return DAG.UnrollVectorOp(Op.getNode());
3093 }
3094
3095 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3096 "Invalid type for custom lowering!");
3097 if (VT != MVT::v4i16)
3098 return DAG.UnrollVectorOp(Op.getNode());
3099
3100 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3101 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman14e809c2011-11-09 23:36:02 +00003102}
3103
Bob Wilson76a312b2010-03-19 22:51:32 +00003104static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman14e809c2011-11-09 23:36:02 +00003105 EVT VT = Op.getValueType();
3106 if (VT.isVector())
3107 return LowerVectorFP_TO_INT(Op, DAG);
3108
Bob Wilson76a312b2010-03-19 22:51:32 +00003109 DebugLoc dl = Op.getDebugLoc();
3110 unsigned Opc;
3111
3112 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003113 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003114 case ISD::FP_TO_SINT:
3115 Opc = ARMISD::FTOSI;
3116 break;
3117 case ISD::FP_TO_UINT:
3118 Opc = ARMISD::FTOUI;
3119 break;
3120 }
3121 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003122 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003123}
3124
Cameron Zwarich3007d332011-03-29 21:41:55 +00003125static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3126 EVT VT = Op.getValueType();
3127 DebugLoc dl = Op.getDebugLoc();
3128
Eli Friedman14e809c2011-11-09 23:36:02 +00003129 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3130 if (VT.getVectorElementType() == MVT::f32)
3131 return Op;
3132 return DAG.UnrollVectorOp(Op.getNode());
3133 }
3134
Duncan Sands1f6a3292011-08-12 14:54:45 +00003135 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3136 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003137 if (VT != MVT::v4f32)
3138 return DAG.UnrollVectorOp(Op.getNode());
3139
3140 unsigned CastOpc;
3141 unsigned Opc;
3142 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003143 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003144 case ISD::SINT_TO_FP:
3145 CastOpc = ISD::SIGN_EXTEND;
3146 Opc = ISD::SINT_TO_FP;
3147 break;
3148 case ISD::UINT_TO_FP:
3149 CastOpc = ISD::ZERO_EXTEND;
3150 Opc = ISD::UINT_TO_FP;
3151 break;
3152 }
3153
3154 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3155 return DAG.getNode(Opc, dl, VT, Op);
3156}
3157
Bob Wilson76a312b2010-03-19 22:51:32 +00003158static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3159 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003160 if (VT.isVector())
3161 return LowerVectorINT_TO_FP(Op, DAG);
3162
Bob Wilson76a312b2010-03-19 22:51:32 +00003163 DebugLoc dl = Op.getDebugLoc();
3164 unsigned Opc;
3165
3166 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003167 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003168 case ISD::SINT_TO_FP:
3169 Opc = ARMISD::SITOF;
3170 break;
3171 case ISD::UINT_TO_FP:
3172 Opc = ARMISD::UITOF;
3173 break;
3174 }
3175
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003176 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003177 return DAG.getNode(Opc, dl, VT, Op);
3178}
3179
Evan Cheng515fe3a2010-07-08 02:08:50 +00003180SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003181 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003182 SDValue Tmp0 = Op.getOperand(0);
3183 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003184 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003185 EVT VT = Op.getValueType();
3186 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003187 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3188 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3189 bool UseNEON = !InGPR && Subtarget->hasNEON();
3190
3191 if (UseNEON) {
3192 // Use VBSL to copy the sign bit.
3193 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3194 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3195 DAG.getTargetConstant(EncodedVal, MVT::i32));
3196 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3197 if (VT == MVT::f64)
3198 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3199 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3200 DAG.getConstant(32, MVT::i32));
3201 else /*if (VT == MVT::f32)*/
3202 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3203 if (SrcVT == MVT::f32) {
3204 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3205 if (VT == MVT::f64)
3206 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3207 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3208 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003209 } else if (VT == MVT::f32)
3210 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3211 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3212 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003213 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3214 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3215
3216 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3217 MVT::i32);
3218 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3219 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3220 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003221
Evan Chenge573fb32011-02-23 02:24:55 +00003222 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3223 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3224 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003225 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003226 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3227 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3228 DAG.getConstant(0, MVT::i32));
3229 } else {
3230 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3231 }
3232
3233 return Res;
3234 }
Evan Chengc143dd42011-02-11 02:28:55 +00003235
3236 // Bitcast operand 1 to i32.
3237 if (SrcVT == MVT::f64)
3238 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3239 &Tmp1, 1).getValue(1);
3240 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3241
Evan Chenge573fb32011-02-23 02:24:55 +00003242 // Or in the signbit with integer operations.
3243 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3244 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3245 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3246 if (VT == MVT::f32) {
3247 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3248 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3249 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3250 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003251 }
3252
Evan Chenge573fb32011-02-23 02:24:55 +00003253 // f64: Or the high part with signbit and then combine two parts.
3254 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3255 &Tmp0, 1);
3256 SDValue Lo = Tmp0.getValue(0);
3257 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3258 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3259 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003260}
3261
Evan Cheng2457f2c2010-05-22 01:47:14 +00003262SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3263 MachineFunction &MF = DAG.getMachineFunction();
3264 MachineFrameInfo *MFI = MF.getFrameInfo();
3265 MFI->setReturnAddressIsTaken(true);
3266
3267 EVT VT = Op.getValueType();
3268 DebugLoc dl = Op.getDebugLoc();
3269 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3270 if (Depth) {
3271 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3272 SDValue Offset = DAG.getConstant(4, MVT::i32);
3273 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3274 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003275 MachinePointerInfo(), false, false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003276 }
3277
3278 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003279 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003280 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3281}
3282
Dan Gohmand858e902010-04-17 15:26:15 +00003283SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003284 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3285 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003286
Owen Andersone50ed302009-08-10 22:56:29 +00003287 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003288 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3289 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003290 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003291 ? ARM::R7 : ARM::R11;
3292 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3293 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003294 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3295 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003296 false, false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003297 return FrameAddr;
3298}
3299
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003300/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003301/// expand a bit convert where either the source or destination type is i64 to
3302/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3303/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3304/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003305static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003306 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3307 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003308 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003309
Bob Wilson9f3f0612010-04-17 05:30:19 +00003310 // This function is only supposed to be called for i64 types, either as the
3311 // source or destination of the bit convert.
3312 EVT SrcVT = Op.getValueType();
3313 EVT DstVT = N->getValueType(0);
3314 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003315 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003316
Bob Wilson9f3f0612010-04-17 05:30:19 +00003317 // Turn i64->f64 into VMOVDRR.
3318 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003319 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3320 DAG.getConstant(0, MVT::i32));
3321 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3322 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003323 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003324 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003325 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003326
Jim Grosbache5165492009-11-09 00:11:35 +00003327 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003328 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3329 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3330 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3331 // Merge the pieces into a single i64 value.
3332 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3333 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003334
Bob Wilson9f3f0612010-04-17 05:30:19 +00003335 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003336}
3337
Bob Wilson5bafff32009-06-22 23:27:02 +00003338/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003339/// Zero vectors are used to represent vector negation and in those cases
3340/// will be implemented with the NEON VNEG instruction. However, VNEG does
3341/// not support i64 elements, so sometimes the zero vectors will need to be
3342/// explicitly constructed. Regardless, use a canonical VMOV to create the
3343/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003344static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003345 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003346 // The canonical modified immediate encoding of a zero vector is....0!
3347 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3348 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3349 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003350 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003351}
3352
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003353/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3354/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003355SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3356 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003357 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3358 EVT VT = Op.getValueType();
3359 unsigned VTBits = VT.getSizeInBits();
3360 DebugLoc dl = Op.getDebugLoc();
3361 SDValue ShOpLo = Op.getOperand(0);
3362 SDValue ShOpHi = Op.getOperand(1);
3363 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003364 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003365 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003366
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003367 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3368
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003369 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3370 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3371 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3372 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3373 DAG.getConstant(VTBits, MVT::i32));
3374 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3375 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003376 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003377
3378 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3379 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003380 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003381 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003382 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003383 CCR, Cmp);
3384
3385 SDValue Ops[2] = { Lo, Hi };
3386 return DAG.getMergeValues(Ops, 2, dl);
3387}
3388
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003389/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3390/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003391SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3392 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003393 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3394 EVT VT = Op.getValueType();
3395 unsigned VTBits = VT.getSizeInBits();
3396 DebugLoc dl = Op.getDebugLoc();
3397 SDValue ShOpLo = Op.getOperand(0);
3398 SDValue ShOpHi = Op.getOperand(1);
3399 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003400 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003401
3402 assert(Op.getOpcode() == ISD::SHL_PARTS);
3403 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3404 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3405 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3406 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3407 DAG.getConstant(VTBits, MVT::i32));
3408 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3409 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3410
3411 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3412 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3413 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003414 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003415 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003416 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003417 CCR, Cmp);
3418
3419 SDValue Ops[2] = { Lo, Hi };
3420 return DAG.getMergeValues(Ops, 2, dl);
3421}
3422
Jim Grosbach4725ca72010-09-08 03:54:02 +00003423SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003424 SelectionDAG &DAG) const {
3425 // The rounding mode is in bits 23:22 of the FPSCR.
3426 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3427 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3428 // so that the shift + and get folded into a bitfield extract.
3429 DebugLoc dl = Op.getDebugLoc();
3430 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3431 DAG.getConstant(Intrinsic::arm_get_fpscr,
3432 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003433 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003434 DAG.getConstant(1U << 22, MVT::i32));
3435 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3436 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003437 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003438 DAG.getConstant(3, MVT::i32));
3439}
3440
Jim Grosbach3482c802010-01-18 19:58:49 +00003441static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3442 const ARMSubtarget *ST) {
3443 EVT VT = N->getValueType(0);
3444 DebugLoc dl = N->getDebugLoc();
3445
3446 if (!ST->hasV6T2Ops())
3447 return SDValue();
3448
3449 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3450 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3451}
3452
Bob Wilson5bafff32009-06-22 23:27:02 +00003453static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3454 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003455 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003456 DebugLoc dl = N->getDebugLoc();
3457
Bob Wilsond5448bb2010-11-18 21:16:28 +00003458 if (!VT.isVector())
3459 return SDValue();
3460
Bob Wilson5bafff32009-06-22 23:27:02 +00003461 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003462 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003463
Bob Wilsond5448bb2010-11-18 21:16:28 +00003464 // Left shifts translate directly to the vshiftu intrinsic.
3465 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003466 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003467 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3468 N->getOperand(0), N->getOperand(1));
3469
3470 assert((N->getOpcode() == ISD::SRA ||
3471 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3472
3473 // NEON uses the same intrinsics for both left and right shifts. For
3474 // right shifts, the shift amounts are negative, so negate the vector of
3475 // shift amounts.
3476 EVT ShiftVT = N->getOperand(1).getValueType();
3477 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3478 getZeroVector(ShiftVT, DAG, dl),
3479 N->getOperand(1));
3480 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3481 Intrinsic::arm_neon_vshifts :
3482 Intrinsic::arm_neon_vshiftu);
3483 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3484 DAG.getConstant(vshiftInt, MVT::i32),
3485 N->getOperand(0), NegatedCount);
3486}
3487
3488static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3489 const ARMSubtarget *ST) {
3490 EVT VT = N->getValueType(0);
3491 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003492
Eli Friedmance392eb2009-08-22 03:13:10 +00003493 // We can get here for a node like i32 = ISD::SHL i32, i64
3494 if (VT != MVT::i64)
3495 return SDValue();
3496
3497 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003498 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003499
Chris Lattner27a6c732007-11-24 07:07:01 +00003500 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3501 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003502 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003503 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003504
Chris Lattner27a6c732007-11-24 07:07:01 +00003505 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003506 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003507
Chris Lattner27a6c732007-11-24 07:07:01 +00003508 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003509 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003510 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003511 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003512 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003513
Chris Lattner27a6c732007-11-24 07:07:01 +00003514 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3515 // captures the result into a carry flag.
3516 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003517 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003518
Chris Lattner27a6c732007-11-24 07:07:01 +00003519 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003520 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003521
Chris Lattner27a6c732007-11-24 07:07:01 +00003522 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003523 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003524}
3525
Bob Wilson5bafff32009-06-22 23:27:02 +00003526static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3527 SDValue TmpOp0, TmpOp1;
3528 bool Invert = false;
3529 bool Swap = false;
3530 unsigned Opc = 0;
3531
3532 SDValue Op0 = Op.getOperand(0);
3533 SDValue Op1 = Op.getOperand(1);
3534 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003535 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003536 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3537 DebugLoc dl = Op.getDebugLoc();
3538
3539 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3540 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003541 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003542 case ISD::SETUNE:
3543 case ISD::SETNE: Invert = true; // Fallthrough
3544 case ISD::SETOEQ:
3545 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3546 case ISD::SETOLT:
3547 case ISD::SETLT: Swap = true; // Fallthrough
3548 case ISD::SETOGT:
3549 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3550 case ISD::SETOLE:
3551 case ISD::SETLE: Swap = true; // Fallthrough
3552 case ISD::SETOGE:
3553 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3554 case ISD::SETUGE: Swap = true; // Fallthrough
3555 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3556 case ISD::SETUGT: Swap = true; // Fallthrough
3557 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3558 case ISD::SETUEQ: Invert = true; // Fallthrough
3559 case ISD::SETONE:
3560 // Expand this to (OLT | OGT).
3561 TmpOp0 = Op0;
3562 TmpOp1 = Op1;
3563 Opc = ISD::OR;
3564 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3565 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3566 break;
3567 case ISD::SETUO: Invert = true; // Fallthrough
3568 case ISD::SETO:
3569 // Expand this to (OLT | OGE).
3570 TmpOp0 = Op0;
3571 TmpOp1 = Op1;
3572 Opc = ISD::OR;
3573 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3574 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3575 break;
3576 }
3577 } else {
3578 // Integer comparisons.
3579 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003580 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003581 case ISD::SETNE: Invert = true;
3582 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3583 case ISD::SETLT: Swap = true;
3584 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3585 case ISD::SETLE: Swap = true;
3586 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3587 case ISD::SETULT: Swap = true;
3588 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3589 case ISD::SETULE: Swap = true;
3590 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3591 }
3592
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003593 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003594 if (Opc == ARMISD::VCEQ) {
3595
3596 SDValue AndOp;
3597 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3598 AndOp = Op0;
3599 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3600 AndOp = Op1;
3601
3602 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003603 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003604 AndOp = AndOp.getOperand(0);
3605
3606 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3607 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003608 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3609 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003610 Invert = !Invert;
3611 }
3612 }
3613 }
3614
3615 if (Swap)
3616 std::swap(Op0, Op1);
3617
Owen Andersonc24cb352010-11-08 23:21:22 +00003618 // If one of the operands is a constant vector zero, attempt to fold the
3619 // comparison to a specialized compare-against-zero form.
3620 SDValue SingleOp;
3621 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3622 SingleOp = Op0;
3623 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3624 if (Opc == ARMISD::VCGE)
3625 Opc = ARMISD::VCLEZ;
3626 else if (Opc == ARMISD::VCGT)
3627 Opc = ARMISD::VCLTZ;
3628 SingleOp = Op1;
3629 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003630
Owen Andersonc24cb352010-11-08 23:21:22 +00003631 SDValue Result;
3632 if (SingleOp.getNode()) {
3633 switch (Opc) {
3634 case ARMISD::VCEQ:
3635 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3636 case ARMISD::VCGE:
3637 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3638 case ARMISD::VCLEZ:
3639 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3640 case ARMISD::VCGT:
3641 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3642 case ARMISD::VCLTZ:
3643 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3644 default:
3645 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3646 }
3647 } else {
3648 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3649 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003650
3651 if (Invert)
3652 Result = DAG.getNOT(dl, Result, VT);
3653
3654 return Result;
3655}
3656
Bob Wilsond3c42842010-06-14 22:19:57 +00003657/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3658/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003659/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003660static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3661 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003662 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003663 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003664
Bob Wilson827b2102010-06-15 19:05:35 +00003665 // SplatBitSize is set to the smallest size that splats the vector, so a
3666 // zero vector will always have SplatBitSize == 8. However, NEON modified
3667 // immediate instructions others than VMOV do not support the 8-bit encoding
3668 // of a zero vector, and the default encoding of zero is supposed to be the
3669 // 32-bit version.
3670 if (SplatBits == 0)
3671 SplatBitSize = 32;
3672
Bob Wilson5bafff32009-06-22 23:27:02 +00003673 switch (SplatBitSize) {
3674 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003675 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003676 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003677 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003678 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003679 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003680 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003681 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003682 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003683
3684 case 16:
3685 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003686 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003687 if ((SplatBits & ~0xff) == 0) {
3688 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003689 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003690 Imm = SplatBits;
3691 break;
3692 }
3693 if ((SplatBits & ~0xff00) == 0) {
3694 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003695 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003696 Imm = SplatBits >> 8;
3697 break;
3698 }
3699 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003700
3701 case 32:
3702 // NEON's 32-bit VMOV supports splat values where:
3703 // * only one byte is nonzero, or
3704 // * the least significant byte is 0xff and the second byte is nonzero, or
3705 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003706 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003707 if ((SplatBits & ~0xff) == 0) {
3708 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003709 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003710 Imm = SplatBits;
3711 break;
3712 }
3713 if ((SplatBits & ~0xff00) == 0) {
3714 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003715 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003716 Imm = SplatBits >> 8;
3717 break;
3718 }
3719 if ((SplatBits & ~0xff0000) == 0) {
3720 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003721 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003722 Imm = SplatBits >> 16;
3723 break;
3724 }
3725 if ((SplatBits & ~0xff000000) == 0) {
3726 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003727 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003728 Imm = SplatBits >> 24;
3729 break;
3730 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003731
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003732 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3733 if (type == OtherModImm) return SDValue();
3734
Bob Wilson5bafff32009-06-22 23:27:02 +00003735 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003736 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3737 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003738 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003739 Imm = SplatBits >> 8;
3740 SplatBits |= 0xff;
3741 break;
3742 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003743
3744 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003745 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3746 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003747 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003748 Imm = SplatBits >> 16;
3749 SplatBits |= 0xffff;
3750 break;
3751 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003752
3753 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3754 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3755 // VMOV.I32. A (very) minor optimization would be to replicate the value
3756 // and fall through here to test for a valid 64-bit splat. But, then the
3757 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003758 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003759
3760 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003761 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003762 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003763 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003764 uint64_t BitMask = 0xff;
3765 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003766 unsigned ImmMask = 1;
3767 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003768 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003769 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003770 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003771 Imm |= ImmMask;
3772 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003773 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003774 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003775 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003776 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003777 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003778 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003779 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003780 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003781 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003782 break;
3783 }
3784
Bob Wilson1a913ed2010-06-11 21:34:50 +00003785 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003786 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003787 }
3788
Bob Wilsoncba270d2010-07-13 21:16:48 +00003789 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3790 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003791}
3792
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003793static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003794 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003795 unsigned NumElts = VT.getVectorNumElements();
3796 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003797
3798 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3799 if (M[0] < 0)
3800 return false;
3801
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003802 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003803
3804 // If this is a VEXT shuffle, the immediate value is the index of the first
3805 // element. The other shuffle indices must be the successive elements after
3806 // the first one.
3807 unsigned ExpectedElt = Imm;
3808 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003809 // Increment the expected index. If it wraps around, it may still be
3810 // a VEXT but the source vectors must be swapped.
3811 ExpectedElt += 1;
3812 if (ExpectedElt == NumElts * 2) {
3813 ExpectedElt = 0;
3814 ReverseVEXT = true;
3815 }
3816
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003817 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003818 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003819 return false;
3820 }
3821
3822 // Adjust the index value if the source operands will be swapped.
3823 if (ReverseVEXT)
3824 Imm -= NumElts;
3825
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003826 return true;
3827}
3828
Bob Wilson8bb9e482009-07-26 00:39:34 +00003829/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3830/// instruction with the specified blocksize. (The order of the elements
3831/// within each block of the vector is reversed.)
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003832static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003833 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3834 "Only possible block sizes for VREV are: 16, 32, 64");
3835
Bob Wilson8bb9e482009-07-26 00:39:34 +00003836 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003837 if (EltSz == 64)
3838 return false;
3839
3840 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003841 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003842 // If the first shuffle index is UNDEF, be optimistic.
3843 if (M[0] < 0)
3844 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003845
3846 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3847 return false;
3848
3849 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003850 if (M[i] < 0) continue; // ignore UNDEF indices
3851 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003852 return false;
3853 }
3854
3855 return true;
3856}
3857
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003858static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003859 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3860 // range, then 0 is placed into the resulting vector. So pretty much any mask
3861 // of 8 elements can work here.
3862 return VT == MVT::v8i8 && M.size() == 8;
3863}
3864
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003865static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003866 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3867 if (EltSz == 64)
3868 return false;
3869
Bob Wilsonc692cb72009-08-21 20:54:19 +00003870 unsigned NumElts = VT.getVectorNumElements();
3871 WhichResult = (M[0] == 0 ? 0 : 1);
3872 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003873 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3874 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003875 return false;
3876 }
3877 return true;
3878}
3879
Bob Wilson324f4f12009-12-03 06:40:55 +00003880/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3881/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3882/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003883static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00003884 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3885 if (EltSz == 64)
3886 return false;
3887
3888 unsigned NumElts = VT.getVectorNumElements();
3889 WhichResult = (M[0] == 0 ? 0 : 1);
3890 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003891 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3892 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003893 return false;
3894 }
3895 return true;
3896}
3897
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003898static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003899 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3900 if (EltSz == 64)
3901 return false;
3902
Bob Wilsonc692cb72009-08-21 20:54:19 +00003903 unsigned NumElts = VT.getVectorNumElements();
3904 WhichResult = (M[0] == 0 ? 0 : 1);
3905 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003906 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003907 if ((unsigned) M[i] != 2 * i + WhichResult)
3908 return false;
3909 }
3910
3911 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003912 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003913 return false;
3914
3915 return true;
3916}
3917
Bob Wilson324f4f12009-12-03 06:40:55 +00003918/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3919/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3920/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003921static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00003922 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3923 if (EltSz == 64)
3924 return false;
3925
3926 unsigned Half = VT.getVectorNumElements() / 2;
3927 WhichResult = (M[0] == 0 ? 0 : 1);
3928 for (unsigned j = 0; j != 2; ++j) {
3929 unsigned Idx = WhichResult;
3930 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003931 int MIdx = M[i + j * Half];
3932 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003933 return false;
3934 Idx += 2;
3935 }
3936 }
3937
3938 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3939 if (VT.is64BitVector() && EltSz == 32)
3940 return false;
3941
3942 return true;
3943}
3944
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003945static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003946 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3947 if (EltSz == 64)
3948 return false;
3949
Bob Wilsonc692cb72009-08-21 20:54:19 +00003950 unsigned NumElts = VT.getVectorNumElements();
3951 WhichResult = (M[0] == 0 ? 0 : 1);
3952 unsigned Idx = WhichResult * NumElts / 2;
3953 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003954 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3955 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003956 return false;
3957 Idx += 1;
3958 }
3959
3960 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003961 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003962 return false;
3963
3964 return true;
3965}
3966
Bob Wilson324f4f12009-12-03 06:40:55 +00003967/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3968/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3969/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003970static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00003971 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3972 if (EltSz == 64)
3973 return false;
3974
3975 unsigned NumElts = VT.getVectorNumElements();
3976 WhichResult = (M[0] == 0 ? 0 : 1);
3977 unsigned Idx = WhichResult * NumElts / 2;
3978 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003979 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3980 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003981 return false;
3982 Idx += 1;
3983 }
3984
3985 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3986 if (VT.is64BitVector() && EltSz == 32)
3987 return false;
3988
3989 return true;
3990}
3991
Dale Johannesenf630c712010-07-29 20:10:08 +00003992// If N is an integer constant that can be moved into a register in one
3993// instruction, return an SDValue of such a constant (will become a MOV
3994// instruction). Otherwise return null.
3995static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3996 const ARMSubtarget *ST, DebugLoc dl) {
3997 uint64_t Val;
3998 if (!isa<ConstantSDNode>(N))
3999 return SDValue();
4000 Val = cast<ConstantSDNode>(N)->getZExtValue();
4001
4002 if (ST->isThumb1Only()) {
4003 if (Val <= 255 || ~Val <= 255)
4004 return DAG.getConstant(Val, MVT::i32);
4005 } else {
4006 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
4007 return DAG.getConstant(Val, MVT::i32);
4008 }
4009 return SDValue();
4010}
4011
Bob Wilson5bafff32009-06-22 23:27:02 +00004012// If this is a case we can't handle, return null and let the default
4013// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00004014SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
4015 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00004016 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00004017 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004018 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004019
4020 APInt SplatBits, SplatUndef;
4021 unsigned SplatBitSize;
4022 bool HasAnyUndefs;
4023 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004024 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00004025 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00004026 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00004027 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00004028 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004029 DAG, VmovVT, VT.is128BitVector(),
4030 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004031 if (Val.getNode()) {
4032 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004033 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004034 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004035
4036 // Try an immediate VMVN.
Eli Friedman8e4d0422011-10-13 22:40:23 +00004037 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004038 Val = isNEONModifiedImm(NegatedImm,
4039 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004040 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004041 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004042 if (Val.getNode()) {
4043 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004044 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004045 }
Evan Chengeaa192a2011-11-15 02:12:34 +00004046
4047 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedman2f21e8c2011-12-15 22:56:53 +00004048 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedmaneffab8f2011-12-09 23:54:42 +00004049 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Chengeaa192a2011-11-15 02:12:34 +00004050 if (ImmVal != -1) {
4051 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4052 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4053 }
4054 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004055 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00004056 }
4057
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004058 // Scan through the operands to see if only one value is used.
4059 unsigned NumElts = VT.getVectorNumElements();
4060 bool isOnlyLowElement = true;
4061 bool usesOnlyOneValue = true;
4062 bool isConstant = true;
4063 SDValue Value;
4064 for (unsigned i = 0; i < NumElts; ++i) {
4065 SDValue V = Op.getOperand(i);
4066 if (V.getOpcode() == ISD::UNDEF)
4067 continue;
4068 if (i > 0)
4069 isOnlyLowElement = false;
4070 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4071 isConstant = false;
4072
4073 if (!Value.getNode())
4074 Value = V;
4075 else if (V != Value)
4076 usesOnlyOneValue = false;
4077 }
4078
4079 if (!Value.getNode())
4080 return DAG.getUNDEF(VT);
4081
4082 if (isOnlyLowElement)
4083 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4084
Dale Johannesenf630c712010-07-29 20:10:08 +00004085 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4086
Dale Johannesen575cd142010-10-19 20:00:17 +00004087 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4088 // i32 and try again.
4089 if (usesOnlyOneValue && EltSize <= 32) {
4090 if (!isConstant)
4091 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4092 if (VT.getVectorElementType().isFloatingPoint()) {
4093 SmallVector<SDValue, 8> Ops;
4094 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004095 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00004096 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00004097 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
4098 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00004099 Val = LowerBUILD_VECTOR(Val, DAG, ST);
4100 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004101 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004102 }
Dale Johannesen575cd142010-10-19 20:00:17 +00004103 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
4104 if (Val.getNode())
4105 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004106 }
4107
4108 // If all elements are constants and the case above didn't get hit, fall back
4109 // to the default expansion, which will generate a load from the constant
4110 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004111 if (isConstant)
4112 return SDValue();
4113
Bob Wilson11a1dff2011-01-07 21:37:30 +00004114 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4115 if (NumElts >= 4) {
4116 SDValue shuffle = ReconstructShuffle(Op, DAG);
4117 if (shuffle != SDValue())
4118 return shuffle;
4119 }
4120
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004121 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004122 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4123 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004124 if (EltSize >= 32) {
4125 // Do the expansion with floating-point types, since that is what the VFP
4126 // registers are defined to use, and since i64 is not legal.
4127 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4128 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004129 SmallVector<SDValue, 8> Ops;
4130 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004131 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004132 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004133 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004134 }
4135
4136 return SDValue();
4137}
4138
Bob Wilson11a1dff2011-01-07 21:37:30 +00004139// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004140// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004141SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4142 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004143 DebugLoc dl = Op.getDebugLoc();
4144 EVT VT = Op.getValueType();
4145 unsigned NumElts = VT.getVectorNumElements();
4146
4147 SmallVector<SDValue, 2> SourceVecs;
4148 SmallVector<unsigned, 2> MinElts;
4149 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004150
Bob Wilson11a1dff2011-01-07 21:37:30 +00004151 for (unsigned i = 0; i < NumElts; ++i) {
4152 SDValue V = Op.getOperand(i);
4153 if (V.getOpcode() == ISD::UNDEF)
4154 continue;
4155 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4156 // A shuffle can only come from building a vector from various
4157 // elements of other vectors.
4158 return SDValue();
Eli Friedman46995fa2011-10-14 23:58:49 +00004159 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
4160 VT.getVectorElementType()) {
4161 // This code doesn't know how to handle shuffles where the vector
4162 // element types do not match (this happens because type legalization
4163 // promotes the return type of EXTRACT_VECTOR_ELT).
4164 // FIXME: It might be appropriate to extend this code to handle
4165 // mismatched types.
4166 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004167 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004168
Bob Wilson11a1dff2011-01-07 21:37:30 +00004169 // Record this extraction against the appropriate vector if possible...
4170 SDValue SourceVec = V.getOperand(0);
4171 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4172 bool FoundSource = false;
4173 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4174 if (SourceVecs[j] == SourceVec) {
4175 if (MinElts[j] > EltNo)
4176 MinElts[j] = EltNo;
4177 if (MaxElts[j] < EltNo)
4178 MaxElts[j] = EltNo;
4179 FoundSource = true;
4180 break;
4181 }
4182 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004183
Bob Wilson11a1dff2011-01-07 21:37:30 +00004184 // Or record a new source if not...
4185 if (!FoundSource) {
4186 SourceVecs.push_back(SourceVec);
4187 MinElts.push_back(EltNo);
4188 MaxElts.push_back(EltNo);
4189 }
4190 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004191
Bob Wilson11a1dff2011-01-07 21:37:30 +00004192 // Currently only do something sane when at most two source vectors
4193 // involved.
4194 if (SourceVecs.size() > 2)
4195 return SDValue();
4196
4197 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4198 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004199
Bob Wilson11a1dff2011-01-07 21:37:30 +00004200 // This loop extracts the usage patterns of the source vectors
4201 // and prepares appropriate SDValues for a shuffle if possible.
4202 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4203 if (SourceVecs[i].getValueType() == VT) {
4204 // No VEXT necessary
4205 ShuffleSrcs[i] = SourceVecs[i];
4206 VEXTOffsets[i] = 0;
4207 continue;
4208 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4209 // It probably isn't worth padding out a smaller vector just to
4210 // break it down again in a shuffle.
4211 return SDValue();
4212 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004213
Bob Wilson11a1dff2011-01-07 21:37:30 +00004214 // Since only 64-bit and 128-bit vectors are legal on ARM and
4215 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004216 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4217 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004218
Bob Wilson11a1dff2011-01-07 21:37:30 +00004219 if (MaxElts[i] - MinElts[i] >= NumElts) {
4220 // Span too large for a VEXT to cope
4221 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004222 }
4223
Bob Wilson11a1dff2011-01-07 21:37:30 +00004224 if (MinElts[i] >= NumElts) {
4225 // The extraction can just take the second half
4226 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004227 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4228 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004229 DAG.getIntPtrConstant(NumElts));
4230 } else if (MaxElts[i] < NumElts) {
4231 // The extraction can just take the first half
4232 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004233 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4234 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004235 DAG.getIntPtrConstant(0));
4236 } else {
4237 // An actual VEXT is needed
4238 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004239 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4240 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004241 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004242 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4243 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004244 DAG.getIntPtrConstant(NumElts));
4245 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4246 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4247 }
4248 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004249
Bob Wilson11a1dff2011-01-07 21:37:30 +00004250 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004251
Bob Wilson11a1dff2011-01-07 21:37:30 +00004252 for (unsigned i = 0; i < NumElts; ++i) {
4253 SDValue Entry = Op.getOperand(i);
4254 if (Entry.getOpcode() == ISD::UNDEF) {
4255 Mask.push_back(-1);
4256 continue;
4257 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004258
Bob Wilson11a1dff2011-01-07 21:37:30 +00004259 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004260 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4261 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004262 if (ExtractVec == SourceVecs[0]) {
4263 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4264 } else {
4265 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4266 }
4267 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004268
Bob Wilson11a1dff2011-01-07 21:37:30 +00004269 // Final check before we try to produce nonsense...
4270 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004271 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4272 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004273
Bob Wilson11a1dff2011-01-07 21:37:30 +00004274 return SDValue();
4275}
4276
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004277/// isShuffleMaskLegal - Targets can use this to indicate that they only
4278/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4279/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4280/// are assumed to be legal.
4281bool
4282ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4283 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004284 if (VT.getVectorNumElements() == 4 &&
4285 (VT.is128BitVector() || VT.is64BitVector())) {
4286 unsigned PFIndexes[4];
4287 for (unsigned i = 0; i != 4; ++i) {
4288 if (M[i] < 0)
4289 PFIndexes[i] = 8;
4290 else
4291 PFIndexes[i] = M[i];
4292 }
4293
4294 // Compute the index in the perfect shuffle table.
4295 unsigned PFTableIndex =
4296 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4297 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4298 unsigned Cost = (PFEntry >> 30);
4299
4300 if (Cost <= 4)
4301 return true;
4302 }
4303
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004304 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004305 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004306
Bob Wilson53dd2452010-06-07 23:53:38 +00004307 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4308 return (EltSize >= 32 ||
4309 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004310 isVREVMask(M, VT, 64) ||
4311 isVREVMask(M, VT, 32) ||
4312 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004313 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004314 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004315 isVTRNMask(M, VT, WhichResult) ||
4316 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004317 isVZIPMask(M, VT, WhichResult) ||
4318 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4319 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4320 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004321}
4322
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004323/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4324/// the specified operations to build the shuffle.
4325static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4326 SDValue RHS, SelectionDAG &DAG,
4327 DebugLoc dl) {
4328 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4329 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4330 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4331
4332 enum {
4333 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4334 OP_VREV,
4335 OP_VDUP0,
4336 OP_VDUP1,
4337 OP_VDUP2,
4338 OP_VDUP3,
4339 OP_VEXT1,
4340 OP_VEXT2,
4341 OP_VEXT3,
4342 OP_VUZPL, // VUZP, left result
4343 OP_VUZPR, // VUZP, right result
4344 OP_VZIPL, // VZIP, left result
4345 OP_VZIPR, // VZIP, right result
4346 OP_VTRNL, // VTRN, left result
4347 OP_VTRNR // VTRN, right result
4348 };
4349
4350 if (OpNum == OP_COPY) {
4351 if (LHSID == (1*9+2)*9+3) return LHS;
4352 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4353 return RHS;
4354 }
4355
4356 SDValue OpLHS, OpRHS;
4357 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4358 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4359 EVT VT = OpLHS.getValueType();
4360
4361 switch (OpNum) {
4362 default: llvm_unreachable("Unknown shuffle opcode!");
4363 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004364 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004365 if (VT.getVectorElementType() == MVT::i32 ||
4366 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004367 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4368 // vrev <4 x i16> -> VREV32
4369 if (VT.getVectorElementType() == MVT::i16)
4370 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4371 // vrev <4 x i8> -> VREV16
4372 assert(VT.getVectorElementType() == MVT::i8);
4373 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004374 case OP_VDUP0:
4375 case OP_VDUP1:
4376 case OP_VDUP2:
4377 case OP_VDUP3:
4378 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004379 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004380 case OP_VEXT1:
4381 case OP_VEXT2:
4382 case OP_VEXT3:
4383 return DAG.getNode(ARMISD::VEXT, dl, VT,
4384 OpLHS, OpRHS,
4385 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4386 case OP_VUZPL:
4387 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004388 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004389 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4390 case OP_VZIPL:
4391 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004392 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004393 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4394 case OP_VTRNL:
4395 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004396 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4397 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004398 }
4399}
4400
Bill Wendling69a05a72011-03-14 23:02:38 +00004401static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004402 ArrayRef<int> ShuffleMask,
Bill Wendling69a05a72011-03-14 23:02:38 +00004403 SelectionDAG &DAG) {
4404 // Check to see if we can use the VTBL instruction.
4405 SDValue V1 = Op.getOperand(0);
4406 SDValue V2 = Op.getOperand(1);
4407 DebugLoc DL = Op.getDebugLoc();
4408
4409 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004410 for (ArrayRef<int>::iterator
Bill Wendling69a05a72011-03-14 23:02:38 +00004411 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4412 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4413
4414 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4415 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4416 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4417 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004418
Owen Anderson76706012011-04-05 21:48:57 +00004419 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004420 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4421 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004422}
4423
Bob Wilson5bafff32009-06-22 23:27:02 +00004424static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004425 SDValue V1 = Op.getOperand(0);
4426 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004427 DebugLoc dl = Op.getDebugLoc();
4428 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004429 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsond8e17572009-08-12 22:31:50 +00004430
Bob Wilson28865062009-08-13 02:13:04 +00004431 // Convert shuffles that are directly supported on NEON to target-specific
4432 // DAG nodes, instead of keeping them as shuffles and matching them again
4433 // during code selection. This is more efficient and avoids the possibility
4434 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004435 // FIXME: floating-point vectors should be canonicalized to integer vectors
4436 // of the same time so that they get CSEd properly.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004437 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004438
Bob Wilson53dd2452010-06-07 23:53:38 +00004439 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4440 if (EltSize <= 32) {
4441 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4442 int Lane = SVN->getSplatIndex();
4443 // If this is undef splat, generate it via "just" vdup, if possible.
4444 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004445
Dan Gohman65fd6562011-11-03 21:49:52 +00004446 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson53dd2452010-06-07 23:53:38 +00004447 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4448 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4449 }
Dan Gohman65fd6562011-11-03 21:49:52 +00004450 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
4451 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
4452 // reaches it).
4453 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
4454 !isa<ConstantSDNode>(V1.getOperand(0))) {
4455 bool IsScalarToVector = true;
4456 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
4457 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
4458 IsScalarToVector = false;
4459 break;
4460 }
4461 if (IsScalarToVector)
4462 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4463 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004464 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4465 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004466 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004467
4468 bool ReverseVEXT;
4469 unsigned Imm;
4470 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4471 if (ReverseVEXT)
4472 std::swap(V1, V2);
4473 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4474 DAG.getConstant(Imm, MVT::i32));
4475 }
4476
4477 if (isVREVMask(ShuffleMask, VT, 64))
4478 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4479 if (isVREVMask(ShuffleMask, VT, 32))
4480 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4481 if (isVREVMask(ShuffleMask, VT, 16))
4482 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4483
4484 // Check for Neon shuffles that modify both input vectors in place.
4485 // If both results are used, i.e., if there are two shuffles with the same
4486 // source operands and with masks corresponding to both results of one of
4487 // these operations, DAG memoization will ensure that a single node is
4488 // used for both shuffles.
4489 unsigned WhichResult;
4490 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4491 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4492 V1, V2).getValue(WhichResult);
4493 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4494 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4495 V1, V2).getValue(WhichResult);
4496 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4497 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4498 V1, V2).getValue(WhichResult);
4499
4500 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4501 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4502 V1, V1).getValue(WhichResult);
4503 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4504 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4505 V1, V1).getValue(WhichResult);
4506 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4507 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4508 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004509 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004510
Bob Wilsonc692cb72009-08-21 20:54:19 +00004511 // If the shuffle is not directly supported and it has 4 elements, use
4512 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004513 unsigned NumElts = VT.getVectorNumElements();
4514 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004515 unsigned PFIndexes[4];
4516 for (unsigned i = 0; i != 4; ++i) {
4517 if (ShuffleMask[i] < 0)
4518 PFIndexes[i] = 8;
4519 else
4520 PFIndexes[i] = ShuffleMask[i];
4521 }
4522
4523 // Compute the index in the perfect shuffle table.
4524 unsigned PFTableIndex =
4525 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004526 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4527 unsigned Cost = (PFEntry >> 30);
4528
4529 if (Cost <= 4)
4530 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4531 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004532
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004533 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004534 if (EltSize >= 32) {
4535 // Do the expansion with floating-point types, since that is what the VFP
4536 // registers are defined to use, and since i64 is not legal.
4537 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4538 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004539 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4540 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004541 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004542 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004543 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004544 Ops.push_back(DAG.getUNDEF(EltVT));
4545 else
4546 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4547 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4548 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4549 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004550 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004551 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004552 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004553 }
4554
Bill Wendling69a05a72011-03-14 23:02:38 +00004555 if (VT == MVT::v8i8) {
4556 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4557 if (NewOp.getNode())
4558 return NewOp;
4559 }
4560
Bob Wilson22cac0d2009-08-14 05:16:33 +00004561 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004562}
4563
Eli Friedman5c89cb82011-10-24 23:08:52 +00004564static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
4565 // INSERT_VECTOR_ELT is legal only for immediate indexes.
4566 SDValue Lane = Op.getOperand(2);
4567 if (!isa<ConstantSDNode>(Lane))
4568 return SDValue();
4569
4570 return Op;
4571}
4572
Bob Wilson5bafff32009-06-22 23:27:02 +00004573static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004574 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004575 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004576 if (!isa<ConstantSDNode>(Lane))
4577 return SDValue();
4578
4579 SDValue Vec = Op.getOperand(0);
4580 if (Op.getValueType() == MVT::i32 &&
4581 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4582 DebugLoc dl = Op.getDebugLoc();
4583 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4584 }
4585
4586 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004587}
4588
Bob Wilsona6d65862009-08-03 20:36:38 +00004589static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4590 // The only time a CONCAT_VECTORS operation can have legal types is when
4591 // two 64-bit vectors are concatenated to a 128-bit vector.
4592 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4593 "unexpected CONCAT_VECTORS");
4594 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004595 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004596 SDValue Op0 = Op.getOperand(0);
4597 SDValue Op1 = Op.getOperand(1);
4598 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004599 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004600 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004601 DAG.getIntPtrConstant(0));
4602 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004603 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004604 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004605 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004606 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004607}
4608
Bob Wilson626613d2010-11-23 19:38:38 +00004609/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4610/// element has been zero/sign-extended, depending on the isSigned parameter,
4611/// from an integer type half its size.
4612static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4613 bool isSigned) {
4614 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4615 EVT VT = N->getValueType(0);
4616 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4617 SDNode *BVN = N->getOperand(0).getNode();
4618 if (BVN->getValueType(0) != MVT::v4i32 ||
4619 BVN->getOpcode() != ISD::BUILD_VECTOR)
4620 return false;
4621 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4622 unsigned HiElt = 1 - LoElt;
4623 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4624 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4625 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4626 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4627 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4628 return false;
4629 if (isSigned) {
4630 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4631 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4632 return true;
4633 } else {
4634 if (Hi0->isNullValue() && Hi1->isNullValue())
4635 return true;
4636 }
4637 return false;
4638 }
4639
4640 if (N->getOpcode() != ISD::BUILD_VECTOR)
4641 return false;
4642
4643 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4644 SDNode *Elt = N->getOperand(i).getNode();
4645 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4646 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4647 unsigned HalfSize = EltSize / 2;
4648 if (isSigned) {
Bob Wilson9d45de22011-10-18 18:46:49 +00004649 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004650 return false;
4651 } else {
Bob Wilson9d45de22011-10-18 18:46:49 +00004652 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004653 return false;
4654 }
4655 continue;
4656 }
4657 return false;
4658 }
4659
4660 return true;
4661}
4662
4663/// isSignExtended - Check if a node is a vector value that is sign-extended
4664/// or a constant BUILD_VECTOR with sign-extended elements.
4665static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4666 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4667 return true;
4668 if (isExtendedBUILD_VECTOR(N, DAG, true))
4669 return true;
4670 return false;
4671}
4672
4673/// isZeroExtended - Check if a node is a vector value that is zero-extended
4674/// or a constant BUILD_VECTOR with zero-extended elements.
4675static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4676 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4677 return true;
4678 if (isExtendedBUILD_VECTOR(N, DAG, false))
4679 return true;
4680 return false;
4681}
4682
4683/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4684/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004685static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4686 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4687 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004688 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4689 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4690 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004691 LD->isNonTemporal(), LD->isInvariant(),
4692 LD->getAlignment());
Bob Wilson626613d2010-11-23 19:38:38 +00004693 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4694 // have been legalized as a BITCAST from v4i32.
4695 if (N->getOpcode() == ISD::BITCAST) {
4696 SDNode *BVN = N->getOperand(0).getNode();
4697 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4698 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4699 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4700 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4701 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4702 }
4703 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4704 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4705 EVT VT = N->getValueType(0);
4706 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4707 unsigned NumElts = VT.getVectorNumElements();
4708 MVT TruncVT = MVT::getIntegerVT(EltSize);
4709 SmallVector<SDValue, 8> Ops;
4710 for (unsigned i = 0; i != NumElts; ++i) {
4711 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4712 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004713 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004714 }
4715 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4716 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004717}
4718
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004719static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4720 unsigned Opcode = N->getOpcode();
4721 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4722 SDNode *N0 = N->getOperand(0).getNode();
4723 SDNode *N1 = N->getOperand(1).getNode();
4724 return N0->hasOneUse() && N1->hasOneUse() &&
4725 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4726 }
4727 return false;
4728}
4729
4730static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4731 unsigned Opcode = N->getOpcode();
4732 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4733 SDNode *N0 = N->getOperand(0).getNode();
4734 SDNode *N1 = N->getOperand(1).getNode();
4735 return N0->hasOneUse() && N1->hasOneUse() &&
4736 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4737 }
4738 return false;
4739}
4740
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004741static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4742 // Multiplications are only custom-lowered for 128-bit vectors so that
4743 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4744 EVT VT = Op.getValueType();
4745 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4746 SDNode *N0 = Op.getOperand(0).getNode();
4747 SDNode *N1 = Op.getOperand(1).getNode();
4748 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004749 bool isMLA = false;
4750 bool isN0SExt = isSignExtended(N0, DAG);
4751 bool isN1SExt = isSignExtended(N1, DAG);
4752 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004753 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004754 else {
4755 bool isN0ZExt = isZeroExtended(N0, DAG);
4756 bool isN1ZExt = isZeroExtended(N1, DAG);
4757 if (isN0ZExt && isN1ZExt)
4758 NewOpc = ARMISD::VMULLu;
4759 else if (isN1SExt || isN1ZExt) {
4760 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4761 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4762 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4763 NewOpc = ARMISD::VMULLs;
4764 isMLA = true;
4765 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4766 NewOpc = ARMISD::VMULLu;
4767 isMLA = true;
4768 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4769 std::swap(N0, N1);
4770 NewOpc = ARMISD::VMULLu;
4771 isMLA = true;
4772 }
4773 }
4774
4775 if (!NewOpc) {
4776 if (VT == MVT::v2i64)
4777 // Fall through to expand this. It is not legal.
4778 return SDValue();
4779 else
4780 // Other vector multiplications are legal.
4781 return Op;
4782 }
4783 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004784
4785 // Legalize to a VMULL instruction.
4786 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004787 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004788 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004789 if (!isMLA) {
4790 Op0 = SkipExtension(N0, DAG);
4791 assert(Op0.getValueType().is64BitVector() &&
4792 Op1.getValueType().is64BitVector() &&
4793 "unexpected types for extended operands to VMULL");
4794 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4795 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004796
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004797 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4798 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4799 // vmull q0, d4, d6
4800 // vmlal q0, d5, d6
4801 // is faster than
4802 // vaddl q0, d4, d5
4803 // vmovl q1, d6
4804 // vmul q0, q0, q1
4805 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4806 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4807 EVT Op1VT = Op1.getValueType();
4808 return DAG.getNode(N0->getOpcode(), DL, VT,
4809 DAG.getNode(NewOpc, DL, VT,
4810 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4811 DAG.getNode(NewOpc, DL, VT,
4812 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004813}
4814
Owen Anderson76706012011-04-05 21:48:57 +00004815static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004816LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4817 // Convert to float
4818 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4819 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4820 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4821 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4822 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4823 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4824 // Get reciprocal estimate.
4825 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004826 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004827 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4828 // Because char has a smaller range than uchar, we can actually get away
4829 // without any newton steps. This requires that we use a weird bias
4830 // of 0xb000, however (again, this has been exhaustively tested).
4831 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4832 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4833 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4834 Y = DAG.getConstant(0xb000, MVT::i32);
4835 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4836 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4837 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4838 // Convert back to short.
4839 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4840 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4841 return X;
4842}
4843
Owen Anderson76706012011-04-05 21:48:57 +00004844static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004845LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4846 SDValue N2;
4847 // Convert to float.
4848 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4849 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4850 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4851 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4852 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4853 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004854
Nate Begeman7973f352011-02-11 20:53:29 +00004855 // Use reciprocal estimate and one refinement step.
4856 // float4 recip = vrecpeq_f32(yf);
4857 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004858 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004859 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004860 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004861 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4862 N1, N2);
4863 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4864 // Because short has a smaller range than ushort, we can actually get away
4865 // with only a single newton step. This requires that we use a weird bias
4866 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004867 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004868 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4869 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004870 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004871 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4872 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4873 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4874 // Convert back to integer and return.
4875 // return vmovn_s32(vcvt_s32_f32(result));
4876 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4877 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4878 return N0;
4879}
4880
4881static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4882 EVT VT = Op.getValueType();
4883 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4884 "unexpected type for custom-lowering ISD::SDIV");
4885
4886 DebugLoc dl = Op.getDebugLoc();
4887 SDValue N0 = Op.getOperand(0);
4888 SDValue N1 = Op.getOperand(1);
4889 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004890
Nate Begeman7973f352011-02-11 20:53:29 +00004891 if (VT == MVT::v8i8) {
4892 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4893 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004894
Nate Begeman7973f352011-02-11 20:53:29 +00004895 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4896 DAG.getIntPtrConstant(4));
4897 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004898 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004899 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4900 DAG.getIntPtrConstant(0));
4901 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4902 DAG.getIntPtrConstant(0));
4903
4904 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4905 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4906
4907 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4908 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004909
Nate Begeman7973f352011-02-11 20:53:29 +00004910 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
4911 return N0;
4912 }
4913 return LowerSDIV_v4i16(N0, N1, dl, DAG);
4914}
4915
4916static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
4917 EVT VT = Op.getValueType();
4918 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4919 "unexpected type for custom-lowering ISD::UDIV");
4920
4921 DebugLoc dl = Op.getDebugLoc();
4922 SDValue N0 = Op.getOperand(0);
4923 SDValue N1 = Op.getOperand(1);
4924 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004925
Nate Begeman7973f352011-02-11 20:53:29 +00004926 if (VT == MVT::v8i8) {
4927 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
4928 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004929
Nate Begeman7973f352011-02-11 20:53:29 +00004930 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4931 DAG.getIntPtrConstant(4));
4932 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004933 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004934 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4935 DAG.getIntPtrConstant(0));
4936 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4937 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00004938
Nate Begeman7973f352011-02-11 20:53:29 +00004939 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
4940 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00004941
Nate Begeman7973f352011-02-11 20:53:29 +00004942 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4943 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004944
4945 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00004946 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
4947 N0);
4948 return N0;
4949 }
Owen Anderson76706012011-04-05 21:48:57 +00004950
Nate Begeman7973f352011-02-11 20:53:29 +00004951 // v4i16 sdiv ... Convert to float.
4952 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
4953 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
4954 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
4955 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
4956 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004957 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00004958
4959 // Use reciprocal estimate and two refinement steps.
4960 // float4 recip = vrecpeq_f32(yf);
4961 // recip *= vrecpsq_f32(yf, recip);
4962 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004963 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004964 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00004965 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004966 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004967 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004968 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00004969 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004970 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004971 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004972 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4973 // Simply multiplying by the reciprocal estimate can leave us a few ulps
4974 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
4975 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004976 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00004977 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4978 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4979 N1 = DAG.getConstant(2, MVT::i32);
4980 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4981 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4982 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4983 // Convert back to integer and return.
4984 // return vmovn_u32(vcvt_s32_f32(result));
4985 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4986 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4987 return N0;
4988}
4989
Evan Cheng342e3162011-08-30 01:34:54 +00004990static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
4991 EVT VT = Op.getNode()->getValueType(0);
4992 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
4993
4994 unsigned Opc;
4995 bool ExtraOp = false;
4996 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00004997 default: llvm_unreachable("Invalid code");
Evan Cheng342e3162011-08-30 01:34:54 +00004998 case ISD::ADDC: Opc = ARMISD::ADDC; break;
4999 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
5000 case ISD::SUBC: Opc = ARMISD::SUBC; break;
5001 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
5002 }
5003
5004 if (!ExtraOp)
5005 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5006 Op.getOperand(1));
5007 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5008 Op.getOperand(1), Op.getOperand(2));
5009}
5010
Eli Friedman74bf18c2011-09-15 22:26:18 +00005011static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedman7cc15662011-09-15 22:18:49 +00005012 // Monotonic load/store is legal for all targets
5013 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
5014 return Op;
5015
5016 // Aquire/Release load/store is not legal for targets without a
5017 // dmb or equivalent available.
5018 return SDValue();
5019}
5020
5021
Eli Friedman2bdffe42011-08-31 00:31:29 +00005022static void
Eli Friedman4d3f3292011-08-31 17:52:22 +00005023ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
5024 SelectionDAG &DAG, unsigned NewOp) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005025 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +00005026 assert (Node->getValueType(0) == MVT::i64 &&
5027 "Only know how to expand i64 atomics");
Eli Friedman2bdffe42011-08-31 00:31:29 +00005028
Eli Friedman4d3f3292011-08-31 17:52:22 +00005029 SmallVector<SDValue, 6> Ops;
5030 Ops.push_back(Node->getOperand(0)); // Chain
5031 Ops.push_back(Node->getOperand(1)); // Ptr
5032 // Low part of Val1
5033 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5034 Node->getOperand(2), DAG.getIntPtrConstant(0)));
5035 // High part of Val1
5036 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5037 Node->getOperand(2), DAG.getIntPtrConstant(1)));
Andrew Trick3af7a672011-09-20 03:06:13 +00005038 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) {
Eli Friedman4d3f3292011-08-31 17:52:22 +00005039 // High part of Val1
5040 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5041 Node->getOperand(3), DAG.getIntPtrConstant(0)));
5042 // High part of Val2
5043 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5044 Node->getOperand(3), DAG.getIntPtrConstant(1)));
5045 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005046 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
5047 SDValue Result =
Eli Friedman4d3f3292011-08-31 17:52:22 +00005048 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64,
Eli Friedman2bdffe42011-08-31 00:31:29 +00005049 cast<MemSDNode>(Node)->getMemOperand());
Eli Friedman4d3f3292011-08-31 17:52:22 +00005050 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) };
Eli Friedman2bdffe42011-08-31 00:31:29 +00005051 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
5052 Results.push_back(Result.getValue(2));
5053}
5054
Dan Gohmand858e902010-04-17 15:26:15 +00005055SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005056 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005057 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00005058 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00005059 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005060 case ISD::GlobalAddress:
5061 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
5062 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00005063 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00005064 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00005065 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
5066 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005067 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00005068 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00005069 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00005070 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00005071 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00005072 case ISD::SINT_TO_FP:
5073 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
5074 case ISD::FP_TO_SINT:
5075 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005076 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00005077 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00005078 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005079 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00005080 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00005081 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00005082 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
5083 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00005084 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005085 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00005086 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00005087 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00005088 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00005089 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00005090 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00005091 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Duncan Sands28b77e92011-09-06 19:07:46 +00005092 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00005093 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005094 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedman5c89cb82011-10-24 23:08:52 +00005095 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005096 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00005097 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00005098 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005099 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00005100 case ISD::SDIV: return LowerSDIV(Op, DAG);
5101 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00005102 case ISD::ADDC:
5103 case ISD::ADDE:
5104 case ISD::SUBC:
5105 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Eli Friedman7cc15662011-09-15 22:18:49 +00005106 case ISD::ATOMIC_LOAD:
Eli Friedman74bf18c2011-09-15 22:26:18 +00005107 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005108 }
Evan Chenga8e29892007-01-19 07:51:42 +00005109}
5110
Duncan Sands1607f052008-12-01 11:39:25 +00005111/// ReplaceNodeResults - Replace the results of node with an illegal result
5112/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00005113void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
5114 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005115 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00005116 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00005117 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00005118 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005119 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005120 case ISD::BITCAST:
5121 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005122 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005123 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00005124 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00005125 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005126 break;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005127 case ISD::ATOMIC_LOAD_ADD:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005128 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005129 return;
5130 case ISD::ATOMIC_LOAD_AND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005131 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005132 return;
5133 case ISD::ATOMIC_LOAD_NAND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005134 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005135 return;
5136 case ISD::ATOMIC_LOAD_OR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005137 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005138 return;
5139 case ISD::ATOMIC_LOAD_SUB:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005140 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005141 return;
5142 case ISD::ATOMIC_LOAD_XOR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005143 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005144 return;
5145 case ISD::ATOMIC_SWAP:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005146 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005147 return;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005148 case ISD::ATOMIC_CMP_SWAP:
5149 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG);
5150 return;
Duncan Sands1607f052008-12-01 11:39:25 +00005151 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00005152 if (Res.getNode())
5153 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00005154}
Chris Lattner27a6c732007-11-24 07:07:01 +00005155
Evan Chenga8e29892007-01-19 07:51:42 +00005156//===----------------------------------------------------------------------===//
5157// ARM Scheduler Hooks
5158//===----------------------------------------------------------------------===//
5159
5160MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005161ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
5162 MachineBasicBlock *BB,
5163 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005164 unsigned dest = MI->getOperand(0).getReg();
5165 unsigned ptr = MI->getOperand(1).getReg();
5166 unsigned oldval = MI->getOperand(2).getReg();
5167 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005168 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5169 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005170 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005171
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005172 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5173 unsigned scratch =
Cameron Zwarich141ec632011-05-18 02:29:50 +00005174 MRI.createVirtualRegister(isThumb2 ? ARM::rGPRRegisterClass
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005175 : ARM::GPRRegisterClass);
5176
5177 if (isThumb2) {
Cameron Zwarich141ec632011-05-18 02:29:50 +00005178 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5179 MRI.constrainRegClass(oldval, ARM::rGPRRegisterClass);
5180 MRI.constrainRegClass(newval, ARM::rGPRRegisterClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005181 }
5182
Jim Grosbach5278eb82009-12-11 01:42:04 +00005183 unsigned ldrOpc, strOpc;
5184 switch (Size) {
5185 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005186 case 1:
5187 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00005188 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005189 break;
5190 case 2:
5191 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5192 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5193 break;
5194 case 4:
5195 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5196 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5197 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00005198 }
5199
5200 MachineFunction *MF = BB->getParent();
5201 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5202 MachineFunction::iterator It = BB;
5203 ++It; // insert the new blocks after the current block
5204
5205 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5206 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5207 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5208 MF->insert(It, loop1MBB);
5209 MF->insert(It, loop2MBB);
5210 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005211
5212 // Transfer the remainder of BB and its successor edges to exitMBB.
5213 exitMBB->splice(exitMBB->begin(), BB,
5214 llvm::next(MachineBasicBlock::iterator(MI)),
5215 BB->end());
5216 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005217
5218 // thisMBB:
5219 // ...
5220 // fallthrough --> loop1MBB
5221 BB->addSuccessor(loop1MBB);
5222
5223 // loop1MBB:
5224 // ldrex dest, [ptr]
5225 // cmp dest, oldval
5226 // bne exitMBB
5227 BB = loop1MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005228 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5229 if (ldrOpc == ARM::t2LDREX)
5230 MIB.addImm(0);
5231 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005232 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005233 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005234 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5235 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005236 BB->addSuccessor(loop2MBB);
5237 BB->addSuccessor(exitMBB);
5238
5239 // loop2MBB:
5240 // strex scratch, newval, [ptr]
5241 // cmp scratch, #0
5242 // bne loop1MBB
5243 BB = loop2MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005244 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr);
5245 if (strOpc == ARM::t2STREX)
5246 MIB.addImm(0);
5247 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005248 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005249 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005250 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5251 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005252 BB->addSuccessor(loop1MBB);
5253 BB->addSuccessor(exitMBB);
5254
5255 // exitMBB:
5256 // ...
5257 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005258
Dan Gohman14152b42010-07-06 20:24:04 +00005259 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005260
Jim Grosbach5278eb82009-12-11 01:42:04 +00005261 return BB;
5262}
5263
5264MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005265ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5266 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005267 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5268 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5269
5270 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005271 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005272 MachineFunction::iterator It = BB;
5273 ++It;
5274
5275 unsigned dest = MI->getOperand(0).getReg();
5276 unsigned ptr = MI->getOperand(1).getReg();
5277 unsigned incr = MI->getOperand(2).getReg();
5278 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005279 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005280
5281 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5282 if (isThumb2) {
5283 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5284 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5285 }
5286
Jim Grosbachc3c23542009-12-14 04:22:04 +00005287 unsigned ldrOpc, strOpc;
5288 switch (Size) {
5289 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005290 case 1:
5291 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005292 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005293 break;
5294 case 2:
5295 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5296 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5297 break;
5298 case 4:
5299 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5300 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5301 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005302 }
5303
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005304 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5305 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5306 MF->insert(It, loopMBB);
5307 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005308
5309 // Transfer the remainder of BB and its successor edges to exitMBB.
5310 exitMBB->splice(exitMBB->begin(), BB,
5311 llvm::next(MachineBasicBlock::iterator(MI)),
5312 BB->end());
5313 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005314
Craig Topper44d23822012-02-22 05:59:10 +00005315 const TargetRegisterClass *TRC =
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005316 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5317 unsigned scratch = MRI.createVirtualRegister(TRC);
5318 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005319
5320 // thisMBB:
5321 // ...
5322 // fallthrough --> loopMBB
5323 BB->addSuccessor(loopMBB);
5324
5325 // loopMBB:
5326 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005327 // <binop> scratch2, dest, incr
5328 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005329 // cmp scratch, #0
5330 // bne- loopMBB
5331 // fallthrough --> exitMBB
5332 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005333 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5334 if (ldrOpc == ARM::t2LDREX)
5335 MIB.addImm(0);
5336 AddDefaultPred(MIB);
Jim Grosbachc67b5562009-12-15 00:12:35 +00005337 if (BinOpcode) {
5338 // operand order needs to go the other way for NAND
5339 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5340 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5341 addReg(incr).addReg(dest)).addReg(0);
5342 else
5343 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5344 addReg(dest).addReg(incr)).addReg(0);
5345 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005346
Jim Grosbachb6aed502011-09-09 18:37:27 +00005347 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5348 if (strOpc == ARM::t2STREX)
5349 MIB.addImm(0);
5350 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005351 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005352 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005353 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5354 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005355
5356 BB->addSuccessor(loopMBB);
5357 BB->addSuccessor(exitMBB);
5358
5359 // exitMBB:
5360 // ...
5361 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005362
Dan Gohman14152b42010-07-06 20:24:04 +00005363 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005364
Jim Grosbachc3c23542009-12-14 04:22:04 +00005365 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005366}
5367
Jim Grosbachf7da8822011-04-26 19:44:18 +00005368MachineBasicBlock *
5369ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5370 MachineBasicBlock *BB,
5371 unsigned Size,
5372 bool signExtend,
5373 ARMCC::CondCodes Cond) const {
5374 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5375
5376 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5377 MachineFunction *MF = BB->getParent();
5378 MachineFunction::iterator It = BB;
5379 ++It;
5380
5381 unsigned dest = MI->getOperand(0).getReg();
5382 unsigned ptr = MI->getOperand(1).getReg();
5383 unsigned incr = MI->getOperand(2).getReg();
5384 unsigned oldval = dest;
5385 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005386 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005387
5388 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5389 if (isThumb2) {
5390 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5391 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5392 }
5393
Jim Grosbachf7da8822011-04-26 19:44:18 +00005394 unsigned ldrOpc, strOpc, extendOpc;
5395 switch (Size) {
5396 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5397 case 1:
5398 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5399 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005400 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005401 break;
5402 case 2:
5403 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5404 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005405 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005406 break;
5407 case 4:
5408 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5409 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5410 extendOpc = 0;
5411 break;
5412 }
5413
5414 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5415 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5416 MF->insert(It, loopMBB);
5417 MF->insert(It, exitMBB);
5418
5419 // Transfer the remainder of BB and its successor edges to exitMBB.
5420 exitMBB->splice(exitMBB->begin(), BB,
5421 llvm::next(MachineBasicBlock::iterator(MI)),
5422 BB->end());
5423 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5424
Craig Topper44d23822012-02-22 05:59:10 +00005425 const TargetRegisterClass *TRC =
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005426 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5427 unsigned scratch = MRI.createVirtualRegister(TRC);
5428 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005429
5430 // thisMBB:
5431 // ...
5432 // fallthrough --> loopMBB
5433 BB->addSuccessor(loopMBB);
5434
5435 // loopMBB:
5436 // ldrex dest, ptr
5437 // (sign extend dest, if required)
5438 // cmp dest, incr
5439 // cmov.cond scratch2, dest, incr
5440 // strex scratch, scratch2, ptr
5441 // cmp scratch, #0
5442 // bne- loopMBB
5443 // fallthrough --> exitMBB
5444 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005445 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5446 if (ldrOpc == ARM::t2LDREX)
5447 MIB.addImm(0);
5448 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005449
5450 // Sign extend the value, if necessary.
5451 if (signExtend && extendOpc) {
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005452 oldval = MRI.createVirtualRegister(ARM::GPRRegisterClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005453 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5454 .addReg(dest)
5455 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005456 }
5457
5458 // Build compare and cmov instructions.
5459 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5460 .addReg(oldval).addReg(incr));
5461 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5462 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5463
Jim Grosbachb6aed502011-09-09 18:37:27 +00005464 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5465 if (strOpc == ARM::t2STREX)
5466 MIB.addImm(0);
5467 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005468 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5469 .addReg(scratch).addImm(0));
5470 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5471 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5472
5473 BB->addSuccessor(loopMBB);
5474 BB->addSuccessor(exitMBB);
5475
5476 // exitMBB:
5477 // ...
5478 BB = exitMBB;
5479
5480 MI->eraseFromParent(); // The instruction is gone now.
5481
5482 return BB;
5483}
5484
Eli Friedman2bdffe42011-08-31 00:31:29 +00005485MachineBasicBlock *
5486ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
5487 unsigned Op1, unsigned Op2,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005488 bool NeedsCarry, bool IsCmpxchg) const {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005489 // This also handles ATOMIC_SWAP, indicated by Op1==0.
5490 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5491
5492 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5493 MachineFunction *MF = BB->getParent();
5494 MachineFunction::iterator It = BB;
5495 ++It;
5496
5497 unsigned destlo = MI->getOperand(0).getReg();
5498 unsigned desthi = MI->getOperand(1).getReg();
5499 unsigned ptr = MI->getOperand(2).getReg();
5500 unsigned vallo = MI->getOperand(3).getReg();
5501 unsigned valhi = MI->getOperand(4).getReg();
5502 DebugLoc dl = MI->getDebugLoc();
5503 bool isThumb2 = Subtarget->isThumb2();
5504
5505 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5506 if (isThumb2) {
5507 MRI.constrainRegClass(destlo, ARM::rGPRRegisterClass);
5508 MRI.constrainRegClass(desthi, ARM::rGPRRegisterClass);
5509 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5510 }
5511
5512 unsigned ldrOpc = isThumb2 ? ARM::t2LDREXD : ARM::LDREXD;
5513 unsigned strOpc = isThumb2 ? ARM::t2STREXD : ARM::STREXD;
5514
5515 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman7df496d2011-09-01 22:27:41 +00005516 MachineBasicBlock *contBB = 0, *cont2BB = 0;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005517 if (IsCmpxchg) {
5518 contBB = MF->CreateMachineBasicBlock(LLVM_BB);
5519 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB);
5520 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005521 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5522 MF->insert(It, loopMBB);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005523 if (IsCmpxchg) {
5524 MF->insert(It, contBB);
5525 MF->insert(It, cont2BB);
5526 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005527 MF->insert(It, exitMBB);
5528
5529 // Transfer the remainder of BB and its successor edges to exitMBB.
5530 exitMBB->splice(exitMBB->begin(), BB,
5531 llvm::next(MachineBasicBlock::iterator(MI)),
5532 BB->end());
5533 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5534
Craig Topper44d23822012-02-22 05:59:10 +00005535 const TargetRegisterClass *TRC =
Eli Friedman2bdffe42011-08-31 00:31:29 +00005536 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5537 unsigned storesuccess = MRI.createVirtualRegister(TRC);
5538
5539 // thisMBB:
5540 // ...
5541 // fallthrough --> loopMBB
5542 BB->addSuccessor(loopMBB);
5543
5544 // loopMBB:
5545 // ldrexd r2, r3, ptr
5546 // <binopa> r0, r2, incr
5547 // <binopb> r1, r3, incr
5548 // strexd storesuccess, r0, r1, ptr
5549 // cmp storesuccess, #0
5550 // bne- loopMBB
5551 // fallthrough --> exitMBB
5552 //
5553 // Note that the registers are explicitly specified because there is not any
5554 // way to force the register allocator to allocate a register pair.
5555 //
Andrew Trick3af7a672011-09-20 03:06:13 +00005556 // FIXME: The hardcoded registers are not necessary for Thumb2, but we
Eli Friedman2bdffe42011-08-31 00:31:29 +00005557 // need to properly enforce the restriction that the two output registers
5558 // for ldrexd must be different.
5559 BB = loopMBB;
5560 // Load
5561 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc))
5562 .addReg(ARM::R2, RegState::Define)
5563 .addReg(ARM::R3, RegState::Define).addReg(ptr));
5564 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
5565 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo).addReg(ARM::R2);
5566 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi).addReg(ARM::R3);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005567
5568 if (IsCmpxchg) {
5569 // Add early exit
5570 for (unsigned i = 0; i < 2; i++) {
5571 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr :
5572 ARM::CMPrr))
5573 .addReg(i == 0 ? destlo : desthi)
5574 .addReg(i == 0 ? vallo : valhi));
5575 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5576 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5577 BB->addSuccessor(exitMBB);
5578 BB->addSuccessor(i == 0 ? contBB : cont2BB);
5579 BB = (i == 0 ? contBB : cont2BB);
5580 }
5581
5582 // Copy to physregs for strexd
5583 unsigned setlo = MI->getOperand(5).getReg();
5584 unsigned sethi = MI->getOperand(6).getReg();
5585 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(setlo);
5586 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(sethi);
5587 } else if (Op1) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005588 // Perform binary operation
5589 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), ARM::R0)
5590 .addReg(destlo).addReg(vallo))
5591 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
5592 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), ARM::R1)
5593 .addReg(desthi).addReg(valhi)).addReg(0);
5594 } else {
5595 // Copy to physregs for strexd
5596 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(vallo);
5597 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(valhi);
5598 }
5599
5600 // Store
5601 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), storesuccess)
5602 .addReg(ARM::R0).addReg(ARM::R1).addReg(ptr));
5603 // Cmp+jump
5604 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5605 .addReg(storesuccess).addImm(0));
5606 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5607 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5608
5609 BB->addSuccessor(loopMBB);
5610 BB->addSuccessor(exitMBB);
5611
5612 // exitMBB:
5613 // ...
5614 BB = exitMBB;
5615
5616 MI->eraseFromParent(); // The instruction is gone now.
5617
5618 return BB;
5619}
5620
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005621/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
5622/// registers the function context.
5623void ARMTargetLowering::
5624SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
5625 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005626 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5627 DebugLoc dl = MI->getDebugLoc();
5628 MachineFunction *MF = MBB->getParent();
5629 MachineRegisterInfo *MRI = &MF->getRegInfo();
5630 MachineConstantPool *MCP = MF->getConstantPool();
5631 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5632 const Function *F = MF->getFunction();
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005633
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005634 bool isThumb = Subtarget->isThumb();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005635 bool isThumb2 = Subtarget->isThumb2();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005636
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005637 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005638 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005639 ARMConstantPoolValue *CPV =
5640 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
5641 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
5642
5643 const TargetRegisterClass *TRC =
5644 isThumb ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5645
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005646 // Grab constant pool and fixed stack memory operands.
5647 MachineMemOperand *CPMMO =
5648 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
5649 MachineMemOperand::MOLoad, 4, 4);
5650
5651 MachineMemOperand *FIMMOSt =
5652 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
5653 MachineMemOperand::MOStore, 4, 4);
5654
5655 // Load the address of the dispatch MBB into the jump buffer.
5656 if (isThumb2) {
5657 // Incoming value: jbuf
5658 // ldr.n r5, LCPI1_1
5659 // orr r5, r5, #1
5660 // add r5, pc
5661 // str r5, [$jbuf, #+4] ; &jbuf[1]
5662 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5663 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
5664 .addConstantPoolIndex(CPI)
5665 .addMemOperand(CPMMO));
5666 // Set the low bit because of thumb mode.
5667 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5668 AddDefaultCC(
5669 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
5670 .addReg(NewVReg1, RegState::Kill)
5671 .addImm(0x01)));
5672 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5673 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
5674 .addReg(NewVReg2, RegState::Kill)
5675 .addImm(PCLabelId);
5676 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
5677 .addReg(NewVReg3, RegState::Kill)
5678 .addFrameIndex(FI)
5679 .addImm(36) // &jbuf[1] :: pc
5680 .addMemOperand(FIMMOSt));
5681 } else if (isThumb) {
5682 // Incoming value: jbuf
5683 // ldr.n r1, LCPI1_4
5684 // add r1, pc
5685 // mov r2, #1
5686 // orrs r1, r2
5687 // add r2, $jbuf, #+4 ; &jbuf[1]
5688 // str r1, [r2]
5689 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5690 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
5691 .addConstantPoolIndex(CPI)
5692 .addMemOperand(CPMMO));
5693 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5694 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
5695 .addReg(NewVReg1, RegState::Kill)
5696 .addImm(PCLabelId);
5697 // Set the low bit because of thumb mode.
5698 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5699 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
5700 .addReg(ARM::CPSR, RegState::Define)
5701 .addImm(1));
5702 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5703 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
5704 .addReg(ARM::CPSR, RegState::Define)
5705 .addReg(NewVReg2, RegState::Kill)
5706 .addReg(NewVReg3, RegState::Kill));
5707 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5708 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
5709 .addFrameIndex(FI)
5710 .addImm(36)); // &jbuf[1] :: pc
5711 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
5712 .addReg(NewVReg4, RegState::Kill)
5713 .addReg(NewVReg5, RegState::Kill)
5714 .addImm(0)
5715 .addMemOperand(FIMMOSt));
5716 } else {
5717 // Incoming value: jbuf
5718 // ldr r1, LCPI1_1
5719 // add r1, pc, r1
5720 // str r1, [$jbuf, #+4] ; &jbuf[1]
5721 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5722 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
5723 .addConstantPoolIndex(CPI)
5724 .addImm(0)
5725 .addMemOperand(CPMMO));
5726 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5727 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
5728 .addReg(NewVReg1, RegState::Kill)
5729 .addImm(PCLabelId));
5730 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
5731 .addReg(NewVReg2, RegState::Kill)
5732 .addFrameIndex(FI)
5733 .addImm(36) // &jbuf[1] :: pc
5734 .addMemOperand(FIMMOSt));
5735 }
5736}
5737
5738MachineBasicBlock *ARMTargetLowering::
5739EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
5740 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5741 DebugLoc dl = MI->getDebugLoc();
5742 MachineFunction *MF = MBB->getParent();
5743 MachineRegisterInfo *MRI = &MF->getRegInfo();
5744 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5745 MachineFrameInfo *MFI = MF->getFrameInfo();
5746 int FI = MFI->getFunctionContextIndex();
5747
5748 const TargetRegisterClass *TRC =
5749 Subtarget->isThumb() ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5750
Bill Wendling04f15b42011-10-06 21:29:56 +00005751 // Get a mapping of the call site numbers to all of the landing pads they're
5752 // associated with.
Bill Wendling2a850152011-10-05 00:02:33 +00005753 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
5754 unsigned MaxCSNum = 0;
5755 MachineModuleInfo &MMI = MF->getMMI();
5756 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E; ++BB) {
5757 if (!BB->isLandingPad()) continue;
5758
5759 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
5760 // pad.
5761 for (MachineBasicBlock::iterator
5762 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
5763 if (!II->isEHLabel()) continue;
5764
5765 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendling5cbef192011-10-05 23:28:57 +00005766 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling2a850152011-10-05 00:02:33 +00005767
Bill Wendling5cbef192011-10-05 23:28:57 +00005768 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
5769 for (SmallVectorImpl<unsigned>::iterator
5770 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
5771 CSI != CSE; ++CSI) {
5772 CallSiteNumToLPad[*CSI].push_back(BB);
5773 MaxCSNum = std::max(MaxCSNum, *CSI);
5774 }
Bill Wendling2a850152011-10-05 00:02:33 +00005775 break;
5776 }
5777 }
5778
5779 // Get an ordered list of the machine basic blocks for the jump table.
5780 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling2acf6382011-10-07 23:18:02 +00005781 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling2a850152011-10-05 00:02:33 +00005782 LPadList.reserve(CallSiteNumToLPad.size());
5783 for (unsigned I = 1; I <= MaxCSNum; ++I) {
5784 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
5785 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00005786 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling2a850152011-10-05 00:02:33 +00005787 LPadList.push_back(*II);
Bill Wendling2acf6382011-10-07 23:18:02 +00005788 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
5789 }
Bill Wendling2a850152011-10-05 00:02:33 +00005790 }
5791
Bill Wendling5cbef192011-10-05 23:28:57 +00005792 assert(!LPadList.empty() &&
5793 "No landing pad destinations for the dispatch jump table!");
5794
Bill Wendling04f15b42011-10-06 21:29:56 +00005795 // Create the jump table and associated information.
Bill Wendling2a850152011-10-05 00:02:33 +00005796 MachineJumpTableInfo *JTI =
5797 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
5798 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
5799 unsigned UId = AFI->createJumpTableUId();
5800
Bill Wendling04f15b42011-10-06 21:29:56 +00005801 // Create the MBBs for the dispatch code.
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005802
5803 // Shove the dispatch's address into the return slot in the function context.
5804 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
5805 DispatchBB->setIsLandingPad();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005806
Bill Wendlingbb734682011-10-05 00:39:32 +00005807 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Bill Wendling083a8eb2011-10-06 23:37:36 +00005808 BuildMI(TrapBB, dl, TII->get(Subtarget->isThumb() ? ARM::tTRAP : ARM::TRAP));
Bill Wendlingbb734682011-10-05 00:39:32 +00005809 DispatchBB->addSuccessor(TrapBB);
5810
5811 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
5812 DispatchBB->addSuccessor(DispContBB);
Bill Wendling2a850152011-10-05 00:02:33 +00005813
Bill Wendlinga48ed4f2011-10-17 21:32:56 +00005814 // Insert and MBBs.
Bill Wendling930193c2011-10-06 00:53:33 +00005815 MF->insert(MF->end(), DispatchBB);
5816 MF->insert(MF->end(), DispContBB);
5817 MF->insert(MF->end(), TrapBB);
Bill Wendling930193c2011-10-06 00:53:33 +00005818
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005819 // Insert code into the entry block that creates and registers the function
5820 // context.
5821 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
5822
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005823 MachineMemOperand *FIMMOLd =
Bill Wendling04f15b42011-10-06 21:29:56 +00005824 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendling083a8eb2011-10-06 23:37:36 +00005825 MachineMemOperand::MOLoad |
5826 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling930193c2011-10-06 00:53:33 +00005827
Bob Wilsonf4aea8f2011-12-22 23:39:48 +00005828 if (AFI->isThumb1OnlyFunction())
5829 BuildMI(DispatchBB, dl, TII->get(ARM::tInt_eh_sjlj_dispatchsetup));
5830 else if (!Subtarget->hasVFP2())
5831 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup_nofp));
5832 else
5833 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
Bob Wilsoneaab6ef2011-11-16 07:11:57 +00005834
Bill Wendling952cb502011-10-18 22:49:07 +00005835 unsigned NumLPads = LPadList.size();
Bill Wendling95ce2e92011-10-06 22:53:00 +00005836 if (Subtarget->isThumb2()) {
5837 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5838 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
5839 .addFrameIndex(FI)
5840 .addImm(4)
5841 .addMemOperand(FIMMOLd));
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005842
Bill Wendling952cb502011-10-18 22:49:07 +00005843 if (NumLPads < 256) {
5844 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
5845 .addReg(NewVReg1)
5846 .addImm(LPadList.size()));
5847 } else {
5848 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5849 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00005850 .addImm(NumLPads & 0xFFFF));
5851
5852 unsigned VReg2 = VReg1;
5853 if ((NumLPads & 0xFFFF0000) != 0) {
5854 VReg2 = MRI->createVirtualRegister(TRC);
5855 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
5856 .addReg(VReg1)
5857 .addImm(NumLPads >> 16));
5858 }
5859
Bill Wendling952cb502011-10-18 22:49:07 +00005860 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
5861 .addReg(NewVReg1)
5862 .addReg(VReg2));
5863 }
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005864
Bill Wendling95ce2e92011-10-06 22:53:00 +00005865 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
5866 .addMBB(TrapBB)
5867 .addImm(ARMCC::HI)
5868 .addReg(ARM::CPSR);
Bill Wendlingbb734682011-10-05 00:39:32 +00005869
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005870 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5871 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005872 .addJumpTableIndex(MJTI)
5873 .addImm(UId));
Bill Wendling2a850152011-10-05 00:02:33 +00005874
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005875 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005876 AddDefaultCC(
5877 AddDefaultPred(
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005878 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
5879 .addReg(NewVReg3, RegState::Kill)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005880 .addReg(NewVReg1)
5881 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
5882
5883 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005884 .addReg(NewVReg4, RegState::Kill)
Bill Wendling2a850152011-10-05 00:02:33 +00005885 .addReg(NewVReg1)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005886 .addJumpTableIndex(MJTI)
5887 .addImm(UId);
5888 } else if (Subtarget->isThumb()) {
Bill Wendling083a8eb2011-10-06 23:37:36 +00005889 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5890 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
5891 .addFrameIndex(FI)
5892 .addImm(1)
5893 .addMemOperand(FIMMOLd));
Bill Wendlingf1083d42011-10-07 22:08:37 +00005894
Bill Wendlinga5871dc2011-10-18 23:11:05 +00005895 if (NumLPads < 256) {
5896 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
5897 .addReg(NewVReg1)
5898 .addImm(NumLPads));
5899 } else {
5900 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling922ad782011-10-19 09:24:02 +00005901 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
5902 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
5903
5904 // MachineConstantPool wants an explicit alignment.
5905 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
5906 if (Align == 0)
5907 Align = getTargetData()->getTypeAllocSize(C->getType());
5908 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendlinga5871dc2011-10-18 23:11:05 +00005909
5910 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5911 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
5912 .addReg(VReg1, RegState::Define)
5913 .addConstantPoolIndex(Idx));
5914 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
5915 .addReg(NewVReg1)
5916 .addReg(VReg1));
5917 }
5918
Bill Wendling083a8eb2011-10-06 23:37:36 +00005919 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
5920 .addMBB(TrapBB)
5921 .addImm(ARMCC::HI)
5922 .addReg(ARM::CPSR);
5923
5924 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5925 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
5926 .addReg(ARM::CPSR, RegState::Define)
5927 .addReg(NewVReg1)
5928 .addImm(2));
5929
5930 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling217f0e92011-10-06 23:41:14 +00005931 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendling083a8eb2011-10-06 23:37:36 +00005932 .addJumpTableIndex(MJTI)
5933 .addImm(UId));
5934
5935 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5936 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
5937 .addReg(ARM::CPSR, RegState::Define)
5938 .addReg(NewVReg2, RegState::Kill)
5939 .addReg(NewVReg3));
5940
5941 MachineMemOperand *JTMMOLd =
5942 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
5943 MachineMemOperand::MOLoad, 4, 4);
5944
5945 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5946 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
5947 .addReg(NewVReg4, RegState::Kill)
5948 .addImm(0)
5949 .addMemOperand(JTMMOLd));
5950
5951 unsigned NewVReg6 = MRI->createVirtualRegister(TRC);
5952 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
5953 .addReg(ARM::CPSR, RegState::Define)
5954 .addReg(NewVReg5, RegState::Kill)
5955 .addReg(NewVReg3));
5956
5957 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
5958 .addReg(NewVReg6, RegState::Kill)
5959 .addJumpTableIndex(MJTI)
5960 .addImm(UId);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005961 } else {
5962 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5963 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
5964 .addFrameIndex(FI)
5965 .addImm(4)
5966 .addMemOperand(FIMMOLd));
Bill Wendling564392b2011-10-18 22:11:18 +00005967
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005968 if (NumLPads < 256) {
5969 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
5970 .addReg(NewVReg1)
5971 .addImm(NumLPads));
Bill Wendling922ad782011-10-19 09:24:02 +00005972 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005973 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5974 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00005975 .addImm(NumLPads & 0xFFFF));
5976
5977 unsigned VReg2 = VReg1;
5978 if ((NumLPads & 0xFFFF0000) != 0) {
5979 VReg2 = MRI->createVirtualRegister(TRC);
5980 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
5981 .addReg(VReg1)
5982 .addImm(NumLPads >> 16));
5983 }
5984
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005985 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
5986 .addReg(NewVReg1)
5987 .addReg(VReg2));
Bill Wendling922ad782011-10-19 09:24:02 +00005988 } else {
5989 MachineConstantPool *ConstantPool = MF->getConstantPool();
5990 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
5991 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
5992
5993 // MachineConstantPool wants an explicit alignment.
5994 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
5995 if (Align == 0)
5996 Align = getTargetData()->getTypeAllocSize(C->getType());
5997 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
5998
5999 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6000 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6001 .addReg(VReg1, RegState::Define)
Bill Wendling767f8be2011-10-20 20:37:11 +00006002 .addConstantPoolIndex(Idx)
6003 .addImm(0));
Bill Wendling922ad782011-10-19 09:24:02 +00006004 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6005 .addReg(NewVReg1)
6006 .addReg(VReg1, RegState::Kill));
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006007 }
6008
Bill Wendling95ce2e92011-10-06 22:53:00 +00006009 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6010 .addMBB(TrapBB)
6011 .addImm(ARMCC::HI)
6012 .addReg(ARM::CPSR);
Bill Wendling2a850152011-10-05 00:02:33 +00006013
Bill Wendling564392b2011-10-18 22:11:18 +00006014 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006015 AddDefaultCC(
Bill Wendling564392b2011-10-18 22:11:18 +00006016 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006017 .addReg(NewVReg1)
6018 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling564392b2011-10-18 22:11:18 +00006019 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6020 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006021 .addJumpTableIndex(MJTI)
6022 .addImm(UId));
6023
6024 MachineMemOperand *JTMMOLd =
6025 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6026 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling564392b2011-10-18 22:11:18 +00006027 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006028 AddDefaultPred(
Bill Wendling564392b2011-10-18 22:11:18 +00006029 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6030 .addReg(NewVReg3, RegState::Kill)
6031 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006032 .addImm(0)
6033 .addMemOperand(JTMMOLd));
6034
6035 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
Bill Wendling564392b2011-10-18 22:11:18 +00006036 .addReg(NewVReg5, RegState::Kill)
6037 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006038 .addJumpTableIndex(MJTI)
6039 .addImm(UId);
6040 }
Bill Wendling2a850152011-10-05 00:02:33 +00006041
Bill Wendlingbb734682011-10-05 00:39:32 +00006042 // Add the jump table entries as successors to the MBB.
Bill Wendling2acf6382011-10-07 23:18:02 +00006043 MachineBasicBlock *PrevMBB = 0;
Bill Wendlingbb734682011-10-05 00:39:32 +00006044 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006045 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6046 MachineBasicBlock *CurMBB = *I;
6047 if (PrevMBB != CurMBB)
6048 DispContBB->addSuccessor(CurMBB);
6049 PrevMBB = CurMBB;
6050 }
6051
Bill Wendling24bb9252011-10-17 05:25:09 +00006052 // N.B. the order the invoke BBs are processed in doesn't matter here.
Bill Wendling969c9ef2011-10-14 23:34:37 +00006053 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6054 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
6055 const unsigned *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006056 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling2acf6382011-10-07 23:18:02 +00006057 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6058 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6059 MachineBasicBlock *BB = *I;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006060
6061 // Remove the landing pad successor from the invoke block and replace it
6062 // with the new dispatch block.
Bill Wendlingde39d862011-10-26 07:16:18 +00006063 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6064 BB->succ_end());
6065 while (!Successors.empty()) {
6066 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling2acf6382011-10-07 23:18:02 +00006067 if (SMBB->isLandingPad()) {
6068 BB->removeSuccessor(SMBB);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006069 MBBLPads.push_back(SMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006070 }
6071 }
6072
6073 BB->addSuccessor(DispatchBB);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006074
6075 // Find the invoke call and mark all of the callee-saved registers as
6076 // 'implicit defined' so that they're spilled. This prevents code from
6077 // moving instructions to before the EH block, where they will never be
6078 // executed.
6079 for (MachineBasicBlock::reverse_iterator
6080 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006081 if (!II->isCall()) continue;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006082
6083 DenseMap<unsigned, bool> DefRegs;
6084 for (MachineInstr::mop_iterator
6085 OI = II->operands_begin(), OE = II->operands_end();
6086 OI != OE; ++OI) {
6087 if (!OI->isReg()) continue;
6088 DefRegs[OI->getReg()] = true;
6089 }
6090
6091 MachineInstrBuilder MIB(&*II);
6092
Bill Wendling5d798592011-10-14 23:55:44 +00006093 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006094 unsigned Reg = SavedRegs[i];
6095 if (Subtarget->isThumb2() &&
6096 !ARM::tGPRRegisterClass->contains(Reg) &&
6097 !ARM::hGPRRegisterClass->contains(Reg))
6098 continue;
6099 else if (Subtarget->isThumb1Only() &&
6100 !ARM::tGPRRegisterClass->contains(Reg))
6101 continue;
6102 else if (!Subtarget->isThumb() &&
6103 !ARM::GPRRegisterClass->contains(Reg))
6104 continue;
6105 if (!DefRegs[Reg])
6106 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling5d798592011-10-14 23:55:44 +00006107 }
Bill Wendling969c9ef2011-10-14 23:34:37 +00006108
6109 break;
6110 }
Bill Wendling2acf6382011-10-07 23:18:02 +00006111 }
Bill Wendlingbb734682011-10-05 00:39:32 +00006112
Bill Wendlingf7b02072011-10-18 18:30:49 +00006113 // Mark all former landing pads as non-landing pads. The dispatch is the only
6114 // landing pad now.
6115 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6116 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6117 (*I)->setIsLandingPad(false);
6118
Bill Wendlingbb734682011-10-05 00:39:32 +00006119 // The instruction is gone now.
6120 MI->eraseFromParent();
6121
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006122 return MBB;
6123}
6124
Evan Cheng218977b2010-07-13 19:27:42 +00006125static
6126MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6127 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6128 E = MBB->succ_end(); I != E; ++I)
6129 if (*I != Succ)
6130 return *I;
6131 llvm_unreachable("Expecting a BB with two successors!");
6132}
6133
Jim Grosbache801dc42009-12-12 01:40:06 +00006134MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006135ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00006136 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006137 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00006138 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006139 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00006140 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00006141 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00006142 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00006143 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00006144 }
Jim Grosbachee2c2a42011-09-16 21:55:56 +00006145 // The Thumb2 pre-indexed stores have the same MI operands, they just
6146 // define them differently in the .td files from the isel patterns, so
6147 // they need pseudos.
6148 case ARM::t2STR_preidx:
6149 MI->setDesc(TII->get(ARM::t2STR_PRE));
6150 return BB;
6151 case ARM::t2STRB_preidx:
6152 MI->setDesc(TII->get(ARM::t2STRB_PRE));
6153 return BB;
6154 case ARM::t2STRH_preidx:
6155 MI->setDesc(TII->get(ARM::t2STRH_PRE));
6156 return BB;
6157
Jim Grosbach19dec202011-08-05 20:35:44 +00006158 case ARM::STRi_preidx:
6159 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00006160 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00006161 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
6162 // Decode the offset.
6163 unsigned Offset = MI->getOperand(4).getImm();
6164 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
6165 Offset = ARM_AM::getAM2Offset(Offset);
6166 if (isSub)
6167 Offset = -Offset;
6168
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006169 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00006170 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00006171 .addOperand(MI->getOperand(0)) // Rn_wb
6172 .addOperand(MI->getOperand(1)) // Rt
6173 .addOperand(MI->getOperand(2)) // Rn
6174 .addImm(Offset) // offset (skip GPR==zero_reg)
6175 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006176 .addOperand(MI->getOperand(6))
6177 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00006178 MI->eraseFromParent();
6179 return BB;
6180 }
6181 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00006182 case ARM::STRBr_preidx:
6183 case ARM::STRH_preidx: {
6184 unsigned NewOpc;
6185 switch (MI->getOpcode()) {
6186 default: llvm_unreachable("unexpected opcode!");
6187 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
6188 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
6189 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
6190 }
Jim Grosbach19dec202011-08-05 20:35:44 +00006191 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
6192 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
6193 MIB.addOperand(MI->getOperand(i));
6194 MI->eraseFromParent();
6195 return BB;
6196 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006197 case ARM::ATOMIC_LOAD_ADD_I8:
6198 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6199 case ARM::ATOMIC_LOAD_ADD_I16:
6200 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6201 case ARM::ATOMIC_LOAD_ADD_I32:
6202 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006203
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006204 case ARM::ATOMIC_LOAD_AND_I8:
6205 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6206 case ARM::ATOMIC_LOAD_AND_I16:
6207 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6208 case ARM::ATOMIC_LOAD_AND_I32:
6209 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006210
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006211 case ARM::ATOMIC_LOAD_OR_I8:
6212 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6213 case ARM::ATOMIC_LOAD_OR_I16:
6214 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6215 case ARM::ATOMIC_LOAD_OR_I32:
6216 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006217
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006218 case ARM::ATOMIC_LOAD_XOR_I8:
6219 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6220 case ARM::ATOMIC_LOAD_XOR_I16:
6221 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6222 case ARM::ATOMIC_LOAD_XOR_I32:
6223 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006224
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006225 case ARM::ATOMIC_LOAD_NAND_I8:
6226 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6227 case ARM::ATOMIC_LOAD_NAND_I16:
6228 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6229 case ARM::ATOMIC_LOAD_NAND_I32:
6230 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006231
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006232 case ARM::ATOMIC_LOAD_SUB_I8:
6233 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6234 case ARM::ATOMIC_LOAD_SUB_I16:
6235 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6236 case ARM::ATOMIC_LOAD_SUB_I32:
6237 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006238
Jim Grosbachf7da8822011-04-26 19:44:18 +00006239 case ARM::ATOMIC_LOAD_MIN_I8:
6240 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
6241 case ARM::ATOMIC_LOAD_MIN_I16:
6242 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
6243 case ARM::ATOMIC_LOAD_MIN_I32:
6244 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
6245
6246 case ARM::ATOMIC_LOAD_MAX_I8:
6247 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
6248 case ARM::ATOMIC_LOAD_MAX_I16:
6249 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
6250 case ARM::ATOMIC_LOAD_MAX_I32:
6251 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
6252
6253 case ARM::ATOMIC_LOAD_UMIN_I8:
6254 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
6255 case ARM::ATOMIC_LOAD_UMIN_I16:
6256 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
6257 case ARM::ATOMIC_LOAD_UMIN_I32:
6258 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
6259
6260 case ARM::ATOMIC_LOAD_UMAX_I8:
6261 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
6262 case ARM::ATOMIC_LOAD_UMAX_I16:
6263 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
6264 case ARM::ATOMIC_LOAD_UMAX_I32:
6265 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
6266
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006267 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
6268 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
6269 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00006270
6271 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
6272 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
6273 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006274
Eli Friedman2bdffe42011-08-31 00:31:29 +00006275
6276 case ARM::ATOMADD6432:
6277 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006278 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr,
6279 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006280 case ARM::ATOMSUB6432:
6281 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006282 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6283 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006284 case ARM::ATOMOR6432:
6285 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006286 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006287 case ARM::ATOMXOR6432:
6288 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006289 isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006290 case ARM::ATOMAND6432:
6291 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006292 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006293 case ARM::ATOMSWAP6432:
6294 return EmitAtomicBinary64(MI, BB, 0, 0, false);
Eli Friedman4d3f3292011-08-31 17:52:22 +00006295 case ARM::ATOMCMPXCHG6432:
6296 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
6297 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6298 /*NeedsCarry*/ false, /*IsCmpxchg*/true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006299
Evan Cheng007ea272009-08-12 05:17:19 +00006300 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00006301 // To "insert" a SELECT_CC instruction, we actually have to insert the
6302 // diamond control-flow pattern. The incoming instruction knows the
6303 // destination vreg to set, the condition code register to branch on, the
6304 // true/false values to select between, and a branch opcode to use.
6305 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006306 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00006307 ++It;
6308
6309 // thisMBB:
6310 // ...
6311 // TrueVal = ...
6312 // cmpTY ccX, r1, r2
6313 // bCC copy1MBB
6314 // fallthrough --> copy0MBB
6315 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006316 MachineFunction *F = BB->getParent();
6317 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6318 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00006319 F->insert(It, copy0MBB);
6320 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006321
6322 // Transfer the remainder of BB and its successor edges to sinkMBB.
6323 sinkMBB->splice(sinkMBB->begin(), BB,
6324 llvm::next(MachineBasicBlock::iterator(MI)),
6325 BB->end());
6326 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6327
Dan Gohman258c58c2010-07-06 15:49:48 +00006328 BB->addSuccessor(copy0MBB);
6329 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00006330
Dan Gohman14152b42010-07-06 20:24:04 +00006331 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
6332 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
6333
Evan Chenga8e29892007-01-19 07:51:42 +00006334 // copy0MBB:
6335 // %FalseValue = ...
6336 // # fallthrough to sinkMBB
6337 BB = copy0MBB;
6338
6339 // Update machine-CFG edges
6340 BB->addSuccessor(sinkMBB);
6341
6342 // sinkMBB:
6343 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6344 // ...
6345 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00006346 BuildMI(*BB, BB->begin(), dl,
6347 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00006348 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6349 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6350
Dan Gohman14152b42010-07-06 20:24:04 +00006351 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00006352 return BB;
6353 }
Evan Cheng86198642009-08-07 00:34:42 +00006354
Evan Cheng218977b2010-07-13 19:27:42 +00006355 case ARM::BCCi64:
6356 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00006357 // If there is an unconditional branch to the other successor, remove it.
6358 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00006359
Evan Cheng218977b2010-07-13 19:27:42 +00006360 // Compare both parts that make up the double comparison separately for
6361 // equality.
6362 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
6363
6364 unsigned LHS1 = MI->getOperand(1).getReg();
6365 unsigned LHS2 = MI->getOperand(2).getReg();
6366 if (RHSisZero) {
6367 AddDefaultPred(BuildMI(BB, dl,
6368 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6369 .addReg(LHS1).addImm(0));
6370 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6371 .addReg(LHS2).addImm(0)
6372 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6373 } else {
6374 unsigned RHS1 = MI->getOperand(3).getReg();
6375 unsigned RHS2 = MI->getOperand(4).getReg();
6376 AddDefaultPred(BuildMI(BB, dl,
6377 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6378 .addReg(LHS1).addReg(RHS1));
6379 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6380 .addReg(LHS2).addReg(RHS2)
6381 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6382 }
6383
6384 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
6385 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
6386 if (MI->getOperand(0).getImm() == ARMCC::NE)
6387 std::swap(destMBB, exitMBB);
6388
6389 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6390 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson51f6a7a2011-09-09 21:48:23 +00006391 if (isThumb2)
6392 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
6393 else
6394 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng218977b2010-07-13 19:27:42 +00006395
6396 MI->eraseFromParent(); // The pseudo instruction is gone now.
6397 return BB;
6398 }
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006399
Bill Wendling5bc85282011-10-17 20:37:20 +00006400 case ARM::Int_eh_sjlj_setjmp:
6401 case ARM::Int_eh_sjlj_setjmp_nofp:
6402 case ARM::tInt_eh_sjlj_setjmp:
6403 case ARM::t2Int_eh_sjlj_setjmp:
6404 case ARM::t2Int_eh_sjlj_setjmp_nofp:
6405 EmitSjLjDispatchBlock(MI, BB);
6406 return BB;
6407
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006408 case ARM::ABS:
6409 case ARM::t2ABS: {
6410 // To insert an ABS instruction, we have to insert the
6411 // diamond control-flow pattern. The incoming instruction knows the
6412 // source vreg to test against 0, the destination vreg to set,
6413 // the condition code register to branch on, the
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006414 // true/false values to select between, and a branch opcode to use.
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006415 // It transforms
6416 // V1 = ABS V0
6417 // into
6418 // V2 = MOVS V0
6419 // BCC (branch to SinkBB if V0 >= 0)
6420 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006421 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006422 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6423 MachineFunction::iterator BBI = BB;
6424 ++BBI;
6425 MachineFunction *Fn = BB->getParent();
6426 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6427 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6428 Fn->insert(BBI, RSBBB);
6429 Fn->insert(BBI, SinkBB);
6430
6431 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
6432 unsigned int ABSDstReg = MI->getOperand(0).getReg();
6433 bool isThumb2 = Subtarget->isThumb2();
6434 MachineRegisterInfo &MRI = Fn->getRegInfo();
6435 // In Thumb mode S must not be specified if source register is the SP or
6436 // PC and if destination register is the SP, so restrict register class
6437 unsigned NewMovDstReg = MRI.createVirtualRegister(
6438 isThumb2 ? ARM::rGPRRegisterClass : ARM::GPRRegisterClass);
6439 unsigned NewRsbDstReg = MRI.createVirtualRegister(
6440 isThumb2 ? ARM::rGPRRegisterClass : ARM::GPRRegisterClass);
6441
6442 // Transfer the remainder of BB and its successor edges to sinkMBB.
6443 SinkBB->splice(SinkBB->begin(), BB,
6444 llvm::next(MachineBasicBlock::iterator(MI)),
6445 BB->end());
6446 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
6447
6448 BB->addSuccessor(RSBBB);
6449 BB->addSuccessor(SinkBB);
6450
6451 // fall through to SinkMBB
6452 RSBBB->addSuccessor(SinkBB);
6453
6454 // insert a movs at the end of BB
6455 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVr : ARM::MOVr),
6456 NewMovDstReg)
6457 .addReg(ABSSrcReg, RegState::Kill)
6458 .addImm((unsigned)ARMCC::AL).addReg(0)
6459 .addReg(ARM::CPSR, RegState::Define);
6460
6461 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006462 BuildMI(BB, dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006463 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
6464 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
6465
6466 // insert rsbri in RSBBB
6467 // Note: BCC and rsbri will be converted into predicated rsbmi
6468 // by if-conversion pass
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006469 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006470 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
6471 .addReg(NewMovDstReg, RegState::Kill)
6472 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
6473
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006474 // insert PHI in SinkBB,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006475 // reuse ABSDstReg to not change uses of ABS instruction
6476 BuildMI(*SinkBB, SinkBB->begin(), dl,
6477 TII->get(ARM::PHI), ABSDstReg)
6478 .addReg(NewRsbDstReg).addMBB(RSBBB)
6479 .addReg(NewMovDstReg).addMBB(BB);
6480
6481 // remove ABS instruction
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006482 MI->eraseFromParent();
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006483
6484 // return last added BB
6485 return SinkBB;
6486 }
Evan Chenga8e29892007-01-19 07:51:42 +00006487 }
6488}
6489
Evan Cheng37fefc22011-08-30 19:09:48 +00006490void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
6491 SDNode *Node) const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006492 if (!MI->hasPostISelHook()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006493 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
6494 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
6495 return;
6496 }
6497
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006498 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick4815d562011-09-20 03:17:40 +00006499 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
6500 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
6501 // operand is still set to noreg. If needed, set the optional operand's
6502 // register to CPSR, and remove the redundant implicit def.
Andrew Trick3be654f2011-09-21 02:20:46 +00006503 //
Andrew Trick90b7b122011-10-18 19:18:52 +00006504 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick4815d562011-09-20 03:17:40 +00006505
Andrew Trick3be654f2011-09-21 02:20:46 +00006506 // Rename pseudo opcodes.
6507 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
6508 if (NewOpc) {
6509 const ARMBaseInstrInfo *TII =
6510 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick90b7b122011-10-18 19:18:52 +00006511 MCID = &TII->get(NewOpc);
6512
6513 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
6514 "converted opcode should be the same except for cc_out");
6515
6516 MI->setDesc(*MCID);
6517
6518 // Add the optional cc_out operand
6519 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick3be654f2011-09-21 02:20:46 +00006520 }
Andrew Trick90b7b122011-10-18 19:18:52 +00006521 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick4815d562011-09-20 03:17:40 +00006522
6523 // Any ARM instruction that sets the 's' bit should specify an optional
6524 // "cc_out" operand in the last operand position.
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006525 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006526 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006527 return;
6528 }
Andrew Trick3be654f2011-09-21 02:20:46 +00006529 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
6530 // since we already have an optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006531 bool definesCPSR = false;
6532 bool deadCPSR = false;
Andrew Trick90b7b122011-10-18 19:18:52 +00006533 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick4815d562011-09-20 03:17:40 +00006534 i != e; ++i) {
6535 const MachineOperand &MO = MI->getOperand(i);
6536 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
6537 definesCPSR = true;
6538 if (MO.isDead())
6539 deadCPSR = true;
6540 MI->RemoveOperand(i);
6541 break;
Evan Cheng37fefc22011-08-30 19:09:48 +00006542 }
6543 }
Andrew Trick4815d562011-09-20 03:17:40 +00006544 if (!definesCPSR) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006545 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006546 return;
6547 }
6548 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick3be654f2011-09-21 02:20:46 +00006549 if (deadCPSR) {
6550 assert(!MI->getOperand(ccOutIdx).getReg() &&
6551 "expect uninitialized optional cc_out operand");
Andrew Trick4815d562011-09-20 03:17:40 +00006552 return;
Andrew Trick3be654f2011-09-21 02:20:46 +00006553 }
Andrew Trick4815d562011-09-20 03:17:40 +00006554
Andrew Trick3be654f2011-09-21 02:20:46 +00006555 // If this instruction was defined with an optional CPSR def and its dag node
6556 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006557 MachineOperand &MO = MI->getOperand(ccOutIdx);
6558 MO.setReg(ARM::CPSR);
6559 MO.setIsDef(true);
Evan Cheng37fefc22011-08-30 19:09:48 +00006560}
6561
Evan Chenga8e29892007-01-19 07:51:42 +00006562//===----------------------------------------------------------------------===//
6563// ARM Optimization Hooks
6564//===----------------------------------------------------------------------===//
6565
Chris Lattnerd1980a52009-03-12 06:52:53 +00006566static
6567SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
6568 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00006569 SelectionDAG &DAG = DCI.DAG;
6570 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00006571 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00006572 unsigned Opc = N->getOpcode();
6573 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
6574 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
6575 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
6576 ISD::CondCode CC = ISD::SETCC_INVALID;
6577
6578 if (isSlctCC) {
6579 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
6580 } else {
6581 SDValue CCOp = Slct.getOperand(0);
6582 if (CCOp.getOpcode() == ISD::SETCC)
6583 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
6584 }
6585
6586 bool DoXform = false;
6587 bool InvCC = false;
6588 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
6589 "Bad input!");
6590
6591 if (LHS.getOpcode() == ISD::Constant &&
6592 cast<ConstantSDNode>(LHS)->isNullValue()) {
6593 DoXform = true;
6594 } else if (CC != ISD::SETCC_INVALID &&
6595 RHS.getOpcode() == ISD::Constant &&
6596 cast<ConstantSDNode>(RHS)->isNullValue()) {
6597 std::swap(LHS, RHS);
6598 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006599 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00006600 Op0.getOperand(0).getValueType();
6601 bool isInt = OpVT.isInteger();
6602 CC = ISD::getSetCCInverse(CC, isInt);
6603
6604 if (!TLI.isCondCodeLegal(CC, OpVT))
6605 return SDValue(); // Inverse operator isn't legal.
6606
6607 DoXform = true;
6608 InvCC = true;
6609 }
6610
6611 if (DoXform) {
6612 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
6613 if (isSlctCC)
6614 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
6615 Slct.getOperand(0), Slct.getOperand(1), CC);
6616 SDValue CCOp = Slct.getOperand(0);
6617 if (InvCC)
6618 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
6619 CCOp.getOperand(0), CCOp.getOperand(1), CC);
6620 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
6621 CCOp, OtherOp, Result);
6622 }
6623 return SDValue();
6624}
6625
Eric Christopherfa6f5912011-06-29 21:10:36 +00006626// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00006627// (only after legalization).
6628static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
6629 TargetLowering::DAGCombinerInfo &DCI,
6630 const ARMSubtarget *Subtarget) {
6631
6632 // Only perform optimization if after legalize, and if NEON is available. We
6633 // also expected both operands to be BUILD_VECTORs.
6634 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
6635 || N0.getOpcode() != ISD::BUILD_VECTOR
6636 || N1.getOpcode() != ISD::BUILD_VECTOR)
6637 return SDValue();
6638
6639 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
6640 EVT VT = N->getValueType(0);
6641 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
6642 return SDValue();
6643
6644 // Check that the vector operands are of the right form.
6645 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
6646 // operands, where N is the size of the formed vector.
6647 // Each EXTRACT_VECTOR should have the same input vector and odd or even
6648 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00006649
6650 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00006651 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00006652 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00006653 SDValue Vec = N0->getOperand(0)->getOperand(0);
6654 SDNode *V = Vec.getNode();
6655 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00006656
Eric Christopherfa6f5912011-06-29 21:10:36 +00006657 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00006658 // check to see if each of their operands are an EXTRACT_VECTOR with
6659 // the same vector and appropriate index.
6660 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
6661 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
6662 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00006663
Tanya Lattner189531f2011-06-14 23:48:48 +00006664 SDValue ExtVec0 = N0->getOperand(i);
6665 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00006666
Tanya Lattner189531f2011-06-14 23:48:48 +00006667 // First operand is the vector, verify its the same.
6668 if (V != ExtVec0->getOperand(0).getNode() ||
6669 V != ExtVec1->getOperand(0).getNode())
6670 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00006671
Tanya Lattner189531f2011-06-14 23:48:48 +00006672 // Second is the constant, verify its correct.
6673 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
6674 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00006675
Tanya Lattner189531f2011-06-14 23:48:48 +00006676 // For the constant, we want to see all the even or all the odd.
6677 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
6678 || C1->getZExtValue() != nextIndex+1)
6679 return SDValue();
6680
6681 // Increment index.
6682 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00006683 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00006684 return SDValue();
6685 }
6686
6687 // Create VPADDL node.
6688 SelectionDAG &DAG = DCI.DAG;
6689 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00006690
6691 // Build operand list.
6692 SmallVector<SDValue, 8> Ops;
6693 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
6694 TLI.getPointerTy()));
6695
6696 // Input is the vector.
6697 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00006698
Tanya Lattner189531f2011-06-14 23:48:48 +00006699 // Get widened type and narrowed type.
6700 MVT widenType;
6701 unsigned numElem = VT.getVectorNumElements();
6702 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
6703 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
6704 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
6705 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
6706 default:
Craig Topperbc219812012-02-07 02:50:20 +00006707 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattner189531f2011-06-14 23:48:48 +00006708 }
6709
6710 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
6711 widenType, &Ops[0], Ops.size());
6712 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
6713}
6714
Bob Wilson3d5792a2010-07-29 20:34:14 +00006715/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
6716/// operands N0 and N1. This is a helper for PerformADDCombine that is
6717/// called with the default operands, and if that fails, with commuted
6718/// operands.
6719static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00006720 TargetLowering::DAGCombinerInfo &DCI,
6721 const ARMSubtarget *Subtarget){
6722
6723 // Attempt to create vpaddl for this add.
6724 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
6725 if (Result.getNode())
6726 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00006727
Chris Lattnerd1980a52009-03-12 06:52:53 +00006728 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
6729 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
6730 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
6731 if (Result.getNode()) return Result;
6732 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00006733 return SDValue();
6734}
6735
Bob Wilson3d5792a2010-07-29 20:34:14 +00006736/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
6737///
6738static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00006739 TargetLowering::DAGCombinerInfo &DCI,
6740 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00006741 SDValue N0 = N->getOperand(0);
6742 SDValue N1 = N->getOperand(1);
6743
6744 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00006745 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00006746 if (Result.getNode())
6747 return Result;
6748
6749 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00006750 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00006751}
6752
Chris Lattnerd1980a52009-03-12 06:52:53 +00006753/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00006754///
Chris Lattnerd1980a52009-03-12 06:52:53 +00006755static SDValue PerformSUBCombine(SDNode *N,
6756 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00006757 SDValue N0 = N->getOperand(0);
6758 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00006759
Chris Lattnerd1980a52009-03-12 06:52:53 +00006760 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
6761 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
6762 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
6763 if (Result.getNode()) return Result;
6764 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00006765
Chris Lattnerd1980a52009-03-12 06:52:53 +00006766 return SDValue();
6767}
6768
Evan Cheng463d3582011-03-31 19:38:48 +00006769/// PerformVMULCombine
6770/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
6771/// special multiplier accumulator forwarding.
6772/// vmul d3, d0, d2
6773/// vmla d3, d1, d2
6774/// is faster than
6775/// vadd d3, d0, d1
6776/// vmul d3, d3, d2
6777static SDValue PerformVMULCombine(SDNode *N,
6778 TargetLowering::DAGCombinerInfo &DCI,
6779 const ARMSubtarget *Subtarget) {
6780 if (!Subtarget->hasVMLxForwarding())
6781 return SDValue();
6782
6783 SelectionDAG &DAG = DCI.DAG;
6784 SDValue N0 = N->getOperand(0);
6785 SDValue N1 = N->getOperand(1);
6786 unsigned Opcode = N0.getOpcode();
6787 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
6788 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00006789 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00006790 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
6791 Opcode != ISD::FADD && Opcode != ISD::FSUB)
6792 return SDValue();
6793 std::swap(N0, N1);
6794 }
6795
6796 EVT VT = N->getValueType(0);
6797 DebugLoc DL = N->getDebugLoc();
6798 SDValue N00 = N0->getOperand(0);
6799 SDValue N01 = N0->getOperand(1);
6800 return DAG.getNode(Opcode, DL, VT,
6801 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
6802 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
6803}
6804
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006805static SDValue PerformMULCombine(SDNode *N,
6806 TargetLowering::DAGCombinerInfo &DCI,
6807 const ARMSubtarget *Subtarget) {
6808 SelectionDAG &DAG = DCI.DAG;
6809
6810 if (Subtarget->isThumb1Only())
6811 return SDValue();
6812
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006813 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
6814 return SDValue();
6815
6816 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00006817 if (VT.is64BitVector() || VT.is128BitVector())
6818 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006819 if (VT != MVT::i32)
6820 return SDValue();
6821
6822 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
6823 if (!C)
6824 return SDValue();
6825
6826 uint64_t MulAmt = C->getZExtValue();
6827 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
6828 ShiftAmt = ShiftAmt & (32 - 1);
6829 SDValue V = N->getOperand(0);
6830 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006831
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006832 SDValue Res;
6833 MulAmt >>= ShiftAmt;
6834 if (isPowerOf2_32(MulAmt - 1)) {
6835 // (mul x, 2^N + 1) => (add (shl x, N), x)
6836 Res = DAG.getNode(ISD::ADD, DL, VT,
6837 V, DAG.getNode(ISD::SHL, DL, VT,
6838 V, DAG.getConstant(Log2_32(MulAmt-1),
6839 MVT::i32)));
6840 } else if (isPowerOf2_32(MulAmt + 1)) {
6841 // (mul x, 2^N - 1) => (sub (shl x, N), x)
6842 Res = DAG.getNode(ISD::SUB, DL, VT,
6843 DAG.getNode(ISD::SHL, DL, VT,
6844 V, DAG.getConstant(Log2_32(MulAmt+1),
6845 MVT::i32)),
6846 V);
6847 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006848 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006849
6850 if (ShiftAmt != 0)
6851 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
6852 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006853
6854 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006855 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006856 return SDValue();
6857}
6858
Evan Chengc892aeb2012-02-23 01:19:06 +00006859static bool isCMOVWithZeroOrAllOnesLHS(SDValue N, bool AllOnes) {
6860 if (N.getOpcode() != ARMISD::CMOV || !N.getNode()->hasOneUse())
6861 return false;
6862
6863 SDValue FalseVal = N.getOperand(0);
6864 ConstantSDNode *C = dyn_cast<ConstantSDNode>(FalseVal);
6865 if (!C)
6866 return false;
6867 if (AllOnes)
6868 return C->isAllOnesValue();
6869 return C->isNullValue();
6870}
6871
6872/// formConditionalOp - Combine an operation with a conditional move operand
6873/// to form a conditional op. e.g. (or x, (cmov 0, y, cond)) => (or.cond x, y)
6874/// (and x, (cmov -1, y, cond)) => (and.cond, x, y)
6875static SDValue formConditionalOp(SDNode *N, SelectionDAG &DAG,
6876 bool Commutable) {
6877 SDValue N0 = N->getOperand(0);
6878 SDValue N1 = N->getOperand(1);
6879
6880 bool isAND = N->getOpcode() == ISD::AND;
6881 bool isCand = isCMOVWithZeroOrAllOnesLHS(N1, isAND);
6882 if (!isCand && Commutable) {
6883 isCand = isCMOVWithZeroOrAllOnesLHS(N0, isAND);
6884 if (isCand)
6885 std::swap(N0, N1);
6886 }
6887 if (!isCand)
6888 return SDValue();
6889
6890 unsigned Opc = 0;
6891 switch (N->getOpcode()) {
6892 default: llvm_unreachable("Unexpected node");
6893 case ISD::AND: Opc = ARMISD::CAND; break;
6894 case ISD::OR: Opc = ARMISD::COR; break;
6895 case ISD::XOR: Opc = ARMISD::CXOR; break;
6896 }
6897 return DAG.getNode(Opc, N->getDebugLoc(), N->getValueType(0), N0,
6898 N1.getOperand(1), N1.getOperand(2), N1.getOperand(3),
6899 N1.getOperand(4));
6900}
6901
Owen Anderson080c0922010-11-05 19:27:46 +00006902static SDValue PerformANDCombine(SDNode *N,
Evan Chengc892aeb2012-02-23 01:19:06 +00006903 TargetLowering::DAGCombinerInfo &DCI,
6904 const ARMSubtarget *Subtarget) {
Owen Anderson76706012011-04-05 21:48:57 +00006905
Owen Anderson080c0922010-11-05 19:27:46 +00006906 // Attempt to use immediate-form VBIC
6907 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
6908 DebugLoc dl = N->getDebugLoc();
6909 EVT VT = N->getValueType(0);
6910 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006911
Tanya Lattner0433b212011-04-07 15:24:20 +00006912 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
6913 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00006914
Owen Anderson080c0922010-11-05 19:27:46 +00006915 APInt SplatBits, SplatUndef;
6916 unsigned SplatBitSize;
6917 bool HasAnyUndefs;
6918 if (BVN &&
6919 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
6920 if (SplatBitSize <= 64) {
6921 EVT VbicVT;
6922 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
6923 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006924 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00006925 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00006926 if (Val.getNode()) {
6927 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006928 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00006929 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006930 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00006931 }
6932 }
6933 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006934
Evan Chengc892aeb2012-02-23 01:19:06 +00006935 if (!Subtarget->isThumb1Only()) {
6936 // (and x, (cmov -1, y, cond)) => (and.cond x, y)
6937 SDValue CAND = formConditionalOp(N, DAG, true);
6938 if (CAND.getNode())
6939 return CAND;
6940 }
6941
Owen Anderson080c0922010-11-05 19:27:46 +00006942 return SDValue();
6943}
6944
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006945/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
6946static SDValue PerformORCombine(SDNode *N,
6947 TargetLowering::DAGCombinerInfo &DCI,
6948 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00006949 // Attempt to use immediate-form VORR
6950 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
6951 DebugLoc dl = N->getDebugLoc();
6952 EVT VT = N->getValueType(0);
6953 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006954
Tanya Lattner0433b212011-04-07 15:24:20 +00006955 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
6956 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00006957
Owen Anderson60f48702010-11-03 23:15:26 +00006958 APInt SplatBits, SplatUndef;
6959 unsigned SplatBitSize;
6960 bool HasAnyUndefs;
6961 if (BVN && Subtarget->hasNEON() &&
6962 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
6963 if (SplatBitSize <= 64) {
6964 EVT VorrVT;
6965 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
6966 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00006967 DAG, VorrVT, VT.is128BitVector(),
6968 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00006969 if (Val.getNode()) {
6970 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006971 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00006972 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006973 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00006974 }
6975 }
6976 }
6977
Evan Chengc892aeb2012-02-23 01:19:06 +00006978 if (!Subtarget->isThumb1Only()) {
6979 // (or x, (cmov 0, y, cond)) => (or.cond x, y)
6980 SDValue COR = formConditionalOp(N, DAG, true);
6981 if (COR.getNode())
6982 return COR;
6983 }
6984
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00006985 SDValue N0 = N->getOperand(0);
6986 if (N0.getOpcode() != ISD::AND)
6987 return SDValue();
6988 SDValue N1 = N->getOperand(1);
6989
6990 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
6991 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
6992 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
6993 APInt SplatUndef;
6994 unsigned SplatBitSize;
6995 bool HasAnyUndefs;
6996
6997 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
6998 APInt SplatBits0;
6999 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
7000 HasAnyUndefs) && !HasAnyUndefs) {
7001 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
7002 APInt SplatBits1;
7003 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
7004 HasAnyUndefs) && !HasAnyUndefs &&
7005 SplatBits0 == ~SplatBits1) {
7006 // Canonicalize the vector type to make instruction selection simpler.
7007 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
7008 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
7009 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00007010 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007011 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
7012 }
7013 }
7014 }
7015
Jim Grosbach54238562010-07-17 03:30:54 +00007016 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
7017 // reasonable.
7018
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007019 // BFI is only available on V6T2+
7020 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
7021 return SDValue();
7022
Jim Grosbach54238562010-07-17 03:30:54 +00007023 DebugLoc DL = N->getDebugLoc();
7024 // 1) or (and A, mask), val => ARMbfi A, val, mask
7025 // iff (val & mask) == val
7026 //
7027 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
7028 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00007029 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00007030 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00007031 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00007032 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007033
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007034 if (VT != MVT::i32)
7035 return SDValue();
7036
Evan Cheng30fb13f2010-12-13 20:32:54 +00007037 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00007038
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007039 // The value and the mask need to be constants so we can verify this is
7040 // actually a bitfield set. If the mask is 0xffff, we can do better
7041 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00007042 SDValue MaskOp = N0.getOperand(1);
7043 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
7044 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007045 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00007046 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007047 if (Mask == 0xffff)
7048 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007049 SDValue Res;
7050 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00007051 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
7052 if (N1C) {
7053 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00007054 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00007055 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007056
Evan Chenga9688c42010-12-11 04:11:38 +00007057 if (ARM::isBitFieldInvertedMask(Mask)) {
7058 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007059
Evan Cheng30fb13f2010-12-13 20:32:54 +00007060 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00007061 DAG.getConstant(Val, MVT::i32),
7062 DAG.getConstant(Mask, MVT::i32));
7063
7064 // Do not add new nodes to DAG combiner worklist.
7065 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007066 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00007067 }
Jim Grosbach54238562010-07-17 03:30:54 +00007068 } else if (N1.getOpcode() == ISD::AND) {
7069 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00007070 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7071 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00007072 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00007073 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007074
Eric Christopher29aeed12011-03-26 01:21:03 +00007075 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
7076 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00007077 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007078 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007079 // The pack halfword instruction works better for masks that fit it,
7080 // so use that when it's available.
7081 if (Subtarget->hasT2ExtractPack() &&
7082 (Mask == 0xffff || Mask == 0xffff0000))
7083 return SDValue();
7084 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00007085 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00007086 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00007087 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00007088 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00007089 DAG.getConstant(Mask, MVT::i32));
7090 // Do not add new nodes to DAG combiner worklist.
7091 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007092 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007093 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007094 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007095 // The pack halfword instruction works better for masks that fit it,
7096 // so use that when it's available.
7097 if (Subtarget->hasT2ExtractPack() &&
7098 (Mask2 == 0xffff || Mask2 == 0xffff0000))
7099 return SDValue();
7100 // 2b
7101 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007102 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00007103 DAG.getConstant(lsb, MVT::i32));
7104 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00007105 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00007106 // Do not add new nodes to DAG combiner worklist.
7107 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007108 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007109 }
7110 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007111
Evan Cheng30fb13f2010-12-13 20:32:54 +00007112 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
7113 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
7114 ARM::isBitFieldInvertedMask(~Mask)) {
7115 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
7116 // where lsb(mask) == #shamt and masked bits of B are known zero.
7117 SDValue ShAmt = N00.getOperand(1);
7118 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
7119 unsigned LSB = CountTrailingZeros_32(Mask);
7120 if (ShAmtC != LSB)
7121 return SDValue();
7122
7123 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
7124 DAG.getConstant(~Mask, MVT::i32));
7125
7126 // Do not add new nodes to DAG combiner worklist.
7127 DCI.CombineTo(N, Res, false);
7128 }
7129
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007130 return SDValue();
7131}
7132
Evan Chengc892aeb2012-02-23 01:19:06 +00007133static SDValue PerformXORCombine(SDNode *N,
7134 TargetLowering::DAGCombinerInfo &DCI,
7135 const ARMSubtarget *Subtarget) {
7136 EVT VT = N->getValueType(0);
7137 SelectionDAG &DAG = DCI.DAG;
7138
7139 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7140 return SDValue();
7141
7142 if (!Subtarget->isThumb1Only()) {
7143 // (xor x, (cmov 0, y, cond)) => (xor.cond x, y)
7144 SDValue CXOR = formConditionalOp(N, DAG, true);
7145 if (CXOR.getNode())
7146 return CXOR;
7147 }
7148
7149 return SDValue();
7150}
7151
Evan Chengbf188ae2011-06-15 01:12:31 +00007152/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
7153/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00007154static SDValue PerformBFICombine(SDNode *N,
7155 TargetLowering::DAGCombinerInfo &DCI) {
7156 SDValue N1 = N->getOperand(1);
7157 if (N1.getOpcode() == ISD::AND) {
7158 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7159 if (!N11C)
7160 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007161 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
7162 unsigned LSB = CountTrailingZeros_32(~InvMask);
7163 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
7164 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00007165 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007166 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00007167 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
7168 N->getOperand(0), N1.getOperand(0),
7169 N->getOperand(2));
7170 }
7171 return SDValue();
7172}
7173
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007174/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
7175/// ARMISD::VMOVRRD.
7176static SDValue PerformVMOVRRDCombine(SDNode *N,
7177 TargetLowering::DAGCombinerInfo &DCI) {
7178 // vmovrrd(vmovdrr x, y) -> x,y
7179 SDValue InDouble = N->getOperand(0);
7180 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
7181 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00007182
7183 // vmovrrd(load f64) -> (load i32), (load i32)
7184 SDNode *InNode = InDouble.getNode();
7185 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
7186 InNode->getValueType(0) == MVT::f64 &&
7187 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
7188 !cast<LoadSDNode>(InNode)->isVolatile()) {
7189 // TODO: Should this be done for non-FrameIndex operands?
7190 LoadSDNode *LD = cast<LoadSDNode>(InNode);
7191
7192 SelectionDAG &DAG = DCI.DAG;
7193 DebugLoc DL = LD->getDebugLoc();
7194 SDValue BasePtr = LD->getBasePtr();
7195 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
7196 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007197 LD->isNonTemporal(), LD->isInvariant(),
7198 LD->getAlignment());
Cameron Zwarich4071a712011-04-02 02:40:43 +00007199
7200 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7201 DAG.getConstant(4, MVT::i32));
7202 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
7203 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007204 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich4071a712011-04-02 02:40:43 +00007205 std::min(4U, LD->getAlignment() / 2));
7206
7207 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
7208 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
7209 DCI.RemoveFromWorklist(LD);
7210 DAG.DeleteNode(LD);
7211 return Result;
7212 }
7213
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007214 return SDValue();
7215}
7216
7217/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
7218/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
7219static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
7220 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
7221 SDValue Op0 = N->getOperand(0);
7222 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007223 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007224 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007225 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007226 Op1 = Op1.getOperand(0);
7227 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
7228 Op0.getNode() == Op1.getNode() &&
7229 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007230 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007231 N->getValueType(0), Op0.getOperand(0));
7232 return SDValue();
7233}
7234
Bob Wilson31600902010-12-21 06:43:19 +00007235/// PerformSTORECombine - Target-specific dag combine xforms for
7236/// ISD::STORE.
7237static SDValue PerformSTORECombine(SDNode *N,
7238 TargetLowering::DAGCombinerInfo &DCI) {
7239 // Bitcast an i64 store extracted from a vector to f64.
7240 // Otherwise, the i64 value will be legalized to a pair of i32 values.
7241 StoreSDNode *St = cast<StoreSDNode>(N);
7242 SDValue StVal = St->getValue();
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007243 if (!ISD::isNormalStore(St) || St->isVolatile())
7244 return SDValue();
7245
7246 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
7247 StVal.getNode()->hasOneUse() && !St->isVolatile()) {
7248 SelectionDAG &DAG = DCI.DAG;
7249 DebugLoc DL = St->getDebugLoc();
7250 SDValue BasePtr = St->getBasePtr();
7251 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
7252 StVal.getNode()->getOperand(0), BasePtr,
7253 St->getPointerInfo(), St->isVolatile(),
7254 St->isNonTemporal(), St->getAlignment());
7255
7256 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7257 DAG.getConstant(4, MVT::i32));
7258 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
7259 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
7260 St->isNonTemporal(),
7261 std::min(4U, St->getAlignment() / 2));
7262 }
7263
7264 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00007265 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
7266 return SDValue();
7267
7268 SelectionDAG &DAG = DCI.DAG;
7269 DebugLoc dl = StVal.getDebugLoc();
7270 SDValue IntVec = StVal.getOperand(0);
7271 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7272 IntVec.getValueType().getVectorNumElements());
7273 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
7274 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
7275 Vec, StVal.getOperand(1));
7276 dl = N->getDebugLoc();
7277 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
7278 // Make the DAGCombiner fold the bitcasts.
7279 DCI.AddToWorklist(Vec.getNode());
7280 DCI.AddToWorklist(ExtElt.getNode());
7281 DCI.AddToWorklist(V.getNode());
7282 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
7283 St->getPointerInfo(), St->isVolatile(),
7284 St->isNonTemporal(), St->getAlignment(),
7285 St->getTBAAInfo());
7286}
7287
7288/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
7289/// are normal, non-volatile loads. If so, it is profitable to bitcast an
7290/// i64 vector to have f64 elements, since the value can then be loaded
7291/// directly into a VFP register.
7292static bool hasNormalLoadOperand(SDNode *N) {
7293 unsigned NumElts = N->getValueType(0).getVectorNumElements();
7294 for (unsigned i = 0; i < NumElts; ++i) {
7295 SDNode *Elt = N->getOperand(i).getNode();
7296 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
7297 return true;
7298 }
7299 return false;
7300}
7301
Bob Wilson75f02882010-09-17 22:59:05 +00007302/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
7303/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00007304static SDValue PerformBUILD_VECTORCombine(SDNode *N,
7305 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00007306 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
7307 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
7308 // into a pair of GPRs, which is fine when the value is used as a scalar,
7309 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00007310 SelectionDAG &DAG = DCI.DAG;
7311 if (N->getNumOperands() == 2) {
7312 SDValue RV = PerformVMOVDRRCombine(N, DAG);
7313 if (RV.getNode())
7314 return RV;
7315 }
Bob Wilson75f02882010-09-17 22:59:05 +00007316
Bob Wilson31600902010-12-21 06:43:19 +00007317 // Load i64 elements as f64 values so that type legalization does not split
7318 // them up into i32 values.
7319 EVT VT = N->getValueType(0);
7320 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
7321 return SDValue();
7322 DebugLoc dl = N->getDebugLoc();
7323 SmallVector<SDValue, 8> Ops;
7324 unsigned NumElts = VT.getVectorNumElements();
7325 for (unsigned i = 0; i < NumElts; ++i) {
7326 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
7327 Ops.push_back(V);
7328 // Make the DAGCombiner fold the bitcast.
7329 DCI.AddToWorklist(V.getNode());
7330 }
7331 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
7332 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
7333 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
7334}
7335
7336/// PerformInsertEltCombine - Target-specific dag combine xforms for
7337/// ISD::INSERT_VECTOR_ELT.
7338static SDValue PerformInsertEltCombine(SDNode *N,
7339 TargetLowering::DAGCombinerInfo &DCI) {
7340 // Bitcast an i64 load inserted into a vector to f64.
7341 // Otherwise, the i64 value will be legalized to a pair of i32 values.
7342 EVT VT = N->getValueType(0);
7343 SDNode *Elt = N->getOperand(1).getNode();
7344 if (VT.getVectorElementType() != MVT::i64 ||
7345 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
7346 return SDValue();
7347
7348 SelectionDAG &DAG = DCI.DAG;
7349 DebugLoc dl = N->getDebugLoc();
7350 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7351 VT.getVectorNumElements());
7352 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
7353 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
7354 // Make the DAGCombiner fold the bitcasts.
7355 DCI.AddToWorklist(Vec.getNode());
7356 DCI.AddToWorklist(V.getNode());
7357 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
7358 Vec, V, N->getOperand(2));
7359 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00007360}
7361
Bob Wilsonf20700c2010-10-27 20:38:28 +00007362/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
7363/// ISD::VECTOR_SHUFFLE.
7364static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
7365 // The LLVM shufflevector instruction does not require the shuffle mask
7366 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
7367 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
7368 // operands do not match the mask length, they are extended by concatenating
7369 // them with undef vectors. That is probably the right thing for other
7370 // targets, but for NEON it is better to concatenate two double-register
7371 // size vector operands into a single quad-register size vector. Do that
7372 // transformation here:
7373 // shuffle(concat(v1, undef), concat(v2, undef)) ->
7374 // shuffle(concat(v1, v2), undef)
7375 SDValue Op0 = N->getOperand(0);
7376 SDValue Op1 = N->getOperand(1);
7377 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
7378 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
7379 Op0.getNumOperands() != 2 ||
7380 Op1.getNumOperands() != 2)
7381 return SDValue();
7382 SDValue Concat0Op1 = Op0.getOperand(1);
7383 SDValue Concat1Op1 = Op1.getOperand(1);
7384 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
7385 Concat1Op1.getOpcode() != ISD::UNDEF)
7386 return SDValue();
7387 // Skip the transformation if any of the types are illegal.
7388 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7389 EVT VT = N->getValueType(0);
7390 if (!TLI.isTypeLegal(VT) ||
7391 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
7392 !TLI.isTypeLegal(Concat1Op1.getValueType()))
7393 return SDValue();
7394
7395 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
7396 Op0.getOperand(0), Op1.getOperand(0));
7397 // Translate the shuffle mask.
7398 SmallVector<int, 16> NewMask;
7399 unsigned NumElts = VT.getVectorNumElements();
7400 unsigned HalfElts = NumElts/2;
7401 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
7402 for (unsigned n = 0; n < NumElts; ++n) {
7403 int MaskElt = SVN->getMaskElt(n);
7404 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007405 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00007406 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007407 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00007408 NewElt = HalfElts + MaskElt - NumElts;
7409 NewMask.push_back(NewElt);
7410 }
7411 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
7412 DAG.getUNDEF(VT), NewMask.data());
7413}
7414
Bob Wilson1c3ef902011-02-07 17:43:21 +00007415/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
7416/// NEON load/store intrinsics to merge base address updates.
7417static SDValue CombineBaseUpdate(SDNode *N,
7418 TargetLowering::DAGCombinerInfo &DCI) {
7419 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7420 return SDValue();
7421
7422 SelectionDAG &DAG = DCI.DAG;
7423 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
7424 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
7425 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
7426 SDValue Addr = N->getOperand(AddrOpIdx);
7427
7428 // Search for a use of the address operand that is an increment.
7429 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
7430 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
7431 SDNode *User = *UI;
7432 if (User->getOpcode() != ISD::ADD ||
7433 UI.getUse().getResNo() != Addr.getResNo())
7434 continue;
7435
7436 // Check that the add is independent of the load/store. Otherwise, folding
7437 // it would create a cycle.
7438 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
7439 continue;
7440
7441 // Find the new opcode for the updating load/store.
7442 bool isLoad = true;
7443 bool isLaneOp = false;
7444 unsigned NewOpc = 0;
7445 unsigned NumVecs = 0;
7446 if (isIntrinsic) {
7447 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
7448 switch (IntNo) {
Craig Topperbc219812012-02-07 02:50:20 +00007449 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00007450 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
7451 NumVecs = 1; break;
7452 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
7453 NumVecs = 2; break;
7454 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
7455 NumVecs = 3; break;
7456 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
7457 NumVecs = 4; break;
7458 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
7459 NumVecs = 2; isLaneOp = true; break;
7460 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
7461 NumVecs = 3; isLaneOp = true; break;
7462 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
7463 NumVecs = 4; isLaneOp = true; break;
7464 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
7465 NumVecs = 1; isLoad = false; break;
7466 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
7467 NumVecs = 2; isLoad = false; break;
7468 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
7469 NumVecs = 3; isLoad = false; break;
7470 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
7471 NumVecs = 4; isLoad = false; break;
7472 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
7473 NumVecs = 2; isLoad = false; isLaneOp = true; break;
7474 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
7475 NumVecs = 3; isLoad = false; isLaneOp = true; break;
7476 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
7477 NumVecs = 4; isLoad = false; isLaneOp = true; break;
7478 }
7479 } else {
7480 isLaneOp = true;
7481 switch (N->getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00007482 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00007483 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
7484 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
7485 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
7486 }
7487 }
7488
7489 // Find the size of memory referenced by the load/store.
7490 EVT VecTy;
7491 if (isLoad)
7492 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00007493 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00007494 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
7495 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
7496 if (isLaneOp)
7497 NumBytes /= VecTy.getVectorNumElements();
7498
7499 // If the increment is a constant, it must match the memory ref size.
7500 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
7501 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
7502 uint64_t IncVal = CInc->getZExtValue();
7503 if (IncVal != NumBytes)
7504 continue;
7505 } else if (NumBytes >= 3 * 16) {
7506 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
7507 // separate instructions that make it harder to use a non-constant update.
7508 continue;
7509 }
7510
7511 // Create the new updating load/store node.
7512 EVT Tys[6];
7513 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
7514 unsigned n;
7515 for (n = 0; n < NumResultVecs; ++n)
7516 Tys[n] = VecTy;
7517 Tys[n++] = MVT::i32;
7518 Tys[n] = MVT::Other;
7519 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
7520 SmallVector<SDValue, 8> Ops;
7521 Ops.push_back(N->getOperand(0)); // incoming chain
7522 Ops.push_back(N->getOperand(AddrOpIdx));
7523 Ops.push_back(Inc);
7524 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
7525 Ops.push_back(N->getOperand(i));
7526 }
7527 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
7528 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
7529 Ops.data(), Ops.size(),
7530 MemInt->getMemoryVT(),
7531 MemInt->getMemOperand());
7532
7533 // Update the uses.
7534 std::vector<SDValue> NewResults;
7535 for (unsigned i = 0; i < NumResultVecs; ++i) {
7536 NewResults.push_back(SDValue(UpdN.getNode(), i));
7537 }
7538 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
7539 DCI.CombineTo(N, NewResults);
7540 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
7541
7542 break;
Owen Anderson76706012011-04-05 21:48:57 +00007543 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00007544 return SDValue();
7545}
7546
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007547/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
7548/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
7549/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
7550/// return true.
7551static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
7552 SelectionDAG &DAG = DCI.DAG;
7553 EVT VT = N->getValueType(0);
7554 // vldN-dup instructions only support 64-bit vectors for N > 1.
7555 if (!VT.is64BitVector())
7556 return false;
7557
7558 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
7559 SDNode *VLD = N->getOperand(0).getNode();
7560 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
7561 return false;
7562 unsigned NumVecs = 0;
7563 unsigned NewOpc = 0;
7564 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
7565 if (IntNo == Intrinsic::arm_neon_vld2lane) {
7566 NumVecs = 2;
7567 NewOpc = ARMISD::VLD2DUP;
7568 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
7569 NumVecs = 3;
7570 NewOpc = ARMISD::VLD3DUP;
7571 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
7572 NumVecs = 4;
7573 NewOpc = ARMISD::VLD4DUP;
7574 } else {
7575 return false;
7576 }
7577
7578 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
7579 // numbers match the load.
7580 unsigned VLDLaneNo =
7581 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
7582 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
7583 UI != UE; ++UI) {
7584 // Ignore uses of the chain result.
7585 if (UI.getUse().getResNo() == NumVecs)
7586 continue;
7587 SDNode *User = *UI;
7588 if (User->getOpcode() != ARMISD::VDUPLANE ||
7589 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
7590 return false;
7591 }
7592
7593 // Create the vldN-dup node.
7594 EVT Tys[5];
7595 unsigned n;
7596 for (n = 0; n < NumVecs; ++n)
7597 Tys[n] = VT;
7598 Tys[n] = MVT::Other;
7599 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
7600 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
7601 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
7602 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
7603 Ops, 2, VLDMemInt->getMemoryVT(),
7604 VLDMemInt->getMemOperand());
7605
7606 // Update the uses.
7607 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
7608 UI != UE; ++UI) {
7609 unsigned ResNo = UI.getUse().getResNo();
7610 // Ignore uses of the chain result.
7611 if (ResNo == NumVecs)
7612 continue;
7613 SDNode *User = *UI;
7614 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
7615 }
7616
7617 // Now the vldN-lane intrinsic is dead except for its chain result.
7618 // Update uses of the chain.
7619 std::vector<SDValue> VLDDupResults;
7620 for (unsigned n = 0; n < NumVecs; ++n)
7621 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
7622 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
7623 DCI.CombineTo(VLD, VLDDupResults);
7624
7625 return true;
7626}
7627
Bob Wilson9e82bf12010-07-14 01:22:12 +00007628/// PerformVDUPLANECombine - Target-specific dag combine xforms for
7629/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007630static SDValue PerformVDUPLANECombine(SDNode *N,
7631 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00007632 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007633
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007634 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
7635 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
7636 if (CombineVLDDUP(N, DCI))
7637 return SDValue(N, 0);
7638
7639 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
7640 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007641 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00007642 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00007643 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00007644 return SDValue();
7645
7646 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
7647 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
7648 // The canonical VMOV for a zero vector uses a 32-bit element size.
7649 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7650 unsigned EltBits;
7651 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
7652 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007653 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007654 if (EltSize > VT.getVectorElementType().getSizeInBits())
7655 return SDValue();
7656
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007657 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007658}
7659
Eric Christopherfa6f5912011-06-29 21:10:36 +00007660// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00007661// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
7662static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
7663{
Chad Rosier118c9a02011-06-28 17:26:57 +00007664 integerPart cN;
7665 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00007666 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
7667 I != E; I++) {
7668 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
7669 if (!C)
7670 return false;
7671
Eric Christopherfa6f5912011-06-29 21:10:36 +00007672 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00007673 APFloat APF = C->getValueAPF();
7674 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
7675 != APFloat::opOK || !isExact)
7676 return false;
7677
7678 c0 = (I == 0) ? cN : c0;
7679 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
7680 return false;
7681 }
7682 C = c0;
7683 return true;
7684}
7685
7686/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
7687/// can replace combinations of VMUL and VCVT (floating-point to integer)
7688/// when the VMUL has a constant operand that is a power of 2.
7689///
7690/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
7691/// vmul.f32 d16, d17, d16
7692/// vcvt.s32.f32 d16, d16
7693/// becomes:
7694/// vcvt.s32.f32 d16, d16, #3
7695static SDValue PerformVCVTCombine(SDNode *N,
7696 TargetLowering::DAGCombinerInfo &DCI,
7697 const ARMSubtarget *Subtarget) {
7698 SelectionDAG &DAG = DCI.DAG;
7699 SDValue Op = N->getOperand(0);
7700
7701 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
7702 Op.getOpcode() != ISD::FMUL)
7703 return SDValue();
7704
7705 uint64_t C;
7706 SDValue N0 = Op->getOperand(0);
7707 SDValue ConstVec = Op->getOperand(1);
7708 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
7709
Eric Christopherfa6f5912011-06-29 21:10:36 +00007710 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00007711 !isConstVecPow2(ConstVec, isSigned, C))
7712 return SDValue();
7713
7714 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
7715 Intrinsic::arm_neon_vcvtfp2fxu;
7716 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7717 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00007718 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00007719 DAG.getConstant(Log2_64(C), MVT::i32));
7720}
7721
7722/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
7723/// can replace combinations of VCVT (integer to floating-point) and VDIV
7724/// when the VDIV has a constant operand that is a power of 2.
7725///
7726/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
7727/// vcvt.f32.s32 d16, d16
7728/// vdiv.f32 d16, d17, d16
7729/// becomes:
7730/// vcvt.f32.s32 d16, d16, #3
7731static SDValue PerformVDIVCombine(SDNode *N,
7732 TargetLowering::DAGCombinerInfo &DCI,
7733 const ARMSubtarget *Subtarget) {
7734 SelectionDAG &DAG = DCI.DAG;
7735 SDValue Op = N->getOperand(0);
7736 unsigned OpOpcode = Op.getNode()->getOpcode();
7737
7738 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
7739 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
7740 return SDValue();
7741
7742 uint64_t C;
7743 SDValue ConstVec = N->getOperand(1);
7744 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
7745
7746 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
7747 !isConstVecPow2(ConstVec, isSigned, C))
7748 return SDValue();
7749
Eric Christopherfa6f5912011-06-29 21:10:36 +00007750 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00007751 Intrinsic::arm_neon_vcvtfxu2fp;
7752 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7753 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00007754 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00007755 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
7756}
7757
7758/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00007759/// operand of a vector shift operation, where all the elements of the
7760/// build_vector must have the same constant integer value.
7761static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
7762 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007763 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00007764 Op = Op.getOperand(0);
7765 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
7766 APInt SplatBits, SplatUndef;
7767 unsigned SplatBitSize;
7768 bool HasAnyUndefs;
7769 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
7770 HasAnyUndefs, ElementBits) ||
7771 SplatBitSize > ElementBits)
7772 return false;
7773 Cnt = SplatBits.getSExtValue();
7774 return true;
7775}
7776
7777/// isVShiftLImm - Check if this is a valid build_vector for the immediate
7778/// operand of a vector shift left operation. That value must be in the range:
7779/// 0 <= Value < ElementBits for a left shift; or
7780/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00007781static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00007782 assert(VT.isVector() && "vector shift count is not a vector type");
7783 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
7784 if (! getVShiftImm(Op, ElementBits, Cnt))
7785 return false;
7786 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
7787}
7788
7789/// isVShiftRImm - Check if this is a valid build_vector for the immediate
7790/// operand of a vector shift right operation. For a shift opcode, the value
7791/// is positive, but for an intrinsic the value count must be negative. The
7792/// absolute value must be in the range:
7793/// 1 <= |Value| <= ElementBits for a right shift; or
7794/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00007795static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00007796 int64_t &Cnt) {
7797 assert(VT.isVector() && "vector shift count is not a vector type");
7798 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
7799 if (! getVShiftImm(Op, ElementBits, Cnt))
7800 return false;
7801 if (isIntrinsic)
7802 Cnt = -Cnt;
7803 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
7804}
7805
7806/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
7807static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
7808 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
7809 switch (IntNo) {
7810 default:
7811 // Don't do anything for most intrinsics.
7812 break;
7813
7814 // Vector shifts: check for immediate versions and lower them.
7815 // Note: This is done during DAG combining instead of DAG legalizing because
7816 // the build_vectors for 64-bit vector element shift counts are generally
7817 // not legal, and it is hard to see their values after they get legalized to
7818 // loads from a constant pool.
7819 case Intrinsic::arm_neon_vshifts:
7820 case Intrinsic::arm_neon_vshiftu:
7821 case Intrinsic::arm_neon_vshiftls:
7822 case Intrinsic::arm_neon_vshiftlu:
7823 case Intrinsic::arm_neon_vshiftn:
7824 case Intrinsic::arm_neon_vrshifts:
7825 case Intrinsic::arm_neon_vrshiftu:
7826 case Intrinsic::arm_neon_vrshiftn:
7827 case Intrinsic::arm_neon_vqshifts:
7828 case Intrinsic::arm_neon_vqshiftu:
7829 case Intrinsic::arm_neon_vqshiftsu:
7830 case Intrinsic::arm_neon_vqshiftns:
7831 case Intrinsic::arm_neon_vqshiftnu:
7832 case Intrinsic::arm_neon_vqshiftnsu:
7833 case Intrinsic::arm_neon_vqrshiftns:
7834 case Intrinsic::arm_neon_vqrshiftnu:
7835 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00007836 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007837 int64_t Cnt;
7838 unsigned VShiftOpc = 0;
7839
7840 switch (IntNo) {
7841 case Intrinsic::arm_neon_vshifts:
7842 case Intrinsic::arm_neon_vshiftu:
7843 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
7844 VShiftOpc = ARMISD::VSHL;
7845 break;
7846 }
7847 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
7848 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
7849 ARMISD::VSHRs : ARMISD::VSHRu);
7850 break;
7851 }
7852 return SDValue();
7853
7854 case Intrinsic::arm_neon_vshiftls:
7855 case Intrinsic::arm_neon_vshiftlu:
7856 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
7857 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007858 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007859
7860 case Intrinsic::arm_neon_vrshifts:
7861 case Intrinsic::arm_neon_vrshiftu:
7862 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
7863 break;
7864 return SDValue();
7865
7866 case Intrinsic::arm_neon_vqshifts:
7867 case Intrinsic::arm_neon_vqshiftu:
7868 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
7869 break;
7870 return SDValue();
7871
7872 case Intrinsic::arm_neon_vqshiftsu:
7873 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
7874 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007875 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007876
7877 case Intrinsic::arm_neon_vshiftn:
7878 case Intrinsic::arm_neon_vrshiftn:
7879 case Intrinsic::arm_neon_vqshiftns:
7880 case Intrinsic::arm_neon_vqshiftnu:
7881 case Intrinsic::arm_neon_vqshiftnsu:
7882 case Intrinsic::arm_neon_vqrshiftns:
7883 case Intrinsic::arm_neon_vqrshiftnu:
7884 case Intrinsic::arm_neon_vqrshiftnsu:
7885 // Narrowing shifts require an immediate right shift.
7886 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
7887 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00007888 llvm_unreachable("invalid shift count for narrowing vector shift "
7889 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007890
7891 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00007892 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00007893 }
7894
7895 switch (IntNo) {
7896 case Intrinsic::arm_neon_vshifts:
7897 case Intrinsic::arm_neon_vshiftu:
7898 // Opcode already set above.
7899 break;
7900 case Intrinsic::arm_neon_vshiftls:
7901 case Intrinsic::arm_neon_vshiftlu:
7902 if (Cnt == VT.getVectorElementType().getSizeInBits())
7903 VShiftOpc = ARMISD::VSHLLi;
7904 else
7905 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
7906 ARMISD::VSHLLs : ARMISD::VSHLLu);
7907 break;
7908 case Intrinsic::arm_neon_vshiftn:
7909 VShiftOpc = ARMISD::VSHRN; break;
7910 case Intrinsic::arm_neon_vrshifts:
7911 VShiftOpc = ARMISD::VRSHRs; break;
7912 case Intrinsic::arm_neon_vrshiftu:
7913 VShiftOpc = ARMISD::VRSHRu; break;
7914 case Intrinsic::arm_neon_vrshiftn:
7915 VShiftOpc = ARMISD::VRSHRN; break;
7916 case Intrinsic::arm_neon_vqshifts:
7917 VShiftOpc = ARMISD::VQSHLs; break;
7918 case Intrinsic::arm_neon_vqshiftu:
7919 VShiftOpc = ARMISD::VQSHLu; break;
7920 case Intrinsic::arm_neon_vqshiftsu:
7921 VShiftOpc = ARMISD::VQSHLsu; break;
7922 case Intrinsic::arm_neon_vqshiftns:
7923 VShiftOpc = ARMISD::VQSHRNs; break;
7924 case Intrinsic::arm_neon_vqshiftnu:
7925 VShiftOpc = ARMISD::VQSHRNu; break;
7926 case Intrinsic::arm_neon_vqshiftnsu:
7927 VShiftOpc = ARMISD::VQSHRNsu; break;
7928 case Intrinsic::arm_neon_vqrshiftns:
7929 VShiftOpc = ARMISD::VQRSHRNs; break;
7930 case Intrinsic::arm_neon_vqrshiftnu:
7931 VShiftOpc = ARMISD::VQRSHRNu; break;
7932 case Intrinsic::arm_neon_vqrshiftnsu:
7933 VShiftOpc = ARMISD::VQRSHRNsu; break;
7934 }
7935
7936 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007937 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007938 }
7939
7940 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00007941 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007942 int64_t Cnt;
7943 unsigned VShiftOpc = 0;
7944
7945 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
7946 VShiftOpc = ARMISD::VSLI;
7947 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
7948 VShiftOpc = ARMISD::VSRI;
7949 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00007950 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007951 }
7952
7953 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
7954 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00007955 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007956 }
7957
7958 case Intrinsic::arm_neon_vqrshifts:
7959 case Intrinsic::arm_neon_vqrshiftu:
7960 // No immediate versions of these to check for.
7961 break;
7962 }
7963
7964 return SDValue();
7965}
7966
7967/// PerformShiftCombine - Checks for immediate versions of vector shifts and
7968/// lowers them. As with the vector shift intrinsics, this is done during DAG
7969/// combining instead of DAG legalizing because the build_vectors for 64-bit
7970/// vector element shift counts are generally not legal, and it is hard to see
7971/// their values after they get legalized to loads from a constant pool.
7972static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
7973 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00007974 EVT VT = N->getValueType(0);
Evan Cheng5fb468a2012-02-23 02:58:19 +00007975 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
7976 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
7977 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
7978 SDValue N1 = N->getOperand(1);
7979 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
7980 SDValue N0 = N->getOperand(0);
7981 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
7982 DAG.MaskedValueIsZero(N0.getOperand(0),
7983 APInt::getHighBitsSet(32, 16)))
7984 return DAG.getNode(ISD::ROTR, N->getDebugLoc(), VT, N0, N1);
7985 }
7986 }
Bob Wilson5bafff32009-06-22 23:27:02 +00007987
7988 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00007989 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7990 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00007991 return SDValue();
7992
7993 assert(ST->hasNEON() && "unexpected vector shift");
7994 int64_t Cnt;
7995
7996 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007997 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00007998
7999 case ISD::SHL:
8000 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
8001 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008002 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008003 break;
8004
8005 case ISD::SRA:
8006 case ISD::SRL:
8007 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
8008 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
8009 ARMISD::VSHRs : ARMISD::VSHRu);
8010 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008011 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008012 }
8013 }
8014 return SDValue();
8015}
8016
8017/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
8018/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
8019static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
8020 const ARMSubtarget *ST) {
8021 SDValue N0 = N->getOperand(0);
8022
8023 // Check for sign- and zero-extensions of vector extract operations of 8-
8024 // and 16-bit vector elements. NEON supports these directly. They are
8025 // handled during DAG combining because type legalization will promote them
8026 // to 32-bit types and it is messy to recognize the operations after that.
8027 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
8028 SDValue Vec = N0.getOperand(0);
8029 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00008030 EVT VT = N->getValueType(0);
8031 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008032 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8033
Owen Anderson825b72b2009-08-11 20:47:22 +00008034 if (VT == MVT::i32 &&
8035 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00008036 TLI.isTypeLegal(Vec.getValueType()) &&
8037 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00008038
8039 unsigned Opc = 0;
8040 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008041 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00008042 case ISD::SIGN_EXTEND:
8043 Opc = ARMISD::VGETLANEs;
8044 break;
8045 case ISD::ZERO_EXTEND:
8046 case ISD::ANY_EXTEND:
8047 Opc = ARMISD::VGETLANEu;
8048 break;
8049 }
8050 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
8051 }
8052 }
8053
8054 return SDValue();
8055}
8056
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008057/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
8058/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
8059static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
8060 const ARMSubtarget *ST) {
8061 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00008062 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008063 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
8064 // a NaN; only do the transformation when it matches that behavior.
8065
8066 // For now only do this when using NEON for FP operations; if using VFP, it
8067 // is not obvious that the benefit outweighs the cost of switching to the
8068 // NEON pipeline.
8069 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
8070 N->getValueType(0) != MVT::f32)
8071 return SDValue();
8072
8073 SDValue CondLHS = N->getOperand(0);
8074 SDValue CondRHS = N->getOperand(1);
8075 SDValue LHS = N->getOperand(2);
8076 SDValue RHS = N->getOperand(3);
8077 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
8078
8079 unsigned Opcode = 0;
8080 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00008081 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008082 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00008083 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008084 IsReversed = true ; // x CC y ? y : x
8085 } else {
8086 return SDValue();
8087 }
8088
Bob Wilsone742bb52010-02-24 22:15:53 +00008089 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008090 switch (CC) {
8091 default: break;
8092 case ISD::SETOLT:
8093 case ISD::SETOLE:
8094 case ISD::SETLT:
8095 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008096 case ISD::SETULT:
8097 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00008098 // If LHS is NaN, an ordered comparison will be false and the result will
8099 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
8100 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
8101 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
8102 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8103 break;
8104 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
8105 // will return -0, so vmin can only be used for unsafe math or if one of
8106 // the operands is known to be nonzero.
8107 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008108 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00008109 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8110 break;
8111 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008112 break;
8113
8114 case ISD::SETOGT:
8115 case ISD::SETOGE:
8116 case ISD::SETGT:
8117 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008118 case ISD::SETUGT:
8119 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00008120 // If LHS is NaN, an ordered comparison will be false and the result will
8121 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
8122 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
8123 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
8124 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8125 break;
8126 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
8127 // will return +0, so vmax can only be used for unsafe math or if one of
8128 // the operands is known to be nonzero.
8129 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008130 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00008131 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8132 break;
8133 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008134 break;
8135 }
8136
8137 if (!Opcode)
8138 return SDValue();
8139 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
8140}
8141
Evan Chenge721f5c2011-07-13 00:42:17 +00008142/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
8143SDValue
8144ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
8145 SDValue Cmp = N->getOperand(4);
8146 if (Cmp.getOpcode() != ARMISD::CMPZ)
8147 // Only looking at EQ and NE cases.
8148 return SDValue();
8149
8150 EVT VT = N->getValueType(0);
8151 DebugLoc dl = N->getDebugLoc();
8152 SDValue LHS = Cmp.getOperand(0);
8153 SDValue RHS = Cmp.getOperand(1);
8154 SDValue FalseVal = N->getOperand(0);
8155 SDValue TrueVal = N->getOperand(1);
8156 SDValue ARMcc = N->getOperand(2);
Jim Grosbachb04546f2011-09-13 20:30:37 +00008157 ARMCC::CondCodes CC =
8158 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chenge721f5c2011-07-13 00:42:17 +00008159
8160 // Simplify
8161 // mov r1, r0
8162 // cmp r1, x
8163 // mov r0, y
8164 // moveq r0, x
8165 // to
8166 // cmp r0, x
8167 // movne r0, y
8168 //
8169 // mov r1, r0
8170 // cmp r1, x
8171 // mov r0, x
8172 // movne r0, y
8173 // to
8174 // cmp r0, x
8175 // movne r0, y
8176 /// FIXME: Turn this into a target neutral optimization?
8177 SDValue Res;
Evan Cheng9b88d2d2011-09-28 23:16:31 +00008178 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chenge721f5c2011-07-13 00:42:17 +00008179 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
8180 N->getOperand(3), Cmp);
8181 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
8182 SDValue ARMcc;
8183 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
8184 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
8185 N->getOperand(3), NewCmp);
8186 }
8187
8188 if (Res.getNode()) {
8189 APInt KnownZero, KnownOne;
8190 APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits());
8191 DAG.ComputeMaskedBits(SDValue(N,0), Mask, KnownZero, KnownOne);
8192 // Capture demanded bits information that would be otherwise lost.
8193 if (KnownZero == 0xfffffffe)
8194 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8195 DAG.getValueType(MVT::i1));
8196 else if (KnownZero == 0xffffff00)
8197 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8198 DAG.getValueType(MVT::i8));
8199 else if (KnownZero == 0xffff0000)
8200 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8201 DAG.getValueType(MVT::i16));
8202 }
8203
8204 return Res;
8205}
8206
Dan Gohman475871a2008-07-27 21:46:04 +00008207SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008208 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008209 switch (N->getOpcode()) {
8210 default: break;
Tanya Lattner189531f2011-06-14 23:48:48 +00008211 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008212 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00008213 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008214 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chengc892aeb2012-02-23 01:19:06 +00008215 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
8216 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Cheng0c1aec12010-12-14 03:22:07 +00008217 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00008218 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008219 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00008220 case ISD::STORE: return PerformSTORECombine(N, DCI);
8221 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
8222 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00008223 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008224 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00008225 case ISD::FP_TO_SINT:
8226 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
8227 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008228 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00008229 case ISD::SHL:
8230 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008231 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00008232 case ISD::SIGN_EXTEND:
8233 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008234 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
8235 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00008236 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00008237 case ARMISD::VLD2DUP:
8238 case ARMISD::VLD3DUP:
8239 case ARMISD::VLD4DUP:
8240 return CombineBaseUpdate(N, DCI);
8241 case ISD::INTRINSIC_VOID:
8242 case ISD::INTRINSIC_W_CHAIN:
8243 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
8244 case Intrinsic::arm_neon_vld1:
8245 case Intrinsic::arm_neon_vld2:
8246 case Intrinsic::arm_neon_vld3:
8247 case Intrinsic::arm_neon_vld4:
8248 case Intrinsic::arm_neon_vld2lane:
8249 case Intrinsic::arm_neon_vld3lane:
8250 case Intrinsic::arm_neon_vld4lane:
8251 case Intrinsic::arm_neon_vst1:
8252 case Intrinsic::arm_neon_vst2:
8253 case Intrinsic::arm_neon_vst3:
8254 case Intrinsic::arm_neon_vst4:
8255 case Intrinsic::arm_neon_vst2lane:
8256 case Intrinsic::arm_neon_vst3lane:
8257 case Intrinsic::arm_neon_vst4lane:
8258 return CombineBaseUpdate(N, DCI);
8259 default: break;
8260 }
8261 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008262 }
Dan Gohman475871a2008-07-27 21:46:04 +00008263 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008264}
8265
Evan Cheng31959b12011-02-02 01:06:55 +00008266bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
8267 EVT VT) const {
8268 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
8269}
8270
Bill Wendlingaf566342009-08-15 21:21:19 +00008271bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00008272 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00008273 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00008274
8275 switch (VT.getSimpleVT().SimpleTy) {
8276 default:
8277 return false;
8278 case MVT::i8:
8279 case MVT::i16:
8280 case MVT::i32:
8281 return true;
8282 // FIXME: VLD1 etc with standard alignment is legal.
8283 }
8284}
8285
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008286static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
8287 unsigned AlignCheck) {
8288 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
8289 (DstAlign == 0 || DstAlign % AlignCheck == 0));
8290}
8291
8292EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
8293 unsigned DstAlign, unsigned SrcAlign,
Lang Hamesa1e78882011-11-02 23:37:04 +00008294 bool IsZeroVal,
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008295 bool MemcpyStrSrc,
8296 MachineFunction &MF) const {
8297 const Function *F = MF.getFunction();
8298
8299 // See if we can use NEON instructions for this...
Lang Hamesa1e78882011-11-02 23:37:04 +00008300 if (IsZeroVal &&
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008301 !F->hasFnAttr(Attribute::NoImplicitFloat) &&
8302 Subtarget->hasNEON()) {
8303 if (memOpAlign(SrcAlign, DstAlign, 16) && Size >= 16) {
8304 return MVT::v4i32;
8305 } else if (memOpAlign(SrcAlign, DstAlign, 8) && Size >= 8) {
8306 return MVT::v2i32;
8307 }
8308 }
8309
Lang Hames5207bf22011-11-08 18:56:23 +00008310 // Lowering to i32/i16 if the size permits.
8311 if (Size >= 4) {
8312 return MVT::i32;
8313 } else if (Size >= 2) {
8314 return MVT::i16;
8315 }
8316
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008317 // Let the target-independent logic figure it out.
8318 return MVT::Other;
8319}
8320
Evan Chenge6c835f2009-08-14 20:09:37 +00008321static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
8322 if (V < 0)
8323 return false;
8324
8325 unsigned Scale = 1;
8326 switch (VT.getSimpleVT().SimpleTy) {
8327 default: return false;
8328 case MVT::i1:
8329 case MVT::i8:
8330 // Scale == 1;
8331 break;
8332 case MVT::i16:
8333 // Scale == 2;
8334 Scale = 2;
8335 break;
8336 case MVT::i32:
8337 // Scale == 4;
8338 Scale = 4;
8339 break;
8340 }
8341
8342 if ((V & (Scale - 1)) != 0)
8343 return false;
8344 V /= Scale;
8345 return V == (V & ((1LL << 5) - 1));
8346}
8347
8348static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
8349 const ARMSubtarget *Subtarget) {
8350 bool isNeg = false;
8351 if (V < 0) {
8352 isNeg = true;
8353 V = - V;
8354 }
8355
8356 switch (VT.getSimpleVT().SimpleTy) {
8357 default: return false;
8358 case MVT::i1:
8359 case MVT::i8:
8360 case MVT::i16:
8361 case MVT::i32:
8362 // + imm12 or - imm8
8363 if (isNeg)
8364 return V == (V & ((1LL << 8) - 1));
8365 return V == (V & ((1LL << 12) - 1));
8366 case MVT::f32:
8367 case MVT::f64:
8368 // Same as ARM mode. FIXME: NEON?
8369 if (!Subtarget->hasVFP2())
8370 return false;
8371 if ((V & 3) != 0)
8372 return false;
8373 V >>= 2;
8374 return V == (V & ((1LL << 8) - 1));
8375 }
8376}
8377
Evan Chengb01fad62007-03-12 23:30:29 +00008378/// isLegalAddressImmediate - Return true if the integer value can be used
8379/// as the offset of the target addressing mode for load / store of the
8380/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00008381static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00008382 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00008383 if (V == 0)
8384 return true;
8385
Evan Cheng65011532009-03-09 19:15:00 +00008386 if (!VT.isSimple())
8387 return false;
8388
Evan Chenge6c835f2009-08-14 20:09:37 +00008389 if (Subtarget->isThumb1Only())
8390 return isLegalT1AddressImmediate(V, VT);
8391 else if (Subtarget->isThumb2())
8392 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00008393
Evan Chenge6c835f2009-08-14 20:09:37 +00008394 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00008395 if (V < 0)
8396 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00008397 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00008398 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008399 case MVT::i1:
8400 case MVT::i8:
8401 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00008402 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008403 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008404 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00008405 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008406 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008407 case MVT::f32:
8408 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00008409 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00008410 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00008411 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00008412 return false;
8413 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008414 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00008415 }
Evan Chenga8e29892007-01-19 07:51:42 +00008416}
8417
Evan Chenge6c835f2009-08-14 20:09:37 +00008418bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
8419 EVT VT) const {
8420 int Scale = AM.Scale;
8421 if (Scale < 0)
8422 return false;
8423
8424 switch (VT.getSimpleVT().SimpleTy) {
8425 default: return false;
8426 case MVT::i1:
8427 case MVT::i8:
8428 case MVT::i16:
8429 case MVT::i32:
8430 if (Scale == 1)
8431 return true;
8432 // r + r << imm
8433 Scale = Scale & ~1;
8434 return Scale == 2 || Scale == 4 || Scale == 8;
8435 case MVT::i64:
8436 // r + r
8437 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
8438 return true;
8439 return false;
8440 case MVT::isVoid:
8441 // Note, we allow "void" uses (basically, uses that aren't loads or
8442 // stores), because arm allows folding a scale into many arithmetic
8443 // operations. This should be made more precise and revisited later.
8444
8445 // Allow r << imm, but the imm has to be a multiple of two.
8446 if (Scale & 1) return false;
8447 return isPowerOf2_32(Scale);
8448 }
8449}
8450
Chris Lattner37caf8c2007-04-09 23:33:39 +00008451/// isLegalAddressingMode - Return true if the addressing mode represented
8452/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008453bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008454 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008455 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00008456 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00008457 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008458
Chris Lattner37caf8c2007-04-09 23:33:39 +00008459 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008460 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008461 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008462
Chris Lattner37caf8c2007-04-09 23:33:39 +00008463 switch (AM.Scale) {
8464 case 0: // no scale reg, must be "r+i" or "r", or "i".
8465 break;
8466 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00008467 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00008468 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008469 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00008470 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008471 // ARM doesn't support any R+R*scale+imm addr modes.
8472 if (AM.BaseOffs)
8473 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008474
Bob Wilson2c7dab12009-04-08 17:55:28 +00008475 if (!VT.isSimple())
8476 return false;
8477
Evan Chenge6c835f2009-08-14 20:09:37 +00008478 if (Subtarget->isThumb2())
8479 return isLegalT2ScaledAddressingMode(AM, VT);
8480
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008481 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00008482 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00008483 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008484 case MVT::i1:
8485 case MVT::i8:
8486 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008487 if (Scale < 0) Scale = -Scale;
8488 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008489 return true;
8490 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00008491 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008492 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00008493 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00008494 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008495 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008496 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00008497 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008498
Owen Anderson825b72b2009-08-11 20:47:22 +00008499 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00008500 // Note, we allow "void" uses (basically, uses that aren't loads or
8501 // stores), because arm allows folding a scale into many arithmetic
8502 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008503
Chris Lattner37caf8c2007-04-09 23:33:39 +00008504 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00008505 if (Scale & 1) return false;
8506 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00008507 }
Evan Chengb01fad62007-03-12 23:30:29 +00008508 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00008509 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00008510}
8511
Evan Cheng77e47512009-11-11 19:05:52 +00008512/// isLegalICmpImmediate - Return true if the specified immediate is legal
8513/// icmp immediate, that is the target has icmp instructions which can compare
8514/// a register against the immediate without having to materialize the
8515/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00008516bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00008517 if (!Subtarget->isThumb())
8518 return ARM_AM::getSOImmVal(Imm) != -1;
8519 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00008520 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00008521 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00008522}
8523
Dan Gohmancca82142011-05-03 00:46:49 +00008524/// isLegalAddImmediate - Return true if the specified immediate is legal
8525/// add immediate, that is the target has add instructions which can add
8526/// a register with the immediate without having to materialize the
8527/// immediate into a register.
8528bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
8529 return ARM_AM::getSOImmVal(Imm) != -1;
8530}
8531
Owen Andersone50ed302009-08-10 22:56:29 +00008532static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00008533 bool isSEXTLoad, SDValue &Base,
8534 SDValue &Offset, bool &isInc,
8535 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00008536 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
8537 return false;
8538
Owen Anderson825b72b2009-08-11 20:47:22 +00008539 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00008540 // AddressingMode 3
8541 Base = Ptr->getOperand(0);
8542 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008543 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00008544 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008545 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00008546 isInc = false;
8547 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8548 return true;
8549 }
8550 }
8551 isInc = (Ptr->getOpcode() == ISD::ADD);
8552 Offset = Ptr->getOperand(1);
8553 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00008554 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00008555 // AddressingMode 2
8556 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008557 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00008558 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008559 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00008560 isInc = false;
8561 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8562 Base = Ptr->getOperand(0);
8563 return true;
8564 }
8565 }
8566
8567 if (Ptr->getOpcode() == ISD::ADD) {
8568 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00008569 ARM_AM::ShiftOpc ShOpcVal=
8570 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00008571 if (ShOpcVal != ARM_AM::no_shift) {
8572 Base = Ptr->getOperand(1);
8573 Offset = Ptr->getOperand(0);
8574 } else {
8575 Base = Ptr->getOperand(0);
8576 Offset = Ptr->getOperand(1);
8577 }
8578 return true;
8579 }
8580
8581 isInc = (Ptr->getOpcode() == ISD::ADD);
8582 Base = Ptr->getOperand(0);
8583 Offset = Ptr->getOperand(1);
8584 return true;
8585 }
8586
Jim Grosbache5165492009-11-09 00:11:35 +00008587 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00008588 return false;
8589}
8590
Owen Andersone50ed302009-08-10 22:56:29 +00008591static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00008592 bool isSEXTLoad, SDValue &Base,
8593 SDValue &Offset, bool &isInc,
8594 SelectionDAG &DAG) {
8595 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
8596 return false;
8597
8598 Base = Ptr->getOperand(0);
8599 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
8600 int RHSC = (int)RHS->getZExtValue();
8601 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
8602 assert(Ptr->getOpcode() == ISD::ADD);
8603 isInc = false;
8604 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8605 return true;
8606 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
8607 isInc = Ptr->getOpcode() == ISD::ADD;
8608 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
8609 return true;
8610 }
8611 }
8612
8613 return false;
8614}
8615
Evan Chenga8e29892007-01-19 07:51:42 +00008616/// getPreIndexedAddressParts - returns true by value, base pointer and
8617/// offset pointer and addressing mode by reference if the node's address
8618/// can be legally represented as pre-indexed load / store address.
8619bool
Dan Gohman475871a2008-07-27 21:46:04 +00008620ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
8621 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00008622 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00008623 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008624 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00008625 return false;
8626
Owen Andersone50ed302009-08-10 22:56:29 +00008627 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00008628 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00008629 bool isSEXTLoad = false;
8630 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
8631 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008632 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00008633 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
8634 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
8635 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008636 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00008637 } else
8638 return false;
8639
8640 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00008641 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00008642 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00008643 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
8644 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00008645 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00008646 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00008647 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00008648 if (!isLegal)
8649 return false;
8650
8651 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
8652 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00008653}
8654
8655/// getPostIndexedAddressParts - returns true by value, base pointer and
8656/// offset pointer and addressing mode by reference if this node can be
8657/// combined with a load / store to form a post-indexed load / store.
8658bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00008659 SDValue &Base,
8660 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00008661 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00008662 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008663 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00008664 return false;
8665
Owen Andersone50ed302009-08-10 22:56:29 +00008666 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00008667 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00008668 bool isSEXTLoad = false;
8669 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008670 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00008671 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00008672 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
8673 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008674 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00008675 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00008676 } else
8677 return false;
8678
8679 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00008680 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00008681 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00008682 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00008683 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00008684 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00008685 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
8686 isInc, DAG);
8687 if (!isLegal)
8688 return false;
8689
Evan Cheng28dad2a2010-05-18 21:31:17 +00008690 if (Ptr != Base) {
8691 // Swap base ptr and offset to catch more post-index load / store when
8692 // it's legal. In Thumb2 mode, offset must be an immediate.
8693 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
8694 !Subtarget->isThumb2())
8695 std::swap(Base, Offset);
8696
8697 // Post-indexed load / store update the base pointer.
8698 if (Ptr != Base)
8699 return false;
8700 }
8701
Evan Chenge88d5ce2009-07-02 07:28:31 +00008702 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
8703 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00008704}
8705
Dan Gohman475871a2008-07-27 21:46:04 +00008706void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00008707 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008708 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008709 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008710 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00008711 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008712 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00008713 switch (Op.getOpcode()) {
8714 default: break;
8715 case ARMISD::CMOV: {
8716 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00008717 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00008718 if (KnownZero == 0 && KnownOne == 0) return;
8719
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008720 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00008721 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
8722 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00008723 KnownZero &= KnownZeroRHS;
8724 KnownOne &= KnownOneRHS;
8725 return;
8726 }
8727 }
8728}
8729
8730//===----------------------------------------------------------------------===//
8731// ARM Inline Assembly Support
8732//===----------------------------------------------------------------------===//
8733
Evan Cheng55d42002011-01-08 01:24:27 +00008734bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
8735 // Looking for "rev" which is V6+.
8736 if (!Subtarget->hasV6Ops())
8737 return false;
8738
8739 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
8740 std::string AsmStr = IA->getAsmString();
8741 SmallVector<StringRef, 4> AsmPieces;
8742 SplitString(AsmStr, AsmPieces, ";\n");
8743
8744 switch (AsmPieces.size()) {
8745 default: return false;
8746 case 1:
8747 AsmStr = AsmPieces[0];
8748 AsmPieces.clear();
8749 SplitString(AsmStr, AsmPieces, " \t,");
8750
8751 // rev $0, $1
8752 if (AsmPieces.size() == 3 &&
8753 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
8754 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008755 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00008756 if (Ty && Ty->getBitWidth() == 32)
8757 return IntrinsicLowering::LowerToByteSwap(CI);
8758 }
8759 break;
8760 }
8761
8762 return false;
8763}
8764
Evan Chenga8e29892007-01-19 07:51:42 +00008765/// getConstraintType - Given a constraint letter, return the type of
8766/// constraint it is for this target.
8767ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00008768ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
8769 if (Constraint.size() == 1) {
8770 switch (Constraint[0]) {
8771 default: break;
8772 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008773 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00008774 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00008775 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008776 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00008777 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00008778 // An address with a single base register. Due to the way we
8779 // currently handle addresses it is the same as an 'r' memory constraint.
8780 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00008781 }
Eric Christopher1312ca82011-06-21 22:10:57 +00008782 } else if (Constraint.size() == 2) {
8783 switch (Constraint[0]) {
8784 default: break;
8785 // All 'U+' constraints are addresses.
8786 case 'U': return C_Memory;
8787 }
Evan Chenga8e29892007-01-19 07:51:42 +00008788 }
Chris Lattner4234f572007-03-25 02:14:49 +00008789 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00008790}
8791
John Thompson44ab89e2010-10-29 17:29:13 +00008792/// Examine constraint type and operand type and determine a weight value.
8793/// This object must already have been set up with the operand type
8794/// and the current alternative constraint selected.
8795TargetLowering::ConstraintWeight
8796ARMTargetLowering::getSingleConstraintMatchWeight(
8797 AsmOperandInfo &info, const char *constraint) const {
8798 ConstraintWeight weight = CW_Invalid;
8799 Value *CallOperandVal = info.CallOperandVal;
8800 // If we don't have a value, we can't do a match,
8801 // but allow it at the lowest weight.
8802 if (CallOperandVal == NULL)
8803 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008804 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00008805 // Look at the constraint type.
8806 switch (*constraint) {
8807 default:
8808 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
8809 break;
8810 case 'l':
8811 if (type->isIntegerTy()) {
8812 if (Subtarget->isThumb())
8813 weight = CW_SpecificReg;
8814 else
8815 weight = CW_Register;
8816 }
8817 break;
8818 case 'w':
8819 if (type->isFloatingPointTy())
8820 weight = CW_Register;
8821 break;
8822 }
8823 return weight;
8824}
8825
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008826typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
8827RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00008828ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00008829 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00008830 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00008831 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00008832 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00008833 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00008834 if (Subtarget->isThumb())
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008835 return RCPair(0U, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +00008836 else
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008837 return RCPair(0U, ARM::GPRRegisterClass);
Eric Christopher73744df2011-06-30 23:23:01 +00008838 case 'h': // High regs or no regs.
8839 if (Subtarget->isThumb())
Andrew Trick3af7a672011-09-20 03:06:13 +00008840 return RCPair(0U, ARM::hGPRRegisterClass);
Eric Christopher1070f822011-07-01 00:19:27 +00008841 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008842 case 'r':
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008843 return RCPair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008844 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00008845 if (VT == MVT::f32)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008846 return RCPair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00008847 if (VT.getSizeInBits() == 64)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008848 return RCPair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00008849 if (VT.getSizeInBits() == 128)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008850 return RCPair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008851 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00008852 case 'x':
8853 if (VT == MVT::f32)
Andrew Trick3af7a672011-09-20 03:06:13 +00008854 return RCPair(0U, ARM::SPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008855 if (VT.getSizeInBits() == 64)
Andrew Trick3af7a672011-09-20 03:06:13 +00008856 return RCPair(0U, ARM::DPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008857 if (VT.getSizeInBits() == 128)
Andrew Trick3af7a672011-09-20 03:06:13 +00008858 return RCPair(0U, ARM::QPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008859 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008860 case 't':
8861 if (VT == MVT::f32)
Andrew Trick3af7a672011-09-20 03:06:13 +00008862 return RCPair(0U, ARM::SPRRegisterClass);
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008863 break;
Evan Chenga8e29892007-01-19 07:51:42 +00008864 }
8865 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00008866 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00008867 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00008868
Evan Chenga8e29892007-01-19 07:51:42 +00008869 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
8870}
8871
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008872/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
8873/// vector. If it is invalid, don't add anything to Ops.
8874void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00008875 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008876 std::vector<SDValue>&Ops,
8877 SelectionDAG &DAG) const {
8878 SDValue Result(0, 0);
8879
Eric Christopher100c8332011-06-02 23:16:42 +00008880 // Currently only support length 1 constraints.
8881 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00008882
Eric Christopher100c8332011-06-02 23:16:42 +00008883 char ConstraintLetter = Constraint[0];
8884 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008885 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00008886 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008887 case 'I': case 'J': case 'K': case 'L':
8888 case 'M': case 'N': case 'O':
8889 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
8890 if (!C)
8891 return;
8892
8893 int64_t CVal64 = C->getSExtValue();
8894 int CVal = (int) CVal64;
8895 // None of these constraints allow values larger than 32 bits. Check
8896 // that the value fits in an int.
8897 if (CVal != CVal64)
8898 return;
8899
Eric Christopher100c8332011-06-02 23:16:42 +00008900 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00008901 case 'j':
Andrew Trick3af7a672011-09-20 03:06:13 +00008902 // Constant suitable for movw, must be between 0 and
8903 // 65535.
8904 if (Subtarget->hasV6T2Ops())
8905 if (CVal >= 0 && CVal <= 65535)
8906 break;
8907 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008908 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008909 if (Subtarget->isThumb1Only()) {
8910 // This must be a constant between 0 and 255, for ADD
8911 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008912 if (CVal >= 0 && CVal <= 255)
8913 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008914 } else if (Subtarget->isThumb2()) {
8915 // A constant that can be used as an immediate value in a
8916 // data-processing instruction.
8917 if (ARM_AM::getT2SOImmVal(CVal) != -1)
8918 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008919 } else {
8920 // A constant that can be used as an immediate value in a
8921 // data-processing instruction.
8922 if (ARM_AM::getSOImmVal(CVal) != -1)
8923 break;
8924 }
8925 return;
8926
8927 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008928 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008929 // This must be a constant between -255 and -1, for negated ADD
8930 // immediates. This can be used in GCC with an "n" modifier that
8931 // prints the negated value, for use with SUB instructions. It is
8932 // not useful otherwise but is implemented for compatibility.
8933 if (CVal >= -255 && CVal <= -1)
8934 break;
8935 } else {
8936 // This must be a constant between -4095 and 4095. It is not clear
8937 // what this constraint is intended for. Implemented for
8938 // compatibility with GCC.
8939 if (CVal >= -4095 && CVal <= 4095)
8940 break;
8941 }
8942 return;
8943
8944 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008945 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008946 // A 32-bit value where only one byte has a nonzero value. Exclude
8947 // zero to match GCC. This constraint is used by GCC internally for
8948 // constants that can be loaded with a move/shift combination.
8949 // It is not useful otherwise but is implemented for compatibility.
8950 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
8951 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008952 } else if (Subtarget->isThumb2()) {
8953 // A constant whose bitwise inverse can be used as an immediate
8954 // value in a data-processing instruction. This can be used in GCC
8955 // with a "B" modifier that prints the inverted value, for use with
8956 // BIC and MVN instructions. It is not useful otherwise but is
8957 // implemented for compatibility.
8958 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
8959 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008960 } else {
8961 // A constant whose bitwise inverse can be used as an immediate
8962 // value in a data-processing instruction. This can be used in GCC
8963 // with a "B" modifier that prints the inverted value, for use with
8964 // BIC and MVN instructions. It is not useful otherwise but is
8965 // implemented for compatibility.
8966 if (ARM_AM::getSOImmVal(~CVal) != -1)
8967 break;
8968 }
8969 return;
8970
8971 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008972 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008973 // This must be a constant between -7 and 7,
8974 // for 3-operand ADD/SUB immediate instructions.
8975 if (CVal >= -7 && CVal < 7)
8976 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008977 } else if (Subtarget->isThumb2()) {
8978 // A constant whose negation can be used as an immediate value in a
8979 // data-processing instruction. This can be used in GCC with an "n"
8980 // modifier that prints the negated value, for use with SUB
8981 // instructions. It is not useful otherwise but is implemented for
8982 // compatibility.
8983 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
8984 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008985 } else {
8986 // A constant whose negation can be used as an immediate value in a
8987 // data-processing instruction. This can be used in GCC with an "n"
8988 // modifier that prints the negated value, for use with SUB
8989 // instructions. It is not useful otherwise but is implemented for
8990 // compatibility.
8991 if (ARM_AM::getSOImmVal(-CVal) != -1)
8992 break;
8993 }
8994 return;
8995
8996 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008997 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008998 // This must be a multiple of 4 between 0 and 1020, for
8999 // ADD sp + immediate.
9000 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
9001 break;
9002 } else {
9003 // A power of two or a constant between 0 and 32. This is used in
9004 // GCC for the shift amount on shifted register operands, but it is
9005 // useful in general for any shift amounts.
9006 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
9007 break;
9008 }
9009 return;
9010
9011 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009012 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009013 // This must be a constant between 0 and 31, for shift amounts.
9014 if (CVal >= 0 && CVal <= 31)
9015 break;
9016 }
9017 return;
9018
9019 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009020 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009021 // This must be a multiple of 4 between -508 and 508, for
9022 // ADD/SUB sp = sp + immediate.
9023 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
9024 break;
9025 }
9026 return;
9027 }
9028 Result = DAG.getTargetConstant(CVal, Op.getValueType());
9029 break;
9030 }
9031
9032 if (Result.getNode()) {
9033 Ops.push_back(Result);
9034 return;
9035 }
Dale Johannesen1784d162010-06-25 21:55:36 +00009036 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009037}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00009038
9039bool
9040ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
9041 // The ARM target isn't yet aware of offsets.
9042 return false;
9043}
Evan Cheng39382422009-10-28 01:44:26 +00009044
Jim Grosbach469bbdb2010-07-16 23:05:05 +00009045bool ARM::isBitFieldInvertedMask(unsigned v) {
9046 if (v == 0xffffffff)
9047 return 0;
9048 // there can be 1's on either or both "outsides", all the "inside"
9049 // bits must be 0's
9050 unsigned int lsb = 0, msb = 31;
9051 while (v & (1 << msb)) --msb;
9052 while (v & (1 << lsb)) ++lsb;
9053 for (unsigned int i = lsb; i <= msb; ++i) {
9054 if (v & (1 << i))
9055 return 0;
9056 }
9057 return 1;
9058}
9059
Evan Cheng39382422009-10-28 01:44:26 +00009060/// isFPImmLegal - Returns true if the target can instruction select the
9061/// specified FP immediate natively. If false, the legalizer will
9062/// materialize the FP immediate as a load from a constant pool.
9063bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
9064 if (!Subtarget->hasVFP3())
9065 return false;
9066 if (VT == MVT::f32)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00009067 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00009068 if (VT == MVT::f64)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00009069 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00009070 return false;
9071}
Bob Wilson65ffec42010-09-21 17:56:22 +00009072
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009073/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00009074/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
9075/// specified in the intrinsic calls.
9076bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
9077 const CallInst &I,
9078 unsigned Intrinsic) const {
9079 switch (Intrinsic) {
9080 case Intrinsic::arm_neon_vld1:
9081 case Intrinsic::arm_neon_vld2:
9082 case Intrinsic::arm_neon_vld3:
9083 case Intrinsic::arm_neon_vld4:
9084 case Intrinsic::arm_neon_vld2lane:
9085 case Intrinsic::arm_neon_vld3lane:
9086 case Intrinsic::arm_neon_vld4lane: {
9087 Info.opc = ISD::INTRINSIC_W_CHAIN;
9088 // Conservatively set memVT to the entire set of vectors loaded.
9089 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
9090 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
9091 Info.ptrVal = I.getArgOperand(0);
9092 Info.offset = 0;
9093 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
9094 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
9095 Info.vol = false; // volatile loads with NEON intrinsics not supported
9096 Info.readMem = true;
9097 Info.writeMem = false;
9098 return true;
9099 }
9100 case Intrinsic::arm_neon_vst1:
9101 case Intrinsic::arm_neon_vst2:
9102 case Intrinsic::arm_neon_vst3:
9103 case Intrinsic::arm_neon_vst4:
9104 case Intrinsic::arm_neon_vst2lane:
9105 case Intrinsic::arm_neon_vst3lane:
9106 case Intrinsic::arm_neon_vst4lane: {
9107 Info.opc = ISD::INTRINSIC_VOID;
9108 // Conservatively set memVT to the entire set of vectors stored.
9109 unsigned NumElts = 0;
9110 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009111 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +00009112 if (!ArgTy->isVectorTy())
9113 break;
9114 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
9115 }
9116 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
9117 Info.ptrVal = I.getArgOperand(0);
9118 Info.offset = 0;
9119 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
9120 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
9121 Info.vol = false; // volatile stores with NEON intrinsics not supported
9122 Info.readMem = false;
9123 Info.writeMem = true;
9124 return true;
9125 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009126 case Intrinsic::arm_strexd: {
9127 Info.opc = ISD::INTRINSIC_W_CHAIN;
9128 Info.memVT = MVT::i64;
9129 Info.ptrVal = I.getArgOperand(2);
9130 Info.offset = 0;
9131 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009132 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009133 Info.readMem = false;
9134 Info.writeMem = true;
9135 return true;
9136 }
9137 case Intrinsic::arm_ldrexd: {
9138 Info.opc = ISD::INTRINSIC_W_CHAIN;
9139 Info.memVT = MVT::i64;
9140 Info.ptrVal = I.getArgOperand(0);
9141 Info.offset = 0;
9142 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009143 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009144 Info.readMem = true;
9145 Info.writeMem = false;
9146 return true;
9147 }
Bob Wilson65ffec42010-09-21 17:56:22 +00009148 default:
9149 break;
9150 }
9151
9152 return false;
9153}