blob: b7df378f05f428fc043fad5bb341814f1680607e [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
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000264 if (Subtarget->isAAPCS_ABI()) {
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
536 // a destination type that is wider than the source.
537 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
538 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000539
Bob Wilson1c3ef902011-02-07 17:43:21 +0000540 setTargetDAGCombine(ISD::INTRINSIC_VOID);
541 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000542 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
543 setTargetDAGCombine(ISD::SHL);
544 setTargetDAGCombine(ISD::SRL);
545 setTargetDAGCombine(ISD::SRA);
546 setTargetDAGCombine(ISD::SIGN_EXTEND);
547 setTargetDAGCombine(ISD::ZERO_EXTEND);
548 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000549 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000550 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000551 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000552 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
553 setTargetDAGCombine(ISD::STORE);
Chad Rosieref01edf2011-06-24 19:23:04 +0000554 setTargetDAGCombine(ISD::FP_TO_SINT);
555 setTargetDAGCombine(ISD::FP_TO_UINT);
556 setTargetDAGCombine(ISD::FDIV);
Nadav Rotem004a24b2011-10-15 20:03:12 +0000557
558 setLoadExtAction(ISD::EXTLOAD, MVT::v4i8, Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000559 }
560
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000561 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000562
563 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000564 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000565
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000566 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000567 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000568
Evan Chenga8e29892007-01-19 07:51:42 +0000569 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000570 if (!Subtarget->isThumb1Only()) {
571 for (unsigned im = (unsigned)ISD::PRE_INC;
572 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000573 setIndexedLoadAction(im, MVT::i1, Legal);
574 setIndexedLoadAction(im, MVT::i8, Legal);
575 setIndexedLoadAction(im, MVT::i16, Legal);
576 setIndexedLoadAction(im, MVT::i32, Legal);
577 setIndexedStoreAction(im, MVT::i1, Legal);
578 setIndexedStoreAction(im, MVT::i8, Legal);
579 setIndexedStoreAction(im, MVT::i16, Legal);
580 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000581 }
Evan Chenga8e29892007-01-19 07:51:42 +0000582 }
583
584 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000585 setOperationAction(ISD::MUL, MVT::i64, Expand);
586 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000587 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000588 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
589 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000590 }
Jim Grosbacha7603982011-07-01 21:12:19 +0000591 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
592 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopher2cc40132011-04-19 18:49:19 +0000593 setOperationAction(ISD::MULHS, MVT::i32, Expand);
594
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000595 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000596 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000597 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000598 setOperationAction(ISD::SRL, MVT::i64, Custom);
599 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000600
Evan Cheng342e3162011-08-30 01:34:54 +0000601 if (!Subtarget->isThumb1Only()) {
602 // FIXME: We should do this for Thumb1 as well.
603 setOperationAction(ISD::ADDC, MVT::i32, Custom);
604 setOperationAction(ISD::ADDE, MVT::i32, Custom);
605 setOperationAction(ISD::SUBC, MVT::i32, Custom);
606 setOperationAction(ISD::SUBE, MVT::i32, Custom);
607 }
608
Evan Chenga8e29892007-01-19 07:51:42 +0000609 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000610 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000611 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000612 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000613 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000614 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000615
Chandler Carruth63974b22011-12-13 01:56:10 +0000616 // These just redirect to CTTZ and CTLZ on ARM.
617 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
618 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
619
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000620 // Only ARMv6 has BSWAP.
621 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000622 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000623
Evan Chenga8e29892007-01-19 07:51:42 +0000624 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000625 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000626 // v7M has a hardware divider
627 setOperationAction(ISD::SDIV, MVT::i32, Expand);
628 setOperationAction(ISD::UDIV, MVT::i32, Expand);
629 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000630 setOperationAction(ISD::SREM, MVT::i32, Expand);
631 setOperationAction(ISD::UREM, MVT::i32, Expand);
632 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
633 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000634
Owen Anderson825b72b2009-08-11 20:47:22 +0000635 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
636 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
637 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
638 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000639 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000640
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000641 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000642
Evan Chenga8e29892007-01-19 07:51:42 +0000643 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000644 setOperationAction(ISD::VASTART, MVT::Other, Custom);
645 setOperationAction(ISD::VAARG, MVT::Other, Expand);
646 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
647 setOperationAction(ISD::VAEND, MVT::Other, Expand);
648 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
649 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000650 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000651 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
652 setExceptionPointerRegister(ARM::R0);
653 setExceptionSelectorRegister(ARM::R1);
654
Evan Cheng3a1588a2010-04-15 22:20:34 +0000655 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000656 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
657 // the default expansion.
Eli Friedman4db5aca2011-08-29 18:23:02 +0000658 // FIXME: This should be checking for v6k, not just v6.
Evan Cheng11db0682010-08-11 06:22:01 +0000659 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000660 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000661 // membarrier needs custom lowering; the rest are legal and handled
662 // normally.
663 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000664 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Eli Friedman2bdffe42011-08-31 00:31:29 +0000665 // Custom lowering for 64-bit ops
666 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
667 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
668 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
669 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
670 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
671 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Eli Friedman4d3f3292011-08-31 17:52:22 +0000672 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Eli Friedman26689ac2011-08-03 21:06:02 +0000673 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
674 setInsertFencesForAtomic(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000675 } else {
676 // Set them all for expansion, which will force libcalls.
677 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000678 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000679 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000680 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000681 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000682 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000683 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000684 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000685 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000686 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000687 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000688 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000689 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000690 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedman7cc15662011-09-15 22:18:49 +0000691 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
692 // Unordered/Monotonic case.
693 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
694 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000695 // Since the libcalls include locking, fold in the fences
696 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000697 }
Evan Chenga8e29892007-01-19 07:51:42 +0000698
Evan Cheng416941d2010-11-04 05:19:35 +0000699 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000700
Eli Friedmana2c6f452010-06-26 04:36:50 +0000701 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
702 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000703 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
704 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000705 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000706 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000707
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000708 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
709 !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000710 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
711 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000712 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000713 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
714 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000715
716 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000717 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000718 if (Subtarget->isTargetDarwin()) {
719 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
720 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000721 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000722 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000723
Owen Anderson825b72b2009-08-11 20:47:22 +0000724 setOperationAction(ISD::SETCC, MVT::i32, Expand);
725 setOperationAction(ISD::SETCC, MVT::f32, Expand);
726 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000727 setOperationAction(ISD::SELECT, MVT::i32, Custom);
728 setOperationAction(ISD::SELECT, MVT::f32, Custom);
729 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000730 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
731 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
732 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000733
Owen Anderson825b72b2009-08-11 20:47:22 +0000734 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
735 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
736 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
737 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
738 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000739
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000740 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000741 setOperationAction(ISD::FSIN, MVT::f64, Expand);
742 setOperationAction(ISD::FSIN, MVT::f32, Expand);
743 setOperationAction(ISD::FCOS, MVT::f32, Expand);
744 setOperationAction(ISD::FCOS, MVT::f64, Expand);
745 setOperationAction(ISD::FREM, MVT::f64, Expand);
746 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000747 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
748 !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000749 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
750 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000751 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000752 setOperationAction(ISD::FPOW, MVT::f64, Expand);
753 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000754
Cameron Zwarich33390842011-07-08 21:39:21 +0000755 setOperationAction(ISD::FMA, MVT::f64, Expand);
756 setOperationAction(ISD::FMA, MVT::f32, Expand);
757
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000758 // Various VFP goodness
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000759 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000760 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
761 if (Subtarget->hasVFP2()) {
762 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
763 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
764 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
765 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
766 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000767 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000768 if (!Subtarget->hasFP16()) {
769 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
770 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000771 }
Evan Cheng110cf482008-04-01 01:50:16 +0000772 }
Evan Chenga8e29892007-01-19 07:51:42 +0000773
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000774 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000775 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000776 setTargetDAGCombine(ISD::ADD);
777 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000778 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000779
Owen Anderson080c0922010-11-05 19:27:46 +0000780 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON())
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000781 setTargetDAGCombine(ISD::OR);
Owen Anderson080c0922010-11-05 19:27:46 +0000782 if (Subtarget->hasNEON())
783 setTargetDAGCombine(ISD::AND);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000784
Evan Chenga8e29892007-01-19 07:51:42 +0000785 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000786
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000787 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
788 !Subtarget->hasVFP2())
Evan Chengf7d87ee2010-05-21 00:43:17 +0000789 setSchedulingPreference(Sched::RegPressure);
790 else
791 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000792
Evan Cheng05219282011-01-06 06:52:41 +0000793 //// temporary - rewrite interface to use type
794 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Lang Hames75757f92011-10-26 20:56:52 +0000795 maxStoresPerMemset = 16;
796 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengf6799392010-06-26 01:52:05 +0000797
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000798 // On ARM arguments smaller than 4 bytes are extended, so all arguments
799 // are at least 4 bytes aligned.
800 setMinStackArgumentAlignment(4);
801
Evan Chengfff606d2010-09-24 19:07:23 +0000802 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000803
804 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000805}
806
Andrew Trick32cec0a2011-01-19 02:35:27 +0000807// FIXME: It might make sense to define the representative register class as the
808// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
809// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
810// SPR's representative would be DPR_VFP2. This should work well if register
811// pressure tracking were modified such that a register use would increment the
812// pressure of the register class's representative and all of it's super
813// classes' representatives transitively. We have not implemented this because
814// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000815// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000816// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000817std::pair<const TargetRegisterClass*, uint8_t>
818ARMTargetLowering::findRepresentativeClass(EVT VT) const{
819 const TargetRegisterClass *RRC = 0;
820 uint8_t Cost = 1;
821 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000822 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000823 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000824 // Use DPR as representative register class for all floating point
825 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
826 // the cost is 1 for both f32 and f64.
827 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000828 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000829 RRC = ARM::DPRRegisterClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000830 // When NEON is used for SP, only half of the register file is available
831 // because operations that define both SP and DP results will be constrained
832 // to the VFP2 class (D0-D15). We currently model this constraint prior to
833 // coalescing by double-counting the SP regs. See the FIXME above.
834 if (Subtarget->useNEONForSinglePrecisionFP())
835 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000836 break;
837 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
838 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000839 RRC = ARM::DPRRegisterClass;
840 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000841 break;
842 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000843 RRC = ARM::DPRRegisterClass;
844 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000845 break;
846 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000847 RRC = ARM::DPRRegisterClass;
848 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000849 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000850 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000851 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000852}
853
Evan Chenga8e29892007-01-19 07:51:42 +0000854const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
855 switch (Opcode) {
856 default: return 0;
857 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000858 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000859 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000860 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
861 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000862 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000863 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
864 case ARMISD::tCALL: return "ARMISD::tCALL";
865 case ARMISD::BRCOND: return "ARMISD::BRCOND";
866 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000867 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000868 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
869 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
870 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000871 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000872 case ARMISD::CMPFP: return "ARMISD::CMPFP";
873 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000874 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000875 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
876 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000877
Jim Grosbach3482c802010-01-18 19:58:49 +0000878 case ARMISD::RBIT: return "ARMISD::RBIT";
879
Bob Wilson76a312b2010-03-19 22:51:32 +0000880 case ARMISD::FTOSI: return "ARMISD::FTOSI";
881 case ARMISD::FTOUI: return "ARMISD::FTOUI";
882 case ARMISD::SITOF: return "ARMISD::SITOF";
883 case ARMISD::UITOF: return "ARMISD::UITOF";
884
Evan Chenga8e29892007-01-19 07:51:42 +0000885 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
886 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
887 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000888
Evan Cheng342e3162011-08-30 01:34:54 +0000889 case ARMISD::ADDC: return "ARMISD::ADDC";
890 case ARMISD::ADDE: return "ARMISD::ADDE";
891 case ARMISD::SUBC: return "ARMISD::SUBC";
892 case ARMISD::SUBE: return "ARMISD::SUBE";
893
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000894 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
895 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000896
Evan Chengc5942082009-10-28 06:55:03 +0000897 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
898 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
899
Dale Johannesen51e28e62010-06-03 21:09:53 +0000900 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000901
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000902 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000903
Evan Cheng86198642009-08-07 00:34:42 +0000904 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
905
Jim Grosbach3728e962009-12-10 00:11:09 +0000906 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000907 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000908
Evan Chengdfed19f2010-11-03 06:34:55 +0000909 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
910
Bob Wilson5bafff32009-06-22 23:27:02 +0000911 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000912 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000913 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000914 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
915 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000916 case ARMISD::VCGEU: return "ARMISD::VCGEU";
917 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000918 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
919 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000920 case ARMISD::VCGTU: return "ARMISD::VCGTU";
921 case ARMISD::VTST: return "ARMISD::VTST";
922
923 case ARMISD::VSHL: return "ARMISD::VSHL";
924 case ARMISD::VSHRs: return "ARMISD::VSHRs";
925 case ARMISD::VSHRu: return "ARMISD::VSHRu";
926 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
927 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
928 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
929 case ARMISD::VSHRN: return "ARMISD::VSHRN";
930 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
931 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
932 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
933 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
934 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
935 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
936 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
937 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
938 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
939 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
940 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
941 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
942 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
943 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000944 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000945 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Chengeaa192a2011-11-15 02:12:34 +0000946 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000947 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000948 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000949 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000950 case ARMISD::VREV64: return "ARMISD::VREV64";
951 case ARMISD::VREV32: return "ARMISD::VREV32";
952 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000953 case ARMISD::VZIP: return "ARMISD::VZIP";
954 case ARMISD::VUZP: return "ARMISD::VUZP";
955 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000956 case ARMISD::VTBL1: return "ARMISD::VTBL1";
957 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000958 case ARMISD::VMULLs: return "ARMISD::VMULLs";
959 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000960 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000961 case ARMISD::FMAX: return "ARMISD::FMAX";
962 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000963 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000964 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
965 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000966 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000967 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
968 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
969 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +0000970 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
971 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
972 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
973 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
974 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
975 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
976 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
977 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
978 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
979 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
980 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
981 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
982 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
983 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
984 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
985 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
986 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +0000987 }
988}
989
Duncan Sands28b77e92011-09-06 19:07:46 +0000990EVT ARMTargetLowering::getSetCCResultType(EVT VT) const {
991 if (!VT.isVector()) return getPointerTy();
992 return VT.changeVectorElementTypeToInteger();
993}
994
Evan Cheng06b666c2010-05-15 02:18:07 +0000995/// getRegClassFor - Return the register class that should be used for the
996/// specified value type.
997TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
998 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
999 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1000 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +00001001 if (Subtarget->hasNEON()) {
1002 if (VT == MVT::v4i64)
1003 return ARM::QQPRRegisterClass;
1004 else if (VT == MVT::v8i64)
1005 return ARM::QQQQPRRegisterClass;
1006 }
Evan Cheng06b666c2010-05-15 02:18:07 +00001007 return TargetLowering::getRegClassFor(VT);
1008}
1009
Eric Christopherab695882010-07-21 22:26:11 +00001010// Create a fast isel object.
1011FastISel *
1012ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
1013 return ARM::createFastISel(funcInfo);
1014}
1015
Anton Korobeynikovcec36f42010-07-24 21:52:08 +00001016/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1017/// be used for loads / stores from the global.
1018unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1019 return (Subtarget->isThumb1Only() ? 127 : 4095);
1020}
1021
Evan Cheng1cc39842010-05-20 23:26:43 +00001022Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +00001023 unsigned NumVals = N->getNumValues();
1024 if (!NumVals)
1025 return Sched::RegPressure;
1026
1027 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +00001028 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001029 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +00001030 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +00001031 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman692c1d82011-10-24 17:55:11 +00001032 return Sched::ILP;
Evan Cheng1cc39842010-05-20 23:26:43 +00001033 }
Evan Chengc10f5432010-05-28 23:25:23 +00001034
1035 if (!N->isMachineOpcode())
1036 return Sched::RegPressure;
1037
1038 // Load are scheduled for latency even if there instruction itinerary
1039 // is not available.
1040 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +00001041 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +00001042
Evan Chenge837dea2011-06-28 19:10:37 +00001043 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +00001044 return Sched::RegPressure;
1045 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +00001046 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman692c1d82011-10-24 17:55:11 +00001047 return Sched::ILP;
Evan Chengc10f5432010-05-28 23:25:23 +00001048
Evan Cheng1cc39842010-05-20 23:26:43 +00001049 return Sched::RegPressure;
1050}
1051
Evan Chenga8e29892007-01-19 07:51:42 +00001052//===----------------------------------------------------------------------===//
1053// Lowering Code
1054//===----------------------------------------------------------------------===//
1055
Evan Chenga8e29892007-01-19 07:51:42 +00001056/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1057static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1058 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001059 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +00001060 case ISD::SETNE: return ARMCC::NE;
1061 case ISD::SETEQ: return ARMCC::EQ;
1062 case ISD::SETGT: return ARMCC::GT;
1063 case ISD::SETGE: return ARMCC::GE;
1064 case ISD::SETLT: return ARMCC::LT;
1065 case ISD::SETLE: return ARMCC::LE;
1066 case ISD::SETUGT: return ARMCC::HI;
1067 case ISD::SETUGE: return ARMCC::HS;
1068 case ISD::SETULT: return ARMCC::LO;
1069 case ISD::SETULE: return ARMCC::LS;
1070 }
1071}
1072
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001073/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1074static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001075 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001076 CondCode2 = ARMCC::AL;
1077 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001078 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001079 case ISD::SETEQ:
1080 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1081 case ISD::SETGT:
1082 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1083 case ISD::SETGE:
1084 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1085 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001086 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001087 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1088 case ISD::SETO: CondCode = ARMCC::VC; break;
1089 case ISD::SETUO: CondCode = ARMCC::VS; break;
1090 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1091 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1092 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1093 case ISD::SETLT:
1094 case ISD::SETULT: CondCode = ARMCC::LT; break;
1095 case ISD::SETLE:
1096 case ISD::SETULE: CondCode = ARMCC::LE; break;
1097 case ISD::SETNE:
1098 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1099 }
Evan Chenga8e29892007-01-19 07:51:42 +00001100}
1101
Bob Wilson1f595bb2009-04-17 19:07:39 +00001102//===----------------------------------------------------------------------===//
1103// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001104//===----------------------------------------------------------------------===//
1105
1106#include "ARMGenCallingConv.inc"
1107
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001108/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1109/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001110CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001111 bool Return,
1112 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001113 switch (CC) {
1114 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001115 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001116 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001117 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001118 if (!Subtarget->isAAPCS_ABI())
1119 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1120 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1121 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1122 }
1123 // Fallthrough
1124 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001125 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001126 if (!Subtarget->isAAPCS_ABI())
1127 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1128 else if (Subtarget->hasVFP2() &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001129 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1130 !isVarArg)
Evan Cheng76f920d2010-10-22 18:23:05 +00001131 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1132 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1133 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001134 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikovf349cb82012-01-29 09:06:09 +00001135 if (!isVarArg)
1136 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1137 // Fallthrough
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001138 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001139 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001140 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001141 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001142 }
1143}
1144
Dan Gohman98ca4f22009-08-05 01:29:28 +00001145/// LowerCallResult - Lower the result values of a call into the
1146/// appropriate copies out of appropriate physical registers.
1147SDValue
1148ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001149 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001150 const SmallVectorImpl<ISD::InputArg> &Ins,
1151 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001152 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001153
Bob Wilson1f595bb2009-04-17 19:07:39 +00001154 // Assign locations to each value returned by this call.
1155 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001156 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1157 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001158 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001159 CCAssignFnForNode(CallConv, /* Return*/ true,
1160 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001161
1162 // Copy all of the result registers out of their specified physreg.
1163 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1164 CCValAssign VA = RVLocs[i];
1165
Bob Wilson80915242009-04-25 00:33:20 +00001166 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001167 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001168 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001169 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001170 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001171 Chain = Lo.getValue(1);
1172 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001173 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001174 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001175 InFlag);
1176 Chain = Hi.getValue(1);
1177 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001178 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001179
Owen Anderson825b72b2009-08-11 20:47:22 +00001180 if (VA.getLocVT() == MVT::v2f64) {
1181 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1182 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1183 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001184
1185 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001186 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001187 Chain = Lo.getValue(1);
1188 InFlag = Lo.getValue(2);
1189 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001190 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001191 Chain = Hi.getValue(1);
1192 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001193 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001194 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1195 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001196 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001197 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001198 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1199 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001200 Chain = Val.getValue(1);
1201 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001202 }
Bob Wilson80915242009-04-25 00:33:20 +00001203
1204 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001205 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001206 case CCValAssign::Full: break;
1207 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001208 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001209 break;
1210 }
1211
Dan Gohman98ca4f22009-08-05 01:29:28 +00001212 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001213 }
1214
Dan Gohman98ca4f22009-08-05 01:29:28 +00001215 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001216}
1217
Bob Wilsondee46d72009-04-17 20:35:10 +00001218/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001219SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001220ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1221 SDValue StackPtr, SDValue Arg,
1222 DebugLoc dl, SelectionDAG &DAG,
1223 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001224 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001225 unsigned LocMemOffset = VA.getLocMemOffset();
1226 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1227 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001228 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001229 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001230 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001231}
1232
Dan Gohman98ca4f22009-08-05 01:29:28 +00001233void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001234 SDValue Chain, SDValue &Arg,
1235 RegsToPassVector &RegsToPass,
1236 CCValAssign &VA, CCValAssign &NextVA,
1237 SDValue &StackPtr,
1238 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001239 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001240
Jim Grosbache5165492009-11-09 00:11:35 +00001241 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001242 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001243 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1244
1245 if (NextVA.isRegLoc())
1246 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1247 else {
1248 assert(NextVA.isMemLoc());
1249 if (StackPtr.getNode() == 0)
1250 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1251
Dan Gohman98ca4f22009-08-05 01:29:28 +00001252 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1253 dl, DAG, NextVA,
1254 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001255 }
1256}
1257
Dan Gohman98ca4f22009-08-05 01:29:28 +00001258/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001259/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1260/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001261SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001262ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001263 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001264 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001265 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001266 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001267 const SmallVectorImpl<ISD::InputArg> &Ins,
1268 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001269 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001270 MachineFunction &MF = DAG.getMachineFunction();
1271 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1272 bool IsSibCall = false;
Bob Wilson6d2f9ce2011-10-07 17:17:49 +00001273 // Disable tail calls if they're not supported.
1274 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Bob Wilson703af3a2010-08-13 22:43:33 +00001275 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001276 if (isTailCall) {
1277 // Check if it's really possible to do a tail call.
1278 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1279 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001280 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001281 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1282 // detected sibcalls.
1283 if (isTailCall) {
1284 ++NumTailCalls;
1285 IsSibCall = true;
1286 }
1287 }
Evan Chenga8e29892007-01-19 07:51:42 +00001288
Bob Wilson1f595bb2009-04-17 19:07:39 +00001289 // Analyze operands of the call, assigning locations to each operand.
1290 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001291 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1292 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001293 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001294 CCAssignFnForNode(CallConv, /* Return*/ false,
1295 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001296
Bob Wilson1f595bb2009-04-17 19:07:39 +00001297 // Get a count of how many bytes are to be pushed on the stack.
1298 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001299
Dale Johannesen51e28e62010-06-03 21:09:53 +00001300 // For tail calls, memory operands are available in our caller's stack.
1301 if (IsSibCall)
1302 NumBytes = 0;
1303
Evan Chenga8e29892007-01-19 07:51:42 +00001304 // Adjust the stack pointer for the new arguments...
1305 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001306 if (!IsSibCall)
1307 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001308
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001309 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001310
Bob Wilson5bafff32009-06-22 23:27:02 +00001311 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001312 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001313
Bob Wilson1f595bb2009-04-17 19:07:39 +00001314 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001315 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001316 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1317 i != e;
1318 ++i, ++realArgIdx) {
1319 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001320 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001321 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001322 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001323
Bob Wilson1f595bb2009-04-17 19:07:39 +00001324 // Promote the value if needed.
1325 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001326 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001327 case CCValAssign::Full: break;
1328 case CCValAssign::SExt:
1329 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1330 break;
1331 case CCValAssign::ZExt:
1332 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1333 break;
1334 case CCValAssign::AExt:
1335 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1336 break;
1337 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001338 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001339 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001340 }
1341
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001342 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001343 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001344 if (VA.getLocVT() == MVT::v2f64) {
1345 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1346 DAG.getConstant(0, MVT::i32));
1347 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1348 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001349
Dan Gohman98ca4f22009-08-05 01:29:28 +00001350 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001351 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1352
1353 VA = ArgLocs[++i]; // skip ahead to next loc
1354 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001355 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001356 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1357 } else {
1358 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001359
Dan Gohman98ca4f22009-08-05 01:29:28 +00001360 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1361 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001362 }
1363 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001364 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001365 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001366 }
1367 } else if (VA.isRegLoc()) {
1368 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001369 } else if (isByVal) {
1370 assert(VA.isMemLoc());
1371 unsigned offset = 0;
1372
1373 // True if this byval aggregate will be split between registers
1374 // and memory.
1375 if (CCInfo.isFirstByValRegValid()) {
1376 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1377 unsigned int i, j;
1378 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1379 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1380 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1381 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1382 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001383 false, false, false, 0);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001384 MemOpChains.push_back(Load.getValue(1));
1385 RegsToPass.push_back(std::make_pair(j, Load));
1386 }
1387 offset = ARM::R4 - CCInfo.getFirstByValReg();
1388 CCInfo.clearFirstByValReg();
1389 }
1390
1391 unsigned LocMemOffset = VA.getLocMemOffset();
1392 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1393 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1394 StkPtrOff);
1395 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1396 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1397 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1398 MVT::i32);
1399 MemOpChains.push_back(DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode,
1400 Flags.getByValAlign(),
1401 /*isVolatile=*/false,
Dan Gohman65fd6562011-11-03 21:49:52 +00001402 /*AlwaysInline=*/false,
Stuart Hastingsc7315872011-04-20 16:47:52 +00001403 MachinePointerInfo(0),
1404 MachinePointerInfo(0)));
1405
1406 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001407 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001408
Dan Gohman98ca4f22009-08-05 01:29:28 +00001409 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1410 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001411 }
Evan Chenga8e29892007-01-19 07:51:42 +00001412 }
1413
1414 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001415 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001416 &MemOpChains[0], MemOpChains.size());
1417
1418 // Build a sequence of copy-to-reg nodes chained together with token chain
1419 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001420 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001421 // Tail call byval lowering might overwrite argument registers so in case of
1422 // tail call optimization the copies to registers are lowered later.
1423 if (!isTailCall)
1424 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1425 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1426 RegsToPass[i].second, InFlag);
1427 InFlag = Chain.getValue(1);
1428 }
Evan Chenga8e29892007-01-19 07:51:42 +00001429
Dale Johannesen51e28e62010-06-03 21:09:53 +00001430 // For tail calls lower the arguments to the 'real' stack slot.
1431 if (isTailCall) {
1432 // Force all the incoming stack arguments to be loaded from the stack
1433 // before any new outgoing arguments are stored to the stack, because the
1434 // outgoing stack slots may alias the incoming argument stack slots, and
1435 // the alias isn't otherwise explicit. This is slightly more conservative
1436 // than necessary, because it means that each store effectively depends
1437 // on every argument instead of just those arguments it would clobber.
1438
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001439 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001440 InFlag = SDValue();
1441 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1442 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1443 RegsToPass[i].second, InFlag);
1444 InFlag = Chain.getValue(1);
1445 }
1446 InFlag =SDValue();
1447 }
1448
Bill Wendling056292f2008-09-16 21:48:12 +00001449 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1450 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1451 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001452 bool isDirect = false;
1453 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001454 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001455 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001456
1457 if (EnableARMLongCalls) {
1458 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1459 && "long-calls with non-static relocation model!");
1460 // Handle a global address or an external symbol. If it's not one of
1461 // those, the target's already in a register, so we don't need to do
1462 // anything extra.
1463 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001464 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001465 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001466 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001467 ARMConstantPoolValue *CPV =
1468 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1469
Jim Grosbache7b52522010-04-14 22:28:31 +00001470 // Get the address of the callee into a register
1471 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1472 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1473 Callee = DAG.getLoad(getPointerTy(), dl,
1474 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001475 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001476 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001477 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1478 const char *Sym = S->getSymbol();
1479
1480 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001481 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001482 ARMConstantPoolValue *CPV =
1483 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1484 ARMPCLabelIndex, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001485 // Get the address of the callee into a register
1486 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1487 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1488 Callee = DAG.getLoad(getPointerTy(), dl,
1489 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001490 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001491 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001492 }
1493 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001494 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001495 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001496 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001497 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001498 getTargetMachine().getRelocationModel() != Reloc::Static;
1499 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001500 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001501 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001502 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001503 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001504 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001505 ARMConstantPoolValue *CPV =
1506 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001507 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001508 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001509 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001510 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001511 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001512 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001513 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001514 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001515 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001516 } else {
1517 // On ELF targets for PIC code, direct calls should go through the PLT
1518 unsigned OpFlags = 0;
1519 if (Subtarget->isTargetELF() &&
1520 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1521 OpFlags = ARMII::MO_PLT;
1522 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1523 }
Bill Wendling056292f2008-09-16 21:48:12 +00001524 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001525 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001526 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001527 getTargetMachine().getRelocationModel() != Reloc::Static;
1528 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001529 // tBX takes a register source operand.
1530 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001531 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001532 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001533 ARMConstantPoolValue *CPV =
1534 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1535 ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001536 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001537 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001538 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001539 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001540 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001541 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001542 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001543 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001544 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001545 } else {
1546 unsigned OpFlags = 0;
1547 // On ELF targets for PIC code, direct calls should go through the PLT
1548 if (Subtarget->isTargetELF() &&
1549 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1550 OpFlags = ARMII::MO_PLT;
1551 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1552 }
Evan Chenga8e29892007-01-19 07:51:42 +00001553 }
1554
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001555 // FIXME: handle tail calls differently.
1556 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001557 if (Subtarget->isThumb()) {
1558 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001559 CallOpc = ARMISD::CALL_NOLINK;
1560 else
1561 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1562 } else {
1563 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001564 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1565 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001566 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001567
Dan Gohman475871a2008-07-27 21:46:04 +00001568 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001569 Ops.push_back(Chain);
1570 Ops.push_back(Callee);
1571
1572 // Add argument registers to the end of the list so that they are known live
1573 // into the call.
1574 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1575 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1576 RegsToPass[i].second.getValueType()));
1577
Gabor Greifba36cb52008-08-28 21:40:38 +00001578 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001579 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001580
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001581 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001582 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001583 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001584
Duncan Sands4bdcb612008-07-02 17:40:58 +00001585 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001586 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001587 InFlag = Chain.getValue(1);
1588
Chris Lattnere563bbc2008-10-11 22:08:30 +00001589 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1590 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001591 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001592 InFlag = Chain.getValue(1);
1593
Bob Wilson1f595bb2009-04-17 19:07:39 +00001594 // Handle result values, copying them out of physregs into vregs that we
1595 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001596 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1597 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001598}
1599
Stuart Hastingsf222e592011-02-28 17:17:53 +00001600/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001601/// on the stack. Remember the next parameter register to allocate,
1602/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001603/// this.
1604void
Stuart Hastingsc7315872011-04-20 16:47:52 +00001605llvm::ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
1606 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1607 assert((State->getCallOrPrologue() == Prologue ||
1608 State->getCallOrPrologue() == Call) &&
1609 "unhandled ParmContext");
1610 if ((!State->isFirstByValRegValid()) &&
1611 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1612 State->setFirstByValReg(reg);
1613 // At a call site, a byval parameter that is split between
1614 // registers and memory needs its size truncated here. In a
1615 // function prologue, such byval parameters are reassembled in
1616 // memory, and are not truncated.
1617 if (State->getCallOrPrologue() == Call) {
1618 unsigned excess = 4 * (ARM::R4 - reg);
1619 assert(size >= excess && "expected larger existing stack allocation");
1620 size -= excess;
1621 }
1622 }
1623 // Confiscate any remaining parameter registers to preclude their
1624 // assignment to subsequent parameters.
1625 while (State->AllocateReg(GPRArgRegs, 4))
1626 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001627}
1628
Dale Johannesen51e28e62010-06-03 21:09:53 +00001629/// MatchingStackOffset - Return true if the given stack call argument is
1630/// already available in the same position (relatively) of the caller's
1631/// incoming argument stack.
1632static
1633bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1634 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1635 const ARMInstrInfo *TII) {
1636 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1637 int FI = INT_MAX;
1638 if (Arg.getOpcode() == ISD::CopyFromReg) {
1639 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001640 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001641 return false;
1642 MachineInstr *Def = MRI->getVRegDef(VR);
1643 if (!Def)
1644 return false;
1645 if (!Flags.isByVal()) {
1646 if (!TII->isLoadFromStackSlot(Def, FI))
1647 return false;
1648 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001649 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001650 }
1651 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1652 if (Flags.isByVal())
1653 // ByVal argument is passed in as a pointer but it's now being
1654 // dereferenced. e.g.
1655 // define @foo(%struct.X* %A) {
1656 // tail call @bar(%struct.X* byval %A)
1657 // }
1658 return false;
1659 SDValue Ptr = Ld->getBasePtr();
1660 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1661 if (!FINode)
1662 return false;
1663 FI = FINode->getIndex();
1664 } else
1665 return false;
1666
1667 assert(FI != INT_MAX);
1668 if (!MFI->isFixedObjectIndex(FI))
1669 return false;
1670 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1671}
1672
1673/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1674/// for tail call optimization. Targets which want to do tail call
1675/// optimization should implement this function.
1676bool
1677ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1678 CallingConv::ID CalleeCC,
1679 bool isVarArg,
1680 bool isCalleeStructRet,
1681 bool isCallerStructRet,
1682 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001683 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001684 const SmallVectorImpl<ISD::InputArg> &Ins,
1685 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001686 const Function *CallerF = DAG.getMachineFunction().getFunction();
1687 CallingConv::ID CallerCC = CallerF->getCallingConv();
1688 bool CCMatch = CallerCC == CalleeCC;
1689
1690 // Look for obvious safe cases to perform tail call optimization that do not
1691 // require ABI changes. This is what gcc calls sibcall.
1692
Jim Grosbach7616b642010-06-16 23:45:49 +00001693 // Do not sibcall optimize vararg calls unless the call site is not passing
1694 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001695 if (isVarArg && !Outs.empty())
1696 return false;
1697
1698 // Also avoid sibcall optimization if either caller or callee uses struct
1699 // return semantics.
1700 if (isCalleeStructRet || isCallerStructRet)
1701 return false;
1702
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001703 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001704 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1705 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1706 // support in the assembler and linker to be used. This would need to be
1707 // fixed to fully support tail calls in Thumb1.
1708 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001709 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1710 // LR. This means if we need to reload LR, it takes an extra instructions,
1711 // which outweighs the value of the tail call; but here we don't know yet
1712 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001713 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001714 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001715
1716 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1717 // but we need to make sure there are enough registers; the only valid
1718 // registers are the 4 used for parameters. We don't currently do this
1719 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001720 if (Subtarget->isThumb1Only())
1721 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001722
Dale Johannesen51e28e62010-06-03 21:09:53 +00001723 // If the calling conventions do not match, then we'd better make sure the
1724 // results are returned in the same way as what the caller expects.
1725 if (!CCMatch) {
1726 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001727 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1728 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001729 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1730
1731 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001732 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1733 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001734 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1735
1736 if (RVLocs1.size() != RVLocs2.size())
1737 return false;
1738 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1739 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1740 return false;
1741 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1742 return false;
1743 if (RVLocs1[i].isRegLoc()) {
1744 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1745 return false;
1746 } else {
1747 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1748 return false;
1749 }
1750 }
1751 }
1752
1753 // If the callee takes no arguments then go on to check the results of the
1754 // call.
1755 if (!Outs.empty()) {
1756 // Check if stack adjustment is needed. For now, do not do this if any
1757 // argument is passed on the stack.
1758 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001759 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1760 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001761 CCInfo.AnalyzeCallOperands(Outs,
1762 CCAssignFnForNode(CalleeCC, false, isVarArg));
1763 if (CCInfo.getNextStackOffset()) {
1764 MachineFunction &MF = DAG.getMachineFunction();
1765
1766 // Check if the arguments are already laid out in the right way as
1767 // the caller's fixed stack objects.
1768 MachineFrameInfo *MFI = MF.getFrameInfo();
1769 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1770 const ARMInstrInfo *TII =
1771 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001772 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1773 i != e;
1774 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001775 CCValAssign &VA = ArgLocs[i];
1776 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001777 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001778 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001779 if (VA.getLocInfo() == CCValAssign::Indirect)
1780 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001781 if (VA.needsCustom()) {
1782 // f64 and vector types are split into multiple registers or
1783 // register/stack-slot combinations. The types will not match
1784 // the registers; give up on memory f64 refs until we figure
1785 // out what to do about this.
1786 if (!VA.isRegLoc())
1787 return false;
1788 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001789 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001790 if (RegVT == MVT::v2f64) {
1791 if (!ArgLocs[++i].isRegLoc())
1792 return false;
1793 if (!ArgLocs[++i].isRegLoc())
1794 return false;
1795 }
1796 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001797 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1798 MFI, MRI, TII))
1799 return false;
1800 }
1801 }
1802 }
1803 }
1804
1805 return true;
1806}
1807
Dan Gohman98ca4f22009-08-05 01:29:28 +00001808SDValue
1809ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001810 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001811 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001812 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001813 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001814
Bob Wilsondee46d72009-04-17 20:35:10 +00001815 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001816 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001817
Bob Wilsondee46d72009-04-17 20:35:10 +00001818 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001819 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1820 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001821
Dan Gohman98ca4f22009-08-05 01:29:28 +00001822 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001823 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1824 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001825
1826 // If this is the first return lowered for this function, add
1827 // the regs to the liveout set for the function.
1828 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1829 for (unsigned i = 0; i != RVLocs.size(); ++i)
1830 if (RVLocs[i].isRegLoc())
1831 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001832 }
1833
Bob Wilson1f595bb2009-04-17 19:07:39 +00001834 SDValue Flag;
1835
1836 // Copy the result values into the output registers.
1837 for (unsigned i = 0, realRVLocIdx = 0;
1838 i != RVLocs.size();
1839 ++i, ++realRVLocIdx) {
1840 CCValAssign &VA = RVLocs[i];
1841 assert(VA.isRegLoc() && "Can only return in registers!");
1842
Dan Gohmanc9403652010-07-07 15:54:55 +00001843 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001844
1845 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001846 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001847 case CCValAssign::Full: break;
1848 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001849 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001850 break;
1851 }
1852
Bob Wilson1f595bb2009-04-17 19:07:39 +00001853 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001854 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001855 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001856 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1857 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001858 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001859 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001860
1861 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1862 Flag = Chain.getValue(1);
1863 VA = RVLocs[++i]; // skip ahead to next loc
1864 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1865 HalfGPRs.getValue(1), Flag);
1866 Flag = Chain.getValue(1);
1867 VA = RVLocs[++i]; // skip ahead to next loc
1868
1869 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001870 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1871 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001872 }
1873 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1874 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001875 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001876 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001877 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001878 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001879 VA = RVLocs[++i]; // skip ahead to next loc
1880 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1881 Flag);
1882 } else
1883 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1884
Bob Wilsondee46d72009-04-17 20:35:10 +00001885 // Guarantee that all emitted copies are
1886 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001887 Flag = Chain.getValue(1);
1888 }
1889
1890 SDValue result;
1891 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001892 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001893 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001894 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001895
1896 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001897}
1898
Evan Cheng3d2125c2010-11-30 23:55:39 +00001899bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1900 if (N->getNumValues() != 1)
1901 return false;
1902 if (!N->hasNUsesOfValue(1, 0))
1903 return false;
1904
1905 unsigned NumCopies = 0;
Jason W Kim1de886c2012-02-10 16:07:59 +00001906 SDNode* Copies[2] = { 0, 0 };
Evan Cheng3d2125c2010-11-30 23:55:39 +00001907 SDNode *Use = *N->use_begin();
1908 if (Use->getOpcode() == ISD::CopyToReg) {
1909 Copies[NumCopies++] = Use;
1910 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1911 // f64 returned in a pair of GPRs.
1912 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1913 UI != UE; ++UI) {
1914 if (UI->getOpcode() != ISD::CopyToReg)
1915 return false;
1916 Copies[UI.getUse().getResNo()] = *UI;
1917 ++NumCopies;
1918 }
1919 } else if (Use->getOpcode() == ISD::BITCAST) {
1920 // f32 returned in a single GPR.
1921 if (!Use->hasNUsesOfValue(1, 0))
1922 return false;
1923 Use = *Use->use_begin();
1924 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1925 return false;
1926 Copies[NumCopies++] = Use;
1927 } else {
1928 return false;
1929 }
1930
1931 if (NumCopies != 1 && NumCopies != 2)
1932 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001933
1934 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001935 for (unsigned i = 0; i < NumCopies; ++i) {
1936 SDNode *Copy = Copies[i];
1937 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1938 UI != UE; ++UI) {
1939 if (UI->getOpcode() == ISD::CopyToReg) {
1940 SDNode *Use = *UI;
Jason W Kim1de886c2012-02-10 16:07:59 +00001941 if (Use == Copies[0] || ((NumCopies == 2) && (Use == Copies[1])))
Evan Cheng3d2125c2010-11-30 23:55:39 +00001942 continue;
1943 return false;
1944 }
1945 if (UI->getOpcode() != ARMISD::RET_FLAG)
1946 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001947 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001948 }
1949 }
1950
Evan Cheng1bf891a2010-12-01 22:59:46 +00001951 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001952}
1953
Evan Cheng485fafc2011-03-21 01:19:09 +00001954bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1955 if (!EnableARMTailCalls)
1956 return false;
1957
1958 if (!CI->isTailCall())
1959 return false;
1960
1961 return !Subtarget->isThumb1Only();
1962}
1963
Bob Wilsonb62d2572009-11-03 00:02:05 +00001964// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1965// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1966// one of the above mentioned nodes. It has to be wrapped because otherwise
1967// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1968// be used to form addressing mode. These wrapped nodes will be selected
1969// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001970static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001971 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001972 // FIXME there is no actual debug info here
1973 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001974 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001975 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001976 if (CP->isMachineConstantPoolEntry())
1977 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1978 CP->getAlignment());
1979 else
1980 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1981 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001982 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001983}
1984
Jim Grosbache1102ca2010-07-19 17:20:38 +00001985unsigned ARMTargetLowering::getJumpTableEncoding() const {
1986 return MachineJumpTableInfo::EK_Inline;
1987}
1988
Dan Gohmand858e902010-04-17 15:26:15 +00001989SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1990 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001991 MachineFunction &MF = DAG.getMachineFunction();
1992 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1993 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001994 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001995 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001996 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001997 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1998 SDValue CPAddr;
1999 if (RelocM == Reloc::Static) {
2000 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2001 } else {
2002 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002003 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00002004 ARMConstantPoolValue *CPV =
2005 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2006 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson907eebd2009-11-02 20:59:23 +00002007 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2008 }
2009 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2010 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002011 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002012 false, false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00002013 if (RelocM == Reloc::Static)
2014 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00002015 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00002016 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00002017}
2018
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002019// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00002020SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002021ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002022 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002023 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002024 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002025 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00002026 MachineFunction &MF = DAG.getMachineFunction();
2027 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002028 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002029 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002030 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2031 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002032 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002033 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00002034 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002035 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002036 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002037 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002038
Evan Chenge7e0d622009-11-06 22:24:13 +00002039 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002040 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002041
2042 // call __tls_get_addr.
2043 ArgListTy Args;
2044 ArgListEntry Entry;
2045 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002046 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002047 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002048 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00002049 std::pair<SDValue, SDValue> CallResult =
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002050 LowerCallTo(Chain, (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00002051 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002052 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002053 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002054 return CallResult.first;
2055}
2056
2057// Lower ISD::GlobalTLSAddress using the "initial exec" or
2058// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00002059SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002060ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002061 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002062 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002063 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002064 SDValue Offset;
2065 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002066 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002067 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002068 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002069
Chris Lattner4fb63d02009-07-15 04:12:33 +00002070 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002071 MachineFunction &MF = DAG.getMachineFunction();
2072 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002073 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002074 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002075 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2076 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002077 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2078 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2079 true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002080 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002081 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002082 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002083 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002084 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002085 Chain = Offset.getValue(1);
2086
Evan Chenge7e0d622009-11-06 22:24:13 +00002087 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002088 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002089
Evan Cheng9eda6892009-10-31 03:39:36 +00002090 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002091 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002092 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002093 } else {
2094 // local exec model
Bill Wendling5bb77992011-10-01 08:00:54 +00002095 ARMConstantPoolValue *CPV =
2096 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002097 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002098 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002099 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002100 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002101 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002102 }
2103
2104 // The address of the thread local variable is the add of the thread
2105 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002106 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002107}
2108
Dan Gohman475871a2008-07-27 21:46:04 +00002109SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002110ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002111 // TODO: implement the "local dynamic" model
2112 assert(Subtarget->isTargetELF() &&
2113 "TLS not implemented for non-ELF targets");
2114 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2115 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
2116 // otherwise use the "Local Exec" TLS Model
2117 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
2118 return LowerToTLSGeneralDynamicModel(GA, DAG);
2119 else
2120 return LowerToTLSExecModels(GA, DAG);
2121}
2122
Dan Gohman475871a2008-07-27 21:46:04 +00002123SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002124 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002125 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002126 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002127 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002128 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2129 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002130 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002131 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002132 ARMConstantPoolConstant::Create(GV,
2133 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002134 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002135 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002136 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002137 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002138 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002139 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002140 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002141 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002142 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002143 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002144 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002145 MachinePointerInfo::getGOT(),
2146 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002147 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002148 }
2149
2150 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloy015cca62011-10-26 08:53:19 +00002151 // pair. This is always cheaper.
2152 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002153 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002154 // FIXME: Once remat is capable of dealing with instructions with register
2155 // operands, expand this into two nodes.
2156 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2157 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002158 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002159 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2160 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2161 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2162 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002163 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002164 }
2165}
2166
Dan Gohman475871a2008-07-27 21:46:04 +00002167SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002168 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002169 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002170 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002171 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002172 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002173 MachineFunction &MF = DAG.getMachineFunction();
2174 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2175
Jakob Stoklund Olesen8f37a242012-01-07 20:49:15 +00002176 // FIXME: Enable this for static codegen when tool issues are fixed. Also
2177 // update ARMFastISel::ARMMaterializeGV.
Evan Chengf31151f2011-10-26 01:17:44 +00002178 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002179 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002180 // FIXME: Once remat is capable of dealing with instructions with register
2181 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002182 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002183 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2184 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2185
Evan Cheng53519f02011-01-21 18:55:51 +00002186 unsigned Wrapper = (RelocM == Reloc::PIC_)
2187 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2188 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002189 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002190 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2191 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002192 MachinePointerInfo::getGOT(),
2193 false, false, false, 0);
Evan Chengfc8475b2011-01-19 02:16:49 +00002194 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002195 }
2196
2197 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002198 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002199 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002200 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002201 } else {
2202 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002203 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2204 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002205 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue,
2206 PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002207 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002208 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002209 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002210
Evan Cheng9eda6892009-10-31 03:39:36 +00002211 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002212 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002213 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002214 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002215
2216 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002217 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002218 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002219 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002220
Evan Cheng63476a82009-09-03 07:04:02 +00002221 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002222 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002223 false, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002224
2225 return Result;
2226}
2227
Dan Gohman475871a2008-07-27 21:46:04 +00002228SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002229 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002230 assert(Subtarget->isTargetELF() &&
2231 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002232 MachineFunction &MF = DAG.getMachineFunction();
2233 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002234 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002235 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002236 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002237 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingfe31e672011-10-01 08:58:29 +00002238 ARMConstantPoolValue *CPV =
2239 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2240 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002241 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002242 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002243 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002244 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002245 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002246 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002247 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002248}
2249
Jim Grosbach0e0da732009-05-12 23:59:14 +00002250SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002251ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2252 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002253 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendlingce370cf2011-10-07 21:25:38 +00002254 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2255 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002256 Op.getOperand(1), Val);
2257}
2258
2259SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002260ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2261 DebugLoc dl = Op.getDebugLoc();
2262 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2263 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2264}
2265
2266SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002267ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002268 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002269 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002270 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002271 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002272 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002273 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002274 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002275 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2276 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002277 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002278 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002279 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002280 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002281 EVT PtrVT = getPointerTy();
2282 DebugLoc dl = Op.getDebugLoc();
2283 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2284 SDValue CPAddr;
2285 unsigned PCAdj = (RelocM != Reloc::PIC_)
2286 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002287 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002288 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2289 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002290 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002291 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002292 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002293 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002294 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002295 false, false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002296
2297 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002298 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002299 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2300 }
2301 return Result;
2302 }
Evan Cheng92e39162011-03-29 23:06:19 +00002303 case Intrinsic::arm_neon_vmulls:
2304 case Intrinsic::arm_neon_vmullu: {
2305 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2306 ? ARMISD::VMULLs : ARMISD::VMULLu;
2307 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2308 Op.getOperand(1), Op.getOperand(2));
2309 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002310 }
2311}
2312
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002313static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002314 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002315 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002316 if (!Subtarget->hasDataBarrier()) {
2317 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2318 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2319 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002320 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002321 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002322 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002323 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002324 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002325
2326 SDValue Op5 = Op.getOperand(5);
2327 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2328 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2329 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2330 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2331
2332 ARM_MB::MemBOpt DMBOpt;
2333 if (isDeviceBarrier)
2334 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2335 else
2336 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2337 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2338 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002339}
2340
Eli Friedman26689ac2011-08-03 21:06:02 +00002341
2342static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2343 const ARMSubtarget *Subtarget) {
2344 // FIXME: handle "fence singlethread" more efficiently.
2345 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002346 if (!Subtarget->hasDataBarrier()) {
2347 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2348 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2349 // here.
2350 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2351 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002352 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002353 DAG.getConstant(0, MVT::i32));
2354 }
2355
Eli Friedman26689ac2011-08-03 21:06:02 +00002356 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002357 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002358}
2359
Evan Chengdfed19f2010-11-03 06:34:55 +00002360static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2361 const ARMSubtarget *Subtarget) {
2362 // ARM pre v5TE and Thumb1 does not have preload instructions.
2363 if (!(Subtarget->isThumb2() ||
2364 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2365 // Just preserve the chain.
2366 return Op.getOperand(0);
2367
2368 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002369 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2370 if (!isRead &&
2371 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2372 // ARMv7 with MP extension has PLDW.
2373 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002374
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002375 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2376 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002377 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002378 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002379 isData = ~isData & 1;
2380 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002381
2382 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002383 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2384 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002385}
2386
Dan Gohman1e93df62010-04-17 14:41:14 +00002387static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2388 MachineFunction &MF = DAG.getMachineFunction();
2389 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2390
Evan Chenga8e29892007-01-19 07:51:42 +00002391 // vastart just stores the address of the VarArgsFrameIndex slot into the
2392 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002393 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002394 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002395 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002396 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002397 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2398 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002399}
2400
Dan Gohman475871a2008-07-27 21:46:04 +00002401SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002402ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2403 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002404 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002405 MachineFunction &MF = DAG.getMachineFunction();
2406 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2407
2408 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002409 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002410 RC = ARM::tGPRRegisterClass;
2411 else
2412 RC = ARM::GPRRegisterClass;
2413
2414 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002415 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002416 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002417
2418 SDValue ArgValue2;
2419 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002420 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002421 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002422
2423 // Create load node to retrieve arguments from the stack.
2424 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002425 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002426 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002427 false, false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002428 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002429 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002430 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002431 }
2432
Jim Grosbache5165492009-11-09 00:11:35 +00002433 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002434}
2435
Stuart Hastingsc7315872011-04-20 16:47:52 +00002436void
2437ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2438 unsigned &VARegSize, unsigned &VARegSaveSize)
2439 const {
2440 unsigned NumGPRs;
2441 if (CCInfo.isFirstByValRegValid())
2442 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2443 else {
2444 unsigned int firstUnalloced;
2445 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2446 sizeof(GPRArgRegs) /
2447 sizeof(GPRArgRegs[0]));
2448 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2449 }
2450
2451 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2452 VARegSize = NumGPRs * 4;
2453 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2454}
2455
2456// The remaining GPRs hold either the beginning of variable-argument
2457// data, or the beginning of an aggregate passed by value (usuall
2458// byval). Either way, we allocate stack slots adjacent to the data
2459// provided by our caller, and store the unallocated registers there.
2460// If this is a variadic function, the va_list pointer will begin with
2461// these values; otherwise, this reassembles a (byval) structure that
2462// was split between registers and memory.
2463void
2464ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2465 DebugLoc dl, SDValue &Chain,
2466 unsigned ArgOffset) const {
2467 MachineFunction &MF = DAG.getMachineFunction();
2468 MachineFrameInfo *MFI = MF.getFrameInfo();
2469 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2470 unsigned firstRegToSaveIndex;
2471 if (CCInfo.isFirstByValRegValid())
2472 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2473 else {
2474 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2475 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2476 }
2477
2478 unsigned VARegSize, VARegSaveSize;
2479 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2480 if (VARegSaveSize) {
2481 // If this function is vararg, store any remaining integer argument regs
2482 // to their spots on the stack so that they may be loaded by deferencing
2483 // the result of va_next.
2484 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002485 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2486 ArgOffset + VARegSaveSize
2487 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002488 false));
2489 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2490 getPointerTy());
2491
2492 SmallVector<SDValue, 4> MemOps;
2493 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
2494 TargetRegisterClass *RC;
2495 if (AFI->isThumb1OnlyFunction())
2496 RC = ARM::tGPRRegisterClass;
2497 else
2498 RC = ARM::GPRRegisterClass;
2499
2500 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2501 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2502 SDValue Store =
2503 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002504 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002505 false, false, 0);
2506 MemOps.push_back(Store);
2507 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2508 DAG.getConstant(4, getPointerTy()));
2509 }
2510 if (!MemOps.empty())
2511 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2512 &MemOps[0], MemOps.size());
2513 } else
2514 // This will point to the next argument passed via stack.
2515 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2516}
2517
Bob Wilson5bafff32009-06-22 23:27:02 +00002518SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002519ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002520 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002521 const SmallVectorImpl<ISD::InputArg>
2522 &Ins,
2523 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002524 SmallVectorImpl<SDValue> &InVals)
2525 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002526 MachineFunction &MF = DAG.getMachineFunction();
2527 MachineFrameInfo *MFI = MF.getFrameInfo();
2528
Bob Wilson1f595bb2009-04-17 19:07:39 +00002529 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2530
2531 // Assign locations to all of the incoming arguments.
2532 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002533 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2534 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002535 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002536 CCAssignFnForNode(CallConv, /* Return*/ false,
2537 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002538
2539 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002540 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002541
Stuart Hastingsf222e592011-02-28 17:17:53 +00002542 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002543 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2544 CCValAssign &VA = ArgLocs[i];
2545
Bob Wilsondee46d72009-04-17 20:35:10 +00002546 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002547 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002548 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002549
Bob Wilson1f595bb2009-04-17 19:07:39 +00002550 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002551 // f64 and vector types are split up into multiple registers or
2552 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002553 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002554 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002555 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002556 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002557 SDValue ArgValue2;
2558 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002559 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002560 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2561 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002562 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002563 false, false, false, 0);
Bob Wilson6a234f02010-04-13 22:03:22 +00002564 } else {
2565 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2566 Chain, DAG, dl);
2567 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002568 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2569 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002570 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002571 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002572 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2573 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002574 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002575
Bob Wilson5bafff32009-06-22 23:27:02 +00002576 } else {
2577 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002578
Owen Anderson825b72b2009-08-11 20:47:22 +00002579 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002580 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002581 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002582 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002583 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002584 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002585 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002586 RC = (AFI->isThumb1OnlyFunction() ?
2587 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002588 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002589 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002590
2591 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002592 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002593 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002594 }
2595
2596 // If this is an 8 or 16-bit value, it is really passed promoted
2597 // to 32 bits. Insert an assert[sz]ext to capture this, then
2598 // truncate to the right size.
2599 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002600 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002601 case CCValAssign::Full: break;
2602 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002603 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002604 break;
2605 case CCValAssign::SExt:
2606 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2607 DAG.getValueType(VA.getValVT()));
2608 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2609 break;
2610 case CCValAssign::ZExt:
2611 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2612 DAG.getValueType(VA.getValVT()));
2613 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2614 break;
2615 }
2616
Dan Gohman98ca4f22009-08-05 01:29:28 +00002617 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002618
2619 } else { // VA.isRegLoc()
2620
2621 // sanity check
2622 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002623 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002624
Stuart Hastingsf222e592011-02-28 17:17:53 +00002625 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002626
Stuart Hastingsf222e592011-02-28 17:17:53 +00002627 // Some Ins[] entries become multiple ArgLoc[] entries.
2628 // Process them only once.
2629 if (index != lastInsIndex)
2630 {
2631 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002632 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002633 // This can be changed with more analysis.
2634 // In case of tail call optimization mark all arguments mutable.
2635 // Since they could be overwritten by lowering of arguments in case of
2636 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002637 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002638 unsigned VARegSize, VARegSaveSize;
2639 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2640 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2641 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002642 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002643 int FI = MFI->CreateFixedObject(Bytes,
2644 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002645 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2646 } else {
2647 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2648 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002649
Stuart Hastingsf222e592011-02-28 17:17:53 +00002650 // Create load nodes to retrieve arguments from the stack.
2651 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2652 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2653 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002654 false, false, false, 0));
Stuart Hastingsf222e592011-02-28 17:17:53 +00002655 }
2656 lastInsIndex = index;
2657 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002658 }
2659 }
2660
2661 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002662 if (isVarArg)
2663 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002664
Dan Gohman98ca4f22009-08-05 01:29:28 +00002665 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002666}
2667
2668/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002669static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002670 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002671 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002672 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002673 // Maybe this has already been legalized into the constant pool?
2674 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002675 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002676 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002677 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002678 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002679 }
2680 }
2681 return false;
2682}
2683
Evan Chenga8e29892007-01-19 07:51:42 +00002684/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2685/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002686SDValue
2687ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002688 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002689 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002690 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002691 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002692 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002693 // Constant does not fit, try adjusting it by one?
2694 switch (CC) {
2695 default: break;
2696 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002697 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002698 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002699 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002700 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002701 }
2702 break;
2703 case ISD::SETULT:
2704 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002705 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002706 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002707 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002708 }
2709 break;
2710 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002711 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002712 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002713 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002714 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002715 }
2716 break;
2717 case ISD::SETULE:
2718 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002719 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002720 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002721 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002722 }
2723 break;
2724 }
2725 }
2726 }
2727
2728 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002729 ARMISD::NodeType CompareType;
2730 switch (CondCode) {
2731 default:
2732 CompareType = ARMISD::CMP;
2733 break;
2734 case ARMCC::EQ:
2735 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002736 // Uses only Z Flag
2737 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002738 break;
2739 }
Evan Cheng218977b2010-07-13 19:27:42 +00002740 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002741 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002742}
2743
2744/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002745SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002746ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002747 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002748 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002749 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002750 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002751 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002752 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2753 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002754}
2755
Bob Wilson79f56c92011-03-08 01:17:20 +00002756/// duplicateCmp - Glue values can have only one use, so this function
2757/// duplicates a comparison node.
2758SDValue
2759ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2760 unsigned Opc = Cmp.getOpcode();
2761 DebugLoc DL = Cmp.getDebugLoc();
2762 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2763 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2764
2765 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2766 Cmp = Cmp.getOperand(0);
2767 Opc = Cmp.getOpcode();
2768 if (Opc == ARMISD::CMPFP)
2769 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2770 else {
2771 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2772 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2773 }
2774 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2775}
2776
Bill Wendlingde2b1512010-08-11 08:43:16 +00002777SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2778 SDValue Cond = Op.getOperand(0);
2779 SDValue SelectTrue = Op.getOperand(1);
2780 SDValue SelectFalse = Op.getOperand(2);
2781 DebugLoc dl = Op.getDebugLoc();
2782
2783 // Convert:
2784 //
2785 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2786 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2787 //
2788 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2789 const ConstantSDNode *CMOVTrue =
2790 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2791 const ConstantSDNode *CMOVFalse =
2792 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2793
2794 if (CMOVTrue && CMOVFalse) {
2795 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2796 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2797
2798 SDValue True;
2799 SDValue False;
2800 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2801 True = SelectTrue;
2802 False = SelectFalse;
2803 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2804 True = SelectFalse;
2805 False = SelectTrue;
2806 }
2807
2808 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002809 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002810 SDValue ARMcc = Cond.getOperand(2);
2811 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002812 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002813 assert(True.getValueType() == VT);
2814 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002815 }
2816 }
2817 }
2818
2819 return DAG.getSelectCC(dl, Cond,
2820 DAG.getConstant(0, Cond.getValueType()),
2821 SelectTrue, SelectFalse, ISD::SETNE);
2822}
2823
Dan Gohmand858e902010-04-17 15:26:15 +00002824SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002825 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002826 SDValue LHS = Op.getOperand(0);
2827 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002828 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002829 SDValue TrueVal = Op.getOperand(2);
2830 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002831 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002832
Owen Anderson825b72b2009-08-11 20:47:22 +00002833 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002834 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002835 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002836 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Jim Grosbachb04546f2011-09-13 20:30:37 +00002837 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002838 }
2839
2840 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002841 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002842
Evan Cheng218977b2010-07-13 19:27:42 +00002843 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2844 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002845 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002846 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002847 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002848 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002849 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002850 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002851 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002852 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002853 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002854 }
2855 return Result;
2856}
2857
Evan Cheng218977b2010-07-13 19:27:42 +00002858/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2859/// to morph to an integer compare sequence.
2860static bool canChangeToInt(SDValue Op, bool &SeenZero,
2861 const ARMSubtarget *Subtarget) {
2862 SDNode *N = Op.getNode();
2863 if (!N->hasOneUse())
2864 // Otherwise it requires moving the value from fp to integer registers.
2865 return false;
2866 if (!N->getNumValues())
2867 return false;
2868 EVT VT = Op.getValueType();
2869 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2870 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2871 // vmrs are very slow, e.g. cortex-a8.
2872 return false;
2873
2874 if (isFloatingPointZero(Op)) {
2875 SeenZero = true;
2876 return true;
2877 }
2878 return ISD::isNormalLoad(N);
2879}
2880
2881static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2882 if (isFloatingPointZero(Op))
2883 return DAG.getConstant(0, MVT::i32);
2884
2885 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2886 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002887 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002888 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002889 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002890
2891 llvm_unreachable("Unknown VFP cmp argument!");
2892}
2893
2894static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2895 SDValue &RetVal1, SDValue &RetVal2) {
2896 if (isFloatingPointZero(Op)) {
2897 RetVal1 = DAG.getConstant(0, MVT::i32);
2898 RetVal2 = DAG.getConstant(0, MVT::i32);
2899 return;
2900 }
2901
2902 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2903 SDValue Ptr = Ld->getBasePtr();
2904 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2905 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002906 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002907 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002908 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002909
2910 EVT PtrType = Ptr.getValueType();
2911 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2912 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2913 PtrType, Ptr, DAG.getConstant(4, PtrType));
2914 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2915 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002916 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002917 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002918 Ld->isInvariant(), NewAlign);
Evan Cheng218977b2010-07-13 19:27:42 +00002919 return;
2920 }
2921
2922 llvm_unreachable("Unknown VFP cmp argument!");
2923}
2924
2925/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2926/// f32 and even f64 comparisons to integer ones.
2927SDValue
2928ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2929 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002930 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002931 SDValue LHS = Op.getOperand(2);
2932 SDValue RHS = Op.getOperand(3);
2933 SDValue Dest = Op.getOperand(4);
2934 DebugLoc dl = Op.getDebugLoc();
2935
2936 bool SeenZero = false;
2937 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2938 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002939 // If one of the operand is zero, it's safe to ignore the NaN case since
2940 // we only care about equality comparisons.
2941 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Bob Wilson1b772f92011-03-08 01:17:16 +00002942 // If unsafe fp math optimization is enabled and there are no other uses of
2943 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00002944 // to an integer comparison.
2945 if (CC == ISD::SETOEQ)
2946 CC = ISD::SETEQ;
2947 else if (CC == ISD::SETUNE)
2948 CC = ISD::SETNE;
2949
2950 SDValue ARMcc;
2951 if (LHS.getValueType() == MVT::f32) {
2952 LHS = bitcastf32Toi32(LHS, DAG);
2953 RHS = bitcastf32Toi32(RHS, DAG);
2954 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2955 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2956 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2957 Chain, Dest, ARMcc, CCR, Cmp);
2958 }
2959
2960 SDValue LHS1, LHS2;
2961 SDValue RHS1, RHS2;
2962 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2963 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2964 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2965 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002966 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002967 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2968 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2969 }
2970
2971 return SDValue();
2972}
2973
2974SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2975 SDValue Chain = Op.getOperand(0);
2976 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2977 SDValue LHS = Op.getOperand(2);
2978 SDValue RHS = Op.getOperand(3);
2979 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002980 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002981
Owen Anderson825b72b2009-08-11 20:47:22 +00002982 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002983 SDValue ARMcc;
2984 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002985 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002986 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002987 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002988 }
2989
Owen Anderson825b72b2009-08-11 20:47:22 +00002990 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002991
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002992 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng218977b2010-07-13 19:27:42 +00002993 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2994 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2995 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2996 if (Result.getNode())
2997 return Result;
2998 }
2999
Evan Chenga8e29892007-01-19 07:51:42 +00003000 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00003001 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003002
Evan Cheng218977b2010-07-13 19:27:42 +00003003 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3004 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003005 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003006 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003007 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00003008 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003009 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00003010 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3011 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00003012 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003013 }
3014 return Res;
3015}
3016
Dan Gohmand858e902010-04-17 15:26:15 +00003017SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00003018 SDValue Chain = Op.getOperand(0);
3019 SDValue Table = Op.getOperand(1);
3020 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003021 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003022
Owen Andersone50ed302009-08-10 22:56:29 +00003023 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00003024 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3025 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00003026 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00003027 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00003028 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00003029 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3030 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00003031 if (Subtarget->isThumb2()) {
3032 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3033 // which does another jump to the destination. This also makes it easier
3034 // to translate it to TBB / TBH later.
3035 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00003036 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00003037 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003038 }
Evan Cheng66ac5312009-07-25 00:33:29 +00003039 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00003040 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003041 MachinePointerInfo::getJumpTable(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003042 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003043 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003044 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00003045 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003046 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00003047 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003048 MachinePointerInfo::getJumpTable(),
3049 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003050 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003051 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003052 }
Evan Chenga8e29892007-01-19 07:51:42 +00003053}
3054
Eli Friedman14e809c2011-11-09 23:36:02 +00003055static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
David Blaikie810d6d32012-01-16 05:17:39 +00003056 assert(Op.getValueType().getVectorElementType() == MVT::i32
3057 && "Unexpected custom lowering");
Eli Friedman14e809c2011-11-09 23:36:02 +00003058
3059 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3060 return Op;
3061 return DAG.UnrollVectorOp(Op.getNode());
3062}
3063
Bob Wilson76a312b2010-03-19 22:51:32 +00003064static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman14e809c2011-11-09 23:36:02 +00003065 EVT VT = Op.getValueType();
3066 if (VT.isVector())
3067 return LowerVectorFP_TO_INT(Op, DAG);
3068
Bob Wilson76a312b2010-03-19 22:51:32 +00003069 DebugLoc dl = Op.getDebugLoc();
3070 unsigned Opc;
3071
3072 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003073 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003074 case ISD::FP_TO_SINT:
3075 Opc = ARMISD::FTOSI;
3076 break;
3077 case ISD::FP_TO_UINT:
3078 Opc = ARMISD::FTOUI;
3079 break;
3080 }
3081 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003082 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003083}
3084
Cameron Zwarich3007d332011-03-29 21:41:55 +00003085static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3086 EVT VT = Op.getValueType();
3087 DebugLoc dl = Op.getDebugLoc();
3088
Eli Friedman14e809c2011-11-09 23:36:02 +00003089 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3090 if (VT.getVectorElementType() == MVT::f32)
3091 return Op;
3092 return DAG.UnrollVectorOp(Op.getNode());
3093 }
3094
Duncan Sands1f6a3292011-08-12 14:54:45 +00003095 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3096 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003097 if (VT != MVT::v4f32)
3098 return DAG.UnrollVectorOp(Op.getNode());
3099
3100 unsigned CastOpc;
3101 unsigned Opc;
3102 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003103 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003104 case ISD::SINT_TO_FP:
3105 CastOpc = ISD::SIGN_EXTEND;
3106 Opc = ISD::SINT_TO_FP;
3107 break;
3108 case ISD::UINT_TO_FP:
3109 CastOpc = ISD::ZERO_EXTEND;
3110 Opc = ISD::UINT_TO_FP;
3111 break;
3112 }
3113
3114 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3115 return DAG.getNode(Opc, dl, VT, Op);
3116}
3117
Bob Wilson76a312b2010-03-19 22:51:32 +00003118static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3119 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003120 if (VT.isVector())
3121 return LowerVectorINT_TO_FP(Op, DAG);
3122
Bob Wilson76a312b2010-03-19 22:51:32 +00003123 DebugLoc dl = Op.getDebugLoc();
3124 unsigned Opc;
3125
3126 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003127 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003128 case ISD::SINT_TO_FP:
3129 Opc = ARMISD::SITOF;
3130 break;
3131 case ISD::UINT_TO_FP:
3132 Opc = ARMISD::UITOF;
3133 break;
3134 }
3135
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003136 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003137 return DAG.getNode(Opc, dl, VT, Op);
3138}
3139
Evan Cheng515fe3a2010-07-08 02:08:50 +00003140SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003141 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003142 SDValue Tmp0 = Op.getOperand(0);
3143 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003144 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003145 EVT VT = Op.getValueType();
3146 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003147 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3148 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3149 bool UseNEON = !InGPR && Subtarget->hasNEON();
3150
3151 if (UseNEON) {
3152 // Use VBSL to copy the sign bit.
3153 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3154 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3155 DAG.getTargetConstant(EncodedVal, MVT::i32));
3156 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3157 if (VT == MVT::f64)
3158 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3159 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3160 DAG.getConstant(32, MVT::i32));
3161 else /*if (VT == MVT::f32)*/
3162 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3163 if (SrcVT == MVT::f32) {
3164 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3165 if (VT == MVT::f64)
3166 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3167 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3168 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003169 } else if (VT == MVT::f32)
3170 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3171 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3172 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003173 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3174 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3175
3176 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3177 MVT::i32);
3178 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3179 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3180 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003181
Evan Chenge573fb32011-02-23 02:24:55 +00003182 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3183 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3184 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003185 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003186 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3187 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3188 DAG.getConstant(0, MVT::i32));
3189 } else {
3190 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3191 }
3192
3193 return Res;
3194 }
Evan Chengc143dd42011-02-11 02:28:55 +00003195
3196 // Bitcast operand 1 to i32.
3197 if (SrcVT == MVT::f64)
3198 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3199 &Tmp1, 1).getValue(1);
3200 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3201
Evan Chenge573fb32011-02-23 02:24:55 +00003202 // Or in the signbit with integer operations.
3203 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3204 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3205 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3206 if (VT == MVT::f32) {
3207 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3208 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3209 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3210 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003211 }
3212
Evan Chenge573fb32011-02-23 02:24:55 +00003213 // f64: Or the high part with signbit and then combine two parts.
3214 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3215 &Tmp0, 1);
3216 SDValue Lo = Tmp0.getValue(0);
3217 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3218 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3219 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003220}
3221
Evan Cheng2457f2c2010-05-22 01:47:14 +00003222SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3223 MachineFunction &MF = DAG.getMachineFunction();
3224 MachineFrameInfo *MFI = MF.getFrameInfo();
3225 MFI->setReturnAddressIsTaken(true);
3226
3227 EVT VT = Op.getValueType();
3228 DebugLoc dl = Op.getDebugLoc();
3229 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3230 if (Depth) {
3231 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3232 SDValue Offset = DAG.getConstant(4, MVT::i32);
3233 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3234 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003235 MachinePointerInfo(), false, false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003236 }
3237
3238 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003239 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003240 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3241}
3242
Dan Gohmand858e902010-04-17 15:26:15 +00003243SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003244 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3245 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003246
Owen Andersone50ed302009-08-10 22:56:29 +00003247 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003248 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3249 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003250 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003251 ? ARM::R7 : ARM::R11;
3252 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3253 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003254 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3255 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003256 false, false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003257 return FrameAddr;
3258}
3259
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003260/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003261/// expand a bit convert where either the source or destination type is i64 to
3262/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3263/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3264/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003265static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003266 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3267 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003268 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003269
Bob Wilson9f3f0612010-04-17 05:30:19 +00003270 // This function is only supposed to be called for i64 types, either as the
3271 // source or destination of the bit convert.
3272 EVT SrcVT = Op.getValueType();
3273 EVT DstVT = N->getValueType(0);
3274 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003275 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003276
Bob Wilson9f3f0612010-04-17 05:30:19 +00003277 // Turn i64->f64 into VMOVDRR.
3278 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003279 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3280 DAG.getConstant(0, MVT::i32));
3281 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3282 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003283 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003284 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003285 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003286
Jim Grosbache5165492009-11-09 00:11:35 +00003287 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003288 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3289 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3290 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3291 // Merge the pieces into a single i64 value.
3292 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3293 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003294
Bob Wilson9f3f0612010-04-17 05:30:19 +00003295 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003296}
3297
Bob Wilson5bafff32009-06-22 23:27:02 +00003298/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003299/// Zero vectors are used to represent vector negation and in those cases
3300/// will be implemented with the NEON VNEG instruction. However, VNEG does
3301/// not support i64 elements, so sometimes the zero vectors will need to be
3302/// explicitly constructed. Regardless, use a canonical VMOV to create the
3303/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003304static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003305 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003306 // The canonical modified immediate encoding of a zero vector is....0!
3307 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3308 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3309 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003310 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003311}
3312
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003313/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3314/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003315SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3316 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003317 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3318 EVT VT = Op.getValueType();
3319 unsigned VTBits = VT.getSizeInBits();
3320 DebugLoc dl = Op.getDebugLoc();
3321 SDValue ShOpLo = Op.getOperand(0);
3322 SDValue ShOpHi = Op.getOperand(1);
3323 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003324 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003325 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003326
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003327 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3328
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003329 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3330 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3331 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3332 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3333 DAG.getConstant(VTBits, MVT::i32));
3334 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3335 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003336 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003337
3338 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3339 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003340 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003341 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003342 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003343 CCR, Cmp);
3344
3345 SDValue Ops[2] = { Lo, Hi };
3346 return DAG.getMergeValues(Ops, 2, dl);
3347}
3348
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003349/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3350/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003351SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3352 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003353 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3354 EVT VT = Op.getValueType();
3355 unsigned VTBits = VT.getSizeInBits();
3356 DebugLoc dl = Op.getDebugLoc();
3357 SDValue ShOpLo = Op.getOperand(0);
3358 SDValue ShOpHi = Op.getOperand(1);
3359 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003360 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003361
3362 assert(Op.getOpcode() == ISD::SHL_PARTS);
3363 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3364 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3365 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3366 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3367 DAG.getConstant(VTBits, MVT::i32));
3368 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3369 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3370
3371 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3372 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3373 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003374 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003375 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003376 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003377 CCR, Cmp);
3378
3379 SDValue Ops[2] = { Lo, Hi };
3380 return DAG.getMergeValues(Ops, 2, dl);
3381}
3382
Jim Grosbach4725ca72010-09-08 03:54:02 +00003383SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003384 SelectionDAG &DAG) const {
3385 // The rounding mode is in bits 23:22 of the FPSCR.
3386 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3387 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3388 // so that the shift + and get folded into a bitfield extract.
3389 DebugLoc dl = Op.getDebugLoc();
3390 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3391 DAG.getConstant(Intrinsic::arm_get_fpscr,
3392 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003393 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003394 DAG.getConstant(1U << 22, MVT::i32));
3395 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3396 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003397 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003398 DAG.getConstant(3, MVT::i32));
3399}
3400
Jim Grosbach3482c802010-01-18 19:58:49 +00003401static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3402 const ARMSubtarget *ST) {
3403 EVT VT = N->getValueType(0);
3404 DebugLoc dl = N->getDebugLoc();
3405
3406 if (!ST->hasV6T2Ops())
3407 return SDValue();
3408
3409 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3410 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3411}
3412
Bob Wilson5bafff32009-06-22 23:27:02 +00003413static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3414 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003415 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003416 DebugLoc dl = N->getDebugLoc();
3417
Bob Wilsond5448bb2010-11-18 21:16:28 +00003418 if (!VT.isVector())
3419 return SDValue();
3420
Bob Wilson5bafff32009-06-22 23:27:02 +00003421 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003422 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003423
Bob Wilsond5448bb2010-11-18 21:16:28 +00003424 // Left shifts translate directly to the vshiftu intrinsic.
3425 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003426 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003427 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3428 N->getOperand(0), N->getOperand(1));
3429
3430 assert((N->getOpcode() == ISD::SRA ||
3431 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3432
3433 // NEON uses the same intrinsics for both left and right shifts. For
3434 // right shifts, the shift amounts are negative, so negate the vector of
3435 // shift amounts.
3436 EVT ShiftVT = N->getOperand(1).getValueType();
3437 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3438 getZeroVector(ShiftVT, DAG, dl),
3439 N->getOperand(1));
3440 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3441 Intrinsic::arm_neon_vshifts :
3442 Intrinsic::arm_neon_vshiftu);
3443 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3444 DAG.getConstant(vshiftInt, MVT::i32),
3445 N->getOperand(0), NegatedCount);
3446}
3447
3448static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3449 const ARMSubtarget *ST) {
3450 EVT VT = N->getValueType(0);
3451 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003452
Eli Friedmance392eb2009-08-22 03:13:10 +00003453 // We can get here for a node like i32 = ISD::SHL i32, i64
3454 if (VT != MVT::i64)
3455 return SDValue();
3456
3457 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003458 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003459
Chris Lattner27a6c732007-11-24 07:07:01 +00003460 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3461 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003462 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003463 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003464
Chris Lattner27a6c732007-11-24 07:07:01 +00003465 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003466 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003467
Chris Lattner27a6c732007-11-24 07:07:01 +00003468 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003469 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003470 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003471 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003472 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003473
Chris Lattner27a6c732007-11-24 07:07:01 +00003474 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3475 // captures the result into a carry flag.
3476 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003477 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003478
Chris Lattner27a6c732007-11-24 07:07:01 +00003479 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003480 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003481
Chris Lattner27a6c732007-11-24 07:07:01 +00003482 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003483 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003484}
3485
Bob Wilson5bafff32009-06-22 23:27:02 +00003486static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3487 SDValue TmpOp0, TmpOp1;
3488 bool Invert = false;
3489 bool Swap = false;
3490 unsigned Opc = 0;
3491
3492 SDValue Op0 = Op.getOperand(0);
3493 SDValue Op1 = Op.getOperand(1);
3494 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003495 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003496 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3497 DebugLoc dl = Op.getDebugLoc();
3498
3499 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3500 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003501 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003502 case ISD::SETUNE:
3503 case ISD::SETNE: Invert = true; // Fallthrough
3504 case ISD::SETOEQ:
3505 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3506 case ISD::SETOLT:
3507 case ISD::SETLT: Swap = true; // Fallthrough
3508 case ISD::SETOGT:
3509 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3510 case ISD::SETOLE:
3511 case ISD::SETLE: Swap = true; // Fallthrough
3512 case ISD::SETOGE:
3513 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3514 case ISD::SETUGE: Swap = true; // Fallthrough
3515 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3516 case ISD::SETUGT: Swap = true; // Fallthrough
3517 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3518 case ISD::SETUEQ: Invert = true; // Fallthrough
3519 case ISD::SETONE:
3520 // Expand this to (OLT | OGT).
3521 TmpOp0 = Op0;
3522 TmpOp1 = Op1;
3523 Opc = ISD::OR;
3524 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3525 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3526 break;
3527 case ISD::SETUO: Invert = true; // Fallthrough
3528 case ISD::SETO:
3529 // Expand this to (OLT | OGE).
3530 TmpOp0 = Op0;
3531 TmpOp1 = Op1;
3532 Opc = ISD::OR;
3533 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3534 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3535 break;
3536 }
3537 } else {
3538 // Integer comparisons.
3539 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003540 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003541 case ISD::SETNE: Invert = true;
3542 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3543 case ISD::SETLT: Swap = true;
3544 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3545 case ISD::SETLE: Swap = true;
3546 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3547 case ISD::SETULT: Swap = true;
3548 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3549 case ISD::SETULE: Swap = true;
3550 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3551 }
3552
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003553 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003554 if (Opc == ARMISD::VCEQ) {
3555
3556 SDValue AndOp;
3557 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3558 AndOp = Op0;
3559 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3560 AndOp = Op1;
3561
3562 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003563 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003564 AndOp = AndOp.getOperand(0);
3565
3566 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3567 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003568 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3569 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003570 Invert = !Invert;
3571 }
3572 }
3573 }
3574
3575 if (Swap)
3576 std::swap(Op0, Op1);
3577
Owen Andersonc24cb352010-11-08 23:21:22 +00003578 // If one of the operands is a constant vector zero, attempt to fold the
3579 // comparison to a specialized compare-against-zero form.
3580 SDValue SingleOp;
3581 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3582 SingleOp = Op0;
3583 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3584 if (Opc == ARMISD::VCGE)
3585 Opc = ARMISD::VCLEZ;
3586 else if (Opc == ARMISD::VCGT)
3587 Opc = ARMISD::VCLTZ;
3588 SingleOp = Op1;
3589 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003590
Owen Andersonc24cb352010-11-08 23:21:22 +00003591 SDValue Result;
3592 if (SingleOp.getNode()) {
3593 switch (Opc) {
3594 case ARMISD::VCEQ:
3595 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3596 case ARMISD::VCGE:
3597 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3598 case ARMISD::VCLEZ:
3599 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3600 case ARMISD::VCGT:
3601 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3602 case ARMISD::VCLTZ:
3603 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3604 default:
3605 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3606 }
3607 } else {
3608 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3609 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003610
3611 if (Invert)
3612 Result = DAG.getNOT(dl, Result, VT);
3613
3614 return Result;
3615}
3616
Bob Wilsond3c42842010-06-14 22:19:57 +00003617/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3618/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003619/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003620static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3621 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003622 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003623 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003624
Bob Wilson827b2102010-06-15 19:05:35 +00003625 // SplatBitSize is set to the smallest size that splats the vector, so a
3626 // zero vector will always have SplatBitSize == 8. However, NEON modified
3627 // immediate instructions others than VMOV do not support the 8-bit encoding
3628 // of a zero vector, and the default encoding of zero is supposed to be the
3629 // 32-bit version.
3630 if (SplatBits == 0)
3631 SplatBitSize = 32;
3632
Bob Wilson5bafff32009-06-22 23:27:02 +00003633 switch (SplatBitSize) {
3634 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003635 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003636 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003637 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003638 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003639 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003640 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003641 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003642 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003643
3644 case 16:
3645 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003646 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003647 if ((SplatBits & ~0xff) == 0) {
3648 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003649 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003650 Imm = SplatBits;
3651 break;
3652 }
3653 if ((SplatBits & ~0xff00) == 0) {
3654 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003655 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003656 Imm = SplatBits >> 8;
3657 break;
3658 }
3659 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003660
3661 case 32:
3662 // NEON's 32-bit VMOV supports splat values where:
3663 // * only one byte is nonzero, or
3664 // * the least significant byte is 0xff and the second byte is nonzero, or
3665 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003666 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003667 if ((SplatBits & ~0xff) == 0) {
3668 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003669 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003670 Imm = SplatBits;
3671 break;
3672 }
3673 if ((SplatBits & ~0xff00) == 0) {
3674 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003675 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003676 Imm = SplatBits >> 8;
3677 break;
3678 }
3679 if ((SplatBits & ~0xff0000) == 0) {
3680 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003681 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003682 Imm = SplatBits >> 16;
3683 break;
3684 }
3685 if ((SplatBits & ~0xff000000) == 0) {
3686 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003687 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003688 Imm = SplatBits >> 24;
3689 break;
3690 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003691
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003692 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3693 if (type == OtherModImm) return SDValue();
3694
Bob Wilson5bafff32009-06-22 23:27:02 +00003695 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003696 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3697 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003698 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003699 Imm = SplatBits >> 8;
3700 SplatBits |= 0xff;
3701 break;
3702 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003703
3704 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003705 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3706 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003707 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003708 Imm = SplatBits >> 16;
3709 SplatBits |= 0xffff;
3710 break;
3711 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003712
3713 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3714 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3715 // VMOV.I32. A (very) minor optimization would be to replicate the value
3716 // and fall through here to test for a valid 64-bit splat. But, then the
3717 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003718 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003719
3720 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003721 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003722 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003723 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003724 uint64_t BitMask = 0xff;
3725 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003726 unsigned ImmMask = 1;
3727 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003728 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003729 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003730 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003731 Imm |= ImmMask;
3732 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003733 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003734 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003735 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003736 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003737 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003738 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003739 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003740 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003741 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003742 break;
3743 }
3744
Bob Wilson1a913ed2010-06-11 21:34:50 +00003745 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003746 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003747 }
3748
Bob Wilsoncba270d2010-07-13 21:16:48 +00003749 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3750 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003751}
3752
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003753static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003754 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003755 unsigned NumElts = VT.getVectorNumElements();
3756 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003757
3758 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3759 if (M[0] < 0)
3760 return false;
3761
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003762 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003763
3764 // If this is a VEXT shuffle, the immediate value is the index of the first
3765 // element. The other shuffle indices must be the successive elements after
3766 // the first one.
3767 unsigned ExpectedElt = Imm;
3768 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003769 // Increment the expected index. If it wraps around, it may still be
3770 // a VEXT but the source vectors must be swapped.
3771 ExpectedElt += 1;
3772 if (ExpectedElt == NumElts * 2) {
3773 ExpectedElt = 0;
3774 ReverseVEXT = true;
3775 }
3776
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003777 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003778 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003779 return false;
3780 }
3781
3782 // Adjust the index value if the source operands will be swapped.
3783 if (ReverseVEXT)
3784 Imm -= NumElts;
3785
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003786 return true;
3787}
3788
Bob Wilson8bb9e482009-07-26 00:39:34 +00003789/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3790/// instruction with the specified blocksize. (The order of the elements
3791/// within each block of the vector is reversed.)
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003792static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003793 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3794 "Only possible block sizes for VREV are: 16, 32, 64");
3795
Bob Wilson8bb9e482009-07-26 00:39:34 +00003796 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003797 if (EltSz == 64)
3798 return false;
3799
3800 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003801 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003802 // If the first shuffle index is UNDEF, be optimistic.
3803 if (M[0] < 0)
3804 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003805
3806 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3807 return false;
3808
3809 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003810 if (M[i] < 0) continue; // ignore UNDEF indices
3811 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003812 return false;
3813 }
3814
3815 return true;
3816}
3817
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003818static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003819 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3820 // range, then 0 is placed into the resulting vector. So pretty much any mask
3821 // of 8 elements can work here.
3822 return VT == MVT::v8i8 && M.size() == 8;
3823}
3824
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003825static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003826 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3827 if (EltSz == 64)
3828 return false;
3829
Bob Wilsonc692cb72009-08-21 20:54:19 +00003830 unsigned NumElts = VT.getVectorNumElements();
3831 WhichResult = (M[0] == 0 ? 0 : 1);
3832 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003833 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3834 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003835 return false;
3836 }
3837 return true;
3838}
3839
Bob Wilson324f4f12009-12-03 06:40:55 +00003840/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3841/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3842/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003843static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00003844 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3845 if (EltSz == 64)
3846 return false;
3847
3848 unsigned NumElts = VT.getVectorNumElements();
3849 WhichResult = (M[0] == 0 ? 0 : 1);
3850 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003851 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3852 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003853 return false;
3854 }
3855 return true;
3856}
3857
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003858static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003859 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3860 if (EltSz == 64)
3861 return false;
3862
Bob Wilsonc692cb72009-08-21 20:54:19 +00003863 unsigned NumElts = VT.getVectorNumElements();
3864 WhichResult = (M[0] == 0 ? 0 : 1);
3865 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003866 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003867 if ((unsigned) M[i] != 2 * i + WhichResult)
3868 return false;
3869 }
3870
3871 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003872 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003873 return false;
3874
3875 return true;
3876}
3877
Bob Wilson324f4f12009-12-03 06:40:55 +00003878/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3879/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3880/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003881static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00003882 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3883 if (EltSz == 64)
3884 return false;
3885
3886 unsigned Half = VT.getVectorNumElements() / 2;
3887 WhichResult = (M[0] == 0 ? 0 : 1);
3888 for (unsigned j = 0; j != 2; ++j) {
3889 unsigned Idx = WhichResult;
3890 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003891 int MIdx = M[i + j * Half];
3892 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003893 return false;
3894 Idx += 2;
3895 }
3896 }
3897
3898 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3899 if (VT.is64BitVector() && EltSz == 32)
3900 return false;
3901
3902 return true;
3903}
3904
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003905static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003906 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3907 if (EltSz == 64)
3908 return false;
3909
Bob Wilsonc692cb72009-08-21 20:54:19 +00003910 unsigned NumElts = VT.getVectorNumElements();
3911 WhichResult = (M[0] == 0 ? 0 : 1);
3912 unsigned Idx = WhichResult * NumElts / 2;
3913 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003914 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3915 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003916 return false;
3917 Idx += 1;
3918 }
3919
3920 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003921 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003922 return false;
3923
3924 return true;
3925}
3926
Bob Wilson324f4f12009-12-03 06:40:55 +00003927/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3928/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3929/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003930static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00003931 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3932 if (EltSz == 64)
3933 return false;
3934
3935 unsigned NumElts = VT.getVectorNumElements();
3936 WhichResult = (M[0] == 0 ? 0 : 1);
3937 unsigned Idx = WhichResult * NumElts / 2;
3938 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003939 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3940 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003941 return false;
3942 Idx += 1;
3943 }
3944
3945 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3946 if (VT.is64BitVector() && EltSz == 32)
3947 return false;
3948
3949 return true;
3950}
3951
Dale Johannesenf630c712010-07-29 20:10:08 +00003952// If N is an integer constant that can be moved into a register in one
3953// instruction, return an SDValue of such a constant (will become a MOV
3954// instruction). Otherwise return null.
3955static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3956 const ARMSubtarget *ST, DebugLoc dl) {
3957 uint64_t Val;
3958 if (!isa<ConstantSDNode>(N))
3959 return SDValue();
3960 Val = cast<ConstantSDNode>(N)->getZExtValue();
3961
3962 if (ST->isThumb1Only()) {
3963 if (Val <= 255 || ~Val <= 255)
3964 return DAG.getConstant(Val, MVT::i32);
3965 } else {
3966 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3967 return DAG.getConstant(Val, MVT::i32);
3968 }
3969 return SDValue();
3970}
3971
Bob Wilson5bafff32009-06-22 23:27:02 +00003972// If this is a case we can't handle, return null and let the default
3973// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003974SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3975 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003976 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003977 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003978 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003979
3980 APInt SplatBits, SplatUndef;
3981 unsigned SplatBitSize;
3982 bool HasAnyUndefs;
3983 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003984 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003985 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003986 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003987 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003988 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003989 DAG, VmovVT, VT.is128BitVector(),
3990 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003991 if (Val.getNode()) {
3992 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003993 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003994 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003995
3996 // Try an immediate VMVN.
Eli Friedman8e4d0422011-10-13 22:40:23 +00003997 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003998 Val = isNEONModifiedImm(NegatedImm,
3999 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004000 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004001 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004002 if (Val.getNode()) {
4003 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004004 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004005 }
Evan Chengeaa192a2011-11-15 02:12:34 +00004006
4007 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedman2f21e8c2011-12-15 22:56:53 +00004008 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedmaneffab8f2011-12-09 23:54:42 +00004009 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Chengeaa192a2011-11-15 02:12:34 +00004010 if (ImmVal != -1) {
4011 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4012 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4013 }
4014 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004015 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00004016 }
4017
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004018 // Scan through the operands to see if only one value is used.
4019 unsigned NumElts = VT.getVectorNumElements();
4020 bool isOnlyLowElement = true;
4021 bool usesOnlyOneValue = true;
4022 bool isConstant = true;
4023 SDValue Value;
4024 for (unsigned i = 0; i < NumElts; ++i) {
4025 SDValue V = Op.getOperand(i);
4026 if (V.getOpcode() == ISD::UNDEF)
4027 continue;
4028 if (i > 0)
4029 isOnlyLowElement = false;
4030 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4031 isConstant = false;
4032
4033 if (!Value.getNode())
4034 Value = V;
4035 else if (V != Value)
4036 usesOnlyOneValue = false;
4037 }
4038
4039 if (!Value.getNode())
4040 return DAG.getUNDEF(VT);
4041
4042 if (isOnlyLowElement)
4043 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4044
Dale Johannesenf630c712010-07-29 20:10:08 +00004045 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4046
Dale Johannesen575cd142010-10-19 20:00:17 +00004047 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4048 // i32 and try again.
4049 if (usesOnlyOneValue && EltSize <= 32) {
4050 if (!isConstant)
4051 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4052 if (VT.getVectorElementType().isFloatingPoint()) {
4053 SmallVector<SDValue, 8> Ops;
4054 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004055 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00004056 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00004057 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
4058 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00004059 Val = LowerBUILD_VECTOR(Val, DAG, ST);
4060 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004061 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004062 }
Dale Johannesen575cd142010-10-19 20:00:17 +00004063 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
4064 if (Val.getNode())
4065 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004066 }
4067
4068 // If all elements are constants and the case above didn't get hit, fall back
4069 // to the default expansion, which will generate a load from the constant
4070 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004071 if (isConstant)
4072 return SDValue();
4073
Bob Wilson11a1dff2011-01-07 21:37:30 +00004074 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4075 if (NumElts >= 4) {
4076 SDValue shuffle = ReconstructShuffle(Op, DAG);
4077 if (shuffle != SDValue())
4078 return shuffle;
4079 }
4080
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004081 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004082 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4083 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004084 if (EltSize >= 32) {
4085 // Do the expansion with floating-point types, since that is what the VFP
4086 // registers are defined to use, and since i64 is not legal.
4087 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4088 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004089 SmallVector<SDValue, 8> Ops;
4090 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004091 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004092 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004093 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004094 }
4095
4096 return SDValue();
4097}
4098
Bob Wilson11a1dff2011-01-07 21:37:30 +00004099// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004100// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004101SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4102 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004103 DebugLoc dl = Op.getDebugLoc();
4104 EVT VT = Op.getValueType();
4105 unsigned NumElts = VT.getVectorNumElements();
4106
4107 SmallVector<SDValue, 2> SourceVecs;
4108 SmallVector<unsigned, 2> MinElts;
4109 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004110
Bob Wilson11a1dff2011-01-07 21:37:30 +00004111 for (unsigned i = 0; i < NumElts; ++i) {
4112 SDValue V = Op.getOperand(i);
4113 if (V.getOpcode() == ISD::UNDEF)
4114 continue;
4115 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4116 // A shuffle can only come from building a vector from various
4117 // elements of other vectors.
4118 return SDValue();
Eli Friedman46995fa2011-10-14 23:58:49 +00004119 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
4120 VT.getVectorElementType()) {
4121 // This code doesn't know how to handle shuffles where the vector
4122 // element types do not match (this happens because type legalization
4123 // promotes the return type of EXTRACT_VECTOR_ELT).
4124 // FIXME: It might be appropriate to extend this code to handle
4125 // mismatched types.
4126 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004127 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004128
Bob Wilson11a1dff2011-01-07 21:37:30 +00004129 // Record this extraction against the appropriate vector if possible...
4130 SDValue SourceVec = V.getOperand(0);
4131 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4132 bool FoundSource = false;
4133 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4134 if (SourceVecs[j] == SourceVec) {
4135 if (MinElts[j] > EltNo)
4136 MinElts[j] = EltNo;
4137 if (MaxElts[j] < EltNo)
4138 MaxElts[j] = EltNo;
4139 FoundSource = true;
4140 break;
4141 }
4142 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004143
Bob Wilson11a1dff2011-01-07 21:37:30 +00004144 // Or record a new source if not...
4145 if (!FoundSource) {
4146 SourceVecs.push_back(SourceVec);
4147 MinElts.push_back(EltNo);
4148 MaxElts.push_back(EltNo);
4149 }
4150 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004151
Bob Wilson11a1dff2011-01-07 21:37:30 +00004152 // Currently only do something sane when at most two source vectors
4153 // involved.
4154 if (SourceVecs.size() > 2)
4155 return SDValue();
4156
4157 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4158 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004159
Bob Wilson11a1dff2011-01-07 21:37:30 +00004160 // This loop extracts the usage patterns of the source vectors
4161 // and prepares appropriate SDValues for a shuffle if possible.
4162 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4163 if (SourceVecs[i].getValueType() == VT) {
4164 // No VEXT necessary
4165 ShuffleSrcs[i] = SourceVecs[i];
4166 VEXTOffsets[i] = 0;
4167 continue;
4168 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4169 // It probably isn't worth padding out a smaller vector just to
4170 // break it down again in a shuffle.
4171 return SDValue();
4172 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004173
Bob Wilson11a1dff2011-01-07 21:37:30 +00004174 // Since only 64-bit and 128-bit vectors are legal on ARM and
4175 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004176 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4177 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004178
Bob Wilson11a1dff2011-01-07 21:37:30 +00004179 if (MaxElts[i] - MinElts[i] >= NumElts) {
4180 // Span too large for a VEXT to cope
4181 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004182 }
4183
Bob Wilson11a1dff2011-01-07 21:37:30 +00004184 if (MinElts[i] >= NumElts) {
4185 // The extraction can just take the second half
4186 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004187 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4188 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004189 DAG.getIntPtrConstant(NumElts));
4190 } else if (MaxElts[i] < NumElts) {
4191 // The extraction can just take the first half
4192 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004193 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4194 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004195 DAG.getIntPtrConstant(0));
4196 } else {
4197 // An actual VEXT is needed
4198 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004199 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4200 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004201 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004202 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4203 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004204 DAG.getIntPtrConstant(NumElts));
4205 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4206 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4207 }
4208 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004209
Bob Wilson11a1dff2011-01-07 21:37:30 +00004210 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004211
Bob Wilson11a1dff2011-01-07 21:37:30 +00004212 for (unsigned i = 0; i < NumElts; ++i) {
4213 SDValue Entry = Op.getOperand(i);
4214 if (Entry.getOpcode() == ISD::UNDEF) {
4215 Mask.push_back(-1);
4216 continue;
4217 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004218
Bob Wilson11a1dff2011-01-07 21:37:30 +00004219 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004220 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4221 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004222 if (ExtractVec == SourceVecs[0]) {
4223 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4224 } else {
4225 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4226 }
4227 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004228
Bob Wilson11a1dff2011-01-07 21:37:30 +00004229 // Final check before we try to produce nonsense...
4230 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004231 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4232 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004233
Bob Wilson11a1dff2011-01-07 21:37:30 +00004234 return SDValue();
4235}
4236
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004237/// isShuffleMaskLegal - Targets can use this to indicate that they only
4238/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4239/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4240/// are assumed to be legal.
4241bool
4242ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4243 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004244 if (VT.getVectorNumElements() == 4 &&
4245 (VT.is128BitVector() || VT.is64BitVector())) {
4246 unsigned PFIndexes[4];
4247 for (unsigned i = 0; i != 4; ++i) {
4248 if (M[i] < 0)
4249 PFIndexes[i] = 8;
4250 else
4251 PFIndexes[i] = M[i];
4252 }
4253
4254 // Compute the index in the perfect shuffle table.
4255 unsigned PFTableIndex =
4256 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4257 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4258 unsigned Cost = (PFEntry >> 30);
4259
4260 if (Cost <= 4)
4261 return true;
4262 }
4263
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004264 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004265 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004266
Bob Wilson53dd2452010-06-07 23:53:38 +00004267 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4268 return (EltSize >= 32 ||
4269 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004270 isVREVMask(M, VT, 64) ||
4271 isVREVMask(M, VT, 32) ||
4272 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004273 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004274 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004275 isVTRNMask(M, VT, WhichResult) ||
4276 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004277 isVZIPMask(M, VT, WhichResult) ||
4278 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4279 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4280 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004281}
4282
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004283/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4284/// the specified operations to build the shuffle.
4285static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4286 SDValue RHS, SelectionDAG &DAG,
4287 DebugLoc dl) {
4288 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4289 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4290 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4291
4292 enum {
4293 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4294 OP_VREV,
4295 OP_VDUP0,
4296 OP_VDUP1,
4297 OP_VDUP2,
4298 OP_VDUP3,
4299 OP_VEXT1,
4300 OP_VEXT2,
4301 OP_VEXT3,
4302 OP_VUZPL, // VUZP, left result
4303 OP_VUZPR, // VUZP, right result
4304 OP_VZIPL, // VZIP, left result
4305 OP_VZIPR, // VZIP, right result
4306 OP_VTRNL, // VTRN, left result
4307 OP_VTRNR // VTRN, right result
4308 };
4309
4310 if (OpNum == OP_COPY) {
4311 if (LHSID == (1*9+2)*9+3) return LHS;
4312 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4313 return RHS;
4314 }
4315
4316 SDValue OpLHS, OpRHS;
4317 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4318 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4319 EVT VT = OpLHS.getValueType();
4320
4321 switch (OpNum) {
4322 default: llvm_unreachable("Unknown shuffle opcode!");
4323 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004324 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004325 if (VT.getVectorElementType() == MVT::i32 ||
4326 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004327 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4328 // vrev <4 x i16> -> VREV32
4329 if (VT.getVectorElementType() == MVT::i16)
4330 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4331 // vrev <4 x i8> -> VREV16
4332 assert(VT.getVectorElementType() == MVT::i8);
4333 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004334 case OP_VDUP0:
4335 case OP_VDUP1:
4336 case OP_VDUP2:
4337 case OP_VDUP3:
4338 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004339 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004340 case OP_VEXT1:
4341 case OP_VEXT2:
4342 case OP_VEXT3:
4343 return DAG.getNode(ARMISD::VEXT, dl, VT,
4344 OpLHS, OpRHS,
4345 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4346 case OP_VUZPL:
4347 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004348 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004349 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4350 case OP_VZIPL:
4351 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004352 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004353 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4354 case OP_VTRNL:
4355 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004356 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4357 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004358 }
4359}
4360
Bill Wendling69a05a72011-03-14 23:02:38 +00004361static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004362 ArrayRef<int> ShuffleMask,
Bill Wendling69a05a72011-03-14 23:02:38 +00004363 SelectionDAG &DAG) {
4364 // Check to see if we can use the VTBL instruction.
4365 SDValue V1 = Op.getOperand(0);
4366 SDValue V2 = Op.getOperand(1);
4367 DebugLoc DL = Op.getDebugLoc();
4368
4369 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004370 for (ArrayRef<int>::iterator
Bill Wendling69a05a72011-03-14 23:02:38 +00004371 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4372 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4373
4374 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4375 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4376 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4377 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004378
Owen Anderson76706012011-04-05 21:48:57 +00004379 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004380 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4381 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004382}
4383
Bob Wilson5bafff32009-06-22 23:27:02 +00004384static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004385 SDValue V1 = Op.getOperand(0);
4386 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004387 DebugLoc dl = Op.getDebugLoc();
4388 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004389 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsond8e17572009-08-12 22:31:50 +00004390
Bob Wilson28865062009-08-13 02:13:04 +00004391 // Convert shuffles that are directly supported on NEON to target-specific
4392 // DAG nodes, instead of keeping them as shuffles and matching them again
4393 // during code selection. This is more efficient and avoids the possibility
4394 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004395 // FIXME: floating-point vectors should be canonicalized to integer vectors
4396 // of the same time so that they get CSEd properly.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004397 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004398
Bob Wilson53dd2452010-06-07 23:53:38 +00004399 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4400 if (EltSize <= 32) {
4401 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4402 int Lane = SVN->getSplatIndex();
4403 // If this is undef splat, generate it via "just" vdup, if possible.
4404 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004405
Dan Gohman65fd6562011-11-03 21:49:52 +00004406 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson53dd2452010-06-07 23:53:38 +00004407 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4408 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4409 }
Dan Gohman65fd6562011-11-03 21:49:52 +00004410 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
4411 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
4412 // reaches it).
4413 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
4414 !isa<ConstantSDNode>(V1.getOperand(0))) {
4415 bool IsScalarToVector = true;
4416 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
4417 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
4418 IsScalarToVector = false;
4419 break;
4420 }
4421 if (IsScalarToVector)
4422 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4423 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004424 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4425 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004426 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004427
4428 bool ReverseVEXT;
4429 unsigned Imm;
4430 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4431 if (ReverseVEXT)
4432 std::swap(V1, V2);
4433 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4434 DAG.getConstant(Imm, MVT::i32));
4435 }
4436
4437 if (isVREVMask(ShuffleMask, VT, 64))
4438 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4439 if (isVREVMask(ShuffleMask, VT, 32))
4440 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4441 if (isVREVMask(ShuffleMask, VT, 16))
4442 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4443
4444 // Check for Neon shuffles that modify both input vectors in place.
4445 // If both results are used, i.e., if there are two shuffles with the same
4446 // source operands and with masks corresponding to both results of one of
4447 // these operations, DAG memoization will ensure that a single node is
4448 // used for both shuffles.
4449 unsigned WhichResult;
4450 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4451 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4452 V1, V2).getValue(WhichResult);
4453 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4454 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4455 V1, V2).getValue(WhichResult);
4456 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4457 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4458 V1, V2).getValue(WhichResult);
4459
4460 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4461 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4462 V1, V1).getValue(WhichResult);
4463 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4464 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4465 V1, V1).getValue(WhichResult);
4466 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4467 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4468 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004469 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004470
Bob Wilsonc692cb72009-08-21 20:54:19 +00004471 // If the shuffle is not directly supported and it has 4 elements, use
4472 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004473 unsigned NumElts = VT.getVectorNumElements();
4474 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004475 unsigned PFIndexes[4];
4476 for (unsigned i = 0; i != 4; ++i) {
4477 if (ShuffleMask[i] < 0)
4478 PFIndexes[i] = 8;
4479 else
4480 PFIndexes[i] = ShuffleMask[i];
4481 }
4482
4483 // Compute the index in the perfect shuffle table.
4484 unsigned PFTableIndex =
4485 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004486 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4487 unsigned Cost = (PFEntry >> 30);
4488
4489 if (Cost <= 4)
4490 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4491 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004492
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004493 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004494 if (EltSize >= 32) {
4495 // Do the expansion with floating-point types, since that is what the VFP
4496 // registers are defined to use, and since i64 is not legal.
4497 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4498 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004499 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4500 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004501 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004502 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004503 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004504 Ops.push_back(DAG.getUNDEF(EltVT));
4505 else
4506 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4507 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4508 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4509 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004510 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004511 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004512 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004513 }
4514
Bill Wendling69a05a72011-03-14 23:02:38 +00004515 if (VT == MVT::v8i8) {
4516 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4517 if (NewOp.getNode())
4518 return NewOp;
4519 }
4520
Bob Wilson22cac0d2009-08-14 05:16:33 +00004521 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004522}
4523
Eli Friedman5c89cb82011-10-24 23:08:52 +00004524static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
4525 // INSERT_VECTOR_ELT is legal only for immediate indexes.
4526 SDValue Lane = Op.getOperand(2);
4527 if (!isa<ConstantSDNode>(Lane))
4528 return SDValue();
4529
4530 return Op;
4531}
4532
Bob Wilson5bafff32009-06-22 23:27:02 +00004533static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004534 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004535 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004536 if (!isa<ConstantSDNode>(Lane))
4537 return SDValue();
4538
4539 SDValue Vec = Op.getOperand(0);
4540 if (Op.getValueType() == MVT::i32 &&
4541 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4542 DebugLoc dl = Op.getDebugLoc();
4543 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4544 }
4545
4546 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004547}
4548
Bob Wilsona6d65862009-08-03 20:36:38 +00004549static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4550 // The only time a CONCAT_VECTORS operation can have legal types is when
4551 // two 64-bit vectors are concatenated to a 128-bit vector.
4552 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4553 "unexpected CONCAT_VECTORS");
4554 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004555 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004556 SDValue Op0 = Op.getOperand(0);
4557 SDValue Op1 = Op.getOperand(1);
4558 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004559 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004560 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004561 DAG.getIntPtrConstant(0));
4562 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004563 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004564 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004565 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004566 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004567}
4568
Bob Wilson626613d2010-11-23 19:38:38 +00004569/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4570/// element has been zero/sign-extended, depending on the isSigned parameter,
4571/// from an integer type half its size.
4572static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4573 bool isSigned) {
4574 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4575 EVT VT = N->getValueType(0);
4576 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4577 SDNode *BVN = N->getOperand(0).getNode();
4578 if (BVN->getValueType(0) != MVT::v4i32 ||
4579 BVN->getOpcode() != ISD::BUILD_VECTOR)
4580 return false;
4581 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4582 unsigned HiElt = 1 - LoElt;
4583 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4584 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4585 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4586 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4587 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4588 return false;
4589 if (isSigned) {
4590 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4591 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4592 return true;
4593 } else {
4594 if (Hi0->isNullValue() && Hi1->isNullValue())
4595 return true;
4596 }
4597 return false;
4598 }
4599
4600 if (N->getOpcode() != ISD::BUILD_VECTOR)
4601 return false;
4602
4603 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4604 SDNode *Elt = N->getOperand(i).getNode();
4605 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4606 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4607 unsigned HalfSize = EltSize / 2;
4608 if (isSigned) {
Bob Wilson9d45de22011-10-18 18:46:49 +00004609 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004610 return false;
4611 } else {
Bob Wilson9d45de22011-10-18 18:46:49 +00004612 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004613 return false;
4614 }
4615 continue;
4616 }
4617 return false;
4618 }
4619
4620 return true;
4621}
4622
4623/// isSignExtended - Check if a node is a vector value that is sign-extended
4624/// or a constant BUILD_VECTOR with sign-extended elements.
4625static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4626 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4627 return true;
4628 if (isExtendedBUILD_VECTOR(N, DAG, true))
4629 return true;
4630 return false;
4631}
4632
4633/// isZeroExtended - Check if a node is a vector value that is zero-extended
4634/// or a constant BUILD_VECTOR with zero-extended elements.
4635static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4636 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4637 return true;
4638 if (isExtendedBUILD_VECTOR(N, DAG, false))
4639 return true;
4640 return false;
4641}
4642
4643/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4644/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004645static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4646 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4647 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004648 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4649 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4650 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004651 LD->isNonTemporal(), LD->isInvariant(),
4652 LD->getAlignment());
Bob Wilson626613d2010-11-23 19:38:38 +00004653 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4654 // have been legalized as a BITCAST from v4i32.
4655 if (N->getOpcode() == ISD::BITCAST) {
4656 SDNode *BVN = N->getOperand(0).getNode();
4657 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4658 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4659 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4660 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4661 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4662 }
4663 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4664 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4665 EVT VT = N->getValueType(0);
4666 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4667 unsigned NumElts = VT.getVectorNumElements();
4668 MVT TruncVT = MVT::getIntegerVT(EltSize);
4669 SmallVector<SDValue, 8> Ops;
4670 for (unsigned i = 0; i != NumElts; ++i) {
4671 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4672 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004673 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004674 }
4675 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4676 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004677}
4678
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004679static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4680 unsigned Opcode = N->getOpcode();
4681 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4682 SDNode *N0 = N->getOperand(0).getNode();
4683 SDNode *N1 = N->getOperand(1).getNode();
4684 return N0->hasOneUse() && N1->hasOneUse() &&
4685 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4686 }
4687 return false;
4688}
4689
4690static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4691 unsigned Opcode = N->getOpcode();
4692 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4693 SDNode *N0 = N->getOperand(0).getNode();
4694 SDNode *N1 = N->getOperand(1).getNode();
4695 return N0->hasOneUse() && N1->hasOneUse() &&
4696 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4697 }
4698 return false;
4699}
4700
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004701static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4702 // Multiplications are only custom-lowered for 128-bit vectors so that
4703 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4704 EVT VT = Op.getValueType();
4705 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4706 SDNode *N0 = Op.getOperand(0).getNode();
4707 SDNode *N1 = Op.getOperand(1).getNode();
4708 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004709 bool isMLA = false;
4710 bool isN0SExt = isSignExtended(N0, DAG);
4711 bool isN1SExt = isSignExtended(N1, DAG);
4712 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004713 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004714 else {
4715 bool isN0ZExt = isZeroExtended(N0, DAG);
4716 bool isN1ZExt = isZeroExtended(N1, DAG);
4717 if (isN0ZExt && isN1ZExt)
4718 NewOpc = ARMISD::VMULLu;
4719 else if (isN1SExt || isN1ZExt) {
4720 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4721 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4722 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4723 NewOpc = ARMISD::VMULLs;
4724 isMLA = true;
4725 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4726 NewOpc = ARMISD::VMULLu;
4727 isMLA = true;
4728 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4729 std::swap(N0, N1);
4730 NewOpc = ARMISD::VMULLu;
4731 isMLA = true;
4732 }
4733 }
4734
4735 if (!NewOpc) {
4736 if (VT == MVT::v2i64)
4737 // Fall through to expand this. It is not legal.
4738 return SDValue();
4739 else
4740 // Other vector multiplications are legal.
4741 return Op;
4742 }
4743 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004744
4745 // Legalize to a VMULL instruction.
4746 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004747 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004748 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004749 if (!isMLA) {
4750 Op0 = SkipExtension(N0, DAG);
4751 assert(Op0.getValueType().is64BitVector() &&
4752 Op1.getValueType().is64BitVector() &&
4753 "unexpected types for extended operands to VMULL");
4754 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4755 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004756
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004757 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4758 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4759 // vmull q0, d4, d6
4760 // vmlal q0, d5, d6
4761 // is faster than
4762 // vaddl q0, d4, d5
4763 // vmovl q1, d6
4764 // vmul q0, q0, q1
4765 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4766 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4767 EVT Op1VT = Op1.getValueType();
4768 return DAG.getNode(N0->getOpcode(), DL, VT,
4769 DAG.getNode(NewOpc, DL, VT,
4770 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4771 DAG.getNode(NewOpc, DL, VT,
4772 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004773}
4774
Owen Anderson76706012011-04-05 21:48:57 +00004775static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004776LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4777 // Convert to float
4778 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4779 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4780 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4781 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4782 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4783 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4784 // Get reciprocal estimate.
4785 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004786 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004787 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4788 // Because char has a smaller range than uchar, we can actually get away
4789 // without any newton steps. This requires that we use a weird bias
4790 // of 0xb000, however (again, this has been exhaustively tested).
4791 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4792 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4793 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4794 Y = DAG.getConstant(0xb000, MVT::i32);
4795 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4796 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4797 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4798 // Convert back to short.
4799 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4800 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4801 return X;
4802}
4803
Owen Anderson76706012011-04-05 21:48:57 +00004804static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004805LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4806 SDValue N2;
4807 // Convert to float.
4808 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4809 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4810 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4811 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4812 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4813 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004814
Nate Begeman7973f352011-02-11 20:53:29 +00004815 // Use reciprocal estimate and one refinement step.
4816 // float4 recip = vrecpeq_f32(yf);
4817 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004818 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004819 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004820 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004821 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4822 N1, N2);
4823 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4824 // Because short has a smaller range than ushort, we can actually get away
4825 // with only a single newton step. This requires that we use a weird bias
4826 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004827 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004828 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4829 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004830 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004831 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4832 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4833 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4834 // Convert back to integer and return.
4835 // return vmovn_s32(vcvt_s32_f32(result));
4836 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4837 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4838 return N0;
4839}
4840
4841static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4842 EVT VT = Op.getValueType();
4843 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4844 "unexpected type for custom-lowering ISD::SDIV");
4845
4846 DebugLoc dl = Op.getDebugLoc();
4847 SDValue N0 = Op.getOperand(0);
4848 SDValue N1 = Op.getOperand(1);
4849 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004850
Nate Begeman7973f352011-02-11 20:53:29 +00004851 if (VT == MVT::v8i8) {
4852 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4853 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004854
Nate Begeman7973f352011-02-11 20:53:29 +00004855 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4856 DAG.getIntPtrConstant(4));
4857 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004858 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004859 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4860 DAG.getIntPtrConstant(0));
4861 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4862 DAG.getIntPtrConstant(0));
4863
4864 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4865 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4866
4867 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4868 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004869
Nate Begeman7973f352011-02-11 20:53:29 +00004870 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
4871 return N0;
4872 }
4873 return LowerSDIV_v4i16(N0, N1, dl, DAG);
4874}
4875
4876static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
4877 EVT VT = Op.getValueType();
4878 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4879 "unexpected type for custom-lowering ISD::UDIV");
4880
4881 DebugLoc dl = Op.getDebugLoc();
4882 SDValue N0 = Op.getOperand(0);
4883 SDValue N1 = Op.getOperand(1);
4884 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004885
Nate Begeman7973f352011-02-11 20:53:29 +00004886 if (VT == MVT::v8i8) {
4887 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
4888 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004889
Nate Begeman7973f352011-02-11 20:53:29 +00004890 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4891 DAG.getIntPtrConstant(4));
4892 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004893 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004894 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4895 DAG.getIntPtrConstant(0));
4896 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4897 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00004898
Nate Begeman7973f352011-02-11 20:53:29 +00004899 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
4900 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00004901
Nate Begeman7973f352011-02-11 20:53:29 +00004902 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4903 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004904
4905 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00004906 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
4907 N0);
4908 return N0;
4909 }
Owen Anderson76706012011-04-05 21:48:57 +00004910
Nate Begeman7973f352011-02-11 20:53:29 +00004911 // v4i16 sdiv ... Convert to float.
4912 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
4913 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
4914 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
4915 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
4916 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004917 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00004918
4919 // Use reciprocal estimate and two refinement steps.
4920 // float4 recip = vrecpeq_f32(yf);
4921 // recip *= vrecpsq_f32(yf, recip);
4922 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004923 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004924 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00004925 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004926 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004927 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004928 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00004929 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004930 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004931 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004932 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4933 // Simply multiplying by the reciprocal estimate can leave us a few ulps
4934 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
4935 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004936 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00004937 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4938 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4939 N1 = DAG.getConstant(2, MVT::i32);
4940 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4941 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4942 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4943 // Convert back to integer and return.
4944 // return vmovn_u32(vcvt_s32_f32(result));
4945 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4946 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4947 return N0;
4948}
4949
Evan Cheng342e3162011-08-30 01:34:54 +00004950static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
4951 EVT VT = Op.getNode()->getValueType(0);
4952 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
4953
4954 unsigned Opc;
4955 bool ExtraOp = false;
4956 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00004957 default: llvm_unreachable("Invalid code");
Evan Cheng342e3162011-08-30 01:34:54 +00004958 case ISD::ADDC: Opc = ARMISD::ADDC; break;
4959 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
4960 case ISD::SUBC: Opc = ARMISD::SUBC; break;
4961 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
4962 }
4963
4964 if (!ExtraOp)
4965 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
4966 Op.getOperand(1));
4967 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
4968 Op.getOperand(1), Op.getOperand(2));
4969}
4970
Eli Friedman74bf18c2011-09-15 22:26:18 +00004971static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedman7cc15662011-09-15 22:18:49 +00004972 // Monotonic load/store is legal for all targets
4973 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
4974 return Op;
4975
4976 // Aquire/Release load/store is not legal for targets without a
4977 // dmb or equivalent available.
4978 return SDValue();
4979}
4980
4981
Eli Friedman2bdffe42011-08-31 00:31:29 +00004982static void
Eli Friedman4d3f3292011-08-31 17:52:22 +00004983ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
4984 SelectionDAG &DAG, unsigned NewOp) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00004985 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +00004986 assert (Node->getValueType(0) == MVT::i64 &&
4987 "Only know how to expand i64 atomics");
Eli Friedman2bdffe42011-08-31 00:31:29 +00004988
Eli Friedman4d3f3292011-08-31 17:52:22 +00004989 SmallVector<SDValue, 6> Ops;
4990 Ops.push_back(Node->getOperand(0)); // Chain
4991 Ops.push_back(Node->getOperand(1)); // Ptr
4992 // Low part of Val1
4993 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4994 Node->getOperand(2), DAG.getIntPtrConstant(0)));
4995 // High part of Val1
4996 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4997 Node->getOperand(2), DAG.getIntPtrConstant(1)));
Andrew Trick3af7a672011-09-20 03:06:13 +00004998 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) {
Eli Friedman4d3f3292011-08-31 17:52:22 +00004999 // High part of Val1
5000 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5001 Node->getOperand(3), DAG.getIntPtrConstant(0)));
5002 // High part of Val2
5003 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5004 Node->getOperand(3), DAG.getIntPtrConstant(1)));
5005 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005006 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
5007 SDValue Result =
Eli Friedman4d3f3292011-08-31 17:52:22 +00005008 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64,
Eli Friedman2bdffe42011-08-31 00:31:29 +00005009 cast<MemSDNode>(Node)->getMemOperand());
Eli Friedman4d3f3292011-08-31 17:52:22 +00005010 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) };
Eli Friedman2bdffe42011-08-31 00:31:29 +00005011 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
5012 Results.push_back(Result.getValue(2));
5013}
5014
Dan Gohmand858e902010-04-17 15:26:15 +00005015SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005016 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005017 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00005018 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00005019 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005020 case ISD::GlobalAddress:
5021 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
5022 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00005023 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00005024 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00005025 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
5026 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005027 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00005028 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00005029 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00005030 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00005031 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00005032 case ISD::SINT_TO_FP:
5033 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
5034 case ISD::FP_TO_SINT:
5035 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005036 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00005037 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00005038 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005039 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00005040 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00005041 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00005042 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
5043 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00005044 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005045 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00005046 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00005047 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00005048 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00005049 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00005050 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00005051 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Duncan Sands28b77e92011-09-06 19:07:46 +00005052 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00005053 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005054 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedman5c89cb82011-10-24 23:08:52 +00005055 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005056 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00005057 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00005058 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005059 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00005060 case ISD::SDIV: return LowerSDIV(Op, DAG);
5061 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00005062 case ISD::ADDC:
5063 case ISD::ADDE:
5064 case ISD::SUBC:
5065 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Eli Friedman7cc15662011-09-15 22:18:49 +00005066 case ISD::ATOMIC_LOAD:
Eli Friedman74bf18c2011-09-15 22:26:18 +00005067 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005068 }
Evan Chenga8e29892007-01-19 07:51:42 +00005069}
5070
Duncan Sands1607f052008-12-01 11:39:25 +00005071/// ReplaceNodeResults - Replace the results of node with an illegal result
5072/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00005073void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
5074 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005075 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00005076 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00005077 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00005078 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005079 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005080 case ISD::BITCAST:
5081 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005082 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005083 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00005084 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00005085 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005086 break;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005087 case ISD::ATOMIC_LOAD_ADD:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005088 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005089 return;
5090 case ISD::ATOMIC_LOAD_AND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005091 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005092 return;
5093 case ISD::ATOMIC_LOAD_NAND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005094 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005095 return;
5096 case ISD::ATOMIC_LOAD_OR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005097 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005098 return;
5099 case ISD::ATOMIC_LOAD_SUB:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005100 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005101 return;
5102 case ISD::ATOMIC_LOAD_XOR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005103 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005104 return;
5105 case ISD::ATOMIC_SWAP:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005106 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005107 return;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005108 case ISD::ATOMIC_CMP_SWAP:
5109 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG);
5110 return;
Duncan Sands1607f052008-12-01 11:39:25 +00005111 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00005112 if (Res.getNode())
5113 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00005114}
Chris Lattner27a6c732007-11-24 07:07:01 +00005115
Evan Chenga8e29892007-01-19 07:51:42 +00005116//===----------------------------------------------------------------------===//
5117// ARM Scheduler Hooks
5118//===----------------------------------------------------------------------===//
5119
5120MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005121ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
5122 MachineBasicBlock *BB,
5123 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005124 unsigned dest = MI->getOperand(0).getReg();
5125 unsigned ptr = MI->getOperand(1).getReg();
5126 unsigned oldval = MI->getOperand(2).getReg();
5127 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005128 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5129 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005130 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005131
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005132 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5133 unsigned scratch =
Cameron Zwarich141ec632011-05-18 02:29:50 +00005134 MRI.createVirtualRegister(isThumb2 ? ARM::rGPRRegisterClass
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005135 : ARM::GPRRegisterClass);
5136
5137 if (isThumb2) {
Cameron Zwarich141ec632011-05-18 02:29:50 +00005138 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5139 MRI.constrainRegClass(oldval, ARM::rGPRRegisterClass);
5140 MRI.constrainRegClass(newval, ARM::rGPRRegisterClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005141 }
5142
Jim Grosbach5278eb82009-12-11 01:42:04 +00005143 unsigned ldrOpc, strOpc;
5144 switch (Size) {
5145 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005146 case 1:
5147 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00005148 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005149 break;
5150 case 2:
5151 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5152 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5153 break;
5154 case 4:
5155 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5156 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5157 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00005158 }
5159
5160 MachineFunction *MF = BB->getParent();
5161 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5162 MachineFunction::iterator It = BB;
5163 ++It; // insert the new blocks after the current block
5164
5165 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5166 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5167 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5168 MF->insert(It, loop1MBB);
5169 MF->insert(It, loop2MBB);
5170 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005171
5172 // Transfer the remainder of BB and its successor edges to exitMBB.
5173 exitMBB->splice(exitMBB->begin(), BB,
5174 llvm::next(MachineBasicBlock::iterator(MI)),
5175 BB->end());
5176 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005177
5178 // thisMBB:
5179 // ...
5180 // fallthrough --> loop1MBB
5181 BB->addSuccessor(loop1MBB);
5182
5183 // loop1MBB:
5184 // ldrex dest, [ptr]
5185 // cmp dest, oldval
5186 // bne exitMBB
5187 BB = loop1MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005188 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5189 if (ldrOpc == ARM::t2LDREX)
5190 MIB.addImm(0);
5191 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005192 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005193 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005194 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5195 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005196 BB->addSuccessor(loop2MBB);
5197 BB->addSuccessor(exitMBB);
5198
5199 // loop2MBB:
5200 // strex scratch, newval, [ptr]
5201 // cmp scratch, #0
5202 // bne loop1MBB
5203 BB = loop2MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005204 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr);
5205 if (strOpc == ARM::t2STREX)
5206 MIB.addImm(0);
5207 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005208 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005209 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005210 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5211 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005212 BB->addSuccessor(loop1MBB);
5213 BB->addSuccessor(exitMBB);
5214
5215 // exitMBB:
5216 // ...
5217 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005218
Dan Gohman14152b42010-07-06 20:24:04 +00005219 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005220
Jim Grosbach5278eb82009-12-11 01:42:04 +00005221 return BB;
5222}
5223
5224MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005225ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5226 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005227 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5228 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5229
5230 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005231 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005232 MachineFunction::iterator It = BB;
5233 ++It;
5234
5235 unsigned dest = MI->getOperand(0).getReg();
5236 unsigned ptr = MI->getOperand(1).getReg();
5237 unsigned incr = MI->getOperand(2).getReg();
5238 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005239 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005240
5241 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5242 if (isThumb2) {
5243 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5244 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5245 }
5246
Jim Grosbachc3c23542009-12-14 04:22:04 +00005247 unsigned ldrOpc, strOpc;
5248 switch (Size) {
5249 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005250 case 1:
5251 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005252 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005253 break;
5254 case 2:
5255 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5256 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5257 break;
5258 case 4:
5259 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5260 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5261 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005262 }
5263
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005264 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5265 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5266 MF->insert(It, loopMBB);
5267 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005268
5269 // Transfer the remainder of BB and its successor edges to exitMBB.
5270 exitMBB->splice(exitMBB->begin(), BB,
5271 llvm::next(MachineBasicBlock::iterator(MI)),
5272 BB->end());
5273 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005274
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005275 TargetRegisterClass *TRC =
5276 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5277 unsigned scratch = MRI.createVirtualRegister(TRC);
5278 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005279
5280 // thisMBB:
5281 // ...
5282 // fallthrough --> loopMBB
5283 BB->addSuccessor(loopMBB);
5284
5285 // loopMBB:
5286 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005287 // <binop> scratch2, dest, incr
5288 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005289 // cmp scratch, #0
5290 // bne- loopMBB
5291 // fallthrough --> exitMBB
5292 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005293 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5294 if (ldrOpc == ARM::t2LDREX)
5295 MIB.addImm(0);
5296 AddDefaultPred(MIB);
Jim Grosbachc67b5562009-12-15 00:12:35 +00005297 if (BinOpcode) {
5298 // operand order needs to go the other way for NAND
5299 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5300 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5301 addReg(incr).addReg(dest)).addReg(0);
5302 else
5303 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5304 addReg(dest).addReg(incr)).addReg(0);
5305 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005306
Jim Grosbachb6aed502011-09-09 18:37:27 +00005307 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5308 if (strOpc == ARM::t2STREX)
5309 MIB.addImm(0);
5310 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005311 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005312 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005313 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5314 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005315
5316 BB->addSuccessor(loopMBB);
5317 BB->addSuccessor(exitMBB);
5318
5319 // exitMBB:
5320 // ...
5321 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005322
Dan Gohman14152b42010-07-06 20:24:04 +00005323 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005324
Jim Grosbachc3c23542009-12-14 04:22:04 +00005325 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005326}
5327
Jim Grosbachf7da8822011-04-26 19:44:18 +00005328MachineBasicBlock *
5329ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5330 MachineBasicBlock *BB,
5331 unsigned Size,
5332 bool signExtend,
5333 ARMCC::CondCodes Cond) const {
5334 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5335
5336 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5337 MachineFunction *MF = BB->getParent();
5338 MachineFunction::iterator It = BB;
5339 ++It;
5340
5341 unsigned dest = MI->getOperand(0).getReg();
5342 unsigned ptr = MI->getOperand(1).getReg();
5343 unsigned incr = MI->getOperand(2).getReg();
5344 unsigned oldval = dest;
5345 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005346 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005347
5348 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5349 if (isThumb2) {
5350 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5351 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5352 }
5353
Jim Grosbachf7da8822011-04-26 19:44:18 +00005354 unsigned ldrOpc, strOpc, extendOpc;
5355 switch (Size) {
5356 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5357 case 1:
5358 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5359 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005360 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005361 break;
5362 case 2:
5363 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5364 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005365 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005366 break;
5367 case 4:
5368 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5369 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5370 extendOpc = 0;
5371 break;
5372 }
5373
5374 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5375 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5376 MF->insert(It, loopMBB);
5377 MF->insert(It, exitMBB);
5378
5379 // Transfer the remainder of BB and its successor edges to exitMBB.
5380 exitMBB->splice(exitMBB->begin(), BB,
5381 llvm::next(MachineBasicBlock::iterator(MI)),
5382 BB->end());
5383 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5384
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005385 TargetRegisterClass *TRC =
5386 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5387 unsigned scratch = MRI.createVirtualRegister(TRC);
5388 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005389
5390 // thisMBB:
5391 // ...
5392 // fallthrough --> loopMBB
5393 BB->addSuccessor(loopMBB);
5394
5395 // loopMBB:
5396 // ldrex dest, ptr
5397 // (sign extend dest, if required)
5398 // cmp dest, incr
5399 // cmov.cond scratch2, dest, incr
5400 // strex scratch, scratch2, ptr
5401 // cmp scratch, #0
5402 // bne- loopMBB
5403 // fallthrough --> exitMBB
5404 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005405 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5406 if (ldrOpc == ARM::t2LDREX)
5407 MIB.addImm(0);
5408 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005409
5410 // Sign extend the value, if necessary.
5411 if (signExtend && extendOpc) {
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005412 oldval = MRI.createVirtualRegister(ARM::GPRRegisterClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005413 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5414 .addReg(dest)
5415 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005416 }
5417
5418 // Build compare and cmov instructions.
5419 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5420 .addReg(oldval).addReg(incr));
5421 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5422 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5423
Jim Grosbachb6aed502011-09-09 18:37:27 +00005424 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5425 if (strOpc == ARM::t2STREX)
5426 MIB.addImm(0);
5427 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005428 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5429 .addReg(scratch).addImm(0));
5430 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5431 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5432
5433 BB->addSuccessor(loopMBB);
5434 BB->addSuccessor(exitMBB);
5435
5436 // exitMBB:
5437 // ...
5438 BB = exitMBB;
5439
5440 MI->eraseFromParent(); // The instruction is gone now.
5441
5442 return BB;
5443}
5444
Eli Friedman2bdffe42011-08-31 00:31:29 +00005445MachineBasicBlock *
5446ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
5447 unsigned Op1, unsigned Op2,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005448 bool NeedsCarry, bool IsCmpxchg) const {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005449 // This also handles ATOMIC_SWAP, indicated by Op1==0.
5450 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5451
5452 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5453 MachineFunction *MF = BB->getParent();
5454 MachineFunction::iterator It = BB;
5455 ++It;
5456
5457 unsigned destlo = MI->getOperand(0).getReg();
5458 unsigned desthi = MI->getOperand(1).getReg();
5459 unsigned ptr = MI->getOperand(2).getReg();
5460 unsigned vallo = MI->getOperand(3).getReg();
5461 unsigned valhi = MI->getOperand(4).getReg();
5462 DebugLoc dl = MI->getDebugLoc();
5463 bool isThumb2 = Subtarget->isThumb2();
5464
5465 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5466 if (isThumb2) {
5467 MRI.constrainRegClass(destlo, ARM::rGPRRegisterClass);
5468 MRI.constrainRegClass(desthi, ARM::rGPRRegisterClass);
5469 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5470 }
5471
5472 unsigned ldrOpc = isThumb2 ? ARM::t2LDREXD : ARM::LDREXD;
5473 unsigned strOpc = isThumb2 ? ARM::t2STREXD : ARM::STREXD;
5474
5475 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman7df496d2011-09-01 22:27:41 +00005476 MachineBasicBlock *contBB = 0, *cont2BB = 0;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005477 if (IsCmpxchg) {
5478 contBB = MF->CreateMachineBasicBlock(LLVM_BB);
5479 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB);
5480 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005481 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5482 MF->insert(It, loopMBB);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005483 if (IsCmpxchg) {
5484 MF->insert(It, contBB);
5485 MF->insert(It, cont2BB);
5486 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005487 MF->insert(It, exitMBB);
5488
5489 // Transfer the remainder of BB and its successor edges to exitMBB.
5490 exitMBB->splice(exitMBB->begin(), BB,
5491 llvm::next(MachineBasicBlock::iterator(MI)),
5492 BB->end());
5493 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5494
5495 TargetRegisterClass *TRC =
5496 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5497 unsigned storesuccess = MRI.createVirtualRegister(TRC);
5498
5499 // thisMBB:
5500 // ...
5501 // fallthrough --> loopMBB
5502 BB->addSuccessor(loopMBB);
5503
5504 // loopMBB:
5505 // ldrexd r2, r3, ptr
5506 // <binopa> r0, r2, incr
5507 // <binopb> r1, r3, incr
5508 // strexd storesuccess, r0, r1, ptr
5509 // cmp storesuccess, #0
5510 // bne- loopMBB
5511 // fallthrough --> exitMBB
5512 //
5513 // Note that the registers are explicitly specified because there is not any
5514 // way to force the register allocator to allocate a register pair.
5515 //
Andrew Trick3af7a672011-09-20 03:06:13 +00005516 // FIXME: The hardcoded registers are not necessary for Thumb2, but we
Eli Friedman2bdffe42011-08-31 00:31:29 +00005517 // need to properly enforce the restriction that the two output registers
5518 // for ldrexd must be different.
5519 BB = loopMBB;
5520 // Load
5521 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc))
5522 .addReg(ARM::R2, RegState::Define)
5523 .addReg(ARM::R3, RegState::Define).addReg(ptr));
5524 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
5525 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo).addReg(ARM::R2);
5526 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi).addReg(ARM::R3);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005527
5528 if (IsCmpxchg) {
5529 // Add early exit
5530 for (unsigned i = 0; i < 2; i++) {
5531 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr :
5532 ARM::CMPrr))
5533 .addReg(i == 0 ? destlo : desthi)
5534 .addReg(i == 0 ? vallo : valhi));
5535 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5536 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5537 BB->addSuccessor(exitMBB);
5538 BB->addSuccessor(i == 0 ? contBB : cont2BB);
5539 BB = (i == 0 ? contBB : cont2BB);
5540 }
5541
5542 // Copy to physregs for strexd
5543 unsigned setlo = MI->getOperand(5).getReg();
5544 unsigned sethi = MI->getOperand(6).getReg();
5545 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(setlo);
5546 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(sethi);
5547 } else if (Op1) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005548 // Perform binary operation
5549 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), ARM::R0)
5550 .addReg(destlo).addReg(vallo))
5551 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
5552 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), ARM::R1)
5553 .addReg(desthi).addReg(valhi)).addReg(0);
5554 } else {
5555 // Copy to physregs for strexd
5556 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(vallo);
5557 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(valhi);
5558 }
5559
5560 // Store
5561 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), storesuccess)
5562 .addReg(ARM::R0).addReg(ARM::R1).addReg(ptr));
5563 // Cmp+jump
5564 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5565 .addReg(storesuccess).addImm(0));
5566 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5567 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5568
5569 BB->addSuccessor(loopMBB);
5570 BB->addSuccessor(exitMBB);
5571
5572 // exitMBB:
5573 // ...
5574 BB = exitMBB;
5575
5576 MI->eraseFromParent(); // The instruction is gone now.
5577
5578 return BB;
5579}
5580
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005581/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
5582/// registers the function context.
5583void ARMTargetLowering::
5584SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
5585 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005586 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5587 DebugLoc dl = MI->getDebugLoc();
5588 MachineFunction *MF = MBB->getParent();
5589 MachineRegisterInfo *MRI = &MF->getRegInfo();
5590 MachineConstantPool *MCP = MF->getConstantPool();
5591 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5592 const Function *F = MF->getFunction();
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005593
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005594 bool isThumb = Subtarget->isThumb();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005595 bool isThumb2 = Subtarget->isThumb2();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005596
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005597 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005598 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005599 ARMConstantPoolValue *CPV =
5600 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
5601 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
5602
5603 const TargetRegisterClass *TRC =
5604 isThumb ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5605
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005606 // Grab constant pool and fixed stack memory operands.
5607 MachineMemOperand *CPMMO =
5608 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
5609 MachineMemOperand::MOLoad, 4, 4);
5610
5611 MachineMemOperand *FIMMOSt =
5612 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
5613 MachineMemOperand::MOStore, 4, 4);
5614
5615 // Load the address of the dispatch MBB into the jump buffer.
5616 if (isThumb2) {
5617 // Incoming value: jbuf
5618 // ldr.n r5, LCPI1_1
5619 // orr r5, r5, #1
5620 // add r5, pc
5621 // str r5, [$jbuf, #+4] ; &jbuf[1]
5622 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5623 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
5624 .addConstantPoolIndex(CPI)
5625 .addMemOperand(CPMMO));
5626 // Set the low bit because of thumb mode.
5627 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5628 AddDefaultCC(
5629 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
5630 .addReg(NewVReg1, RegState::Kill)
5631 .addImm(0x01)));
5632 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5633 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
5634 .addReg(NewVReg2, RegState::Kill)
5635 .addImm(PCLabelId);
5636 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
5637 .addReg(NewVReg3, RegState::Kill)
5638 .addFrameIndex(FI)
5639 .addImm(36) // &jbuf[1] :: pc
5640 .addMemOperand(FIMMOSt));
5641 } else if (isThumb) {
5642 // Incoming value: jbuf
5643 // ldr.n r1, LCPI1_4
5644 // add r1, pc
5645 // mov r2, #1
5646 // orrs r1, r2
5647 // add r2, $jbuf, #+4 ; &jbuf[1]
5648 // str r1, [r2]
5649 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5650 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
5651 .addConstantPoolIndex(CPI)
5652 .addMemOperand(CPMMO));
5653 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5654 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
5655 .addReg(NewVReg1, RegState::Kill)
5656 .addImm(PCLabelId);
5657 // Set the low bit because of thumb mode.
5658 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5659 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
5660 .addReg(ARM::CPSR, RegState::Define)
5661 .addImm(1));
5662 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5663 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
5664 .addReg(ARM::CPSR, RegState::Define)
5665 .addReg(NewVReg2, RegState::Kill)
5666 .addReg(NewVReg3, RegState::Kill));
5667 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5668 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
5669 .addFrameIndex(FI)
5670 .addImm(36)); // &jbuf[1] :: pc
5671 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
5672 .addReg(NewVReg4, RegState::Kill)
5673 .addReg(NewVReg5, RegState::Kill)
5674 .addImm(0)
5675 .addMemOperand(FIMMOSt));
5676 } else {
5677 // Incoming value: jbuf
5678 // ldr r1, LCPI1_1
5679 // add r1, pc, r1
5680 // str r1, [$jbuf, #+4] ; &jbuf[1]
5681 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5682 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
5683 .addConstantPoolIndex(CPI)
5684 .addImm(0)
5685 .addMemOperand(CPMMO));
5686 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5687 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
5688 .addReg(NewVReg1, RegState::Kill)
5689 .addImm(PCLabelId));
5690 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
5691 .addReg(NewVReg2, RegState::Kill)
5692 .addFrameIndex(FI)
5693 .addImm(36) // &jbuf[1] :: pc
5694 .addMemOperand(FIMMOSt));
5695 }
5696}
5697
5698MachineBasicBlock *ARMTargetLowering::
5699EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
5700 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5701 DebugLoc dl = MI->getDebugLoc();
5702 MachineFunction *MF = MBB->getParent();
5703 MachineRegisterInfo *MRI = &MF->getRegInfo();
5704 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5705 MachineFrameInfo *MFI = MF->getFrameInfo();
5706 int FI = MFI->getFunctionContextIndex();
5707
5708 const TargetRegisterClass *TRC =
5709 Subtarget->isThumb() ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5710
Bill Wendling04f15b42011-10-06 21:29:56 +00005711 // Get a mapping of the call site numbers to all of the landing pads they're
5712 // associated with.
Bill Wendling2a850152011-10-05 00:02:33 +00005713 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
5714 unsigned MaxCSNum = 0;
5715 MachineModuleInfo &MMI = MF->getMMI();
5716 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E; ++BB) {
5717 if (!BB->isLandingPad()) continue;
5718
5719 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
5720 // pad.
5721 for (MachineBasicBlock::iterator
5722 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
5723 if (!II->isEHLabel()) continue;
5724
5725 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendling5cbef192011-10-05 23:28:57 +00005726 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling2a850152011-10-05 00:02:33 +00005727
Bill Wendling5cbef192011-10-05 23:28:57 +00005728 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
5729 for (SmallVectorImpl<unsigned>::iterator
5730 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
5731 CSI != CSE; ++CSI) {
5732 CallSiteNumToLPad[*CSI].push_back(BB);
5733 MaxCSNum = std::max(MaxCSNum, *CSI);
5734 }
Bill Wendling2a850152011-10-05 00:02:33 +00005735 break;
5736 }
5737 }
5738
5739 // Get an ordered list of the machine basic blocks for the jump table.
5740 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling2acf6382011-10-07 23:18:02 +00005741 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling2a850152011-10-05 00:02:33 +00005742 LPadList.reserve(CallSiteNumToLPad.size());
5743 for (unsigned I = 1; I <= MaxCSNum; ++I) {
5744 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
5745 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00005746 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling2a850152011-10-05 00:02:33 +00005747 LPadList.push_back(*II);
Bill Wendling2acf6382011-10-07 23:18:02 +00005748 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
5749 }
Bill Wendling2a850152011-10-05 00:02:33 +00005750 }
5751
Bill Wendling5cbef192011-10-05 23:28:57 +00005752 assert(!LPadList.empty() &&
5753 "No landing pad destinations for the dispatch jump table!");
5754
Bill Wendling04f15b42011-10-06 21:29:56 +00005755 // Create the jump table and associated information.
Bill Wendling2a850152011-10-05 00:02:33 +00005756 MachineJumpTableInfo *JTI =
5757 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
5758 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
5759 unsigned UId = AFI->createJumpTableUId();
5760
Bill Wendling04f15b42011-10-06 21:29:56 +00005761 // Create the MBBs for the dispatch code.
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005762
5763 // Shove the dispatch's address into the return slot in the function context.
5764 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
5765 DispatchBB->setIsLandingPad();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005766
Bill Wendlingbb734682011-10-05 00:39:32 +00005767 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Bill Wendling083a8eb2011-10-06 23:37:36 +00005768 BuildMI(TrapBB, dl, TII->get(Subtarget->isThumb() ? ARM::tTRAP : ARM::TRAP));
Bill Wendlingbb734682011-10-05 00:39:32 +00005769 DispatchBB->addSuccessor(TrapBB);
5770
5771 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
5772 DispatchBB->addSuccessor(DispContBB);
Bill Wendling2a850152011-10-05 00:02:33 +00005773
Bill Wendlinga48ed4f2011-10-17 21:32:56 +00005774 // Insert and MBBs.
Bill Wendling930193c2011-10-06 00:53:33 +00005775 MF->insert(MF->end(), DispatchBB);
5776 MF->insert(MF->end(), DispContBB);
5777 MF->insert(MF->end(), TrapBB);
Bill Wendling930193c2011-10-06 00:53:33 +00005778
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005779 // Insert code into the entry block that creates and registers the function
5780 // context.
5781 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
5782
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005783 MachineMemOperand *FIMMOLd =
Bill Wendling04f15b42011-10-06 21:29:56 +00005784 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendling083a8eb2011-10-06 23:37:36 +00005785 MachineMemOperand::MOLoad |
5786 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling930193c2011-10-06 00:53:33 +00005787
Bob Wilsonf4aea8f2011-12-22 23:39:48 +00005788 if (AFI->isThumb1OnlyFunction())
5789 BuildMI(DispatchBB, dl, TII->get(ARM::tInt_eh_sjlj_dispatchsetup));
5790 else if (!Subtarget->hasVFP2())
5791 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup_nofp));
5792 else
5793 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
Bob Wilsoneaab6ef2011-11-16 07:11:57 +00005794
Bill Wendling952cb502011-10-18 22:49:07 +00005795 unsigned NumLPads = LPadList.size();
Bill Wendling95ce2e92011-10-06 22:53:00 +00005796 if (Subtarget->isThumb2()) {
5797 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5798 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
5799 .addFrameIndex(FI)
5800 .addImm(4)
5801 .addMemOperand(FIMMOLd));
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005802
Bill Wendling952cb502011-10-18 22:49:07 +00005803 if (NumLPads < 256) {
5804 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
5805 .addReg(NewVReg1)
5806 .addImm(LPadList.size()));
5807 } else {
5808 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5809 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00005810 .addImm(NumLPads & 0xFFFF));
5811
5812 unsigned VReg2 = VReg1;
5813 if ((NumLPads & 0xFFFF0000) != 0) {
5814 VReg2 = MRI->createVirtualRegister(TRC);
5815 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
5816 .addReg(VReg1)
5817 .addImm(NumLPads >> 16));
5818 }
5819
Bill Wendling952cb502011-10-18 22:49:07 +00005820 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
5821 .addReg(NewVReg1)
5822 .addReg(VReg2));
5823 }
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005824
Bill Wendling95ce2e92011-10-06 22:53:00 +00005825 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
5826 .addMBB(TrapBB)
5827 .addImm(ARMCC::HI)
5828 .addReg(ARM::CPSR);
Bill Wendlingbb734682011-10-05 00:39:32 +00005829
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005830 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5831 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005832 .addJumpTableIndex(MJTI)
5833 .addImm(UId));
Bill Wendling2a850152011-10-05 00:02:33 +00005834
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005835 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005836 AddDefaultCC(
5837 AddDefaultPred(
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005838 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
5839 .addReg(NewVReg3, RegState::Kill)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005840 .addReg(NewVReg1)
5841 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
5842
5843 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005844 .addReg(NewVReg4, RegState::Kill)
Bill Wendling2a850152011-10-05 00:02:33 +00005845 .addReg(NewVReg1)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005846 .addJumpTableIndex(MJTI)
5847 .addImm(UId);
5848 } else if (Subtarget->isThumb()) {
Bill Wendling083a8eb2011-10-06 23:37:36 +00005849 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5850 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
5851 .addFrameIndex(FI)
5852 .addImm(1)
5853 .addMemOperand(FIMMOLd));
Bill Wendlingf1083d42011-10-07 22:08:37 +00005854
Bill Wendlinga5871dc2011-10-18 23:11:05 +00005855 if (NumLPads < 256) {
5856 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
5857 .addReg(NewVReg1)
5858 .addImm(NumLPads));
5859 } else {
5860 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling922ad782011-10-19 09:24:02 +00005861 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
5862 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
5863
5864 // MachineConstantPool wants an explicit alignment.
5865 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
5866 if (Align == 0)
5867 Align = getTargetData()->getTypeAllocSize(C->getType());
5868 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendlinga5871dc2011-10-18 23:11:05 +00005869
5870 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5871 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
5872 .addReg(VReg1, RegState::Define)
5873 .addConstantPoolIndex(Idx));
5874 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
5875 .addReg(NewVReg1)
5876 .addReg(VReg1));
5877 }
5878
Bill Wendling083a8eb2011-10-06 23:37:36 +00005879 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
5880 .addMBB(TrapBB)
5881 .addImm(ARMCC::HI)
5882 .addReg(ARM::CPSR);
5883
5884 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5885 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
5886 .addReg(ARM::CPSR, RegState::Define)
5887 .addReg(NewVReg1)
5888 .addImm(2));
5889
5890 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling217f0e92011-10-06 23:41:14 +00005891 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendling083a8eb2011-10-06 23:37:36 +00005892 .addJumpTableIndex(MJTI)
5893 .addImm(UId));
5894
5895 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5896 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
5897 .addReg(ARM::CPSR, RegState::Define)
5898 .addReg(NewVReg2, RegState::Kill)
5899 .addReg(NewVReg3));
5900
5901 MachineMemOperand *JTMMOLd =
5902 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
5903 MachineMemOperand::MOLoad, 4, 4);
5904
5905 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5906 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
5907 .addReg(NewVReg4, RegState::Kill)
5908 .addImm(0)
5909 .addMemOperand(JTMMOLd));
5910
5911 unsigned NewVReg6 = MRI->createVirtualRegister(TRC);
5912 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
5913 .addReg(ARM::CPSR, RegState::Define)
5914 .addReg(NewVReg5, RegState::Kill)
5915 .addReg(NewVReg3));
5916
5917 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
5918 .addReg(NewVReg6, RegState::Kill)
5919 .addJumpTableIndex(MJTI)
5920 .addImm(UId);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005921 } else {
5922 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5923 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
5924 .addFrameIndex(FI)
5925 .addImm(4)
5926 .addMemOperand(FIMMOLd));
Bill Wendling564392b2011-10-18 22:11:18 +00005927
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005928 if (NumLPads < 256) {
5929 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
5930 .addReg(NewVReg1)
5931 .addImm(NumLPads));
Bill Wendling922ad782011-10-19 09:24:02 +00005932 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005933 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5934 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00005935 .addImm(NumLPads & 0xFFFF));
5936
5937 unsigned VReg2 = VReg1;
5938 if ((NumLPads & 0xFFFF0000) != 0) {
5939 VReg2 = MRI->createVirtualRegister(TRC);
5940 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
5941 .addReg(VReg1)
5942 .addImm(NumLPads >> 16));
5943 }
5944
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005945 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
5946 .addReg(NewVReg1)
5947 .addReg(VReg2));
Bill Wendling922ad782011-10-19 09:24:02 +00005948 } else {
5949 MachineConstantPool *ConstantPool = MF->getConstantPool();
5950 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
5951 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
5952
5953 // MachineConstantPool wants an explicit alignment.
5954 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
5955 if (Align == 0)
5956 Align = getTargetData()->getTypeAllocSize(C->getType());
5957 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
5958
5959 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5960 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
5961 .addReg(VReg1, RegState::Define)
Bill Wendling767f8be2011-10-20 20:37:11 +00005962 .addConstantPoolIndex(Idx)
5963 .addImm(0));
Bill Wendling922ad782011-10-19 09:24:02 +00005964 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
5965 .addReg(NewVReg1)
5966 .addReg(VReg1, RegState::Kill));
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005967 }
5968
Bill Wendling95ce2e92011-10-06 22:53:00 +00005969 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
5970 .addMBB(TrapBB)
5971 .addImm(ARMCC::HI)
5972 .addReg(ARM::CPSR);
Bill Wendling2a850152011-10-05 00:02:33 +00005973
Bill Wendling564392b2011-10-18 22:11:18 +00005974 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005975 AddDefaultCC(
Bill Wendling564392b2011-10-18 22:11:18 +00005976 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005977 .addReg(NewVReg1)
5978 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling564392b2011-10-18 22:11:18 +00005979 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5980 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005981 .addJumpTableIndex(MJTI)
5982 .addImm(UId));
5983
5984 MachineMemOperand *JTMMOLd =
5985 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
5986 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling564392b2011-10-18 22:11:18 +00005987 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005988 AddDefaultPred(
Bill Wendling564392b2011-10-18 22:11:18 +00005989 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
5990 .addReg(NewVReg3, RegState::Kill)
5991 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005992 .addImm(0)
5993 .addMemOperand(JTMMOLd));
5994
5995 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
Bill Wendling564392b2011-10-18 22:11:18 +00005996 .addReg(NewVReg5, RegState::Kill)
5997 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005998 .addJumpTableIndex(MJTI)
5999 .addImm(UId);
6000 }
Bill Wendling2a850152011-10-05 00:02:33 +00006001
Bill Wendlingbb734682011-10-05 00:39:32 +00006002 // Add the jump table entries as successors to the MBB.
Bill Wendling2acf6382011-10-07 23:18:02 +00006003 MachineBasicBlock *PrevMBB = 0;
Bill Wendlingbb734682011-10-05 00:39:32 +00006004 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006005 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6006 MachineBasicBlock *CurMBB = *I;
6007 if (PrevMBB != CurMBB)
6008 DispContBB->addSuccessor(CurMBB);
6009 PrevMBB = CurMBB;
6010 }
6011
Bill Wendling24bb9252011-10-17 05:25:09 +00006012 // N.B. the order the invoke BBs are processed in doesn't matter here.
Bill Wendling969c9ef2011-10-14 23:34:37 +00006013 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6014 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
6015 const unsigned *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006016 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling2acf6382011-10-07 23:18:02 +00006017 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6018 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6019 MachineBasicBlock *BB = *I;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006020
6021 // Remove the landing pad successor from the invoke block and replace it
6022 // with the new dispatch block.
Bill Wendlingde39d862011-10-26 07:16:18 +00006023 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6024 BB->succ_end());
6025 while (!Successors.empty()) {
6026 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling2acf6382011-10-07 23:18:02 +00006027 if (SMBB->isLandingPad()) {
6028 BB->removeSuccessor(SMBB);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006029 MBBLPads.push_back(SMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006030 }
6031 }
6032
6033 BB->addSuccessor(DispatchBB);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006034
6035 // Find the invoke call and mark all of the callee-saved registers as
6036 // 'implicit defined' so that they're spilled. This prevents code from
6037 // moving instructions to before the EH block, where they will never be
6038 // executed.
6039 for (MachineBasicBlock::reverse_iterator
6040 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006041 if (!II->isCall()) continue;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006042
6043 DenseMap<unsigned, bool> DefRegs;
6044 for (MachineInstr::mop_iterator
6045 OI = II->operands_begin(), OE = II->operands_end();
6046 OI != OE; ++OI) {
6047 if (!OI->isReg()) continue;
6048 DefRegs[OI->getReg()] = true;
6049 }
6050
6051 MachineInstrBuilder MIB(&*II);
6052
Bill Wendling5d798592011-10-14 23:55:44 +00006053 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006054 unsigned Reg = SavedRegs[i];
6055 if (Subtarget->isThumb2() &&
6056 !ARM::tGPRRegisterClass->contains(Reg) &&
6057 !ARM::hGPRRegisterClass->contains(Reg))
6058 continue;
6059 else if (Subtarget->isThumb1Only() &&
6060 !ARM::tGPRRegisterClass->contains(Reg))
6061 continue;
6062 else if (!Subtarget->isThumb() &&
6063 !ARM::GPRRegisterClass->contains(Reg))
6064 continue;
6065 if (!DefRegs[Reg])
6066 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling5d798592011-10-14 23:55:44 +00006067 }
Bill Wendling969c9ef2011-10-14 23:34:37 +00006068
6069 break;
6070 }
Bill Wendling2acf6382011-10-07 23:18:02 +00006071 }
Bill Wendlingbb734682011-10-05 00:39:32 +00006072
Bill Wendlingf7b02072011-10-18 18:30:49 +00006073 // Mark all former landing pads as non-landing pads. The dispatch is the only
6074 // landing pad now.
6075 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6076 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6077 (*I)->setIsLandingPad(false);
6078
Bill Wendlingbb734682011-10-05 00:39:32 +00006079 // The instruction is gone now.
6080 MI->eraseFromParent();
6081
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006082 return MBB;
6083}
6084
Evan Cheng218977b2010-07-13 19:27:42 +00006085static
6086MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6087 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6088 E = MBB->succ_end(); I != E; ++I)
6089 if (*I != Succ)
6090 return *I;
6091 llvm_unreachable("Expecting a BB with two successors!");
6092}
6093
Jim Grosbache801dc42009-12-12 01:40:06 +00006094MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006095ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00006096 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006097 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00006098 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006099 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00006100 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00006101 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00006102 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00006103 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00006104 }
Jim Grosbachee2c2a42011-09-16 21:55:56 +00006105 // The Thumb2 pre-indexed stores have the same MI operands, they just
6106 // define them differently in the .td files from the isel patterns, so
6107 // they need pseudos.
6108 case ARM::t2STR_preidx:
6109 MI->setDesc(TII->get(ARM::t2STR_PRE));
6110 return BB;
6111 case ARM::t2STRB_preidx:
6112 MI->setDesc(TII->get(ARM::t2STRB_PRE));
6113 return BB;
6114 case ARM::t2STRH_preidx:
6115 MI->setDesc(TII->get(ARM::t2STRH_PRE));
6116 return BB;
6117
Jim Grosbach19dec202011-08-05 20:35:44 +00006118 case ARM::STRi_preidx:
6119 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00006120 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00006121 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
6122 // Decode the offset.
6123 unsigned Offset = MI->getOperand(4).getImm();
6124 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
6125 Offset = ARM_AM::getAM2Offset(Offset);
6126 if (isSub)
6127 Offset = -Offset;
6128
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006129 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00006130 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00006131 .addOperand(MI->getOperand(0)) // Rn_wb
6132 .addOperand(MI->getOperand(1)) // Rt
6133 .addOperand(MI->getOperand(2)) // Rn
6134 .addImm(Offset) // offset (skip GPR==zero_reg)
6135 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006136 .addOperand(MI->getOperand(6))
6137 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00006138 MI->eraseFromParent();
6139 return BB;
6140 }
6141 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00006142 case ARM::STRBr_preidx:
6143 case ARM::STRH_preidx: {
6144 unsigned NewOpc;
6145 switch (MI->getOpcode()) {
6146 default: llvm_unreachable("unexpected opcode!");
6147 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
6148 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
6149 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
6150 }
Jim Grosbach19dec202011-08-05 20:35:44 +00006151 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
6152 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
6153 MIB.addOperand(MI->getOperand(i));
6154 MI->eraseFromParent();
6155 return BB;
6156 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006157 case ARM::ATOMIC_LOAD_ADD_I8:
6158 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6159 case ARM::ATOMIC_LOAD_ADD_I16:
6160 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6161 case ARM::ATOMIC_LOAD_ADD_I32:
6162 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006163
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006164 case ARM::ATOMIC_LOAD_AND_I8:
6165 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6166 case ARM::ATOMIC_LOAD_AND_I16:
6167 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6168 case ARM::ATOMIC_LOAD_AND_I32:
6169 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006170
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006171 case ARM::ATOMIC_LOAD_OR_I8:
6172 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6173 case ARM::ATOMIC_LOAD_OR_I16:
6174 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6175 case ARM::ATOMIC_LOAD_OR_I32:
6176 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006177
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006178 case ARM::ATOMIC_LOAD_XOR_I8:
6179 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6180 case ARM::ATOMIC_LOAD_XOR_I16:
6181 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6182 case ARM::ATOMIC_LOAD_XOR_I32:
6183 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006184
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006185 case ARM::ATOMIC_LOAD_NAND_I8:
6186 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6187 case ARM::ATOMIC_LOAD_NAND_I16:
6188 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6189 case ARM::ATOMIC_LOAD_NAND_I32:
6190 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006191
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006192 case ARM::ATOMIC_LOAD_SUB_I8:
6193 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6194 case ARM::ATOMIC_LOAD_SUB_I16:
6195 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6196 case ARM::ATOMIC_LOAD_SUB_I32:
6197 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006198
Jim Grosbachf7da8822011-04-26 19:44:18 +00006199 case ARM::ATOMIC_LOAD_MIN_I8:
6200 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
6201 case ARM::ATOMIC_LOAD_MIN_I16:
6202 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
6203 case ARM::ATOMIC_LOAD_MIN_I32:
6204 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
6205
6206 case ARM::ATOMIC_LOAD_MAX_I8:
6207 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
6208 case ARM::ATOMIC_LOAD_MAX_I16:
6209 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
6210 case ARM::ATOMIC_LOAD_MAX_I32:
6211 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
6212
6213 case ARM::ATOMIC_LOAD_UMIN_I8:
6214 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
6215 case ARM::ATOMIC_LOAD_UMIN_I16:
6216 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
6217 case ARM::ATOMIC_LOAD_UMIN_I32:
6218 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
6219
6220 case ARM::ATOMIC_LOAD_UMAX_I8:
6221 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
6222 case ARM::ATOMIC_LOAD_UMAX_I16:
6223 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
6224 case ARM::ATOMIC_LOAD_UMAX_I32:
6225 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
6226
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006227 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
6228 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
6229 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00006230
6231 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
6232 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
6233 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006234
Eli Friedman2bdffe42011-08-31 00:31:29 +00006235
6236 case ARM::ATOMADD6432:
6237 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006238 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr,
6239 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006240 case ARM::ATOMSUB6432:
6241 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006242 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6243 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006244 case ARM::ATOMOR6432:
6245 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006246 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006247 case ARM::ATOMXOR6432:
6248 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006249 isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006250 case ARM::ATOMAND6432:
6251 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006252 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006253 case ARM::ATOMSWAP6432:
6254 return EmitAtomicBinary64(MI, BB, 0, 0, false);
Eli Friedman4d3f3292011-08-31 17:52:22 +00006255 case ARM::ATOMCMPXCHG6432:
6256 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
6257 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6258 /*NeedsCarry*/ false, /*IsCmpxchg*/true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006259
Evan Cheng007ea272009-08-12 05:17:19 +00006260 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00006261 // To "insert" a SELECT_CC instruction, we actually have to insert the
6262 // diamond control-flow pattern. The incoming instruction knows the
6263 // destination vreg to set, the condition code register to branch on, the
6264 // true/false values to select between, and a branch opcode to use.
6265 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006266 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00006267 ++It;
6268
6269 // thisMBB:
6270 // ...
6271 // TrueVal = ...
6272 // cmpTY ccX, r1, r2
6273 // bCC copy1MBB
6274 // fallthrough --> copy0MBB
6275 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006276 MachineFunction *F = BB->getParent();
6277 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6278 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00006279 F->insert(It, copy0MBB);
6280 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006281
6282 // Transfer the remainder of BB and its successor edges to sinkMBB.
6283 sinkMBB->splice(sinkMBB->begin(), BB,
6284 llvm::next(MachineBasicBlock::iterator(MI)),
6285 BB->end());
6286 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6287
Dan Gohman258c58c2010-07-06 15:49:48 +00006288 BB->addSuccessor(copy0MBB);
6289 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00006290
Dan Gohman14152b42010-07-06 20:24:04 +00006291 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
6292 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
6293
Evan Chenga8e29892007-01-19 07:51:42 +00006294 // copy0MBB:
6295 // %FalseValue = ...
6296 // # fallthrough to sinkMBB
6297 BB = copy0MBB;
6298
6299 // Update machine-CFG edges
6300 BB->addSuccessor(sinkMBB);
6301
6302 // sinkMBB:
6303 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6304 // ...
6305 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00006306 BuildMI(*BB, BB->begin(), dl,
6307 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00006308 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6309 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6310
Dan Gohman14152b42010-07-06 20:24:04 +00006311 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00006312 return BB;
6313 }
Evan Cheng86198642009-08-07 00:34:42 +00006314
Evan Cheng218977b2010-07-13 19:27:42 +00006315 case ARM::BCCi64:
6316 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00006317 // If there is an unconditional branch to the other successor, remove it.
6318 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00006319
Evan Cheng218977b2010-07-13 19:27:42 +00006320 // Compare both parts that make up the double comparison separately for
6321 // equality.
6322 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
6323
6324 unsigned LHS1 = MI->getOperand(1).getReg();
6325 unsigned LHS2 = MI->getOperand(2).getReg();
6326 if (RHSisZero) {
6327 AddDefaultPred(BuildMI(BB, dl,
6328 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6329 .addReg(LHS1).addImm(0));
6330 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6331 .addReg(LHS2).addImm(0)
6332 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6333 } else {
6334 unsigned RHS1 = MI->getOperand(3).getReg();
6335 unsigned RHS2 = MI->getOperand(4).getReg();
6336 AddDefaultPred(BuildMI(BB, dl,
6337 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6338 .addReg(LHS1).addReg(RHS1));
6339 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6340 .addReg(LHS2).addReg(RHS2)
6341 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6342 }
6343
6344 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
6345 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
6346 if (MI->getOperand(0).getImm() == ARMCC::NE)
6347 std::swap(destMBB, exitMBB);
6348
6349 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6350 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson51f6a7a2011-09-09 21:48:23 +00006351 if (isThumb2)
6352 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
6353 else
6354 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng218977b2010-07-13 19:27:42 +00006355
6356 MI->eraseFromParent(); // The pseudo instruction is gone now.
6357 return BB;
6358 }
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006359
Bill Wendling5bc85282011-10-17 20:37:20 +00006360 case ARM::Int_eh_sjlj_setjmp:
6361 case ARM::Int_eh_sjlj_setjmp_nofp:
6362 case ARM::tInt_eh_sjlj_setjmp:
6363 case ARM::t2Int_eh_sjlj_setjmp:
6364 case ARM::t2Int_eh_sjlj_setjmp_nofp:
6365 EmitSjLjDispatchBlock(MI, BB);
6366 return BB;
6367
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006368 case ARM::ABS:
6369 case ARM::t2ABS: {
6370 // To insert an ABS instruction, we have to insert the
6371 // diamond control-flow pattern. The incoming instruction knows the
6372 // source vreg to test against 0, the destination vreg to set,
6373 // the condition code register to branch on, the
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006374 // true/false values to select between, and a branch opcode to use.
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006375 // It transforms
6376 // V1 = ABS V0
6377 // into
6378 // V2 = MOVS V0
6379 // BCC (branch to SinkBB if V0 >= 0)
6380 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006381 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006382 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6383 MachineFunction::iterator BBI = BB;
6384 ++BBI;
6385 MachineFunction *Fn = BB->getParent();
6386 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6387 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6388 Fn->insert(BBI, RSBBB);
6389 Fn->insert(BBI, SinkBB);
6390
6391 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
6392 unsigned int ABSDstReg = MI->getOperand(0).getReg();
6393 bool isThumb2 = Subtarget->isThumb2();
6394 MachineRegisterInfo &MRI = Fn->getRegInfo();
6395 // In Thumb mode S must not be specified if source register is the SP or
6396 // PC and if destination register is the SP, so restrict register class
6397 unsigned NewMovDstReg = MRI.createVirtualRegister(
6398 isThumb2 ? ARM::rGPRRegisterClass : ARM::GPRRegisterClass);
6399 unsigned NewRsbDstReg = MRI.createVirtualRegister(
6400 isThumb2 ? ARM::rGPRRegisterClass : ARM::GPRRegisterClass);
6401
6402 // Transfer the remainder of BB and its successor edges to sinkMBB.
6403 SinkBB->splice(SinkBB->begin(), BB,
6404 llvm::next(MachineBasicBlock::iterator(MI)),
6405 BB->end());
6406 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
6407
6408 BB->addSuccessor(RSBBB);
6409 BB->addSuccessor(SinkBB);
6410
6411 // fall through to SinkMBB
6412 RSBBB->addSuccessor(SinkBB);
6413
6414 // insert a movs at the end of BB
6415 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVr : ARM::MOVr),
6416 NewMovDstReg)
6417 .addReg(ABSSrcReg, RegState::Kill)
6418 .addImm((unsigned)ARMCC::AL).addReg(0)
6419 .addReg(ARM::CPSR, RegState::Define);
6420
6421 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006422 BuildMI(BB, dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006423 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
6424 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
6425
6426 // insert rsbri in RSBBB
6427 // Note: BCC and rsbri will be converted into predicated rsbmi
6428 // by if-conversion pass
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006429 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006430 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
6431 .addReg(NewMovDstReg, RegState::Kill)
6432 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
6433
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006434 // insert PHI in SinkBB,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006435 // reuse ABSDstReg to not change uses of ABS instruction
6436 BuildMI(*SinkBB, SinkBB->begin(), dl,
6437 TII->get(ARM::PHI), ABSDstReg)
6438 .addReg(NewRsbDstReg).addMBB(RSBBB)
6439 .addReg(NewMovDstReg).addMBB(BB);
6440
6441 // remove ABS instruction
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006442 MI->eraseFromParent();
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006443
6444 // return last added BB
6445 return SinkBB;
6446 }
Evan Chenga8e29892007-01-19 07:51:42 +00006447 }
6448}
6449
Evan Cheng37fefc22011-08-30 19:09:48 +00006450void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
6451 SDNode *Node) const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006452 if (!MI->hasPostISelHook()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006453 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
6454 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
6455 return;
6456 }
6457
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006458 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick4815d562011-09-20 03:17:40 +00006459 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
6460 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
6461 // operand is still set to noreg. If needed, set the optional operand's
6462 // register to CPSR, and remove the redundant implicit def.
Andrew Trick3be654f2011-09-21 02:20:46 +00006463 //
Andrew Trick90b7b122011-10-18 19:18:52 +00006464 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick4815d562011-09-20 03:17:40 +00006465
Andrew Trick3be654f2011-09-21 02:20:46 +00006466 // Rename pseudo opcodes.
6467 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
6468 if (NewOpc) {
6469 const ARMBaseInstrInfo *TII =
6470 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick90b7b122011-10-18 19:18:52 +00006471 MCID = &TII->get(NewOpc);
6472
6473 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
6474 "converted opcode should be the same except for cc_out");
6475
6476 MI->setDesc(*MCID);
6477
6478 // Add the optional cc_out operand
6479 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick3be654f2011-09-21 02:20:46 +00006480 }
Andrew Trick90b7b122011-10-18 19:18:52 +00006481 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick4815d562011-09-20 03:17:40 +00006482
6483 // Any ARM instruction that sets the 's' bit should specify an optional
6484 // "cc_out" operand in the last operand position.
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006485 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006486 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006487 return;
6488 }
Andrew Trick3be654f2011-09-21 02:20:46 +00006489 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
6490 // since we already have an optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006491 bool definesCPSR = false;
6492 bool deadCPSR = false;
Andrew Trick90b7b122011-10-18 19:18:52 +00006493 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick4815d562011-09-20 03:17:40 +00006494 i != e; ++i) {
6495 const MachineOperand &MO = MI->getOperand(i);
6496 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
6497 definesCPSR = true;
6498 if (MO.isDead())
6499 deadCPSR = true;
6500 MI->RemoveOperand(i);
6501 break;
Evan Cheng37fefc22011-08-30 19:09:48 +00006502 }
6503 }
Andrew Trick4815d562011-09-20 03:17:40 +00006504 if (!definesCPSR) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006505 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006506 return;
6507 }
6508 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick3be654f2011-09-21 02:20:46 +00006509 if (deadCPSR) {
6510 assert(!MI->getOperand(ccOutIdx).getReg() &&
6511 "expect uninitialized optional cc_out operand");
Andrew Trick4815d562011-09-20 03:17:40 +00006512 return;
Andrew Trick3be654f2011-09-21 02:20:46 +00006513 }
Andrew Trick4815d562011-09-20 03:17:40 +00006514
Andrew Trick3be654f2011-09-21 02:20:46 +00006515 // If this instruction was defined with an optional CPSR def and its dag node
6516 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006517 MachineOperand &MO = MI->getOperand(ccOutIdx);
6518 MO.setReg(ARM::CPSR);
6519 MO.setIsDef(true);
Evan Cheng37fefc22011-08-30 19:09:48 +00006520}
6521
Evan Chenga8e29892007-01-19 07:51:42 +00006522//===----------------------------------------------------------------------===//
6523// ARM Optimization Hooks
6524//===----------------------------------------------------------------------===//
6525
Chris Lattnerd1980a52009-03-12 06:52:53 +00006526static
6527SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
6528 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00006529 SelectionDAG &DAG = DCI.DAG;
6530 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00006531 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00006532 unsigned Opc = N->getOpcode();
6533 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
6534 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
6535 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
6536 ISD::CondCode CC = ISD::SETCC_INVALID;
6537
6538 if (isSlctCC) {
6539 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
6540 } else {
6541 SDValue CCOp = Slct.getOperand(0);
6542 if (CCOp.getOpcode() == ISD::SETCC)
6543 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
6544 }
6545
6546 bool DoXform = false;
6547 bool InvCC = false;
6548 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
6549 "Bad input!");
6550
6551 if (LHS.getOpcode() == ISD::Constant &&
6552 cast<ConstantSDNode>(LHS)->isNullValue()) {
6553 DoXform = true;
6554 } else if (CC != ISD::SETCC_INVALID &&
6555 RHS.getOpcode() == ISD::Constant &&
6556 cast<ConstantSDNode>(RHS)->isNullValue()) {
6557 std::swap(LHS, RHS);
6558 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006559 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00006560 Op0.getOperand(0).getValueType();
6561 bool isInt = OpVT.isInteger();
6562 CC = ISD::getSetCCInverse(CC, isInt);
6563
6564 if (!TLI.isCondCodeLegal(CC, OpVT))
6565 return SDValue(); // Inverse operator isn't legal.
6566
6567 DoXform = true;
6568 InvCC = true;
6569 }
6570
6571 if (DoXform) {
6572 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
6573 if (isSlctCC)
6574 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
6575 Slct.getOperand(0), Slct.getOperand(1), CC);
6576 SDValue CCOp = Slct.getOperand(0);
6577 if (InvCC)
6578 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
6579 CCOp.getOperand(0), CCOp.getOperand(1), CC);
6580 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
6581 CCOp, OtherOp, Result);
6582 }
6583 return SDValue();
6584}
6585
Eric Christopherfa6f5912011-06-29 21:10:36 +00006586// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00006587// (only after legalization).
6588static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
6589 TargetLowering::DAGCombinerInfo &DCI,
6590 const ARMSubtarget *Subtarget) {
6591
6592 // Only perform optimization if after legalize, and if NEON is available. We
6593 // also expected both operands to be BUILD_VECTORs.
6594 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
6595 || N0.getOpcode() != ISD::BUILD_VECTOR
6596 || N1.getOpcode() != ISD::BUILD_VECTOR)
6597 return SDValue();
6598
6599 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
6600 EVT VT = N->getValueType(0);
6601 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
6602 return SDValue();
6603
6604 // Check that the vector operands are of the right form.
6605 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
6606 // operands, where N is the size of the formed vector.
6607 // Each EXTRACT_VECTOR should have the same input vector and odd or even
6608 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00006609
6610 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00006611 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00006612 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00006613 SDValue Vec = N0->getOperand(0)->getOperand(0);
6614 SDNode *V = Vec.getNode();
6615 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00006616
Eric Christopherfa6f5912011-06-29 21:10:36 +00006617 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00006618 // check to see if each of their operands are an EXTRACT_VECTOR with
6619 // the same vector and appropriate index.
6620 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
6621 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
6622 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00006623
Tanya Lattner189531f2011-06-14 23:48:48 +00006624 SDValue ExtVec0 = N0->getOperand(i);
6625 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00006626
Tanya Lattner189531f2011-06-14 23:48:48 +00006627 // First operand is the vector, verify its the same.
6628 if (V != ExtVec0->getOperand(0).getNode() ||
6629 V != ExtVec1->getOperand(0).getNode())
6630 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00006631
Tanya Lattner189531f2011-06-14 23:48:48 +00006632 // Second is the constant, verify its correct.
6633 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
6634 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00006635
Tanya Lattner189531f2011-06-14 23:48:48 +00006636 // For the constant, we want to see all the even or all the odd.
6637 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
6638 || C1->getZExtValue() != nextIndex+1)
6639 return SDValue();
6640
6641 // Increment index.
6642 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00006643 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00006644 return SDValue();
6645 }
6646
6647 // Create VPADDL node.
6648 SelectionDAG &DAG = DCI.DAG;
6649 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00006650
6651 // Build operand list.
6652 SmallVector<SDValue, 8> Ops;
6653 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
6654 TLI.getPointerTy()));
6655
6656 // Input is the vector.
6657 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00006658
Tanya Lattner189531f2011-06-14 23:48:48 +00006659 // Get widened type and narrowed type.
6660 MVT widenType;
6661 unsigned numElem = VT.getVectorNumElements();
6662 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
6663 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
6664 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
6665 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
6666 default:
Craig Topperbc219812012-02-07 02:50:20 +00006667 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattner189531f2011-06-14 23:48:48 +00006668 }
6669
6670 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
6671 widenType, &Ops[0], Ops.size());
6672 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
6673}
6674
Bob Wilson3d5792a2010-07-29 20:34:14 +00006675/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
6676/// operands N0 and N1. This is a helper for PerformADDCombine that is
6677/// called with the default operands, and if that fails, with commuted
6678/// operands.
6679static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00006680 TargetLowering::DAGCombinerInfo &DCI,
6681 const ARMSubtarget *Subtarget){
6682
6683 // Attempt to create vpaddl for this add.
6684 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
6685 if (Result.getNode())
6686 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00006687
Chris Lattnerd1980a52009-03-12 06:52:53 +00006688 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
6689 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
6690 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
6691 if (Result.getNode()) return Result;
6692 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00006693 return SDValue();
6694}
6695
Bob Wilson3d5792a2010-07-29 20:34:14 +00006696/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
6697///
6698static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00006699 TargetLowering::DAGCombinerInfo &DCI,
6700 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00006701 SDValue N0 = N->getOperand(0);
6702 SDValue N1 = N->getOperand(1);
6703
6704 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00006705 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00006706 if (Result.getNode())
6707 return Result;
6708
6709 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00006710 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00006711}
6712
Chris Lattnerd1980a52009-03-12 06:52:53 +00006713/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00006714///
Chris Lattnerd1980a52009-03-12 06:52:53 +00006715static SDValue PerformSUBCombine(SDNode *N,
6716 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00006717 SDValue N0 = N->getOperand(0);
6718 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00006719
Chris Lattnerd1980a52009-03-12 06:52:53 +00006720 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
6721 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
6722 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
6723 if (Result.getNode()) return Result;
6724 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00006725
Chris Lattnerd1980a52009-03-12 06:52:53 +00006726 return SDValue();
6727}
6728
Evan Cheng463d3582011-03-31 19:38:48 +00006729/// PerformVMULCombine
6730/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
6731/// special multiplier accumulator forwarding.
6732/// vmul d3, d0, d2
6733/// vmla d3, d1, d2
6734/// is faster than
6735/// vadd d3, d0, d1
6736/// vmul d3, d3, d2
6737static SDValue PerformVMULCombine(SDNode *N,
6738 TargetLowering::DAGCombinerInfo &DCI,
6739 const ARMSubtarget *Subtarget) {
6740 if (!Subtarget->hasVMLxForwarding())
6741 return SDValue();
6742
6743 SelectionDAG &DAG = DCI.DAG;
6744 SDValue N0 = N->getOperand(0);
6745 SDValue N1 = N->getOperand(1);
6746 unsigned Opcode = N0.getOpcode();
6747 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
6748 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00006749 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00006750 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
6751 Opcode != ISD::FADD && Opcode != ISD::FSUB)
6752 return SDValue();
6753 std::swap(N0, N1);
6754 }
6755
6756 EVT VT = N->getValueType(0);
6757 DebugLoc DL = N->getDebugLoc();
6758 SDValue N00 = N0->getOperand(0);
6759 SDValue N01 = N0->getOperand(1);
6760 return DAG.getNode(Opcode, DL, VT,
6761 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
6762 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
6763}
6764
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006765static SDValue PerformMULCombine(SDNode *N,
6766 TargetLowering::DAGCombinerInfo &DCI,
6767 const ARMSubtarget *Subtarget) {
6768 SelectionDAG &DAG = DCI.DAG;
6769
6770 if (Subtarget->isThumb1Only())
6771 return SDValue();
6772
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006773 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
6774 return SDValue();
6775
6776 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00006777 if (VT.is64BitVector() || VT.is128BitVector())
6778 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006779 if (VT != MVT::i32)
6780 return SDValue();
6781
6782 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
6783 if (!C)
6784 return SDValue();
6785
6786 uint64_t MulAmt = C->getZExtValue();
6787 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
6788 ShiftAmt = ShiftAmt & (32 - 1);
6789 SDValue V = N->getOperand(0);
6790 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006791
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006792 SDValue Res;
6793 MulAmt >>= ShiftAmt;
6794 if (isPowerOf2_32(MulAmt - 1)) {
6795 // (mul x, 2^N + 1) => (add (shl x, N), x)
6796 Res = DAG.getNode(ISD::ADD, DL, VT,
6797 V, DAG.getNode(ISD::SHL, DL, VT,
6798 V, DAG.getConstant(Log2_32(MulAmt-1),
6799 MVT::i32)));
6800 } else if (isPowerOf2_32(MulAmt + 1)) {
6801 // (mul x, 2^N - 1) => (sub (shl x, N), x)
6802 Res = DAG.getNode(ISD::SUB, DL, VT,
6803 DAG.getNode(ISD::SHL, DL, VT,
6804 V, DAG.getConstant(Log2_32(MulAmt+1),
6805 MVT::i32)),
6806 V);
6807 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006808 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006809
6810 if (ShiftAmt != 0)
6811 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
6812 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006813
6814 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006815 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006816 return SDValue();
6817}
6818
Owen Anderson080c0922010-11-05 19:27:46 +00006819static SDValue PerformANDCombine(SDNode *N,
6820 TargetLowering::DAGCombinerInfo &DCI) {
Owen Anderson76706012011-04-05 21:48:57 +00006821
Owen Anderson080c0922010-11-05 19:27:46 +00006822 // Attempt to use immediate-form VBIC
6823 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
6824 DebugLoc dl = N->getDebugLoc();
6825 EVT VT = N->getValueType(0);
6826 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006827
Tanya Lattner0433b212011-04-07 15:24:20 +00006828 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
6829 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00006830
Owen Anderson080c0922010-11-05 19:27:46 +00006831 APInt SplatBits, SplatUndef;
6832 unsigned SplatBitSize;
6833 bool HasAnyUndefs;
6834 if (BVN &&
6835 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
6836 if (SplatBitSize <= 64) {
6837 EVT VbicVT;
6838 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
6839 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006840 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00006841 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00006842 if (Val.getNode()) {
6843 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006844 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00006845 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006846 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00006847 }
6848 }
6849 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006850
Owen Anderson080c0922010-11-05 19:27:46 +00006851 return SDValue();
6852}
6853
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006854/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
6855static SDValue PerformORCombine(SDNode *N,
6856 TargetLowering::DAGCombinerInfo &DCI,
6857 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00006858 // Attempt to use immediate-form VORR
6859 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
6860 DebugLoc dl = N->getDebugLoc();
6861 EVT VT = N->getValueType(0);
6862 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006863
Tanya Lattner0433b212011-04-07 15:24:20 +00006864 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
6865 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00006866
Owen Anderson60f48702010-11-03 23:15:26 +00006867 APInt SplatBits, SplatUndef;
6868 unsigned SplatBitSize;
6869 bool HasAnyUndefs;
6870 if (BVN && Subtarget->hasNEON() &&
6871 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
6872 if (SplatBitSize <= 64) {
6873 EVT VorrVT;
6874 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
6875 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00006876 DAG, VorrVT, VT.is128BitVector(),
6877 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00006878 if (Val.getNode()) {
6879 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006880 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00006881 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006882 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00006883 }
6884 }
6885 }
6886
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00006887 SDValue N0 = N->getOperand(0);
6888 if (N0.getOpcode() != ISD::AND)
6889 return SDValue();
6890 SDValue N1 = N->getOperand(1);
6891
6892 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
6893 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
6894 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
6895 APInt SplatUndef;
6896 unsigned SplatBitSize;
6897 bool HasAnyUndefs;
6898
6899 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
6900 APInt SplatBits0;
6901 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
6902 HasAnyUndefs) && !HasAnyUndefs) {
6903 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
6904 APInt SplatBits1;
6905 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
6906 HasAnyUndefs) && !HasAnyUndefs &&
6907 SplatBits0 == ~SplatBits1) {
6908 // Canonicalize the vector type to make instruction selection simpler.
6909 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
6910 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
6911 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00006912 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00006913 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
6914 }
6915 }
6916 }
6917
Jim Grosbach54238562010-07-17 03:30:54 +00006918 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
6919 // reasonable.
6920
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006921 // BFI is only available on V6T2+
6922 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
6923 return SDValue();
6924
Jim Grosbach54238562010-07-17 03:30:54 +00006925 DebugLoc DL = N->getDebugLoc();
6926 // 1) or (and A, mask), val => ARMbfi A, val, mask
6927 // iff (val & mask) == val
6928 //
6929 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
6930 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00006931 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00006932 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00006933 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00006934 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006935
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006936 if (VT != MVT::i32)
6937 return SDValue();
6938
Evan Cheng30fb13f2010-12-13 20:32:54 +00006939 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00006940
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006941 // The value and the mask need to be constants so we can verify this is
6942 // actually a bitfield set. If the mask is 0xffff, we can do better
6943 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00006944 SDValue MaskOp = N0.getOperand(1);
6945 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
6946 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006947 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00006948 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006949 if (Mask == 0xffff)
6950 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006951 SDValue Res;
6952 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00006953 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
6954 if (N1C) {
6955 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00006956 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00006957 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006958
Evan Chenga9688c42010-12-11 04:11:38 +00006959 if (ARM::isBitFieldInvertedMask(Mask)) {
6960 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006961
Evan Cheng30fb13f2010-12-13 20:32:54 +00006962 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00006963 DAG.getConstant(Val, MVT::i32),
6964 DAG.getConstant(Mask, MVT::i32));
6965
6966 // Do not add new nodes to DAG combiner worklist.
6967 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00006968 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00006969 }
Jim Grosbach54238562010-07-17 03:30:54 +00006970 } else if (N1.getOpcode() == ISD::AND) {
6971 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00006972 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
6973 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00006974 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00006975 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006976
Eric Christopher29aeed12011-03-26 01:21:03 +00006977 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
6978 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00006979 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00006980 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00006981 // The pack halfword instruction works better for masks that fit it,
6982 // so use that when it's available.
6983 if (Subtarget->hasT2ExtractPack() &&
6984 (Mask == 0xffff || Mask == 0xffff0000))
6985 return SDValue();
6986 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00006987 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00006988 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00006989 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00006990 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00006991 DAG.getConstant(Mask, MVT::i32));
6992 // Do not add new nodes to DAG combiner worklist.
6993 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00006994 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006995 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00006996 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00006997 // The pack halfword instruction works better for masks that fit it,
6998 // so use that when it's available.
6999 if (Subtarget->hasT2ExtractPack() &&
7000 (Mask2 == 0xffff || Mask2 == 0xffff0000))
7001 return SDValue();
7002 // 2b
7003 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007004 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00007005 DAG.getConstant(lsb, MVT::i32));
7006 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00007007 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00007008 // Do not add new nodes to DAG combiner worklist.
7009 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007010 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007011 }
7012 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007013
Evan Cheng30fb13f2010-12-13 20:32:54 +00007014 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
7015 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
7016 ARM::isBitFieldInvertedMask(~Mask)) {
7017 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
7018 // where lsb(mask) == #shamt and masked bits of B are known zero.
7019 SDValue ShAmt = N00.getOperand(1);
7020 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
7021 unsigned LSB = CountTrailingZeros_32(Mask);
7022 if (ShAmtC != LSB)
7023 return SDValue();
7024
7025 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
7026 DAG.getConstant(~Mask, MVT::i32));
7027
7028 // Do not add new nodes to DAG combiner worklist.
7029 DCI.CombineTo(N, Res, false);
7030 }
7031
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007032 return SDValue();
7033}
7034
Evan Chengbf188ae2011-06-15 01:12:31 +00007035/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
7036/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00007037static SDValue PerformBFICombine(SDNode *N,
7038 TargetLowering::DAGCombinerInfo &DCI) {
7039 SDValue N1 = N->getOperand(1);
7040 if (N1.getOpcode() == ISD::AND) {
7041 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7042 if (!N11C)
7043 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007044 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
7045 unsigned LSB = CountTrailingZeros_32(~InvMask);
7046 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
7047 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00007048 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007049 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00007050 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
7051 N->getOperand(0), N1.getOperand(0),
7052 N->getOperand(2));
7053 }
7054 return SDValue();
7055}
7056
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007057/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
7058/// ARMISD::VMOVRRD.
7059static SDValue PerformVMOVRRDCombine(SDNode *N,
7060 TargetLowering::DAGCombinerInfo &DCI) {
7061 // vmovrrd(vmovdrr x, y) -> x,y
7062 SDValue InDouble = N->getOperand(0);
7063 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
7064 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00007065
7066 // vmovrrd(load f64) -> (load i32), (load i32)
7067 SDNode *InNode = InDouble.getNode();
7068 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
7069 InNode->getValueType(0) == MVT::f64 &&
7070 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
7071 !cast<LoadSDNode>(InNode)->isVolatile()) {
7072 // TODO: Should this be done for non-FrameIndex operands?
7073 LoadSDNode *LD = cast<LoadSDNode>(InNode);
7074
7075 SelectionDAG &DAG = DCI.DAG;
7076 DebugLoc DL = LD->getDebugLoc();
7077 SDValue BasePtr = LD->getBasePtr();
7078 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
7079 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007080 LD->isNonTemporal(), LD->isInvariant(),
7081 LD->getAlignment());
Cameron Zwarich4071a712011-04-02 02:40:43 +00007082
7083 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7084 DAG.getConstant(4, MVT::i32));
7085 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
7086 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007087 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich4071a712011-04-02 02:40:43 +00007088 std::min(4U, LD->getAlignment() / 2));
7089
7090 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
7091 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
7092 DCI.RemoveFromWorklist(LD);
7093 DAG.DeleteNode(LD);
7094 return Result;
7095 }
7096
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007097 return SDValue();
7098}
7099
7100/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
7101/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
7102static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
7103 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
7104 SDValue Op0 = N->getOperand(0);
7105 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007106 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007107 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007108 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007109 Op1 = Op1.getOperand(0);
7110 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
7111 Op0.getNode() == Op1.getNode() &&
7112 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007113 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007114 N->getValueType(0), Op0.getOperand(0));
7115 return SDValue();
7116}
7117
Bob Wilson31600902010-12-21 06:43:19 +00007118/// PerformSTORECombine - Target-specific dag combine xforms for
7119/// ISD::STORE.
7120static SDValue PerformSTORECombine(SDNode *N,
7121 TargetLowering::DAGCombinerInfo &DCI) {
7122 // Bitcast an i64 store extracted from a vector to f64.
7123 // Otherwise, the i64 value will be legalized to a pair of i32 values.
7124 StoreSDNode *St = cast<StoreSDNode>(N);
7125 SDValue StVal = St->getValue();
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007126 if (!ISD::isNormalStore(St) || St->isVolatile())
7127 return SDValue();
7128
7129 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
7130 StVal.getNode()->hasOneUse() && !St->isVolatile()) {
7131 SelectionDAG &DAG = DCI.DAG;
7132 DebugLoc DL = St->getDebugLoc();
7133 SDValue BasePtr = St->getBasePtr();
7134 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
7135 StVal.getNode()->getOperand(0), BasePtr,
7136 St->getPointerInfo(), St->isVolatile(),
7137 St->isNonTemporal(), St->getAlignment());
7138
7139 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7140 DAG.getConstant(4, MVT::i32));
7141 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
7142 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
7143 St->isNonTemporal(),
7144 std::min(4U, St->getAlignment() / 2));
7145 }
7146
7147 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00007148 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
7149 return SDValue();
7150
7151 SelectionDAG &DAG = DCI.DAG;
7152 DebugLoc dl = StVal.getDebugLoc();
7153 SDValue IntVec = StVal.getOperand(0);
7154 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7155 IntVec.getValueType().getVectorNumElements());
7156 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
7157 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
7158 Vec, StVal.getOperand(1));
7159 dl = N->getDebugLoc();
7160 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
7161 // Make the DAGCombiner fold the bitcasts.
7162 DCI.AddToWorklist(Vec.getNode());
7163 DCI.AddToWorklist(ExtElt.getNode());
7164 DCI.AddToWorklist(V.getNode());
7165 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
7166 St->getPointerInfo(), St->isVolatile(),
7167 St->isNonTemporal(), St->getAlignment(),
7168 St->getTBAAInfo());
7169}
7170
7171/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
7172/// are normal, non-volatile loads. If so, it is profitable to bitcast an
7173/// i64 vector to have f64 elements, since the value can then be loaded
7174/// directly into a VFP register.
7175static bool hasNormalLoadOperand(SDNode *N) {
7176 unsigned NumElts = N->getValueType(0).getVectorNumElements();
7177 for (unsigned i = 0; i < NumElts; ++i) {
7178 SDNode *Elt = N->getOperand(i).getNode();
7179 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
7180 return true;
7181 }
7182 return false;
7183}
7184
Bob Wilson75f02882010-09-17 22:59:05 +00007185/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
7186/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00007187static SDValue PerformBUILD_VECTORCombine(SDNode *N,
7188 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00007189 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
7190 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
7191 // into a pair of GPRs, which is fine when the value is used as a scalar,
7192 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00007193 SelectionDAG &DAG = DCI.DAG;
7194 if (N->getNumOperands() == 2) {
7195 SDValue RV = PerformVMOVDRRCombine(N, DAG);
7196 if (RV.getNode())
7197 return RV;
7198 }
Bob Wilson75f02882010-09-17 22:59:05 +00007199
Bob Wilson31600902010-12-21 06:43:19 +00007200 // Load i64 elements as f64 values so that type legalization does not split
7201 // them up into i32 values.
7202 EVT VT = N->getValueType(0);
7203 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
7204 return SDValue();
7205 DebugLoc dl = N->getDebugLoc();
7206 SmallVector<SDValue, 8> Ops;
7207 unsigned NumElts = VT.getVectorNumElements();
7208 for (unsigned i = 0; i < NumElts; ++i) {
7209 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
7210 Ops.push_back(V);
7211 // Make the DAGCombiner fold the bitcast.
7212 DCI.AddToWorklist(V.getNode());
7213 }
7214 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
7215 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
7216 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
7217}
7218
7219/// PerformInsertEltCombine - Target-specific dag combine xforms for
7220/// ISD::INSERT_VECTOR_ELT.
7221static SDValue PerformInsertEltCombine(SDNode *N,
7222 TargetLowering::DAGCombinerInfo &DCI) {
7223 // Bitcast an i64 load inserted into a vector to f64.
7224 // Otherwise, the i64 value will be legalized to a pair of i32 values.
7225 EVT VT = N->getValueType(0);
7226 SDNode *Elt = N->getOperand(1).getNode();
7227 if (VT.getVectorElementType() != MVT::i64 ||
7228 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
7229 return SDValue();
7230
7231 SelectionDAG &DAG = DCI.DAG;
7232 DebugLoc dl = N->getDebugLoc();
7233 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7234 VT.getVectorNumElements());
7235 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
7236 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
7237 // Make the DAGCombiner fold the bitcasts.
7238 DCI.AddToWorklist(Vec.getNode());
7239 DCI.AddToWorklist(V.getNode());
7240 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
7241 Vec, V, N->getOperand(2));
7242 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00007243}
7244
Bob Wilsonf20700c2010-10-27 20:38:28 +00007245/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
7246/// ISD::VECTOR_SHUFFLE.
7247static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
7248 // The LLVM shufflevector instruction does not require the shuffle mask
7249 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
7250 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
7251 // operands do not match the mask length, they are extended by concatenating
7252 // them with undef vectors. That is probably the right thing for other
7253 // targets, but for NEON it is better to concatenate two double-register
7254 // size vector operands into a single quad-register size vector. Do that
7255 // transformation here:
7256 // shuffle(concat(v1, undef), concat(v2, undef)) ->
7257 // shuffle(concat(v1, v2), undef)
7258 SDValue Op0 = N->getOperand(0);
7259 SDValue Op1 = N->getOperand(1);
7260 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
7261 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
7262 Op0.getNumOperands() != 2 ||
7263 Op1.getNumOperands() != 2)
7264 return SDValue();
7265 SDValue Concat0Op1 = Op0.getOperand(1);
7266 SDValue Concat1Op1 = Op1.getOperand(1);
7267 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
7268 Concat1Op1.getOpcode() != ISD::UNDEF)
7269 return SDValue();
7270 // Skip the transformation if any of the types are illegal.
7271 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7272 EVT VT = N->getValueType(0);
7273 if (!TLI.isTypeLegal(VT) ||
7274 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
7275 !TLI.isTypeLegal(Concat1Op1.getValueType()))
7276 return SDValue();
7277
7278 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
7279 Op0.getOperand(0), Op1.getOperand(0));
7280 // Translate the shuffle mask.
7281 SmallVector<int, 16> NewMask;
7282 unsigned NumElts = VT.getVectorNumElements();
7283 unsigned HalfElts = NumElts/2;
7284 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
7285 for (unsigned n = 0; n < NumElts; ++n) {
7286 int MaskElt = SVN->getMaskElt(n);
7287 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007288 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00007289 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007290 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00007291 NewElt = HalfElts + MaskElt - NumElts;
7292 NewMask.push_back(NewElt);
7293 }
7294 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
7295 DAG.getUNDEF(VT), NewMask.data());
7296}
7297
Bob Wilson1c3ef902011-02-07 17:43:21 +00007298/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
7299/// NEON load/store intrinsics to merge base address updates.
7300static SDValue CombineBaseUpdate(SDNode *N,
7301 TargetLowering::DAGCombinerInfo &DCI) {
7302 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7303 return SDValue();
7304
7305 SelectionDAG &DAG = DCI.DAG;
7306 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
7307 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
7308 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
7309 SDValue Addr = N->getOperand(AddrOpIdx);
7310
7311 // Search for a use of the address operand that is an increment.
7312 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
7313 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
7314 SDNode *User = *UI;
7315 if (User->getOpcode() != ISD::ADD ||
7316 UI.getUse().getResNo() != Addr.getResNo())
7317 continue;
7318
7319 // Check that the add is independent of the load/store. Otherwise, folding
7320 // it would create a cycle.
7321 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
7322 continue;
7323
7324 // Find the new opcode for the updating load/store.
7325 bool isLoad = true;
7326 bool isLaneOp = false;
7327 unsigned NewOpc = 0;
7328 unsigned NumVecs = 0;
7329 if (isIntrinsic) {
7330 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
7331 switch (IntNo) {
Craig Topperbc219812012-02-07 02:50:20 +00007332 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00007333 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
7334 NumVecs = 1; break;
7335 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
7336 NumVecs = 2; break;
7337 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
7338 NumVecs = 3; break;
7339 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
7340 NumVecs = 4; break;
7341 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
7342 NumVecs = 2; isLaneOp = true; break;
7343 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
7344 NumVecs = 3; isLaneOp = true; break;
7345 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
7346 NumVecs = 4; isLaneOp = true; break;
7347 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
7348 NumVecs = 1; isLoad = false; break;
7349 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
7350 NumVecs = 2; isLoad = false; break;
7351 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
7352 NumVecs = 3; isLoad = false; break;
7353 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
7354 NumVecs = 4; isLoad = false; break;
7355 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
7356 NumVecs = 2; isLoad = false; isLaneOp = true; break;
7357 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
7358 NumVecs = 3; isLoad = false; isLaneOp = true; break;
7359 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
7360 NumVecs = 4; isLoad = false; isLaneOp = true; break;
7361 }
7362 } else {
7363 isLaneOp = true;
7364 switch (N->getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00007365 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00007366 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
7367 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
7368 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
7369 }
7370 }
7371
7372 // Find the size of memory referenced by the load/store.
7373 EVT VecTy;
7374 if (isLoad)
7375 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00007376 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00007377 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
7378 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
7379 if (isLaneOp)
7380 NumBytes /= VecTy.getVectorNumElements();
7381
7382 // If the increment is a constant, it must match the memory ref size.
7383 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
7384 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
7385 uint64_t IncVal = CInc->getZExtValue();
7386 if (IncVal != NumBytes)
7387 continue;
7388 } else if (NumBytes >= 3 * 16) {
7389 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
7390 // separate instructions that make it harder to use a non-constant update.
7391 continue;
7392 }
7393
7394 // Create the new updating load/store node.
7395 EVT Tys[6];
7396 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
7397 unsigned n;
7398 for (n = 0; n < NumResultVecs; ++n)
7399 Tys[n] = VecTy;
7400 Tys[n++] = MVT::i32;
7401 Tys[n] = MVT::Other;
7402 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
7403 SmallVector<SDValue, 8> Ops;
7404 Ops.push_back(N->getOperand(0)); // incoming chain
7405 Ops.push_back(N->getOperand(AddrOpIdx));
7406 Ops.push_back(Inc);
7407 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
7408 Ops.push_back(N->getOperand(i));
7409 }
7410 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
7411 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
7412 Ops.data(), Ops.size(),
7413 MemInt->getMemoryVT(),
7414 MemInt->getMemOperand());
7415
7416 // Update the uses.
7417 std::vector<SDValue> NewResults;
7418 for (unsigned i = 0; i < NumResultVecs; ++i) {
7419 NewResults.push_back(SDValue(UpdN.getNode(), i));
7420 }
7421 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
7422 DCI.CombineTo(N, NewResults);
7423 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
7424
7425 break;
Owen Anderson76706012011-04-05 21:48:57 +00007426 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00007427 return SDValue();
7428}
7429
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007430/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
7431/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
7432/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
7433/// return true.
7434static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
7435 SelectionDAG &DAG = DCI.DAG;
7436 EVT VT = N->getValueType(0);
7437 // vldN-dup instructions only support 64-bit vectors for N > 1.
7438 if (!VT.is64BitVector())
7439 return false;
7440
7441 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
7442 SDNode *VLD = N->getOperand(0).getNode();
7443 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
7444 return false;
7445 unsigned NumVecs = 0;
7446 unsigned NewOpc = 0;
7447 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
7448 if (IntNo == Intrinsic::arm_neon_vld2lane) {
7449 NumVecs = 2;
7450 NewOpc = ARMISD::VLD2DUP;
7451 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
7452 NumVecs = 3;
7453 NewOpc = ARMISD::VLD3DUP;
7454 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
7455 NumVecs = 4;
7456 NewOpc = ARMISD::VLD4DUP;
7457 } else {
7458 return false;
7459 }
7460
7461 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
7462 // numbers match the load.
7463 unsigned VLDLaneNo =
7464 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
7465 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
7466 UI != UE; ++UI) {
7467 // Ignore uses of the chain result.
7468 if (UI.getUse().getResNo() == NumVecs)
7469 continue;
7470 SDNode *User = *UI;
7471 if (User->getOpcode() != ARMISD::VDUPLANE ||
7472 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
7473 return false;
7474 }
7475
7476 // Create the vldN-dup node.
7477 EVT Tys[5];
7478 unsigned n;
7479 for (n = 0; n < NumVecs; ++n)
7480 Tys[n] = VT;
7481 Tys[n] = MVT::Other;
7482 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
7483 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
7484 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
7485 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
7486 Ops, 2, VLDMemInt->getMemoryVT(),
7487 VLDMemInt->getMemOperand());
7488
7489 // Update the uses.
7490 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
7491 UI != UE; ++UI) {
7492 unsigned ResNo = UI.getUse().getResNo();
7493 // Ignore uses of the chain result.
7494 if (ResNo == NumVecs)
7495 continue;
7496 SDNode *User = *UI;
7497 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
7498 }
7499
7500 // Now the vldN-lane intrinsic is dead except for its chain result.
7501 // Update uses of the chain.
7502 std::vector<SDValue> VLDDupResults;
7503 for (unsigned n = 0; n < NumVecs; ++n)
7504 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
7505 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
7506 DCI.CombineTo(VLD, VLDDupResults);
7507
7508 return true;
7509}
7510
Bob Wilson9e82bf12010-07-14 01:22:12 +00007511/// PerformVDUPLANECombine - Target-specific dag combine xforms for
7512/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007513static SDValue PerformVDUPLANECombine(SDNode *N,
7514 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00007515 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007516
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007517 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
7518 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
7519 if (CombineVLDDUP(N, DCI))
7520 return SDValue(N, 0);
7521
7522 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
7523 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007524 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00007525 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00007526 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00007527 return SDValue();
7528
7529 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
7530 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
7531 // The canonical VMOV for a zero vector uses a 32-bit element size.
7532 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7533 unsigned EltBits;
7534 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
7535 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007536 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007537 if (EltSize > VT.getVectorElementType().getSizeInBits())
7538 return SDValue();
7539
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007540 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007541}
7542
Eric Christopherfa6f5912011-06-29 21:10:36 +00007543// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00007544// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
7545static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
7546{
Chad Rosier118c9a02011-06-28 17:26:57 +00007547 integerPart cN;
7548 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00007549 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
7550 I != E; I++) {
7551 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
7552 if (!C)
7553 return false;
7554
Eric Christopherfa6f5912011-06-29 21:10:36 +00007555 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00007556 APFloat APF = C->getValueAPF();
7557 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
7558 != APFloat::opOK || !isExact)
7559 return false;
7560
7561 c0 = (I == 0) ? cN : c0;
7562 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
7563 return false;
7564 }
7565 C = c0;
7566 return true;
7567}
7568
7569/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
7570/// can replace combinations of VMUL and VCVT (floating-point to integer)
7571/// when the VMUL has a constant operand that is a power of 2.
7572///
7573/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
7574/// vmul.f32 d16, d17, d16
7575/// vcvt.s32.f32 d16, d16
7576/// becomes:
7577/// vcvt.s32.f32 d16, d16, #3
7578static SDValue PerformVCVTCombine(SDNode *N,
7579 TargetLowering::DAGCombinerInfo &DCI,
7580 const ARMSubtarget *Subtarget) {
7581 SelectionDAG &DAG = DCI.DAG;
7582 SDValue Op = N->getOperand(0);
7583
7584 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
7585 Op.getOpcode() != ISD::FMUL)
7586 return SDValue();
7587
7588 uint64_t C;
7589 SDValue N0 = Op->getOperand(0);
7590 SDValue ConstVec = Op->getOperand(1);
7591 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
7592
Eric Christopherfa6f5912011-06-29 21:10:36 +00007593 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00007594 !isConstVecPow2(ConstVec, isSigned, C))
7595 return SDValue();
7596
7597 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
7598 Intrinsic::arm_neon_vcvtfp2fxu;
7599 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7600 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00007601 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00007602 DAG.getConstant(Log2_64(C), MVT::i32));
7603}
7604
7605/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
7606/// can replace combinations of VCVT (integer to floating-point) and VDIV
7607/// when the VDIV has a constant operand that is a power of 2.
7608///
7609/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
7610/// vcvt.f32.s32 d16, d16
7611/// vdiv.f32 d16, d17, d16
7612/// becomes:
7613/// vcvt.f32.s32 d16, d16, #3
7614static SDValue PerformVDIVCombine(SDNode *N,
7615 TargetLowering::DAGCombinerInfo &DCI,
7616 const ARMSubtarget *Subtarget) {
7617 SelectionDAG &DAG = DCI.DAG;
7618 SDValue Op = N->getOperand(0);
7619 unsigned OpOpcode = Op.getNode()->getOpcode();
7620
7621 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
7622 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
7623 return SDValue();
7624
7625 uint64_t C;
7626 SDValue ConstVec = N->getOperand(1);
7627 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
7628
7629 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
7630 !isConstVecPow2(ConstVec, isSigned, C))
7631 return SDValue();
7632
Eric Christopherfa6f5912011-06-29 21:10:36 +00007633 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00007634 Intrinsic::arm_neon_vcvtfxu2fp;
7635 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7636 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00007637 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00007638 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
7639}
7640
7641/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00007642/// operand of a vector shift operation, where all the elements of the
7643/// build_vector must have the same constant integer value.
7644static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
7645 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007646 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00007647 Op = Op.getOperand(0);
7648 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
7649 APInt SplatBits, SplatUndef;
7650 unsigned SplatBitSize;
7651 bool HasAnyUndefs;
7652 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
7653 HasAnyUndefs, ElementBits) ||
7654 SplatBitSize > ElementBits)
7655 return false;
7656 Cnt = SplatBits.getSExtValue();
7657 return true;
7658}
7659
7660/// isVShiftLImm - Check if this is a valid build_vector for the immediate
7661/// operand of a vector shift left operation. That value must be in the range:
7662/// 0 <= Value < ElementBits for a left shift; or
7663/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00007664static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00007665 assert(VT.isVector() && "vector shift count is not a vector type");
7666 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
7667 if (! getVShiftImm(Op, ElementBits, Cnt))
7668 return false;
7669 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
7670}
7671
7672/// isVShiftRImm - Check if this is a valid build_vector for the immediate
7673/// operand of a vector shift right operation. For a shift opcode, the value
7674/// is positive, but for an intrinsic the value count must be negative. The
7675/// absolute value must be in the range:
7676/// 1 <= |Value| <= ElementBits for a right shift; or
7677/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00007678static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00007679 int64_t &Cnt) {
7680 assert(VT.isVector() && "vector shift count is not a vector type");
7681 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
7682 if (! getVShiftImm(Op, ElementBits, Cnt))
7683 return false;
7684 if (isIntrinsic)
7685 Cnt = -Cnt;
7686 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
7687}
7688
7689/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
7690static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
7691 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
7692 switch (IntNo) {
7693 default:
7694 // Don't do anything for most intrinsics.
7695 break;
7696
7697 // Vector shifts: check for immediate versions and lower them.
7698 // Note: This is done during DAG combining instead of DAG legalizing because
7699 // the build_vectors for 64-bit vector element shift counts are generally
7700 // not legal, and it is hard to see their values after they get legalized to
7701 // loads from a constant pool.
7702 case Intrinsic::arm_neon_vshifts:
7703 case Intrinsic::arm_neon_vshiftu:
7704 case Intrinsic::arm_neon_vshiftls:
7705 case Intrinsic::arm_neon_vshiftlu:
7706 case Intrinsic::arm_neon_vshiftn:
7707 case Intrinsic::arm_neon_vrshifts:
7708 case Intrinsic::arm_neon_vrshiftu:
7709 case Intrinsic::arm_neon_vrshiftn:
7710 case Intrinsic::arm_neon_vqshifts:
7711 case Intrinsic::arm_neon_vqshiftu:
7712 case Intrinsic::arm_neon_vqshiftsu:
7713 case Intrinsic::arm_neon_vqshiftns:
7714 case Intrinsic::arm_neon_vqshiftnu:
7715 case Intrinsic::arm_neon_vqshiftnsu:
7716 case Intrinsic::arm_neon_vqrshiftns:
7717 case Intrinsic::arm_neon_vqrshiftnu:
7718 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00007719 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007720 int64_t Cnt;
7721 unsigned VShiftOpc = 0;
7722
7723 switch (IntNo) {
7724 case Intrinsic::arm_neon_vshifts:
7725 case Intrinsic::arm_neon_vshiftu:
7726 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
7727 VShiftOpc = ARMISD::VSHL;
7728 break;
7729 }
7730 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
7731 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
7732 ARMISD::VSHRs : ARMISD::VSHRu);
7733 break;
7734 }
7735 return SDValue();
7736
7737 case Intrinsic::arm_neon_vshiftls:
7738 case Intrinsic::arm_neon_vshiftlu:
7739 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
7740 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007741 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007742
7743 case Intrinsic::arm_neon_vrshifts:
7744 case Intrinsic::arm_neon_vrshiftu:
7745 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
7746 break;
7747 return SDValue();
7748
7749 case Intrinsic::arm_neon_vqshifts:
7750 case Intrinsic::arm_neon_vqshiftu:
7751 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
7752 break;
7753 return SDValue();
7754
7755 case Intrinsic::arm_neon_vqshiftsu:
7756 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
7757 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007758 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007759
7760 case Intrinsic::arm_neon_vshiftn:
7761 case Intrinsic::arm_neon_vrshiftn:
7762 case Intrinsic::arm_neon_vqshiftns:
7763 case Intrinsic::arm_neon_vqshiftnu:
7764 case Intrinsic::arm_neon_vqshiftnsu:
7765 case Intrinsic::arm_neon_vqrshiftns:
7766 case Intrinsic::arm_neon_vqrshiftnu:
7767 case Intrinsic::arm_neon_vqrshiftnsu:
7768 // Narrowing shifts require an immediate right shift.
7769 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
7770 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00007771 llvm_unreachable("invalid shift count for narrowing vector shift "
7772 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007773
7774 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00007775 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00007776 }
7777
7778 switch (IntNo) {
7779 case Intrinsic::arm_neon_vshifts:
7780 case Intrinsic::arm_neon_vshiftu:
7781 // Opcode already set above.
7782 break;
7783 case Intrinsic::arm_neon_vshiftls:
7784 case Intrinsic::arm_neon_vshiftlu:
7785 if (Cnt == VT.getVectorElementType().getSizeInBits())
7786 VShiftOpc = ARMISD::VSHLLi;
7787 else
7788 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
7789 ARMISD::VSHLLs : ARMISD::VSHLLu);
7790 break;
7791 case Intrinsic::arm_neon_vshiftn:
7792 VShiftOpc = ARMISD::VSHRN; break;
7793 case Intrinsic::arm_neon_vrshifts:
7794 VShiftOpc = ARMISD::VRSHRs; break;
7795 case Intrinsic::arm_neon_vrshiftu:
7796 VShiftOpc = ARMISD::VRSHRu; break;
7797 case Intrinsic::arm_neon_vrshiftn:
7798 VShiftOpc = ARMISD::VRSHRN; break;
7799 case Intrinsic::arm_neon_vqshifts:
7800 VShiftOpc = ARMISD::VQSHLs; break;
7801 case Intrinsic::arm_neon_vqshiftu:
7802 VShiftOpc = ARMISD::VQSHLu; break;
7803 case Intrinsic::arm_neon_vqshiftsu:
7804 VShiftOpc = ARMISD::VQSHLsu; break;
7805 case Intrinsic::arm_neon_vqshiftns:
7806 VShiftOpc = ARMISD::VQSHRNs; break;
7807 case Intrinsic::arm_neon_vqshiftnu:
7808 VShiftOpc = ARMISD::VQSHRNu; break;
7809 case Intrinsic::arm_neon_vqshiftnsu:
7810 VShiftOpc = ARMISD::VQSHRNsu; break;
7811 case Intrinsic::arm_neon_vqrshiftns:
7812 VShiftOpc = ARMISD::VQRSHRNs; break;
7813 case Intrinsic::arm_neon_vqrshiftnu:
7814 VShiftOpc = ARMISD::VQRSHRNu; break;
7815 case Intrinsic::arm_neon_vqrshiftnsu:
7816 VShiftOpc = ARMISD::VQRSHRNsu; break;
7817 }
7818
7819 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007820 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007821 }
7822
7823 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00007824 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007825 int64_t Cnt;
7826 unsigned VShiftOpc = 0;
7827
7828 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
7829 VShiftOpc = ARMISD::VSLI;
7830 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
7831 VShiftOpc = ARMISD::VSRI;
7832 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00007833 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007834 }
7835
7836 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
7837 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00007838 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007839 }
7840
7841 case Intrinsic::arm_neon_vqrshifts:
7842 case Intrinsic::arm_neon_vqrshiftu:
7843 // No immediate versions of these to check for.
7844 break;
7845 }
7846
7847 return SDValue();
7848}
7849
7850/// PerformShiftCombine - Checks for immediate versions of vector shifts and
7851/// lowers them. As with the vector shift intrinsics, this is done during DAG
7852/// combining instead of DAG legalizing because the build_vectors for 64-bit
7853/// vector element shift counts are generally not legal, and it is hard to see
7854/// their values after they get legalized to loads from a constant pool.
7855static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
7856 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00007857 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00007858
7859 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00007860 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7861 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00007862 return SDValue();
7863
7864 assert(ST->hasNEON() && "unexpected vector shift");
7865 int64_t Cnt;
7866
7867 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007868 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00007869
7870 case ISD::SHL:
7871 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
7872 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007873 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007874 break;
7875
7876 case ISD::SRA:
7877 case ISD::SRL:
7878 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
7879 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
7880 ARMISD::VSHRs : ARMISD::VSHRu);
7881 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007882 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007883 }
7884 }
7885 return SDValue();
7886}
7887
7888/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
7889/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
7890static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
7891 const ARMSubtarget *ST) {
7892 SDValue N0 = N->getOperand(0);
7893
7894 // Check for sign- and zero-extensions of vector extract operations of 8-
7895 // and 16-bit vector elements. NEON supports these directly. They are
7896 // handled during DAG combining because type legalization will promote them
7897 // to 32-bit types and it is messy to recognize the operations after that.
7898 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
7899 SDValue Vec = N0.getOperand(0);
7900 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00007901 EVT VT = N->getValueType(0);
7902 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007903 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7904
Owen Anderson825b72b2009-08-11 20:47:22 +00007905 if (VT == MVT::i32 &&
7906 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00007907 TLI.isTypeLegal(Vec.getValueType()) &&
7908 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00007909
7910 unsigned Opc = 0;
7911 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007912 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00007913 case ISD::SIGN_EXTEND:
7914 Opc = ARMISD::VGETLANEs;
7915 break;
7916 case ISD::ZERO_EXTEND:
7917 case ISD::ANY_EXTEND:
7918 Opc = ARMISD::VGETLANEu;
7919 break;
7920 }
7921 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
7922 }
7923 }
7924
7925 return SDValue();
7926}
7927
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007928/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
7929/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
7930static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
7931 const ARMSubtarget *ST) {
7932 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00007933 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007934 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
7935 // a NaN; only do the transformation when it matches that behavior.
7936
7937 // For now only do this when using NEON for FP operations; if using VFP, it
7938 // is not obvious that the benefit outweighs the cost of switching to the
7939 // NEON pipeline.
7940 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
7941 N->getValueType(0) != MVT::f32)
7942 return SDValue();
7943
7944 SDValue CondLHS = N->getOperand(0);
7945 SDValue CondRHS = N->getOperand(1);
7946 SDValue LHS = N->getOperand(2);
7947 SDValue RHS = N->getOperand(3);
7948 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
7949
7950 unsigned Opcode = 0;
7951 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00007952 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007953 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00007954 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007955 IsReversed = true ; // x CC y ? y : x
7956 } else {
7957 return SDValue();
7958 }
7959
Bob Wilsone742bb52010-02-24 22:15:53 +00007960 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007961 switch (CC) {
7962 default: break;
7963 case ISD::SETOLT:
7964 case ISD::SETOLE:
7965 case ISD::SETLT:
7966 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007967 case ISD::SETULT:
7968 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00007969 // If LHS is NaN, an ordered comparison will be false and the result will
7970 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
7971 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
7972 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
7973 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
7974 break;
7975 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
7976 // will return -0, so vmin can only be used for unsafe math or if one of
7977 // the operands is known to be nonzero.
7978 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00007979 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00007980 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
7981 break;
7982 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007983 break;
7984
7985 case ISD::SETOGT:
7986 case ISD::SETOGE:
7987 case ISD::SETGT:
7988 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007989 case ISD::SETUGT:
7990 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00007991 // If LHS is NaN, an ordered comparison will be false and the result will
7992 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
7993 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
7994 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
7995 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
7996 break;
7997 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
7998 // will return +0, so vmax can only be used for unsafe math or if one of
7999 // the operands is known to be nonzero.
8000 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008001 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00008002 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8003 break;
8004 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008005 break;
8006 }
8007
8008 if (!Opcode)
8009 return SDValue();
8010 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
8011}
8012
Evan Chenge721f5c2011-07-13 00:42:17 +00008013/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
8014SDValue
8015ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
8016 SDValue Cmp = N->getOperand(4);
8017 if (Cmp.getOpcode() != ARMISD::CMPZ)
8018 // Only looking at EQ and NE cases.
8019 return SDValue();
8020
8021 EVT VT = N->getValueType(0);
8022 DebugLoc dl = N->getDebugLoc();
8023 SDValue LHS = Cmp.getOperand(0);
8024 SDValue RHS = Cmp.getOperand(1);
8025 SDValue FalseVal = N->getOperand(0);
8026 SDValue TrueVal = N->getOperand(1);
8027 SDValue ARMcc = N->getOperand(2);
Jim Grosbachb04546f2011-09-13 20:30:37 +00008028 ARMCC::CondCodes CC =
8029 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chenge721f5c2011-07-13 00:42:17 +00008030
8031 // Simplify
8032 // mov r1, r0
8033 // cmp r1, x
8034 // mov r0, y
8035 // moveq r0, x
8036 // to
8037 // cmp r0, x
8038 // movne r0, y
8039 //
8040 // mov r1, r0
8041 // cmp r1, x
8042 // mov r0, x
8043 // movne r0, y
8044 // to
8045 // cmp r0, x
8046 // movne r0, y
8047 /// FIXME: Turn this into a target neutral optimization?
8048 SDValue Res;
Evan Cheng9b88d2d2011-09-28 23:16:31 +00008049 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chenge721f5c2011-07-13 00:42:17 +00008050 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
8051 N->getOperand(3), Cmp);
8052 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
8053 SDValue ARMcc;
8054 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
8055 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
8056 N->getOperand(3), NewCmp);
8057 }
8058
8059 if (Res.getNode()) {
8060 APInt KnownZero, KnownOne;
8061 APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits());
8062 DAG.ComputeMaskedBits(SDValue(N,0), Mask, KnownZero, KnownOne);
8063 // Capture demanded bits information that would be otherwise lost.
8064 if (KnownZero == 0xfffffffe)
8065 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8066 DAG.getValueType(MVT::i1));
8067 else if (KnownZero == 0xffffff00)
8068 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8069 DAG.getValueType(MVT::i8));
8070 else if (KnownZero == 0xffff0000)
8071 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8072 DAG.getValueType(MVT::i16));
8073 }
8074
8075 return Res;
8076}
8077
Dan Gohman475871a2008-07-27 21:46:04 +00008078SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008079 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008080 switch (N->getOpcode()) {
8081 default: break;
Tanya Lattner189531f2011-06-14 23:48:48 +00008082 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008083 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00008084 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008085 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00008086 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00008087 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00008088 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008089 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00008090 case ISD::STORE: return PerformSTORECombine(N, DCI);
8091 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
8092 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00008093 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008094 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00008095 case ISD::FP_TO_SINT:
8096 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
8097 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008098 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00008099 case ISD::SHL:
8100 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008101 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00008102 case ISD::SIGN_EXTEND:
8103 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008104 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
8105 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00008106 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00008107 case ARMISD::VLD2DUP:
8108 case ARMISD::VLD3DUP:
8109 case ARMISD::VLD4DUP:
8110 return CombineBaseUpdate(N, DCI);
8111 case ISD::INTRINSIC_VOID:
8112 case ISD::INTRINSIC_W_CHAIN:
8113 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
8114 case Intrinsic::arm_neon_vld1:
8115 case Intrinsic::arm_neon_vld2:
8116 case Intrinsic::arm_neon_vld3:
8117 case Intrinsic::arm_neon_vld4:
8118 case Intrinsic::arm_neon_vld2lane:
8119 case Intrinsic::arm_neon_vld3lane:
8120 case Intrinsic::arm_neon_vld4lane:
8121 case Intrinsic::arm_neon_vst1:
8122 case Intrinsic::arm_neon_vst2:
8123 case Intrinsic::arm_neon_vst3:
8124 case Intrinsic::arm_neon_vst4:
8125 case Intrinsic::arm_neon_vst2lane:
8126 case Intrinsic::arm_neon_vst3lane:
8127 case Intrinsic::arm_neon_vst4lane:
8128 return CombineBaseUpdate(N, DCI);
8129 default: break;
8130 }
8131 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008132 }
Dan Gohman475871a2008-07-27 21:46:04 +00008133 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008134}
8135
Evan Cheng31959b12011-02-02 01:06:55 +00008136bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
8137 EVT VT) const {
8138 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
8139}
8140
Bill Wendlingaf566342009-08-15 21:21:19 +00008141bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00008142 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00008143 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00008144
8145 switch (VT.getSimpleVT().SimpleTy) {
8146 default:
8147 return false;
8148 case MVT::i8:
8149 case MVT::i16:
8150 case MVT::i32:
8151 return true;
8152 // FIXME: VLD1 etc with standard alignment is legal.
8153 }
8154}
8155
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008156static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
8157 unsigned AlignCheck) {
8158 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
8159 (DstAlign == 0 || DstAlign % AlignCheck == 0));
8160}
8161
8162EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
8163 unsigned DstAlign, unsigned SrcAlign,
Lang Hamesa1e78882011-11-02 23:37:04 +00008164 bool IsZeroVal,
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008165 bool MemcpyStrSrc,
8166 MachineFunction &MF) const {
8167 const Function *F = MF.getFunction();
8168
8169 // See if we can use NEON instructions for this...
Lang Hamesa1e78882011-11-02 23:37:04 +00008170 if (IsZeroVal &&
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008171 !F->hasFnAttr(Attribute::NoImplicitFloat) &&
8172 Subtarget->hasNEON()) {
8173 if (memOpAlign(SrcAlign, DstAlign, 16) && Size >= 16) {
8174 return MVT::v4i32;
8175 } else if (memOpAlign(SrcAlign, DstAlign, 8) && Size >= 8) {
8176 return MVT::v2i32;
8177 }
8178 }
8179
Lang Hames5207bf22011-11-08 18:56:23 +00008180 // Lowering to i32/i16 if the size permits.
8181 if (Size >= 4) {
8182 return MVT::i32;
8183 } else if (Size >= 2) {
8184 return MVT::i16;
8185 }
8186
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008187 // Let the target-independent logic figure it out.
8188 return MVT::Other;
8189}
8190
Evan Chenge6c835f2009-08-14 20:09:37 +00008191static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
8192 if (V < 0)
8193 return false;
8194
8195 unsigned Scale = 1;
8196 switch (VT.getSimpleVT().SimpleTy) {
8197 default: return false;
8198 case MVT::i1:
8199 case MVT::i8:
8200 // Scale == 1;
8201 break;
8202 case MVT::i16:
8203 // Scale == 2;
8204 Scale = 2;
8205 break;
8206 case MVT::i32:
8207 // Scale == 4;
8208 Scale = 4;
8209 break;
8210 }
8211
8212 if ((V & (Scale - 1)) != 0)
8213 return false;
8214 V /= Scale;
8215 return V == (V & ((1LL << 5) - 1));
8216}
8217
8218static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
8219 const ARMSubtarget *Subtarget) {
8220 bool isNeg = false;
8221 if (V < 0) {
8222 isNeg = true;
8223 V = - V;
8224 }
8225
8226 switch (VT.getSimpleVT().SimpleTy) {
8227 default: return false;
8228 case MVT::i1:
8229 case MVT::i8:
8230 case MVT::i16:
8231 case MVT::i32:
8232 // + imm12 or - imm8
8233 if (isNeg)
8234 return V == (V & ((1LL << 8) - 1));
8235 return V == (V & ((1LL << 12) - 1));
8236 case MVT::f32:
8237 case MVT::f64:
8238 // Same as ARM mode. FIXME: NEON?
8239 if (!Subtarget->hasVFP2())
8240 return false;
8241 if ((V & 3) != 0)
8242 return false;
8243 V >>= 2;
8244 return V == (V & ((1LL << 8) - 1));
8245 }
8246}
8247
Evan Chengb01fad62007-03-12 23:30:29 +00008248/// isLegalAddressImmediate - Return true if the integer value can be used
8249/// as the offset of the target addressing mode for load / store of the
8250/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00008251static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00008252 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00008253 if (V == 0)
8254 return true;
8255
Evan Cheng65011532009-03-09 19:15:00 +00008256 if (!VT.isSimple())
8257 return false;
8258
Evan Chenge6c835f2009-08-14 20:09:37 +00008259 if (Subtarget->isThumb1Only())
8260 return isLegalT1AddressImmediate(V, VT);
8261 else if (Subtarget->isThumb2())
8262 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00008263
Evan Chenge6c835f2009-08-14 20:09:37 +00008264 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00008265 if (V < 0)
8266 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00008267 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00008268 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008269 case MVT::i1:
8270 case MVT::i8:
8271 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00008272 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008273 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008274 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00008275 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008276 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008277 case MVT::f32:
8278 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00008279 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00008280 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00008281 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00008282 return false;
8283 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008284 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00008285 }
Evan Chenga8e29892007-01-19 07:51:42 +00008286}
8287
Evan Chenge6c835f2009-08-14 20:09:37 +00008288bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
8289 EVT VT) const {
8290 int Scale = AM.Scale;
8291 if (Scale < 0)
8292 return false;
8293
8294 switch (VT.getSimpleVT().SimpleTy) {
8295 default: return false;
8296 case MVT::i1:
8297 case MVT::i8:
8298 case MVT::i16:
8299 case MVT::i32:
8300 if (Scale == 1)
8301 return true;
8302 // r + r << imm
8303 Scale = Scale & ~1;
8304 return Scale == 2 || Scale == 4 || Scale == 8;
8305 case MVT::i64:
8306 // r + r
8307 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
8308 return true;
8309 return false;
8310 case MVT::isVoid:
8311 // Note, we allow "void" uses (basically, uses that aren't loads or
8312 // stores), because arm allows folding a scale into many arithmetic
8313 // operations. This should be made more precise and revisited later.
8314
8315 // Allow r << imm, but the imm has to be a multiple of two.
8316 if (Scale & 1) return false;
8317 return isPowerOf2_32(Scale);
8318 }
8319}
8320
Chris Lattner37caf8c2007-04-09 23:33:39 +00008321/// isLegalAddressingMode - Return true if the addressing mode represented
8322/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008323bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008324 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008325 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00008326 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00008327 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008328
Chris Lattner37caf8c2007-04-09 23:33:39 +00008329 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008330 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008331 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008332
Chris Lattner37caf8c2007-04-09 23:33:39 +00008333 switch (AM.Scale) {
8334 case 0: // no scale reg, must be "r+i" or "r", or "i".
8335 break;
8336 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00008337 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00008338 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008339 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00008340 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008341 // ARM doesn't support any R+R*scale+imm addr modes.
8342 if (AM.BaseOffs)
8343 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008344
Bob Wilson2c7dab12009-04-08 17:55:28 +00008345 if (!VT.isSimple())
8346 return false;
8347
Evan Chenge6c835f2009-08-14 20:09:37 +00008348 if (Subtarget->isThumb2())
8349 return isLegalT2ScaledAddressingMode(AM, VT);
8350
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008351 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00008352 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00008353 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008354 case MVT::i1:
8355 case MVT::i8:
8356 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008357 if (Scale < 0) Scale = -Scale;
8358 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008359 return true;
8360 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00008361 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008362 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00008363 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00008364 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008365 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008366 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00008367 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008368
Owen Anderson825b72b2009-08-11 20:47:22 +00008369 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00008370 // Note, we allow "void" uses (basically, uses that aren't loads or
8371 // stores), because arm allows folding a scale into many arithmetic
8372 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008373
Chris Lattner37caf8c2007-04-09 23:33:39 +00008374 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00008375 if (Scale & 1) return false;
8376 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00008377 }
Evan Chengb01fad62007-03-12 23:30:29 +00008378 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00008379 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00008380}
8381
Evan Cheng77e47512009-11-11 19:05:52 +00008382/// isLegalICmpImmediate - Return true if the specified immediate is legal
8383/// icmp immediate, that is the target has icmp instructions which can compare
8384/// a register against the immediate without having to materialize the
8385/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00008386bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00008387 if (!Subtarget->isThumb())
8388 return ARM_AM::getSOImmVal(Imm) != -1;
8389 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00008390 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00008391 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00008392}
8393
Dan Gohmancca82142011-05-03 00:46:49 +00008394/// isLegalAddImmediate - Return true if the specified immediate is legal
8395/// add immediate, that is the target has add instructions which can add
8396/// a register with the immediate without having to materialize the
8397/// immediate into a register.
8398bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
8399 return ARM_AM::getSOImmVal(Imm) != -1;
8400}
8401
Owen Andersone50ed302009-08-10 22:56:29 +00008402static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00008403 bool isSEXTLoad, SDValue &Base,
8404 SDValue &Offset, bool &isInc,
8405 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00008406 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
8407 return false;
8408
Owen Anderson825b72b2009-08-11 20:47:22 +00008409 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00008410 // AddressingMode 3
8411 Base = Ptr->getOperand(0);
8412 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008413 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00008414 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008415 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00008416 isInc = false;
8417 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8418 return true;
8419 }
8420 }
8421 isInc = (Ptr->getOpcode() == ISD::ADD);
8422 Offset = Ptr->getOperand(1);
8423 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00008424 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00008425 // AddressingMode 2
8426 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008427 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00008428 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008429 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00008430 isInc = false;
8431 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8432 Base = Ptr->getOperand(0);
8433 return true;
8434 }
8435 }
8436
8437 if (Ptr->getOpcode() == ISD::ADD) {
8438 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00008439 ARM_AM::ShiftOpc ShOpcVal=
8440 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00008441 if (ShOpcVal != ARM_AM::no_shift) {
8442 Base = Ptr->getOperand(1);
8443 Offset = Ptr->getOperand(0);
8444 } else {
8445 Base = Ptr->getOperand(0);
8446 Offset = Ptr->getOperand(1);
8447 }
8448 return true;
8449 }
8450
8451 isInc = (Ptr->getOpcode() == ISD::ADD);
8452 Base = Ptr->getOperand(0);
8453 Offset = Ptr->getOperand(1);
8454 return true;
8455 }
8456
Jim Grosbache5165492009-11-09 00:11:35 +00008457 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00008458 return false;
8459}
8460
Owen Andersone50ed302009-08-10 22:56:29 +00008461static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00008462 bool isSEXTLoad, SDValue &Base,
8463 SDValue &Offset, bool &isInc,
8464 SelectionDAG &DAG) {
8465 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
8466 return false;
8467
8468 Base = Ptr->getOperand(0);
8469 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
8470 int RHSC = (int)RHS->getZExtValue();
8471 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
8472 assert(Ptr->getOpcode() == ISD::ADD);
8473 isInc = false;
8474 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8475 return true;
8476 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
8477 isInc = Ptr->getOpcode() == ISD::ADD;
8478 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
8479 return true;
8480 }
8481 }
8482
8483 return false;
8484}
8485
Evan Chenga8e29892007-01-19 07:51:42 +00008486/// getPreIndexedAddressParts - returns true by value, base pointer and
8487/// offset pointer and addressing mode by reference if the node's address
8488/// can be legally represented as pre-indexed load / store address.
8489bool
Dan Gohman475871a2008-07-27 21:46:04 +00008490ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
8491 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00008492 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00008493 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008494 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00008495 return false;
8496
Owen Andersone50ed302009-08-10 22:56:29 +00008497 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00008498 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00008499 bool isSEXTLoad = false;
8500 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
8501 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008502 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00008503 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
8504 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
8505 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008506 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00008507 } else
8508 return false;
8509
8510 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00008511 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00008512 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00008513 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
8514 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00008515 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00008516 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00008517 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00008518 if (!isLegal)
8519 return false;
8520
8521 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
8522 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00008523}
8524
8525/// getPostIndexedAddressParts - returns true by value, base pointer and
8526/// offset pointer and addressing mode by reference if this node can be
8527/// combined with a load / store to form a post-indexed load / store.
8528bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00008529 SDValue &Base,
8530 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00008531 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00008532 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008533 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00008534 return false;
8535
Owen Andersone50ed302009-08-10 22:56:29 +00008536 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00008537 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00008538 bool isSEXTLoad = false;
8539 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008540 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00008541 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00008542 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
8543 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008544 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00008545 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00008546 } else
8547 return false;
8548
8549 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00008550 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00008551 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00008552 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00008553 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00008554 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00008555 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
8556 isInc, DAG);
8557 if (!isLegal)
8558 return false;
8559
Evan Cheng28dad2a2010-05-18 21:31:17 +00008560 if (Ptr != Base) {
8561 // Swap base ptr and offset to catch more post-index load / store when
8562 // it's legal. In Thumb2 mode, offset must be an immediate.
8563 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
8564 !Subtarget->isThumb2())
8565 std::swap(Base, Offset);
8566
8567 // Post-indexed load / store update the base pointer.
8568 if (Ptr != Base)
8569 return false;
8570 }
8571
Evan Chenge88d5ce2009-07-02 07:28:31 +00008572 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
8573 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00008574}
8575
Dan Gohman475871a2008-07-27 21:46:04 +00008576void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00008577 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008578 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008579 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008580 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00008581 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008582 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00008583 switch (Op.getOpcode()) {
8584 default: break;
8585 case ARMISD::CMOV: {
8586 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00008587 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00008588 if (KnownZero == 0 && KnownOne == 0) return;
8589
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008590 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00008591 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
8592 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00008593 KnownZero &= KnownZeroRHS;
8594 KnownOne &= KnownOneRHS;
8595 return;
8596 }
8597 }
8598}
8599
8600//===----------------------------------------------------------------------===//
8601// ARM Inline Assembly Support
8602//===----------------------------------------------------------------------===//
8603
Evan Cheng55d42002011-01-08 01:24:27 +00008604bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
8605 // Looking for "rev" which is V6+.
8606 if (!Subtarget->hasV6Ops())
8607 return false;
8608
8609 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
8610 std::string AsmStr = IA->getAsmString();
8611 SmallVector<StringRef, 4> AsmPieces;
8612 SplitString(AsmStr, AsmPieces, ";\n");
8613
8614 switch (AsmPieces.size()) {
8615 default: return false;
8616 case 1:
8617 AsmStr = AsmPieces[0];
8618 AsmPieces.clear();
8619 SplitString(AsmStr, AsmPieces, " \t,");
8620
8621 // rev $0, $1
8622 if (AsmPieces.size() == 3 &&
8623 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
8624 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008625 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00008626 if (Ty && Ty->getBitWidth() == 32)
8627 return IntrinsicLowering::LowerToByteSwap(CI);
8628 }
8629 break;
8630 }
8631
8632 return false;
8633}
8634
Evan Chenga8e29892007-01-19 07:51:42 +00008635/// getConstraintType - Given a constraint letter, return the type of
8636/// constraint it is for this target.
8637ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00008638ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
8639 if (Constraint.size() == 1) {
8640 switch (Constraint[0]) {
8641 default: break;
8642 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008643 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00008644 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00008645 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008646 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00008647 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00008648 // An address with a single base register. Due to the way we
8649 // currently handle addresses it is the same as an 'r' memory constraint.
8650 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00008651 }
Eric Christopher1312ca82011-06-21 22:10:57 +00008652 } else if (Constraint.size() == 2) {
8653 switch (Constraint[0]) {
8654 default: break;
8655 // All 'U+' constraints are addresses.
8656 case 'U': return C_Memory;
8657 }
Evan Chenga8e29892007-01-19 07:51:42 +00008658 }
Chris Lattner4234f572007-03-25 02:14:49 +00008659 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00008660}
8661
John Thompson44ab89e2010-10-29 17:29:13 +00008662/// Examine constraint type and operand type and determine a weight value.
8663/// This object must already have been set up with the operand type
8664/// and the current alternative constraint selected.
8665TargetLowering::ConstraintWeight
8666ARMTargetLowering::getSingleConstraintMatchWeight(
8667 AsmOperandInfo &info, const char *constraint) const {
8668 ConstraintWeight weight = CW_Invalid;
8669 Value *CallOperandVal = info.CallOperandVal;
8670 // If we don't have a value, we can't do a match,
8671 // but allow it at the lowest weight.
8672 if (CallOperandVal == NULL)
8673 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008674 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00008675 // Look at the constraint type.
8676 switch (*constraint) {
8677 default:
8678 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
8679 break;
8680 case 'l':
8681 if (type->isIntegerTy()) {
8682 if (Subtarget->isThumb())
8683 weight = CW_SpecificReg;
8684 else
8685 weight = CW_Register;
8686 }
8687 break;
8688 case 'w':
8689 if (type->isFloatingPointTy())
8690 weight = CW_Register;
8691 break;
8692 }
8693 return weight;
8694}
8695
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008696typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
8697RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00008698ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00008699 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00008700 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00008701 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00008702 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00008703 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00008704 if (Subtarget->isThumb())
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008705 return RCPair(0U, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +00008706 else
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008707 return RCPair(0U, ARM::GPRRegisterClass);
Eric Christopher73744df2011-06-30 23:23:01 +00008708 case 'h': // High regs or no regs.
8709 if (Subtarget->isThumb())
Andrew Trick3af7a672011-09-20 03:06:13 +00008710 return RCPair(0U, ARM::hGPRRegisterClass);
Eric Christopher1070f822011-07-01 00:19:27 +00008711 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008712 case 'r':
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008713 return RCPair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008714 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00008715 if (VT == MVT::f32)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008716 return RCPair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00008717 if (VT.getSizeInBits() == 64)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008718 return RCPair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00008719 if (VT.getSizeInBits() == 128)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008720 return RCPair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008721 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00008722 case 'x':
8723 if (VT == MVT::f32)
Andrew Trick3af7a672011-09-20 03:06:13 +00008724 return RCPair(0U, ARM::SPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008725 if (VT.getSizeInBits() == 64)
Andrew Trick3af7a672011-09-20 03:06:13 +00008726 return RCPair(0U, ARM::DPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008727 if (VT.getSizeInBits() == 128)
Andrew Trick3af7a672011-09-20 03:06:13 +00008728 return RCPair(0U, ARM::QPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008729 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008730 case 't':
8731 if (VT == MVT::f32)
Andrew Trick3af7a672011-09-20 03:06:13 +00008732 return RCPair(0U, ARM::SPRRegisterClass);
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008733 break;
Evan Chenga8e29892007-01-19 07:51:42 +00008734 }
8735 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00008736 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00008737 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00008738
Evan Chenga8e29892007-01-19 07:51:42 +00008739 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
8740}
8741
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008742/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
8743/// vector. If it is invalid, don't add anything to Ops.
8744void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00008745 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008746 std::vector<SDValue>&Ops,
8747 SelectionDAG &DAG) const {
8748 SDValue Result(0, 0);
8749
Eric Christopher100c8332011-06-02 23:16:42 +00008750 // Currently only support length 1 constraints.
8751 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00008752
Eric Christopher100c8332011-06-02 23:16:42 +00008753 char ConstraintLetter = Constraint[0];
8754 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008755 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00008756 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008757 case 'I': case 'J': case 'K': case 'L':
8758 case 'M': case 'N': case 'O':
8759 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
8760 if (!C)
8761 return;
8762
8763 int64_t CVal64 = C->getSExtValue();
8764 int CVal = (int) CVal64;
8765 // None of these constraints allow values larger than 32 bits. Check
8766 // that the value fits in an int.
8767 if (CVal != CVal64)
8768 return;
8769
Eric Christopher100c8332011-06-02 23:16:42 +00008770 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00008771 case 'j':
Andrew Trick3af7a672011-09-20 03:06:13 +00008772 // Constant suitable for movw, must be between 0 and
8773 // 65535.
8774 if (Subtarget->hasV6T2Ops())
8775 if (CVal >= 0 && CVal <= 65535)
8776 break;
8777 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008778 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008779 if (Subtarget->isThumb1Only()) {
8780 // This must be a constant between 0 and 255, for ADD
8781 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008782 if (CVal >= 0 && CVal <= 255)
8783 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008784 } else if (Subtarget->isThumb2()) {
8785 // A constant that can be used as an immediate value in a
8786 // data-processing instruction.
8787 if (ARM_AM::getT2SOImmVal(CVal) != -1)
8788 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008789 } else {
8790 // A constant that can be used as an immediate value in a
8791 // data-processing instruction.
8792 if (ARM_AM::getSOImmVal(CVal) != -1)
8793 break;
8794 }
8795 return;
8796
8797 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008798 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008799 // This must be a constant between -255 and -1, for negated ADD
8800 // immediates. This can be used in GCC with an "n" modifier that
8801 // prints the negated value, for use with SUB instructions. It is
8802 // not useful otherwise but is implemented for compatibility.
8803 if (CVal >= -255 && CVal <= -1)
8804 break;
8805 } else {
8806 // This must be a constant between -4095 and 4095. It is not clear
8807 // what this constraint is intended for. Implemented for
8808 // compatibility with GCC.
8809 if (CVal >= -4095 && CVal <= 4095)
8810 break;
8811 }
8812 return;
8813
8814 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008815 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008816 // A 32-bit value where only one byte has a nonzero value. Exclude
8817 // zero to match GCC. This constraint is used by GCC internally for
8818 // constants that can be loaded with a move/shift combination.
8819 // It is not useful otherwise but is implemented for compatibility.
8820 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
8821 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008822 } else if (Subtarget->isThumb2()) {
8823 // A constant whose bitwise inverse can be used as an immediate
8824 // value in a data-processing instruction. This can be used in GCC
8825 // with a "B" modifier that prints the inverted value, for use with
8826 // BIC and MVN instructions. It is not useful otherwise but is
8827 // implemented for compatibility.
8828 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
8829 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008830 } else {
8831 // A constant whose bitwise inverse can be used as an immediate
8832 // value in a data-processing instruction. This can be used in GCC
8833 // with a "B" modifier that prints the inverted value, for use with
8834 // BIC and MVN instructions. It is not useful otherwise but is
8835 // implemented for compatibility.
8836 if (ARM_AM::getSOImmVal(~CVal) != -1)
8837 break;
8838 }
8839 return;
8840
8841 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008842 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008843 // This must be a constant between -7 and 7,
8844 // for 3-operand ADD/SUB immediate instructions.
8845 if (CVal >= -7 && CVal < 7)
8846 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008847 } else if (Subtarget->isThumb2()) {
8848 // A constant whose negation can be used as an immediate value in a
8849 // data-processing instruction. This can be used in GCC with an "n"
8850 // modifier that prints the negated value, for use with SUB
8851 // instructions. It is not useful otherwise but is implemented for
8852 // compatibility.
8853 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
8854 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008855 } else {
8856 // A constant whose negation can be used as an immediate value in a
8857 // data-processing instruction. This can be used in GCC with an "n"
8858 // modifier that prints the negated value, for use with SUB
8859 // instructions. It is not useful otherwise but is implemented for
8860 // compatibility.
8861 if (ARM_AM::getSOImmVal(-CVal) != -1)
8862 break;
8863 }
8864 return;
8865
8866 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008867 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008868 // This must be a multiple of 4 between 0 and 1020, for
8869 // ADD sp + immediate.
8870 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
8871 break;
8872 } else {
8873 // A power of two or a constant between 0 and 32. This is used in
8874 // GCC for the shift amount on shifted register operands, but it is
8875 // useful in general for any shift amounts.
8876 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
8877 break;
8878 }
8879 return;
8880
8881 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008882 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008883 // This must be a constant between 0 and 31, for shift amounts.
8884 if (CVal >= 0 && CVal <= 31)
8885 break;
8886 }
8887 return;
8888
8889 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008890 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008891 // This must be a multiple of 4 between -508 and 508, for
8892 // ADD/SUB sp = sp + immediate.
8893 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
8894 break;
8895 }
8896 return;
8897 }
8898 Result = DAG.getTargetConstant(CVal, Op.getValueType());
8899 break;
8900 }
8901
8902 if (Result.getNode()) {
8903 Ops.push_back(Result);
8904 return;
8905 }
Dale Johannesen1784d162010-06-25 21:55:36 +00008906 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008907}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00008908
8909bool
8910ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
8911 // The ARM target isn't yet aware of offsets.
8912 return false;
8913}
Evan Cheng39382422009-10-28 01:44:26 +00008914
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008915bool ARM::isBitFieldInvertedMask(unsigned v) {
8916 if (v == 0xffffffff)
8917 return 0;
8918 // there can be 1's on either or both "outsides", all the "inside"
8919 // bits must be 0's
8920 unsigned int lsb = 0, msb = 31;
8921 while (v & (1 << msb)) --msb;
8922 while (v & (1 << lsb)) ++lsb;
8923 for (unsigned int i = lsb; i <= msb; ++i) {
8924 if (v & (1 << i))
8925 return 0;
8926 }
8927 return 1;
8928}
8929
Evan Cheng39382422009-10-28 01:44:26 +00008930/// isFPImmLegal - Returns true if the target can instruction select the
8931/// specified FP immediate natively. If false, the legalizer will
8932/// materialize the FP immediate as a load from a constant pool.
8933bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
8934 if (!Subtarget->hasVFP3())
8935 return false;
8936 if (VT == MVT::f32)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00008937 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00008938 if (VT == MVT::f64)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00008939 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00008940 return false;
8941}
Bob Wilson65ffec42010-09-21 17:56:22 +00008942
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008943/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00008944/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
8945/// specified in the intrinsic calls.
8946bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
8947 const CallInst &I,
8948 unsigned Intrinsic) const {
8949 switch (Intrinsic) {
8950 case Intrinsic::arm_neon_vld1:
8951 case Intrinsic::arm_neon_vld2:
8952 case Intrinsic::arm_neon_vld3:
8953 case Intrinsic::arm_neon_vld4:
8954 case Intrinsic::arm_neon_vld2lane:
8955 case Intrinsic::arm_neon_vld3lane:
8956 case Intrinsic::arm_neon_vld4lane: {
8957 Info.opc = ISD::INTRINSIC_W_CHAIN;
8958 // Conservatively set memVT to the entire set of vectors loaded.
8959 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
8960 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
8961 Info.ptrVal = I.getArgOperand(0);
8962 Info.offset = 0;
8963 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
8964 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
8965 Info.vol = false; // volatile loads with NEON intrinsics not supported
8966 Info.readMem = true;
8967 Info.writeMem = false;
8968 return true;
8969 }
8970 case Intrinsic::arm_neon_vst1:
8971 case Intrinsic::arm_neon_vst2:
8972 case Intrinsic::arm_neon_vst3:
8973 case Intrinsic::arm_neon_vst4:
8974 case Intrinsic::arm_neon_vst2lane:
8975 case Intrinsic::arm_neon_vst3lane:
8976 case Intrinsic::arm_neon_vst4lane: {
8977 Info.opc = ISD::INTRINSIC_VOID;
8978 // Conservatively set memVT to the entire set of vectors stored.
8979 unsigned NumElts = 0;
8980 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008981 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +00008982 if (!ArgTy->isVectorTy())
8983 break;
8984 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
8985 }
8986 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
8987 Info.ptrVal = I.getArgOperand(0);
8988 Info.offset = 0;
8989 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
8990 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
8991 Info.vol = false; // volatile stores with NEON intrinsics not supported
8992 Info.readMem = false;
8993 Info.writeMem = true;
8994 return true;
8995 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00008996 case Intrinsic::arm_strexd: {
8997 Info.opc = ISD::INTRINSIC_W_CHAIN;
8998 Info.memVT = MVT::i64;
8999 Info.ptrVal = I.getArgOperand(2);
9000 Info.offset = 0;
9001 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009002 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009003 Info.readMem = false;
9004 Info.writeMem = true;
9005 return true;
9006 }
9007 case Intrinsic::arm_ldrexd: {
9008 Info.opc = ISD::INTRINSIC_W_CHAIN;
9009 Info.memVT = MVT::i64;
9010 Info.ptrVal = I.getArgOperand(0);
9011 Info.offset = 0;
9012 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009013 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009014 Info.readMem = true;
9015 Info.writeMem = false;
9016 return true;
9017 }
Bob Wilson65ffec42010-09-21 17:56:22 +00009018 default:
9019 break;
9020 }
9021
9022 return false;
9023}