blob: c6c1f5be935c9010131988d68569cc8b26b2195c [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 Chenga8e29892007-01-19 07:51:42 +000046#include "llvm/ADT/VectorExtras.h"
Evan Cheng55d42002011-01-08 01:24:27 +000047#include "llvm/ADT/StringExtras.h"
Dale Johannesen51e28e62010-06-03 21:09:53 +000048#include "llvm/ADT/Statistic.h"
Jim Grosbache7b52522010-04-14 22:28:31 +000049#include "llvm/Support/CommandLine.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000050#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000051#include "llvm/Support/MathExtras.h"
Jim Grosbache801dc42009-12-12 01:40:06 +000052#include "llvm/Support/raw_ostream.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000053#include <sstream>
Evan Chenga8e29892007-01-19 07:51:42 +000054using namespace llvm;
55
Dale Johannesen51e28e62010-06-03 21:09:53 +000056STATISTIC(NumTailCalls, "Number of tail calls");
Evan Chengfc8475b2011-01-19 02:16:49 +000057STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Dale Johannesen51e28e62010-06-03 21:09:53 +000058
Bob Wilson703af3a2010-08-13 22:43:33 +000059// This option should go away when tail calls fully work.
60static cl::opt<bool>
61EnableARMTailCalls("arm-tail-calls", cl::Hidden,
62 cl::desc("Generate tail calls (TEMPORARY OPTION)."),
63 cl::init(false));
64
Eric Christopher836c6242010-12-15 23:47:29 +000065cl::opt<bool>
Jim Grosbache7b52522010-04-14 22:28:31 +000066EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng515fe3a2010-07-08 02:08:50 +000067 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbache7b52522010-04-14 22:28:31 +000068 cl::init(false));
69
Evan Cheng46df4eb2010-06-16 07:35:02 +000070static cl::opt<bool>
71ARMInterworking("arm-interworking", cl::Hidden,
72 cl::desc("Enable / disable ARM interworking (for debugging only)"),
73 cl::init(true));
74
Benjamin Kramer0861f572011-11-26 23:01:57 +000075namespace {
Cameron Zwaricha86686e2011-06-10 20:59:24 +000076 class ARMCCState : public CCState {
77 public:
78 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
79 const TargetMachine &TM, SmallVector<CCValAssign, 16> &locs,
80 LLVMContext &C, ParmContext PC)
81 : CCState(CC, isVarArg, MF, TM, locs, C) {
82 assert(((PC == Call) || (PC == Prologue)) &&
83 "ARMCCState users must specify whether their context is call"
84 "or prologue generation.");
85 CallOrPrologue = PC;
86 }
87 };
88}
89
Stuart Hastingsc7315872011-04-20 16:47:52 +000090// The APCS parameter registers.
91static const unsigned GPRArgRegs[] = {
92 ARM::R0, ARM::R1, ARM::R2, ARM::R3
93};
94
Owen Andersone50ed302009-08-10 22:56:29 +000095void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
96 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000097 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000098 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000099 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
100 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000101
Owen Anderson70671842009-08-10 20:18:46 +0000102 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000103 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000104 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000105 }
106
Owen Andersone50ed302009-08-10 22:56:29 +0000107 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000108 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Duncan Sands28b77e92011-09-06 19:07:46 +0000109 setOperationAction(ISD::SETCC, VT.getSimpleVT(), Custom);
Eli Friedman5c89cb82011-10-24 23:08:52 +0000110 setOperationAction(ISD::INSERT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson3468c2e2010-11-03 16:24:50 +0000111 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Eli Friedman14e809c2011-11-09 23:36:02 +0000112 if (ElemTy == MVT::i32) {
113 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Custom);
114 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Custom);
115 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Custom);
116 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Custom);
117 } else {
Bob Wilson0696fdf2009-09-16 20:20:44 +0000118 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
119 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
120 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
121 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
122 }
Owen Anderson70671842009-08-10 20:18:46 +0000123 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
124 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Bob Wilson07f6e802010-06-16 21:34:01 +0000125 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
Bob Wilson5e8b8332011-01-07 04:59:04 +0000126 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Legal);
Bob Wilsond0910c42010-04-06 22:02:24 +0000127 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
128 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Eli Friedman15f58c52011-11-11 03:16:38 +0000129 setOperationAction(ISD::SIGN_EXTEND_INREG, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000130 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000131 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
132 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
133 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +0000134 }
135
136 // Promote all bit-wise operations.
137 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000138 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000139 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
140 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000141 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000142 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000143 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000144 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000145 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000146 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000147 }
Bob Wilson16330762009-09-16 00:17:28 +0000148
149 // Neon does not support vector divide/remainder operations.
150 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
151 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
152 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
153 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
154 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
155 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000156}
157
Owen Andersone50ed302009-08-10 22:56:29 +0000158void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000159 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000160 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000161}
162
Owen Andersone50ed302009-08-10 22:56:29 +0000163void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000164 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000165 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000166}
167
Chris Lattnerf0144122009-07-28 03:13:23 +0000168static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
169 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000170 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000171
Chris Lattner80ec2792009-08-02 00:34:36 +0000172 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000173}
174
Evan Chenga8e29892007-01-19 07:51:42 +0000175ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000176 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000177 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng31446872010-07-23 22:39:59 +0000178 RegInfo = TM.getRegisterInfo();
Evan Cheng3ef1c872010-09-10 01:29:16 +0000179 Itins = TM.getInstrItineraryData();
Evan Chenga8e29892007-01-19 07:51:42 +0000180
Duncan Sands28b77e92011-09-06 19:07:46 +0000181 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
182
Evan Chengb1df8f22007-04-27 08:15:43 +0000183 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000184 // Uses VFP for Thumb libfuncs if available.
185 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
186 // Single-precision floating-point arithmetic.
187 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
188 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
189 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
190 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000191
Evan Chengb1df8f22007-04-27 08:15:43 +0000192 // Double-precision floating-point arithmetic.
193 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
194 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
195 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
196 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000197
Evan Chengb1df8f22007-04-27 08:15:43 +0000198 // Single-precision comparisons.
199 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
200 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
201 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
202 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
203 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
204 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
205 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
206 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000207
Evan Chengb1df8f22007-04-27 08:15:43 +0000208 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
209 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
210 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
211 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
212 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
213 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
214 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
215 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000216
Evan Chengb1df8f22007-04-27 08:15:43 +0000217 // Double-precision comparisons.
218 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
219 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
220 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
221 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
222 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
223 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
224 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
225 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000226
Evan Chengb1df8f22007-04-27 08:15:43 +0000227 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
228 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
229 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
230 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
231 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
232 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
233 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
234 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000235
Evan Chengb1df8f22007-04-27 08:15:43 +0000236 // Floating-point to integer conversions.
237 // i64 conversions are done via library routines even when generating VFP
238 // instructions, so use the same ones.
239 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
240 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
241 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
242 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000243
Evan Chengb1df8f22007-04-27 08:15:43 +0000244 // Conversions between floating types.
245 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
246 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
247
248 // Integer to floating-point conversions.
249 // i64 conversions are done via library routines even when generating VFP
250 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000251 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
252 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000253 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
254 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
255 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
256 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
257 }
Evan Chenga8e29892007-01-19 07:51:42 +0000258 }
259
Bob Wilson2f954612009-05-22 17:38:41 +0000260 // These libcalls are not available in 32-bit.
261 setLibcallName(RTLIB::SHL_I128, 0);
262 setLibcallName(RTLIB::SRL_I128, 0);
263 setLibcallName(RTLIB::SRA_I128, 0);
264
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000265 if (Subtarget->isAAPCS_ABI()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000266 // Double-precision floating-point arithmetic helper functions
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000267 // RTABI chapter 4.1.2, Table 2
268 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd");
269 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv");
270 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul");
271 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub");
272 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS);
273 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS);
274 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS);
275 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS);
276
277 // Double-precision floating-point comparison helper functions
278 // RTABI chapter 4.1.2, Table 3
279 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq");
280 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
281 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq");
282 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ);
283 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt");
284 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
285 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple");
286 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
287 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge");
288 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
289 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt");
290 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
291 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun");
292 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
293 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun");
294 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
295 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS);
296 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS);
297 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS);
298 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS);
299 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS);
300 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS);
301 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS);
302 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS);
303
304 // Single-precision floating-point arithmetic helper functions
305 // RTABI chapter 4.1.2, Table 4
306 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd");
307 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv");
308 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul");
309 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub");
310 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS);
311 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS);
312 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS);
313 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS);
314
315 // Single-precision floating-point comparison helper functions
316 // RTABI chapter 4.1.2, Table 5
317 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq");
318 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
319 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq");
320 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ);
321 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt");
322 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
323 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple");
324 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
325 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge");
326 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
327 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt");
328 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
329 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun");
330 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
331 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun");
332 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
333 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS);
334 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS);
335 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS);
336 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS);
337 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS);
338 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS);
339 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS);
340 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS);
341
342 // Floating-point to integer conversions.
343 // RTABI chapter 4.1.2, Table 6
344 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz");
345 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz");
346 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz");
347 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz");
348 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz");
349 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz");
350 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz");
351 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz");
352 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS);
353 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS);
354 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS);
355 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS);
356 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS);
357 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS);
358 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS);
359 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS);
360
361 // Conversions between floating types.
362 // RTABI chapter 4.1.2, Table 7
363 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f");
364 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d");
365 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000366 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000367
368 // Integer to floating-point conversions.
369 // RTABI chapter 4.1.2, Table 8
370 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d");
371 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d");
372 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d");
373 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d");
374 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f");
375 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f");
376 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f");
377 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f");
378 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
379 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
380 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
381 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
382 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
383 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
384 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
385 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
386
387 // Long long helper functions
388 // RTABI chapter 4.2, Table 9
389 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul");
390 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
391 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
392 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
393 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
394 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
395 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
396 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
397 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
398 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
399 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
400 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
401
402 // Integer division functions
403 // RTABI chapter 4.3.1
404 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
405 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
406 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
407 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
408 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
409 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
410 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
411 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
412 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
413 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);
Renato Golin1ec11fb2011-05-22 21:41:23 +0000416
417 // Memory operations
418 // RTABI chapter 4.3.4
419 setLibcallName(RTLIB::MEMCPY, "__aeabi_memcpy");
420 setLibcallName(RTLIB::MEMMOVE, "__aeabi_memmove");
421 setLibcallName(RTLIB::MEMSET, "__aeabi_memset");
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000422 }
423
Bob Wilson2fef4572011-10-07 16:59:21 +0000424 // Use divmod compiler-rt calls for iOS 5.0 and later.
425 if (Subtarget->getTargetTriple().getOS() == Triple::IOS &&
426 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
427 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
428 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
429 }
430
David Goodwinf1daf7d2009-07-08 23:10:31 +0000431 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000432 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000433 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000434 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000435 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
436 !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000437 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000438 if (!Subtarget->isFPOnlySP())
439 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000440
Owen Anderson825b72b2009-08-11 20:47:22 +0000441 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000442 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000443
Eli Friedman9f1f26a2011-11-08 01:43:53 +0000444 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
445 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
446 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
447 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
448 setTruncStoreAction((MVT::SimpleValueType)VT,
449 (MVT::SimpleValueType)InnerVT, Expand);
450 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
451 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
452 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
453 }
454
Bob Wilson5bafff32009-06-22 23:27:02 +0000455 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000456 addDRTypeForNEON(MVT::v2f32);
457 addDRTypeForNEON(MVT::v8i8);
458 addDRTypeForNEON(MVT::v4i16);
459 addDRTypeForNEON(MVT::v2i32);
460 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000461
Owen Anderson825b72b2009-08-11 20:47:22 +0000462 addQRTypeForNEON(MVT::v4f32);
463 addQRTypeForNEON(MVT::v2f64);
464 addQRTypeForNEON(MVT::v16i8);
465 addQRTypeForNEON(MVT::v8i16);
466 addQRTypeForNEON(MVT::v4i32);
467 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000468
Bob Wilson74dc72e2009-09-15 23:55:57 +0000469 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
470 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000471 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
472 // supported for v4f32.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000473 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
474 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
475 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000476 // FIXME: Code duplication: FDIV and FREM are expanded always, see
477 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000478 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
479 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000480 // FIXME: Create unittest.
481 // In another words, find a way when "copysign" appears in DAG with vector
482 // operands.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000483 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000484 // FIXME: Code duplication: SETCC has custom operation action, see
485 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sands28b77e92011-09-06 19:07:46 +0000486 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000487 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000488 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
489 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
490 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
491 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
492 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
493 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
494 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
495 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
496 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
497 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
498 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
499 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000500 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000501 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
502 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
503 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
504 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
505 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000506
507 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
508 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
509 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
510 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
511 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
512 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
513 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
514 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
515 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
516 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Bob Wilson74dc72e2009-09-15 23:55:57 +0000517
Bob Wilson642b3292009-09-16 00:32:15 +0000518 // Neon does not support some operations on v1i64 and v2i64 types.
519 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000520 // Custom handling for some quad-vector types to detect VMULL.
521 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
522 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
523 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begeman7973f352011-02-11 20:53:29 +0000524 // Custom handling for some vector types to avoid expensive expansions
525 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
526 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
527 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
528 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000529 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
530 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000531 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
532 // a destination type that is wider than the source.
533 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
534 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000535
Bob Wilson1c3ef902011-02-07 17:43:21 +0000536 setTargetDAGCombine(ISD::INTRINSIC_VOID);
537 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000538 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
539 setTargetDAGCombine(ISD::SHL);
540 setTargetDAGCombine(ISD::SRL);
541 setTargetDAGCombine(ISD::SRA);
542 setTargetDAGCombine(ISD::SIGN_EXTEND);
543 setTargetDAGCombine(ISD::ZERO_EXTEND);
544 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000545 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000546 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000547 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000548 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
549 setTargetDAGCombine(ISD::STORE);
Chad Rosieref01edf2011-06-24 19:23:04 +0000550 setTargetDAGCombine(ISD::FP_TO_SINT);
551 setTargetDAGCombine(ISD::FP_TO_UINT);
552 setTargetDAGCombine(ISD::FDIV);
Nadav Rotem004a24b2011-10-15 20:03:12 +0000553
554 setLoadExtAction(ISD::EXTLOAD, MVT::v4i8, Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000555 }
556
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000557 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000558
559 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000560 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000561
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000562 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000563 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000564
Evan Chenga8e29892007-01-19 07:51:42 +0000565 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000566 if (!Subtarget->isThumb1Only()) {
567 for (unsigned im = (unsigned)ISD::PRE_INC;
568 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000569 setIndexedLoadAction(im, MVT::i1, Legal);
570 setIndexedLoadAction(im, MVT::i8, Legal);
571 setIndexedLoadAction(im, MVT::i16, Legal);
572 setIndexedLoadAction(im, MVT::i32, Legal);
573 setIndexedStoreAction(im, MVT::i1, Legal);
574 setIndexedStoreAction(im, MVT::i8, Legal);
575 setIndexedStoreAction(im, MVT::i16, Legal);
576 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000577 }
Evan Chenga8e29892007-01-19 07:51:42 +0000578 }
579
580 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000581 setOperationAction(ISD::MUL, MVT::i64, Expand);
582 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000583 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000584 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
585 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000586 }
Jim Grosbacha7603982011-07-01 21:12:19 +0000587 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
588 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopher2cc40132011-04-19 18:49:19 +0000589 setOperationAction(ISD::MULHS, MVT::i32, Expand);
590
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000591 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000592 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000593 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000594 setOperationAction(ISD::SRL, MVT::i64, Custom);
595 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000596
Evan Cheng342e3162011-08-30 01:34:54 +0000597 if (!Subtarget->isThumb1Only()) {
598 // FIXME: We should do this for Thumb1 as well.
599 setOperationAction(ISD::ADDC, MVT::i32, Custom);
600 setOperationAction(ISD::ADDE, MVT::i32, Custom);
601 setOperationAction(ISD::SUBC, MVT::i32, Custom);
602 setOperationAction(ISD::SUBE, MVT::i32, Custom);
603 }
604
Evan Chenga8e29892007-01-19 07:51:42 +0000605 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000606 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000607 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000608 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000609 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000610 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000611
Chandler Carruth63974b22011-12-13 01:56:10 +0000612 // These just redirect to CTTZ and CTLZ on ARM.
613 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
614 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
615
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000616 // Only ARMv6 has BSWAP.
617 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000618 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000619
Evan Chenga8e29892007-01-19 07:51:42 +0000620 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000621 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000622 // v7M has a hardware divider
623 setOperationAction(ISD::SDIV, MVT::i32, Expand);
624 setOperationAction(ISD::UDIV, MVT::i32, Expand);
625 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000626 setOperationAction(ISD::SREM, MVT::i32, Expand);
627 setOperationAction(ISD::UREM, MVT::i32, Expand);
628 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
629 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000630
Owen Anderson825b72b2009-08-11 20:47:22 +0000631 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
632 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
633 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
634 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000635 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000636
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000637 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000638
Evan Chenga8e29892007-01-19 07:51:42 +0000639 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000640 setOperationAction(ISD::VASTART, MVT::Other, Custom);
641 setOperationAction(ISD::VAARG, MVT::Other, Expand);
642 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
643 setOperationAction(ISD::VAEND, MVT::Other, Expand);
644 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
645 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000646 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000647 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
648 setExceptionPointerRegister(ARM::R0);
649 setExceptionSelectorRegister(ARM::R1);
650
Evan Cheng3a1588a2010-04-15 22:20:34 +0000651 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000652 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
653 // the default expansion.
Eli Friedman4db5aca2011-08-29 18:23:02 +0000654 // FIXME: This should be checking for v6k, not just v6.
Evan Cheng11db0682010-08-11 06:22:01 +0000655 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000656 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000657 // membarrier needs custom lowering; the rest are legal and handled
658 // normally.
659 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000660 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Eli Friedman2bdffe42011-08-31 00:31:29 +0000661 // Custom lowering for 64-bit ops
662 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
663 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
664 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
665 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
666 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
667 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Eli Friedman4d3f3292011-08-31 17:52:22 +0000668 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Eli Friedman26689ac2011-08-03 21:06:02 +0000669 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
670 setInsertFencesForAtomic(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000671 } else {
672 // Set them all for expansion, which will force libcalls.
673 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000674 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000675 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000676 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000677 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000678 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000679 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000680 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000681 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000682 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000683 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000684 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000685 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000686 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedman7cc15662011-09-15 22:18:49 +0000687 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
688 // Unordered/Monotonic case.
689 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
690 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000691 // Since the libcalls include locking, fold in the fences
692 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000693 }
Evan Chenga8e29892007-01-19 07:51:42 +0000694
Evan Cheng416941d2010-11-04 05:19:35 +0000695 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000696
Eli Friedmana2c6f452010-06-26 04:36:50 +0000697 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
698 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000699 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
700 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000701 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000702 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000703
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000704 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
705 !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000706 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
707 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000708 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000709 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
710 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000711
712 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000713 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000714 if (Subtarget->isTargetDarwin()) {
715 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
716 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000717 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000718 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000719
Owen Anderson825b72b2009-08-11 20:47:22 +0000720 setOperationAction(ISD::SETCC, MVT::i32, Expand);
721 setOperationAction(ISD::SETCC, MVT::f32, Expand);
722 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000723 setOperationAction(ISD::SELECT, MVT::i32, Custom);
724 setOperationAction(ISD::SELECT, MVT::f32, Custom);
725 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000726 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
727 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
728 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000729
Owen Anderson825b72b2009-08-11 20:47:22 +0000730 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
731 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
732 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
733 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
734 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000735
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000736 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000737 setOperationAction(ISD::FSIN, MVT::f64, Expand);
738 setOperationAction(ISD::FSIN, MVT::f32, Expand);
739 setOperationAction(ISD::FCOS, MVT::f32, Expand);
740 setOperationAction(ISD::FCOS, MVT::f64, Expand);
741 setOperationAction(ISD::FREM, MVT::f64, Expand);
742 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000743 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
744 !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000745 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
746 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000747 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000748 setOperationAction(ISD::FPOW, MVT::f64, Expand);
749 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000750
Cameron Zwarich33390842011-07-08 21:39:21 +0000751 setOperationAction(ISD::FMA, MVT::f64, Expand);
752 setOperationAction(ISD::FMA, MVT::f32, Expand);
753
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000754 // Various VFP goodness
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000755 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000756 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
757 if (Subtarget->hasVFP2()) {
758 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
759 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
760 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
761 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
762 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000763 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000764 if (!Subtarget->hasFP16()) {
765 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
766 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000767 }
Evan Cheng110cf482008-04-01 01:50:16 +0000768 }
Evan Chenga8e29892007-01-19 07:51:42 +0000769
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000770 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000771 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000772 setTargetDAGCombine(ISD::ADD);
773 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000774 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000775
Owen Anderson080c0922010-11-05 19:27:46 +0000776 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON())
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000777 setTargetDAGCombine(ISD::OR);
Owen Anderson080c0922010-11-05 19:27:46 +0000778 if (Subtarget->hasNEON())
779 setTargetDAGCombine(ISD::AND);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000780
Evan Chenga8e29892007-01-19 07:51:42 +0000781 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000782
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000783 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
784 !Subtarget->hasVFP2())
Evan Chengf7d87ee2010-05-21 00:43:17 +0000785 setSchedulingPreference(Sched::RegPressure);
786 else
787 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000788
Evan Cheng05219282011-01-06 06:52:41 +0000789 //// temporary - rewrite interface to use type
790 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Lang Hames75757f92011-10-26 20:56:52 +0000791 maxStoresPerMemset = 16;
792 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengf6799392010-06-26 01:52:05 +0000793
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000794 // On ARM arguments smaller than 4 bytes are extended, so all arguments
795 // are at least 4 bytes aligned.
796 setMinStackArgumentAlignment(4);
797
Evan Chengfff606d2010-09-24 19:07:23 +0000798 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000799
800 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000801}
802
Andrew Trick32cec0a2011-01-19 02:35:27 +0000803// FIXME: It might make sense to define the representative register class as the
804// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
805// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
806// SPR's representative would be DPR_VFP2. This should work well if register
807// pressure tracking were modified such that a register use would increment the
808// pressure of the register class's representative and all of it's super
809// classes' representatives transitively. We have not implemented this because
810// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000811// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000812// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000813std::pair<const TargetRegisterClass*, uint8_t>
814ARMTargetLowering::findRepresentativeClass(EVT VT) const{
815 const TargetRegisterClass *RRC = 0;
816 uint8_t Cost = 1;
817 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000818 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000819 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000820 // Use DPR as representative register class for all floating point
821 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
822 // the cost is 1 for both f32 and f64.
823 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000824 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000825 RRC = ARM::DPRRegisterClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000826 // When NEON is used for SP, only half of the register file is available
827 // because operations that define both SP and DP results will be constrained
828 // to the VFP2 class (D0-D15). We currently model this constraint prior to
829 // coalescing by double-counting the SP regs. See the FIXME above.
830 if (Subtarget->useNEONForSinglePrecisionFP())
831 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000832 break;
833 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
834 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000835 RRC = ARM::DPRRegisterClass;
836 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000837 break;
838 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000839 RRC = ARM::DPRRegisterClass;
840 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000841 break;
842 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000843 RRC = ARM::DPRRegisterClass;
844 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000845 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000846 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000847 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000848}
849
Evan Chenga8e29892007-01-19 07:51:42 +0000850const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
851 switch (Opcode) {
852 default: return 0;
853 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000854 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000855 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000856 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
857 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000858 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000859 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
860 case ARMISD::tCALL: return "ARMISD::tCALL";
861 case ARMISD::BRCOND: return "ARMISD::BRCOND";
862 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000863 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000864 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
865 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
866 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000867 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000868 case ARMISD::CMPFP: return "ARMISD::CMPFP";
869 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000870 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000871 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
872 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000873
Jim Grosbach3482c802010-01-18 19:58:49 +0000874 case ARMISD::RBIT: return "ARMISD::RBIT";
875
Bob Wilson76a312b2010-03-19 22:51:32 +0000876 case ARMISD::FTOSI: return "ARMISD::FTOSI";
877 case ARMISD::FTOUI: return "ARMISD::FTOUI";
878 case ARMISD::SITOF: return "ARMISD::SITOF";
879 case ARMISD::UITOF: return "ARMISD::UITOF";
880
Evan Chenga8e29892007-01-19 07:51:42 +0000881 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
882 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
883 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000884
Evan Cheng342e3162011-08-30 01:34:54 +0000885 case ARMISD::ADDC: return "ARMISD::ADDC";
886 case ARMISD::ADDE: return "ARMISD::ADDE";
887 case ARMISD::SUBC: return "ARMISD::SUBC";
888 case ARMISD::SUBE: return "ARMISD::SUBE";
889
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000890 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
891 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000892
Evan Chengc5942082009-10-28 06:55:03 +0000893 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
894 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
895
Dale Johannesen51e28e62010-06-03 21:09:53 +0000896 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000897
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000898 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000899
Evan Cheng86198642009-08-07 00:34:42 +0000900 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
901
Jim Grosbach3728e962009-12-10 00:11:09 +0000902 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000903 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000904
Evan Chengdfed19f2010-11-03 06:34:55 +0000905 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
906
Bob Wilson5bafff32009-06-22 23:27:02 +0000907 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000908 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000909 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000910 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
911 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000912 case ARMISD::VCGEU: return "ARMISD::VCGEU";
913 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000914 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
915 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000916 case ARMISD::VCGTU: return "ARMISD::VCGTU";
917 case ARMISD::VTST: return "ARMISD::VTST";
918
919 case ARMISD::VSHL: return "ARMISD::VSHL";
920 case ARMISD::VSHRs: return "ARMISD::VSHRs";
921 case ARMISD::VSHRu: return "ARMISD::VSHRu";
922 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
923 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
924 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
925 case ARMISD::VSHRN: return "ARMISD::VSHRN";
926 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
927 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
928 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
929 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
930 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
931 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
932 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
933 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
934 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
935 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
936 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
937 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
938 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
939 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000940 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000941 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Chengeaa192a2011-11-15 02:12:34 +0000942 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000943 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000944 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000945 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000946 case ARMISD::VREV64: return "ARMISD::VREV64";
947 case ARMISD::VREV32: return "ARMISD::VREV32";
948 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000949 case ARMISD::VZIP: return "ARMISD::VZIP";
950 case ARMISD::VUZP: return "ARMISD::VUZP";
951 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000952 case ARMISD::VTBL1: return "ARMISD::VTBL1";
953 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000954 case ARMISD::VMULLs: return "ARMISD::VMULLs";
955 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000956 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000957 case ARMISD::FMAX: return "ARMISD::FMAX";
958 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000959 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000960 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
961 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000962 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000963 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
964 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
965 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +0000966 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
967 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
968 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
969 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
970 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
971 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
972 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
973 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
974 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
975 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
976 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
977 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
978 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
979 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
980 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
981 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
982 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +0000983 }
984}
985
Duncan Sands28b77e92011-09-06 19:07:46 +0000986EVT ARMTargetLowering::getSetCCResultType(EVT VT) const {
987 if (!VT.isVector()) return getPointerTy();
988 return VT.changeVectorElementTypeToInteger();
989}
990
Evan Cheng06b666c2010-05-15 02:18:07 +0000991/// getRegClassFor - Return the register class that should be used for the
992/// specified value type.
993TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
994 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
995 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
996 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000997 if (Subtarget->hasNEON()) {
998 if (VT == MVT::v4i64)
999 return ARM::QQPRRegisterClass;
1000 else if (VT == MVT::v8i64)
1001 return ARM::QQQQPRRegisterClass;
1002 }
Evan Cheng06b666c2010-05-15 02:18:07 +00001003 return TargetLowering::getRegClassFor(VT);
1004}
1005
Eric Christopherab695882010-07-21 22:26:11 +00001006// Create a fast isel object.
1007FastISel *
1008ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
1009 return ARM::createFastISel(funcInfo);
1010}
1011
Anton Korobeynikovcec36f42010-07-24 21:52:08 +00001012/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1013/// be used for loads / stores from the global.
1014unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1015 return (Subtarget->isThumb1Only() ? 127 : 4095);
1016}
1017
Evan Cheng1cc39842010-05-20 23:26:43 +00001018Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +00001019 unsigned NumVals = N->getNumValues();
1020 if (!NumVals)
1021 return Sched::RegPressure;
1022
1023 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +00001024 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001025 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +00001026 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +00001027 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman692c1d82011-10-24 17:55:11 +00001028 return Sched::ILP;
Evan Cheng1cc39842010-05-20 23:26:43 +00001029 }
Evan Chengc10f5432010-05-28 23:25:23 +00001030
1031 if (!N->isMachineOpcode())
1032 return Sched::RegPressure;
1033
1034 // Load are scheduled for latency even if there instruction itinerary
1035 // is not available.
1036 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +00001037 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +00001038
Evan Chenge837dea2011-06-28 19:10:37 +00001039 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +00001040 return Sched::RegPressure;
1041 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +00001042 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman692c1d82011-10-24 17:55:11 +00001043 return Sched::ILP;
Evan Chengc10f5432010-05-28 23:25:23 +00001044
Evan Cheng1cc39842010-05-20 23:26:43 +00001045 return Sched::RegPressure;
1046}
1047
Evan Chenga8e29892007-01-19 07:51:42 +00001048//===----------------------------------------------------------------------===//
1049// Lowering Code
1050//===----------------------------------------------------------------------===//
1051
Evan Chenga8e29892007-01-19 07:51:42 +00001052/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1053static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1054 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001055 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +00001056 case ISD::SETNE: return ARMCC::NE;
1057 case ISD::SETEQ: return ARMCC::EQ;
1058 case ISD::SETGT: return ARMCC::GT;
1059 case ISD::SETGE: return ARMCC::GE;
1060 case ISD::SETLT: return ARMCC::LT;
1061 case ISD::SETLE: return ARMCC::LE;
1062 case ISD::SETUGT: return ARMCC::HI;
1063 case ISD::SETUGE: return ARMCC::HS;
1064 case ISD::SETULT: return ARMCC::LO;
1065 case ISD::SETULE: return ARMCC::LS;
1066 }
1067}
1068
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001069/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1070static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001071 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001072 CondCode2 = ARMCC::AL;
1073 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001074 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001075 case ISD::SETEQ:
1076 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1077 case ISD::SETGT:
1078 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1079 case ISD::SETGE:
1080 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1081 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001082 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001083 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1084 case ISD::SETO: CondCode = ARMCC::VC; break;
1085 case ISD::SETUO: CondCode = ARMCC::VS; break;
1086 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1087 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1088 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1089 case ISD::SETLT:
1090 case ISD::SETULT: CondCode = ARMCC::LT; break;
1091 case ISD::SETLE:
1092 case ISD::SETULE: CondCode = ARMCC::LE; break;
1093 case ISD::SETNE:
1094 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1095 }
Evan Chenga8e29892007-01-19 07:51:42 +00001096}
1097
Bob Wilson1f595bb2009-04-17 19:07:39 +00001098//===----------------------------------------------------------------------===//
1099// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001100//===----------------------------------------------------------------------===//
1101
1102#include "ARMGenCallingConv.inc"
1103
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001104/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1105/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001106CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001107 bool Return,
1108 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001109 switch (CC) {
1110 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001111 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001112 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001113 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001114 if (!Subtarget->isAAPCS_ABI())
1115 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1116 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1117 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1118 }
1119 // Fallthrough
1120 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001121 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001122 if (!Subtarget->isAAPCS_ABI())
1123 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1124 else if (Subtarget->hasVFP2() &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001125 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1126 !isVarArg)
Evan Cheng76f920d2010-10-22 18:23:05 +00001127 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1128 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1129 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001130 case CallingConv::ARM_AAPCS_VFP:
Evan Cheng76f920d2010-10-22 18:23:05 +00001131 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001132 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001133 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001134 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001135 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001136 }
1137}
1138
Dan Gohman98ca4f22009-08-05 01:29:28 +00001139/// LowerCallResult - Lower the result values of a call into the
1140/// appropriate copies out of appropriate physical registers.
1141SDValue
1142ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001143 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001144 const SmallVectorImpl<ISD::InputArg> &Ins,
1145 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001146 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001147
Bob Wilson1f595bb2009-04-17 19:07:39 +00001148 // Assign locations to each value returned by this call.
1149 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001150 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1151 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001152 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001153 CCAssignFnForNode(CallConv, /* Return*/ true,
1154 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001155
1156 // Copy all of the result registers out of their specified physreg.
1157 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1158 CCValAssign VA = RVLocs[i];
1159
Bob Wilson80915242009-04-25 00:33:20 +00001160 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001161 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001162 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001163 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001164 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001165 Chain = Lo.getValue(1);
1166 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001167 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001168 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001169 InFlag);
1170 Chain = Hi.getValue(1);
1171 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001172 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001173
Owen Anderson825b72b2009-08-11 20:47:22 +00001174 if (VA.getLocVT() == MVT::v2f64) {
1175 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1176 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1177 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001178
1179 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001180 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001181 Chain = Lo.getValue(1);
1182 InFlag = Lo.getValue(2);
1183 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001184 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001185 Chain = Hi.getValue(1);
1186 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001187 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001188 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1189 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001190 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001191 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001192 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1193 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001194 Chain = Val.getValue(1);
1195 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001196 }
Bob Wilson80915242009-04-25 00:33:20 +00001197
1198 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001199 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001200 case CCValAssign::Full: break;
1201 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001202 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001203 break;
1204 }
1205
Dan Gohman98ca4f22009-08-05 01:29:28 +00001206 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001207 }
1208
Dan Gohman98ca4f22009-08-05 01:29:28 +00001209 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001210}
1211
Bob Wilsondee46d72009-04-17 20:35:10 +00001212/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001213SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001214ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1215 SDValue StackPtr, SDValue Arg,
1216 DebugLoc dl, SelectionDAG &DAG,
1217 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001218 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001219 unsigned LocMemOffset = VA.getLocMemOffset();
1220 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1221 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001222 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001223 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001224 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001225}
1226
Dan Gohman98ca4f22009-08-05 01:29:28 +00001227void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001228 SDValue Chain, SDValue &Arg,
1229 RegsToPassVector &RegsToPass,
1230 CCValAssign &VA, CCValAssign &NextVA,
1231 SDValue &StackPtr,
1232 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001233 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001234
Jim Grosbache5165492009-11-09 00:11:35 +00001235 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001236 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001237 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1238
1239 if (NextVA.isRegLoc())
1240 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1241 else {
1242 assert(NextVA.isMemLoc());
1243 if (StackPtr.getNode() == 0)
1244 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1245
Dan Gohman98ca4f22009-08-05 01:29:28 +00001246 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1247 dl, DAG, NextVA,
1248 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001249 }
1250}
1251
Dan Gohman98ca4f22009-08-05 01:29:28 +00001252/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001253/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1254/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001255SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001256ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001257 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001258 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001259 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001260 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001261 const SmallVectorImpl<ISD::InputArg> &Ins,
1262 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001263 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001264 MachineFunction &MF = DAG.getMachineFunction();
1265 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1266 bool IsSibCall = false;
Bob Wilson6d2f9ce2011-10-07 17:17:49 +00001267 // Disable tail calls if they're not supported.
1268 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Bob Wilson703af3a2010-08-13 22:43:33 +00001269 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001270 if (isTailCall) {
1271 // Check if it's really possible to do a tail call.
1272 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1273 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001274 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001275 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1276 // detected sibcalls.
1277 if (isTailCall) {
1278 ++NumTailCalls;
1279 IsSibCall = true;
1280 }
1281 }
Evan Chenga8e29892007-01-19 07:51:42 +00001282
Bob Wilson1f595bb2009-04-17 19:07:39 +00001283 // Analyze operands of the call, assigning locations to each operand.
1284 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001285 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1286 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001287 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001288 CCAssignFnForNode(CallConv, /* Return*/ false,
1289 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001290
Bob Wilson1f595bb2009-04-17 19:07:39 +00001291 // Get a count of how many bytes are to be pushed on the stack.
1292 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001293
Dale Johannesen51e28e62010-06-03 21:09:53 +00001294 // For tail calls, memory operands are available in our caller's stack.
1295 if (IsSibCall)
1296 NumBytes = 0;
1297
Evan Chenga8e29892007-01-19 07:51:42 +00001298 // Adjust the stack pointer for the new arguments...
1299 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001300 if (!IsSibCall)
1301 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001302
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001303 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001304
Bob Wilson5bafff32009-06-22 23:27:02 +00001305 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001306 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001307
Bob Wilson1f595bb2009-04-17 19:07:39 +00001308 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001309 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001310 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1311 i != e;
1312 ++i, ++realArgIdx) {
1313 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001314 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001315 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001316 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001317
Bob Wilson1f595bb2009-04-17 19:07:39 +00001318 // Promote the value if needed.
1319 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001320 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001321 case CCValAssign::Full: break;
1322 case CCValAssign::SExt:
1323 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1324 break;
1325 case CCValAssign::ZExt:
1326 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1327 break;
1328 case CCValAssign::AExt:
1329 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1330 break;
1331 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001332 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001333 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001334 }
1335
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001336 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001337 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001338 if (VA.getLocVT() == MVT::v2f64) {
1339 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1340 DAG.getConstant(0, MVT::i32));
1341 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1342 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001343
Dan Gohman98ca4f22009-08-05 01:29:28 +00001344 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001345 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1346
1347 VA = ArgLocs[++i]; // skip ahead to next loc
1348 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001349 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001350 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1351 } else {
1352 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001353
Dan Gohman98ca4f22009-08-05 01:29:28 +00001354 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1355 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001356 }
1357 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001358 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001359 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001360 }
1361 } else if (VA.isRegLoc()) {
1362 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001363 } else if (isByVal) {
1364 assert(VA.isMemLoc());
1365 unsigned offset = 0;
1366
1367 // True if this byval aggregate will be split between registers
1368 // and memory.
1369 if (CCInfo.isFirstByValRegValid()) {
1370 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1371 unsigned int i, j;
1372 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1373 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1374 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1375 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1376 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001377 false, false, false, 0);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001378 MemOpChains.push_back(Load.getValue(1));
1379 RegsToPass.push_back(std::make_pair(j, Load));
1380 }
1381 offset = ARM::R4 - CCInfo.getFirstByValReg();
1382 CCInfo.clearFirstByValReg();
1383 }
1384
1385 unsigned LocMemOffset = VA.getLocMemOffset();
1386 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1387 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1388 StkPtrOff);
1389 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1390 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1391 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1392 MVT::i32);
1393 MemOpChains.push_back(DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode,
1394 Flags.getByValAlign(),
1395 /*isVolatile=*/false,
Dan Gohman65fd6562011-11-03 21:49:52 +00001396 /*AlwaysInline=*/false,
Stuart Hastingsc7315872011-04-20 16:47:52 +00001397 MachinePointerInfo(0),
1398 MachinePointerInfo(0)));
1399
1400 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001401 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001402
Dan Gohman98ca4f22009-08-05 01:29:28 +00001403 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1404 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001405 }
Evan Chenga8e29892007-01-19 07:51:42 +00001406 }
1407
1408 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001409 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001410 &MemOpChains[0], MemOpChains.size());
1411
1412 // Build a sequence of copy-to-reg nodes chained together with token chain
1413 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001414 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001415 // Tail call byval lowering might overwrite argument registers so in case of
1416 // tail call optimization the copies to registers are lowered later.
1417 if (!isTailCall)
1418 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1419 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1420 RegsToPass[i].second, InFlag);
1421 InFlag = Chain.getValue(1);
1422 }
Evan Chenga8e29892007-01-19 07:51:42 +00001423
Dale Johannesen51e28e62010-06-03 21:09:53 +00001424 // For tail calls lower the arguments to the 'real' stack slot.
1425 if (isTailCall) {
1426 // Force all the incoming stack arguments to be loaded from the stack
1427 // before any new outgoing arguments are stored to the stack, because the
1428 // outgoing stack slots may alias the incoming argument stack slots, and
1429 // the alias isn't otherwise explicit. This is slightly more conservative
1430 // than necessary, because it means that each store effectively depends
1431 // on every argument instead of just those arguments it would clobber.
1432
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001433 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001434 InFlag = SDValue();
1435 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1436 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1437 RegsToPass[i].second, InFlag);
1438 InFlag = Chain.getValue(1);
1439 }
1440 InFlag =SDValue();
1441 }
1442
Bill Wendling056292f2008-09-16 21:48:12 +00001443 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1444 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1445 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001446 bool isDirect = false;
1447 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001448 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001449 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001450
1451 if (EnableARMLongCalls) {
1452 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1453 && "long-calls with non-static relocation model!");
1454 // Handle a global address or an external symbol. If it's not one of
1455 // those, the target's already in a register, so we don't need to do
1456 // anything extra.
1457 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001458 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001459 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001460 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001461 ARMConstantPoolValue *CPV =
1462 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1463
Jim Grosbache7b52522010-04-14 22:28:31 +00001464 // Get the address of the callee into a register
1465 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1466 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1467 Callee = DAG.getLoad(getPointerTy(), dl,
1468 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001469 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001470 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001471 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1472 const char *Sym = S->getSymbol();
1473
1474 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001475 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001476 ARMConstantPoolValue *CPV =
1477 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1478 ARMPCLabelIndex, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001479 // Get the address of the callee into a register
1480 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1481 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1482 Callee = DAG.getLoad(getPointerTy(), dl,
1483 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001484 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001485 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001486 }
1487 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001488 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001489 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001490 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001491 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001492 getTargetMachine().getRelocationModel() != Reloc::Static;
1493 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001494 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001495 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001496 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001497 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001498 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001499 ARMConstantPoolValue *CPV =
1500 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001501 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001502 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001503 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001504 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001505 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001506 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001507 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001508 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001509 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001510 } else {
1511 // On ELF targets for PIC code, direct calls should go through the PLT
1512 unsigned OpFlags = 0;
1513 if (Subtarget->isTargetELF() &&
1514 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1515 OpFlags = ARMII::MO_PLT;
1516 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1517 }
Bill Wendling056292f2008-09-16 21:48:12 +00001518 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001519 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001520 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001521 getTargetMachine().getRelocationModel() != Reloc::Static;
1522 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001523 // tBX takes a register source operand.
1524 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001525 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001526 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001527 ARMConstantPoolValue *CPV =
1528 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1529 ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001530 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001531 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001532 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001533 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001534 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001535 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001536 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001537 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001538 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001539 } else {
1540 unsigned OpFlags = 0;
1541 // On ELF targets for PIC code, direct calls should go through the PLT
1542 if (Subtarget->isTargetELF() &&
1543 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1544 OpFlags = ARMII::MO_PLT;
1545 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1546 }
Evan Chenga8e29892007-01-19 07:51:42 +00001547 }
1548
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001549 // FIXME: handle tail calls differently.
1550 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001551 if (Subtarget->isThumb()) {
1552 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001553 CallOpc = ARMISD::CALL_NOLINK;
1554 else
1555 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1556 } else {
1557 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001558 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1559 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001560 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001561
Dan Gohman475871a2008-07-27 21:46:04 +00001562 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001563 Ops.push_back(Chain);
1564 Ops.push_back(Callee);
1565
1566 // Add argument registers to the end of the list so that they are known live
1567 // into the call.
1568 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1569 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1570 RegsToPass[i].second.getValueType()));
1571
Gabor Greifba36cb52008-08-28 21:40:38 +00001572 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001573 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001574
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001575 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001576 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001577 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001578
Duncan Sands4bdcb612008-07-02 17:40:58 +00001579 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001580 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001581 InFlag = Chain.getValue(1);
1582
Chris Lattnere563bbc2008-10-11 22:08:30 +00001583 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1584 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001585 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001586 InFlag = Chain.getValue(1);
1587
Bob Wilson1f595bb2009-04-17 19:07:39 +00001588 // Handle result values, copying them out of physregs into vregs that we
1589 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001590 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1591 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001592}
1593
Stuart Hastingsf222e592011-02-28 17:17:53 +00001594/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001595/// on the stack. Remember the next parameter register to allocate,
1596/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001597/// this.
1598void
Stuart Hastingsc7315872011-04-20 16:47:52 +00001599llvm::ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
1600 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1601 assert((State->getCallOrPrologue() == Prologue ||
1602 State->getCallOrPrologue() == Call) &&
1603 "unhandled ParmContext");
1604 if ((!State->isFirstByValRegValid()) &&
1605 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1606 State->setFirstByValReg(reg);
1607 // At a call site, a byval parameter that is split between
1608 // registers and memory needs its size truncated here. In a
1609 // function prologue, such byval parameters are reassembled in
1610 // memory, and are not truncated.
1611 if (State->getCallOrPrologue() == Call) {
1612 unsigned excess = 4 * (ARM::R4 - reg);
1613 assert(size >= excess && "expected larger existing stack allocation");
1614 size -= excess;
1615 }
1616 }
1617 // Confiscate any remaining parameter registers to preclude their
1618 // assignment to subsequent parameters.
1619 while (State->AllocateReg(GPRArgRegs, 4))
1620 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001621}
1622
Dale Johannesen51e28e62010-06-03 21:09:53 +00001623/// MatchingStackOffset - Return true if the given stack call argument is
1624/// already available in the same position (relatively) of the caller's
1625/// incoming argument stack.
1626static
1627bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1628 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1629 const ARMInstrInfo *TII) {
1630 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1631 int FI = INT_MAX;
1632 if (Arg.getOpcode() == ISD::CopyFromReg) {
1633 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001634 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001635 return false;
1636 MachineInstr *Def = MRI->getVRegDef(VR);
1637 if (!Def)
1638 return false;
1639 if (!Flags.isByVal()) {
1640 if (!TII->isLoadFromStackSlot(Def, FI))
1641 return false;
1642 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001643 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001644 }
1645 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1646 if (Flags.isByVal())
1647 // ByVal argument is passed in as a pointer but it's now being
1648 // dereferenced. e.g.
1649 // define @foo(%struct.X* %A) {
1650 // tail call @bar(%struct.X* byval %A)
1651 // }
1652 return false;
1653 SDValue Ptr = Ld->getBasePtr();
1654 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1655 if (!FINode)
1656 return false;
1657 FI = FINode->getIndex();
1658 } else
1659 return false;
1660
1661 assert(FI != INT_MAX);
1662 if (!MFI->isFixedObjectIndex(FI))
1663 return false;
1664 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1665}
1666
1667/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1668/// for tail call optimization. Targets which want to do tail call
1669/// optimization should implement this function.
1670bool
1671ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1672 CallingConv::ID CalleeCC,
1673 bool isVarArg,
1674 bool isCalleeStructRet,
1675 bool isCallerStructRet,
1676 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001677 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001678 const SmallVectorImpl<ISD::InputArg> &Ins,
1679 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001680 const Function *CallerF = DAG.getMachineFunction().getFunction();
1681 CallingConv::ID CallerCC = CallerF->getCallingConv();
1682 bool CCMatch = CallerCC == CalleeCC;
1683
1684 // Look for obvious safe cases to perform tail call optimization that do not
1685 // require ABI changes. This is what gcc calls sibcall.
1686
Jim Grosbach7616b642010-06-16 23:45:49 +00001687 // Do not sibcall optimize vararg calls unless the call site is not passing
1688 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001689 if (isVarArg && !Outs.empty())
1690 return false;
1691
1692 // Also avoid sibcall optimization if either caller or callee uses struct
1693 // return semantics.
1694 if (isCalleeStructRet || isCallerStructRet)
1695 return false;
1696
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001697 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001698 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1699 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1700 // support in the assembler and linker to be used. This would need to be
1701 // fixed to fully support tail calls in Thumb1.
1702 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001703 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1704 // LR. This means if we need to reload LR, it takes an extra instructions,
1705 // which outweighs the value of the tail call; but here we don't know yet
1706 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001707 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001708 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001709
1710 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1711 // but we need to make sure there are enough registers; the only valid
1712 // registers are the 4 used for parameters. We don't currently do this
1713 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001714 if (Subtarget->isThumb1Only())
1715 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001716
Dale Johannesen51e28e62010-06-03 21:09:53 +00001717 // If the calling conventions do not match, then we'd better make sure the
1718 // results are returned in the same way as what the caller expects.
1719 if (!CCMatch) {
1720 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001721 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1722 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001723 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1724
1725 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001726 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1727 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001728 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1729
1730 if (RVLocs1.size() != RVLocs2.size())
1731 return false;
1732 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1733 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1734 return false;
1735 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1736 return false;
1737 if (RVLocs1[i].isRegLoc()) {
1738 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1739 return false;
1740 } else {
1741 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1742 return false;
1743 }
1744 }
1745 }
1746
1747 // If the callee takes no arguments then go on to check the results of the
1748 // call.
1749 if (!Outs.empty()) {
1750 // Check if stack adjustment is needed. For now, do not do this if any
1751 // argument is passed on the stack.
1752 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001753 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1754 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001755 CCInfo.AnalyzeCallOperands(Outs,
1756 CCAssignFnForNode(CalleeCC, false, isVarArg));
1757 if (CCInfo.getNextStackOffset()) {
1758 MachineFunction &MF = DAG.getMachineFunction();
1759
1760 // Check if the arguments are already laid out in the right way as
1761 // the caller's fixed stack objects.
1762 MachineFrameInfo *MFI = MF.getFrameInfo();
1763 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1764 const ARMInstrInfo *TII =
1765 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001766 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1767 i != e;
1768 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001769 CCValAssign &VA = ArgLocs[i];
1770 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001771 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001772 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001773 if (VA.getLocInfo() == CCValAssign::Indirect)
1774 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001775 if (VA.needsCustom()) {
1776 // f64 and vector types are split into multiple registers or
1777 // register/stack-slot combinations. The types will not match
1778 // the registers; give up on memory f64 refs until we figure
1779 // out what to do about this.
1780 if (!VA.isRegLoc())
1781 return false;
1782 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001783 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001784 if (RegVT == MVT::v2f64) {
1785 if (!ArgLocs[++i].isRegLoc())
1786 return false;
1787 if (!ArgLocs[++i].isRegLoc())
1788 return false;
1789 }
1790 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001791 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1792 MFI, MRI, TII))
1793 return false;
1794 }
1795 }
1796 }
1797 }
1798
1799 return true;
1800}
1801
Dan Gohman98ca4f22009-08-05 01:29:28 +00001802SDValue
1803ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001804 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001805 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001806 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001807 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001808
Bob Wilsondee46d72009-04-17 20:35:10 +00001809 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001810 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001811
Bob Wilsondee46d72009-04-17 20:35:10 +00001812 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001813 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1814 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001815
Dan Gohman98ca4f22009-08-05 01:29:28 +00001816 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001817 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1818 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001819
1820 // If this is the first return lowered for this function, add
1821 // the regs to the liveout set for the function.
1822 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1823 for (unsigned i = 0; i != RVLocs.size(); ++i)
1824 if (RVLocs[i].isRegLoc())
1825 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001826 }
1827
Bob Wilson1f595bb2009-04-17 19:07:39 +00001828 SDValue Flag;
1829
1830 // Copy the result values into the output registers.
1831 for (unsigned i = 0, realRVLocIdx = 0;
1832 i != RVLocs.size();
1833 ++i, ++realRVLocIdx) {
1834 CCValAssign &VA = RVLocs[i];
1835 assert(VA.isRegLoc() && "Can only return in registers!");
1836
Dan Gohmanc9403652010-07-07 15:54:55 +00001837 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001838
1839 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001840 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001841 case CCValAssign::Full: break;
1842 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001843 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001844 break;
1845 }
1846
Bob Wilson1f595bb2009-04-17 19:07:39 +00001847 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001848 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001849 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001850 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1851 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001852 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001853 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001854
1855 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1856 Flag = Chain.getValue(1);
1857 VA = RVLocs[++i]; // skip ahead to next loc
1858 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1859 HalfGPRs.getValue(1), Flag);
1860 Flag = Chain.getValue(1);
1861 VA = RVLocs[++i]; // skip ahead to next loc
1862
1863 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001864 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1865 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001866 }
1867 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1868 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001869 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001870 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001871 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001872 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001873 VA = RVLocs[++i]; // skip ahead to next loc
1874 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1875 Flag);
1876 } else
1877 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1878
Bob Wilsondee46d72009-04-17 20:35:10 +00001879 // Guarantee that all emitted copies are
1880 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001881 Flag = Chain.getValue(1);
1882 }
1883
1884 SDValue result;
1885 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001886 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001887 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001888 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001889
1890 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001891}
1892
Evan Cheng3d2125c2010-11-30 23:55:39 +00001893bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1894 if (N->getNumValues() != 1)
1895 return false;
1896 if (!N->hasNUsesOfValue(1, 0))
1897 return false;
1898
1899 unsigned NumCopies = 0;
1900 SDNode* Copies[2];
1901 SDNode *Use = *N->use_begin();
1902 if (Use->getOpcode() == ISD::CopyToReg) {
1903 Copies[NumCopies++] = Use;
1904 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1905 // f64 returned in a pair of GPRs.
1906 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1907 UI != UE; ++UI) {
1908 if (UI->getOpcode() != ISD::CopyToReg)
1909 return false;
1910 Copies[UI.getUse().getResNo()] = *UI;
1911 ++NumCopies;
1912 }
1913 } else if (Use->getOpcode() == ISD::BITCAST) {
1914 // f32 returned in a single GPR.
1915 if (!Use->hasNUsesOfValue(1, 0))
1916 return false;
1917 Use = *Use->use_begin();
1918 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1919 return false;
1920 Copies[NumCopies++] = Use;
1921 } else {
1922 return false;
1923 }
1924
1925 if (NumCopies != 1 && NumCopies != 2)
1926 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001927
1928 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001929 for (unsigned i = 0; i < NumCopies; ++i) {
1930 SDNode *Copy = Copies[i];
1931 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1932 UI != UE; ++UI) {
1933 if (UI->getOpcode() == ISD::CopyToReg) {
1934 SDNode *Use = *UI;
1935 if (Use == Copies[0] || Use == Copies[1])
1936 continue;
1937 return false;
1938 }
1939 if (UI->getOpcode() != ARMISD::RET_FLAG)
1940 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001941 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001942 }
1943 }
1944
Evan Cheng1bf891a2010-12-01 22:59:46 +00001945 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001946}
1947
Evan Cheng485fafc2011-03-21 01:19:09 +00001948bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1949 if (!EnableARMTailCalls)
1950 return false;
1951
1952 if (!CI->isTailCall())
1953 return false;
1954
1955 return !Subtarget->isThumb1Only();
1956}
1957
Bob Wilsonb62d2572009-11-03 00:02:05 +00001958// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1959// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1960// one of the above mentioned nodes. It has to be wrapped because otherwise
1961// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1962// be used to form addressing mode. These wrapped nodes will be selected
1963// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001964static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001965 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001966 // FIXME there is no actual debug info here
1967 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001968 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001969 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001970 if (CP->isMachineConstantPoolEntry())
1971 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1972 CP->getAlignment());
1973 else
1974 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1975 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001976 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001977}
1978
Jim Grosbache1102ca2010-07-19 17:20:38 +00001979unsigned ARMTargetLowering::getJumpTableEncoding() const {
1980 return MachineJumpTableInfo::EK_Inline;
1981}
1982
Dan Gohmand858e902010-04-17 15:26:15 +00001983SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1984 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001985 MachineFunction &MF = DAG.getMachineFunction();
1986 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1987 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001988 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001989 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001990 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001991 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1992 SDValue CPAddr;
1993 if (RelocM == Reloc::Static) {
1994 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1995 } else {
1996 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001997 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001998 ARMConstantPoolValue *CPV =
1999 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2000 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson907eebd2009-11-02 20:59:23 +00002001 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2002 }
2003 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2004 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002005 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002006 false, false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00002007 if (RelocM == Reloc::Static)
2008 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00002009 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00002010 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00002011}
2012
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002013// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00002014SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002015ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002016 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002017 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002018 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002019 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00002020 MachineFunction &MF = DAG.getMachineFunction();
2021 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002022 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002023 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002024 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2025 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002026 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002027 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00002028 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002029 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002030 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002031 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002032
Evan Chenge7e0d622009-11-06 22:24:13 +00002033 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002034 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002035
2036 // call __tls_get_addr.
2037 ArgListTy Args;
2038 ArgListEntry Entry;
2039 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002040 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002041 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002042 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00002043 std::pair<SDValue, SDValue> CallResult =
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002044 LowerCallTo(Chain, (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00002045 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002046 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002047 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002048 return CallResult.first;
2049}
2050
2051// Lower ISD::GlobalTLSAddress using the "initial exec" or
2052// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00002053SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002054ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002055 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002056 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002057 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002058 SDValue Offset;
2059 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002060 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002061 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002062 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002063
Chris Lattner4fb63d02009-07-15 04:12:33 +00002064 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002065 MachineFunction &MF = DAG.getMachineFunction();
2066 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002067 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002068 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002069 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2070 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002071 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2072 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2073 true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002074 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002075 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002076 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002077 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002078 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002079 Chain = Offset.getValue(1);
2080
Evan Chenge7e0d622009-11-06 22:24:13 +00002081 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002082 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002083
Evan Cheng9eda6892009-10-31 03:39:36 +00002084 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002085 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002086 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002087 } else {
2088 // local exec model
Bill Wendling5bb77992011-10-01 08:00:54 +00002089 ARMConstantPoolValue *CPV =
2090 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002091 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002092 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002093 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002094 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002095 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002096 }
2097
2098 // The address of the thread local variable is the add of the thread
2099 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002100 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002101}
2102
Dan Gohman475871a2008-07-27 21:46:04 +00002103SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002104ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002105 // TODO: implement the "local dynamic" model
2106 assert(Subtarget->isTargetELF() &&
2107 "TLS not implemented for non-ELF targets");
2108 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2109 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
2110 // otherwise use the "Local Exec" TLS Model
2111 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
2112 return LowerToTLSGeneralDynamicModel(GA, DAG);
2113 else
2114 return LowerToTLSExecModels(GA, DAG);
2115}
2116
Dan Gohman475871a2008-07-27 21:46:04 +00002117SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002118 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002119 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002120 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002121 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002122 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2123 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002124 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002125 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002126 ARMConstantPoolConstant::Create(GV,
2127 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002128 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002129 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002130 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002131 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002132 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002133 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002134 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002135 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002136 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002137 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002138 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002139 MachinePointerInfo::getGOT(),
2140 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002141 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002142 }
2143
2144 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloy015cca62011-10-26 08:53:19 +00002145 // pair. This is always cheaper.
2146 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002147 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002148 // FIXME: Once remat is capable of dealing with instructions with register
2149 // operands, expand this into two nodes.
2150 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2151 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002152 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002153 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2154 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2155 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2156 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002157 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002158 }
2159}
2160
Dan Gohman475871a2008-07-27 21:46:04 +00002161SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002162 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002163 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002164 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002165 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002166 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002167 MachineFunction &MF = DAG.getMachineFunction();
2168 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2169
Evan Cheng4abce0c2011-05-27 20:11:27 +00002170 // FIXME: Enable this for static codegen when tool issues are fixed.
Evan Chengf31151f2011-10-26 01:17:44 +00002171 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002172 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002173 // FIXME: Once remat is capable of dealing with instructions with register
2174 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002175 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002176 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2177 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2178
Evan Cheng53519f02011-01-21 18:55:51 +00002179 unsigned Wrapper = (RelocM == Reloc::PIC_)
2180 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2181 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002182 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002183 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2184 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002185 MachinePointerInfo::getGOT(),
2186 false, false, false, 0);
Evan Chengfc8475b2011-01-19 02:16:49 +00002187 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002188 }
2189
2190 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002191 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002192 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002193 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002194 } else {
2195 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002196 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2197 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002198 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue,
2199 PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002200 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002201 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002202 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002203
Evan Cheng9eda6892009-10-31 03:39:36 +00002204 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002205 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002206 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002207 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002208
2209 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002210 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002211 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002212 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002213
Evan Cheng63476a82009-09-03 07:04:02 +00002214 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002215 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002216 false, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002217
2218 return Result;
2219}
2220
Dan Gohman475871a2008-07-27 21:46:04 +00002221SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002222 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002223 assert(Subtarget->isTargetELF() &&
2224 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002225 MachineFunction &MF = DAG.getMachineFunction();
2226 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002227 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002228 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002229 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002230 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingfe31e672011-10-01 08:58:29 +00002231 ARMConstantPoolValue *CPV =
2232 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2233 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002234 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002235 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002236 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002237 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002238 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002239 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002240 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002241}
2242
Jim Grosbach0e0da732009-05-12 23:59:14 +00002243SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002244ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2245 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002246 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendlingce370cf2011-10-07 21:25:38 +00002247 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2248 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002249 Op.getOperand(1), Val);
2250}
2251
2252SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002253ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2254 DebugLoc dl = Op.getDebugLoc();
2255 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2256 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2257}
2258
2259SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002260ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002261 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002262 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002263 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002264 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002265 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002266 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002267 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002268 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2269 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002270 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002271 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002272 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002273 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002274 EVT PtrVT = getPointerTy();
2275 DebugLoc dl = Op.getDebugLoc();
2276 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2277 SDValue CPAddr;
2278 unsigned PCAdj = (RelocM != Reloc::PIC_)
2279 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002280 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002281 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2282 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002283 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002284 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002285 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002286 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002287 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002288 false, false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002289
2290 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002291 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002292 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2293 }
2294 return Result;
2295 }
Evan Cheng92e39162011-03-29 23:06:19 +00002296 case Intrinsic::arm_neon_vmulls:
2297 case Intrinsic::arm_neon_vmullu: {
2298 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2299 ? ARMISD::VMULLs : ARMISD::VMULLu;
2300 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2301 Op.getOperand(1), Op.getOperand(2));
2302 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002303 }
2304}
2305
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002306static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002307 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002308 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002309 if (!Subtarget->hasDataBarrier()) {
2310 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2311 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2312 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002313 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002314 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002315 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002316 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002317 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002318
2319 SDValue Op5 = Op.getOperand(5);
2320 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2321 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2322 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2323 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2324
2325 ARM_MB::MemBOpt DMBOpt;
2326 if (isDeviceBarrier)
2327 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2328 else
2329 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2330 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2331 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002332}
2333
Eli Friedman26689ac2011-08-03 21:06:02 +00002334
2335static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2336 const ARMSubtarget *Subtarget) {
2337 // FIXME: handle "fence singlethread" more efficiently.
2338 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002339 if (!Subtarget->hasDataBarrier()) {
2340 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2341 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2342 // here.
2343 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2344 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002345 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002346 DAG.getConstant(0, MVT::i32));
2347 }
2348
Eli Friedman26689ac2011-08-03 21:06:02 +00002349 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002350 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002351}
2352
Evan Chengdfed19f2010-11-03 06:34:55 +00002353static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2354 const ARMSubtarget *Subtarget) {
2355 // ARM pre v5TE and Thumb1 does not have preload instructions.
2356 if (!(Subtarget->isThumb2() ||
2357 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2358 // Just preserve the chain.
2359 return Op.getOperand(0);
2360
2361 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002362 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2363 if (!isRead &&
2364 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2365 // ARMv7 with MP extension has PLDW.
2366 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002367
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002368 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2369 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002370 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002371 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002372 isData = ~isData & 1;
2373 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002374
2375 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002376 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2377 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002378}
2379
Dan Gohman1e93df62010-04-17 14:41:14 +00002380static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2381 MachineFunction &MF = DAG.getMachineFunction();
2382 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2383
Evan Chenga8e29892007-01-19 07:51:42 +00002384 // vastart just stores the address of the VarArgsFrameIndex slot into the
2385 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002386 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002387 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002388 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002389 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002390 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2391 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002392}
2393
Dan Gohman475871a2008-07-27 21:46:04 +00002394SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002395ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2396 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002397 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002398 MachineFunction &MF = DAG.getMachineFunction();
2399 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2400
2401 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002402 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002403 RC = ARM::tGPRRegisterClass;
2404 else
2405 RC = ARM::GPRRegisterClass;
2406
2407 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002408 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002409 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002410
2411 SDValue ArgValue2;
2412 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002413 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002414 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002415
2416 // Create load node to retrieve arguments from the stack.
2417 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002418 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002419 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002420 false, false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002421 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002422 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002423 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002424 }
2425
Jim Grosbache5165492009-11-09 00:11:35 +00002426 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002427}
2428
Stuart Hastingsc7315872011-04-20 16:47:52 +00002429void
2430ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2431 unsigned &VARegSize, unsigned &VARegSaveSize)
2432 const {
2433 unsigned NumGPRs;
2434 if (CCInfo.isFirstByValRegValid())
2435 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2436 else {
2437 unsigned int firstUnalloced;
2438 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2439 sizeof(GPRArgRegs) /
2440 sizeof(GPRArgRegs[0]));
2441 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2442 }
2443
2444 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2445 VARegSize = NumGPRs * 4;
2446 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2447}
2448
2449// The remaining GPRs hold either the beginning of variable-argument
2450// data, or the beginning of an aggregate passed by value (usuall
2451// byval). Either way, we allocate stack slots adjacent to the data
2452// provided by our caller, and store the unallocated registers there.
2453// If this is a variadic function, the va_list pointer will begin with
2454// these values; otherwise, this reassembles a (byval) structure that
2455// was split between registers and memory.
2456void
2457ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2458 DebugLoc dl, SDValue &Chain,
2459 unsigned ArgOffset) const {
2460 MachineFunction &MF = DAG.getMachineFunction();
2461 MachineFrameInfo *MFI = MF.getFrameInfo();
2462 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2463 unsigned firstRegToSaveIndex;
2464 if (CCInfo.isFirstByValRegValid())
2465 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2466 else {
2467 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2468 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2469 }
2470
2471 unsigned VARegSize, VARegSaveSize;
2472 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2473 if (VARegSaveSize) {
2474 // If this function is vararg, store any remaining integer argument regs
2475 // to their spots on the stack so that they may be loaded by deferencing
2476 // the result of va_next.
2477 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002478 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2479 ArgOffset + VARegSaveSize
2480 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002481 false));
2482 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2483 getPointerTy());
2484
2485 SmallVector<SDValue, 4> MemOps;
2486 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
2487 TargetRegisterClass *RC;
2488 if (AFI->isThumb1OnlyFunction())
2489 RC = ARM::tGPRRegisterClass;
2490 else
2491 RC = ARM::GPRRegisterClass;
2492
2493 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2494 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2495 SDValue Store =
2496 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002497 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002498 false, false, 0);
2499 MemOps.push_back(Store);
2500 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2501 DAG.getConstant(4, getPointerTy()));
2502 }
2503 if (!MemOps.empty())
2504 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2505 &MemOps[0], MemOps.size());
2506 } else
2507 // This will point to the next argument passed via stack.
2508 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2509}
2510
Bob Wilson5bafff32009-06-22 23:27:02 +00002511SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002512ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002513 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002514 const SmallVectorImpl<ISD::InputArg>
2515 &Ins,
2516 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002517 SmallVectorImpl<SDValue> &InVals)
2518 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002519 MachineFunction &MF = DAG.getMachineFunction();
2520 MachineFrameInfo *MFI = MF.getFrameInfo();
2521
Bob Wilson1f595bb2009-04-17 19:07:39 +00002522 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2523
2524 // Assign locations to all of the incoming arguments.
2525 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002526 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2527 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002528 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002529 CCAssignFnForNode(CallConv, /* Return*/ false,
2530 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002531
2532 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002533 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002534
Stuart Hastingsf222e592011-02-28 17:17:53 +00002535 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002536 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2537 CCValAssign &VA = ArgLocs[i];
2538
Bob Wilsondee46d72009-04-17 20:35:10 +00002539 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002540 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002541 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002542
Bob Wilson1f595bb2009-04-17 19:07:39 +00002543 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002544 // f64 and vector types are split up into multiple registers or
2545 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002546 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002547 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002548 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002549 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002550 SDValue ArgValue2;
2551 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002552 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002553 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2554 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002555 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002556 false, false, false, 0);
Bob Wilson6a234f02010-04-13 22:03:22 +00002557 } else {
2558 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2559 Chain, DAG, dl);
2560 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002561 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2562 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002563 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002564 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002565 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2566 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002567 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002568
Bob Wilson5bafff32009-06-22 23:27:02 +00002569 } else {
2570 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002571
Owen Anderson825b72b2009-08-11 20:47:22 +00002572 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002573 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002574 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002575 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002576 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002577 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002578 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002579 RC = (AFI->isThumb1OnlyFunction() ?
2580 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002581 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002582 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002583
2584 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002585 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002586 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002587 }
2588
2589 // If this is an 8 or 16-bit value, it is really passed promoted
2590 // to 32 bits. Insert an assert[sz]ext to capture this, then
2591 // truncate to the right size.
2592 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002593 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002594 case CCValAssign::Full: break;
2595 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002596 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002597 break;
2598 case CCValAssign::SExt:
2599 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2600 DAG.getValueType(VA.getValVT()));
2601 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2602 break;
2603 case CCValAssign::ZExt:
2604 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2605 DAG.getValueType(VA.getValVT()));
2606 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2607 break;
2608 }
2609
Dan Gohman98ca4f22009-08-05 01:29:28 +00002610 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002611
2612 } else { // VA.isRegLoc()
2613
2614 // sanity check
2615 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002616 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002617
Stuart Hastingsf222e592011-02-28 17:17:53 +00002618 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002619
Stuart Hastingsf222e592011-02-28 17:17:53 +00002620 // Some Ins[] entries become multiple ArgLoc[] entries.
2621 // Process them only once.
2622 if (index != lastInsIndex)
2623 {
2624 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002625 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002626 // This can be changed with more analysis.
2627 // In case of tail call optimization mark all arguments mutable.
2628 // Since they could be overwritten by lowering of arguments in case of
2629 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002630 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002631 unsigned VARegSize, VARegSaveSize;
2632 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2633 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2634 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002635 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002636 int FI = MFI->CreateFixedObject(Bytes,
2637 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002638 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2639 } else {
2640 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2641 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002642
Stuart Hastingsf222e592011-02-28 17:17:53 +00002643 // Create load nodes to retrieve arguments from the stack.
2644 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2645 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2646 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002647 false, false, false, 0));
Stuart Hastingsf222e592011-02-28 17:17:53 +00002648 }
2649 lastInsIndex = index;
2650 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002651 }
2652 }
2653
2654 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002655 if (isVarArg)
2656 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002657
Dan Gohman98ca4f22009-08-05 01:29:28 +00002658 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002659}
2660
2661/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002662static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002663 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002664 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002665 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002666 // Maybe this has already been legalized into the constant pool?
2667 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002668 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002669 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002670 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002671 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002672 }
2673 }
2674 return false;
2675}
2676
Evan Chenga8e29892007-01-19 07:51:42 +00002677/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2678/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002679SDValue
2680ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002681 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002682 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002683 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002684 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002685 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002686 // Constant does not fit, try adjusting it by one?
2687 switch (CC) {
2688 default: break;
2689 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002690 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002691 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002692 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002693 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002694 }
2695 break;
2696 case ISD::SETULT:
2697 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002698 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002699 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002700 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002701 }
2702 break;
2703 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002704 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002705 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002706 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002707 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002708 }
2709 break;
2710 case ISD::SETULE:
2711 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002712 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002713 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002714 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002715 }
2716 break;
2717 }
2718 }
2719 }
2720
2721 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002722 ARMISD::NodeType CompareType;
2723 switch (CondCode) {
2724 default:
2725 CompareType = ARMISD::CMP;
2726 break;
2727 case ARMCC::EQ:
2728 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002729 // Uses only Z Flag
2730 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002731 break;
2732 }
Evan Cheng218977b2010-07-13 19:27:42 +00002733 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002734 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002735}
2736
2737/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002738SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002739ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002740 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002741 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002742 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002743 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002744 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002745 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2746 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002747}
2748
Bob Wilson79f56c92011-03-08 01:17:20 +00002749/// duplicateCmp - Glue values can have only one use, so this function
2750/// duplicates a comparison node.
2751SDValue
2752ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2753 unsigned Opc = Cmp.getOpcode();
2754 DebugLoc DL = Cmp.getDebugLoc();
2755 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2756 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2757
2758 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2759 Cmp = Cmp.getOperand(0);
2760 Opc = Cmp.getOpcode();
2761 if (Opc == ARMISD::CMPFP)
2762 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2763 else {
2764 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2765 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2766 }
2767 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2768}
2769
Bill Wendlingde2b1512010-08-11 08:43:16 +00002770SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2771 SDValue Cond = Op.getOperand(0);
2772 SDValue SelectTrue = Op.getOperand(1);
2773 SDValue SelectFalse = Op.getOperand(2);
2774 DebugLoc dl = Op.getDebugLoc();
2775
2776 // Convert:
2777 //
2778 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2779 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2780 //
2781 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2782 const ConstantSDNode *CMOVTrue =
2783 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2784 const ConstantSDNode *CMOVFalse =
2785 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2786
2787 if (CMOVTrue && CMOVFalse) {
2788 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2789 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2790
2791 SDValue True;
2792 SDValue False;
2793 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2794 True = SelectTrue;
2795 False = SelectFalse;
2796 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2797 True = SelectFalse;
2798 False = SelectTrue;
2799 }
2800
2801 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002802 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002803 SDValue ARMcc = Cond.getOperand(2);
2804 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002805 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002806 assert(True.getValueType() == VT);
2807 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002808 }
2809 }
2810 }
2811
2812 return DAG.getSelectCC(dl, Cond,
2813 DAG.getConstant(0, Cond.getValueType()),
2814 SelectTrue, SelectFalse, ISD::SETNE);
2815}
2816
Dan Gohmand858e902010-04-17 15:26:15 +00002817SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002818 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002819 SDValue LHS = Op.getOperand(0);
2820 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002821 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002822 SDValue TrueVal = Op.getOperand(2);
2823 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002824 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002825
Owen Anderson825b72b2009-08-11 20:47:22 +00002826 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002827 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002828 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002829 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Jim Grosbachb04546f2011-09-13 20:30:37 +00002830 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002831 }
2832
2833 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002834 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002835
Evan Cheng218977b2010-07-13 19:27:42 +00002836 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2837 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002838 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002839 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002840 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002841 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002842 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002843 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002844 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002845 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002846 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002847 }
2848 return Result;
2849}
2850
Evan Cheng218977b2010-07-13 19:27:42 +00002851/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2852/// to morph to an integer compare sequence.
2853static bool canChangeToInt(SDValue Op, bool &SeenZero,
2854 const ARMSubtarget *Subtarget) {
2855 SDNode *N = Op.getNode();
2856 if (!N->hasOneUse())
2857 // Otherwise it requires moving the value from fp to integer registers.
2858 return false;
2859 if (!N->getNumValues())
2860 return false;
2861 EVT VT = Op.getValueType();
2862 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2863 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2864 // vmrs are very slow, e.g. cortex-a8.
2865 return false;
2866
2867 if (isFloatingPointZero(Op)) {
2868 SeenZero = true;
2869 return true;
2870 }
2871 return ISD::isNormalLoad(N);
2872}
2873
2874static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2875 if (isFloatingPointZero(Op))
2876 return DAG.getConstant(0, MVT::i32);
2877
2878 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2879 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002880 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002881 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002882 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002883
2884 llvm_unreachable("Unknown VFP cmp argument!");
2885}
2886
2887static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2888 SDValue &RetVal1, SDValue &RetVal2) {
2889 if (isFloatingPointZero(Op)) {
2890 RetVal1 = DAG.getConstant(0, MVT::i32);
2891 RetVal2 = DAG.getConstant(0, MVT::i32);
2892 return;
2893 }
2894
2895 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2896 SDValue Ptr = Ld->getBasePtr();
2897 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2898 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002899 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002900 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002901 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002902
2903 EVT PtrType = Ptr.getValueType();
2904 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2905 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2906 PtrType, Ptr, DAG.getConstant(4, PtrType));
2907 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2908 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002909 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002910 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002911 Ld->isInvariant(), NewAlign);
Evan Cheng218977b2010-07-13 19:27:42 +00002912 return;
2913 }
2914
2915 llvm_unreachable("Unknown VFP cmp argument!");
2916}
2917
2918/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2919/// f32 and even f64 comparisons to integer ones.
2920SDValue
2921ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2922 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002923 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002924 SDValue LHS = Op.getOperand(2);
2925 SDValue RHS = Op.getOperand(3);
2926 SDValue Dest = Op.getOperand(4);
2927 DebugLoc dl = Op.getDebugLoc();
2928
2929 bool SeenZero = false;
2930 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2931 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002932 // If one of the operand is zero, it's safe to ignore the NaN case since
2933 // we only care about equality comparisons.
2934 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Bob Wilson1b772f92011-03-08 01:17:16 +00002935 // If unsafe fp math optimization is enabled and there are no other uses of
2936 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00002937 // to an integer comparison.
2938 if (CC == ISD::SETOEQ)
2939 CC = ISD::SETEQ;
2940 else if (CC == ISD::SETUNE)
2941 CC = ISD::SETNE;
2942
2943 SDValue ARMcc;
2944 if (LHS.getValueType() == MVT::f32) {
2945 LHS = bitcastf32Toi32(LHS, DAG);
2946 RHS = bitcastf32Toi32(RHS, DAG);
2947 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2948 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2949 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2950 Chain, Dest, ARMcc, CCR, Cmp);
2951 }
2952
2953 SDValue LHS1, LHS2;
2954 SDValue RHS1, RHS2;
2955 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2956 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2957 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2958 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002959 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002960 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2961 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2962 }
2963
2964 return SDValue();
2965}
2966
2967SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2968 SDValue Chain = Op.getOperand(0);
2969 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2970 SDValue LHS = Op.getOperand(2);
2971 SDValue RHS = Op.getOperand(3);
2972 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002973 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002974
Owen Anderson825b72b2009-08-11 20:47:22 +00002975 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002976 SDValue ARMcc;
2977 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002978 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002979 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002980 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002981 }
2982
Owen Anderson825b72b2009-08-11 20:47:22 +00002983 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002984
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002985 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng218977b2010-07-13 19:27:42 +00002986 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2987 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2988 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2989 if (Result.getNode())
2990 return Result;
2991 }
2992
Evan Chenga8e29892007-01-19 07:51:42 +00002993 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002994 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002995
Evan Cheng218977b2010-07-13 19:27:42 +00002996 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2997 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002998 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002999 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003000 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00003001 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003002 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00003003 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3004 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00003005 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003006 }
3007 return Res;
3008}
3009
Dan Gohmand858e902010-04-17 15:26:15 +00003010SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00003011 SDValue Chain = Op.getOperand(0);
3012 SDValue Table = Op.getOperand(1);
3013 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003014 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003015
Owen Andersone50ed302009-08-10 22:56:29 +00003016 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00003017 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3018 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00003019 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00003020 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00003021 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00003022 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3023 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00003024 if (Subtarget->isThumb2()) {
3025 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3026 // which does another jump to the destination. This also makes it easier
3027 // to translate it to TBB / TBH later.
3028 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00003029 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00003030 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003031 }
Evan Cheng66ac5312009-07-25 00:33:29 +00003032 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00003033 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003034 MachinePointerInfo::getJumpTable(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003035 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003036 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003037 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00003038 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003039 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00003040 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003041 MachinePointerInfo::getJumpTable(),
3042 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003043 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003044 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003045 }
Evan Chenga8e29892007-01-19 07:51:42 +00003046}
3047
Eli Friedman14e809c2011-11-09 23:36:02 +00003048static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
3049 EVT VT = Op.getValueType();
3050 assert(VT.getVectorElementType() == MVT::i32 && "Unexpected custom lowering");
3051
3052 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3053 return Op;
3054 return DAG.UnrollVectorOp(Op.getNode());
3055}
3056
Bob Wilson76a312b2010-03-19 22:51:32 +00003057static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman14e809c2011-11-09 23:36:02 +00003058 EVT VT = Op.getValueType();
3059 if (VT.isVector())
3060 return LowerVectorFP_TO_INT(Op, DAG);
3061
Bob Wilson76a312b2010-03-19 22:51:32 +00003062 DebugLoc dl = Op.getDebugLoc();
3063 unsigned Opc;
3064
3065 switch (Op.getOpcode()) {
3066 default:
3067 assert(0 && "Invalid opcode!");
3068 case ISD::FP_TO_SINT:
3069 Opc = ARMISD::FTOSI;
3070 break;
3071 case ISD::FP_TO_UINT:
3072 Opc = ARMISD::FTOUI;
3073 break;
3074 }
3075 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003076 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003077}
3078
Cameron Zwarich3007d332011-03-29 21:41:55 +00003079static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3080 EVT VT = Op.getValueType();
3081 DebugLoc dl = Op.getDebugLoc();
3082
Eli Friedman14e809c2011-11-09 23:36:02 +00003083 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3084 if (VT.getVectorElementType() == MVT::f32)
3085 return Op;
3086 return DAG.UnrollVectorOp(Op.getNode());
3087 }
3088
Duncan Sands1f6a3292011-08-12 14:54:45 +00003089 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3090 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003091 if (VT != MVT::v4f32)
3092 return DAG.UnrollVectorOp(Op.getNode());
3093
3094 unsigned CastOpc;
3095 unsigned Opc;
3096 switch (Op.getOpcode()) {
3097 default:
3098 assert(0 && "Invalid opcode!");
3099 case ISD::SINT_TO_FP:
3100 CastOpc = ISD::SIGN_EXTEND;
3101 Opc = ISD::SINT_TO_FP;
3102 break;
3103 case ISD::UINT_TO_FP:
3104 CastOpc = ISD::ZERO_EXTEND;
3105 Opc = ISD::UINT_TO_FP;
3106 break;
3107 }
3108
3109 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3110 return DAG.getNode(Opc, dl, VT, Op);
3111}
3112
Bob Wilson76a312b2010-03-19 22:51:32 +00003113static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3114 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003115 if (VT.isVector())
3116 return LowerVectorINT_TO_FP(Op, DAG);
3117
Bob Wilson76a312b2010-03-19 22:51:32 +00003118 DebugLoc dl = Op.getDebugLoc();
3119 unsigned Opc;
3120
3121 switch (Op.getOpcode()) {
3122 default:
3123 assert(0 && "Invalid opcode!");
3124 case ISD::SINT_TO_FP:
3125 Opc = ARMISD::SITOF;
3126 break;
3127 case ISD::UINT_TO_FP:
3128 Opc = ARMISD::UITOF;
3129 break;
3130 }
3131
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003132 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003133 return DAG.getNode(Opc, dl, VT, Op);
3134}
3135
Evan Cheng515fe3a2010-07-08 02:08:50 +00003136SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003137 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003138 SDValue Tmp0 = Op.getOperand(0);
3139 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003140 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003141 EVT VT = Op.getValueType();
3142 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003143 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3144 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3145 bool UseNEON = !InGPR && Subtarget->hasNEON();
3146
3147 if (UseNEON) {
3148 // Use VBSL to copy the sign bit.
3149 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3150 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3151 DAG.getTargetConstant(EncodedVal, MVT::i32));
3152 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3153 if (VT == MVT::f64)
3154 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3155 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3156 DAG.getConstant(32, MVT::i32));
3157 else /*if (VT == MVT::f32)*/
3158 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3159 if (SrcVT == MVT::f32) {
3160 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3161 if (VT == MVT::f64)
3162 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3163 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3164 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003165 } else if (VT == MVT::f32)
3166 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3167 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3168 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003169 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3170 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3171
3172 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3173 MVT::i32);
3174 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3175 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3176 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003177
Evan Chenge573fb32011-02-23 02:24:55 +00003178 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3179 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3180 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003181 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003182 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3183 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3184 DAG.getConstant(0, MVT::i32));
3185 } else {
3186 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3187 }
3188
3189 return Res;
3190 }
Evan Chengc143dd42011-02-11 02:28:55 +00003191
3192 // Bitcast operand 1 to i32.
3193 if (SrcVT == MVT::f64)
3194 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3195 &Tmp1, 1).getValue(1);
3196 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3197
Evan Chenge573fb32011-02-23 02:24:55 +00003198 // Or in the signbit with integer operations.
3199 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3200 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3201 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3202 if (VT == MVT::f32) {
3203 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3204 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3205 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3206 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003207 }
3208
Evan Chenge573fb32011-02-23 02:24:55 +00003209 // f64: Or the high part with signbit and then combine two parts.
3210 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3211 &Tmp0, 1);
3212 SDValue Lo = Tmp0.getValue(0);
3213 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3214 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3215 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003216}
3217
Evan Cheng2457f2c2010-05-22 01:47:14 +00003218SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3219 MachineFunction &MF = DAG.getMachineFunction();
3220 MachineFrameInfo *MFI = MF.getFrameInfo();
3221 MFI->setReturnAddressIsTaken(true);
3222
3223 EVT VT = Op.getValueType();
3224 DebugLoc dl = Op.getDebugLoc();
3225 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3226 if (Depth) {
3227 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3228 SDValue Offset = DAG.getConstant(4, MVT::i32);
3229 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3230 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003231 MachinePointerInfo(), false, false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003232 }
3233
3234 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003235 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003236 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3237}
3238
Dan Gohmand858e902010-04-17 15:26:15 +00003239SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003240 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3241 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003242
Owen Andersone50ed302009-08-10 22:56:29 +00003243 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003244 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3245 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003246 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003247 ? ARM::R7 : ARM::R11;
3248 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3249 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003250 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3251 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003252 false, false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003253 return FrameAddr;
3254}
3255
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003256/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003257/// expand a bit convert where either the source or destination type is i64 to
3258/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3259/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3260/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003261static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003262 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3263 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003264 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003265
Bob Wilson9f3f0612010-04-17 05:30:19 +00003266 // This function is only supposed to be called for i64 types, either as the
3267 // source or destination of the bit convert.
3268 EVT SrcVT = Op.getValueType();
3269 EVT DstVT = N->getValueType(0);
3270 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003271 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003272
Bob Wilson9f3f0612010-04-17 05:30:19 +00003273 // Turn i64->f64 into VMOVDRR.
3274 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003275 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3276 DAG.getConstant(0, MVT::i32));
3277 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3278 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003279 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003280 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003281 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003282
Jim Grosbache5165492009-11-09 00:11:35 +00003283 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003284 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3285 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3286 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3287 // Merge the pieces into a single i64 value.
3288 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3289 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003290
Bob Wilson9f3f0612010-04-17 05:30:19 +00003291 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003292}
3293
Bob Wilson5bafff32009-06-22 23:27:02 +00003294/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003295/// Zero vectors are used to represent vector negation and in those cases
3296/// will be implemented with the NEON VNEG instruction. However, VNEG does
3297/// not support i64 elements, so sometimes the zero vectors will need to be
3298/// explicitly constructed. Regardless, use a canonical VMOV to create the
3299/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003300static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003301 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003302 // The canonical modified immediate encoding of a zero vector is....0!
3303 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3304 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3305 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003306 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003307}
3308
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003309/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3310/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003311SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3312 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003313 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3314 EVT VT = Op.getValueType();
3315 unsigned VTBits = VT.getSizeInBits();
3316 DebugLoc dl = Op.getDebugLoc();
3317 SDValue ShOpLo = Op.getOperand(0);
3318 SDValue ShOpHi = Op.getOperand(1);
3319 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003320 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003321 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003322
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003323 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3324
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003325 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3326 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3327 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3328 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3329 DAG.getConstant(VTBits, MVT::i32));
3330 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3331 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003332 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003333
3334 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3335 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003336 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003337 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003338 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003339 CCR, Cmp);
3340
3341 SDValue Ops[2] = { Lo, Hi };
3342 return DAG.getMergeValues(Ops, 2, dl);
3343}
3344
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003345/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3346/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003347SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3348 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003349 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3350 EVT VT = Op.getValueType();
3351 unsigned VTBits = VT.getSizeInBits();
3352 DebugLoc dl = Op.getDebugLoc();
3353 SDValue ShOpLo = Op.getOperand(0);
3354 SDValue ShOpHi = Op.getOperand(1);
3355 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003356 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003357
3358 assert(Op.getOpcode() == ISD::SHL_PARTS);
3359 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3360 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3361 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3362 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3363 DAG.getConstant(VTBits, MVT::i32));
3364 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3365 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3366
3367 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3368 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3369 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003370 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003371 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003372 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003373 CCR, Cmp);
3374
3375 SDValue Ops[2] = { Lo, Hi };
3376 return DAG.getMergeValues(Ops, 2, dl);
3377}
3378
Jim Grosbach4725ca72010-09-08 03:54:02 +00003379SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003380 SelectionDAG &DAG) const {
3381 // The rounding mode is in bits 23:22 of the FPSCR.
3382 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3383 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3384 // so that the shift + and get folded into a bitfield extract.
3385 DebugLoc dl = Op.getDebugLoc();
3386 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3387 DAG.getConstant(Intrinsic::arm_get_fpscr,
3388 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003389 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003390 DAG.getConstant(1U << 22, MVT::i32));
3391 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3392 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003393 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003394 DAG.getConstant(3, MVT::i32));
3395}
3396
Jim Grosbach3482c802010-01-18 19:58:49 +00003397static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3398 const ARMSubtarget *ST) {
3399 EVT VT = N->getValueType(0);
3400 DebugLoc dl = N->getDebugLoc();
3401
3402 if (!ST->hasV6T2Ops())
3403 return SDValue();
3404
3405 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3406 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3407}
3408
Bob Wilson5bafff32009-06-22 23:27:02 +00003409static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3410 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003411 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003412 DebugLoc dl = N->getDebugLoc();
3413
Bob Wilsond5448bb2010-11-18 21:16:28 +00003414 if (!VT.isVector())
3415 return SDValue();
3416
Bob Wilson5bafff32009-06-22 23:27:02 +00003417 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003418 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003419
Bob Wilsond5448bb2010-11-18 21:16:28 +00003420 // Left shifts translate directly to the vshiftu intrinsic.
3421 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003422 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003423 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3424 N->getOperand(0), N->getOperand(1));
3425
3426 assert((N->getOpcode() == ISD::SRA ||
3427 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3428
3429 // NEON uses the same intrinsics for both left and right shifts. For
3430 // right shifts, the shift amounts are negative, so negate the vector of
3431 // shift amounts.
3432 EVT ShiftVT = N->getOperand(1).getValueType();
3433 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3434 getZeroVector(ShiftVT, DAG, dl),
3435 N->getOperand(1));
3436 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3437 Intrinsic::arm_neon_vshifts :
3438 Intrinsic::arm_neon_vshiftu);
3439 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3440 DAG.getConstant(vshiftInt, MVT::i32),
3441 N->getOperand(0), NegatedCount);
3442}
3443
3444static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3445 const ARMSubtarget *ST) {
3446 EVT VT = N->getValueType(0);
3447 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003448
Eli Friedmance392eb2009-08-22 03:13:10 +00003449 // We can get here for a node like i32 = ISD::SHL i32, i64
3450 if (VT != MVT::i64)
3451 return SDValue();
3452
3453 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003454 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003455
Chris Lattner27a6c732007-11-24 07:07:01 +00003456 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3457 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003458 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003459 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003460
Chris Lattner27a6c732007-11-24 07:07:01 +00003461 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003462 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003463
Chris Lattner27a6c732007-11-24 07:07:01 +00003464 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003465 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003466 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003467 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003468 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003469
Chris Lattner27a6c732007-11-24 07:07:01 +00003470 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3471 // captures the result into a carry flag.
3472 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003473 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003474
Chris Lattner27a6c732007-11-24 07:07:01 +00003475 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003476 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003477
Chris Lattner27a6c732007-11-24 07:07:01 +00003478 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003479 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003480}
3481
Bob Wilson5bafff32009-06-22 23:27:02 +00003482static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3483 SDValue TmpOp0, TmpOp1;
3484 bool Invert = false;
3485 bool Swap = false;
3486 unsigned Opc = 0;
3487
3488 SDValue Op0 = Op.getOperand(0);
3489 SDValue Op1 = Op.getOperand(1);
3490 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003491 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003492 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3493 DebugLoc dl = Op.getDebugLoc();
3494
3495 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3496 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003497 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003498 case ISD::SETUNE:
3499 case ISD::SETNE: Invert = true; // Fallthrough
3500 case ISD::SETOEQ:
3501 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3502 case ISD::SETOLT:
3503 case ISD::SETLT: Swap = true; // Fallthrough
3504 case ISD::SETOGT:
3505 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3506 case ISD::SETOLE:
3507 case ISD::SETLE: Swap = true; // Fallthrough
3508 case ISD::SETOGE:
3509 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3510 case ISD::SETUGE: Swap = true; // Fallthrough
3511 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3512 case ISD::SETUGT: Swap = true; // Fallthrough
3513 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3514 case ISD::SETUEQ: Invert = true; // Fallthrough
3515 case ISD::SETONE:
3516 // Expand this to (OLT | OGT).
3517 TmpOp0 = Op0;
3518 TmpOp1 = Op1;
3519 Opc = ISD::OR;
3520 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3521 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3522 break;
3523 case ISD::SETUO: Invert = true; // Fallthrough
3524 case ISD::SETO:
3525 // Expand this to (OLT | OGE).
3526 TmpOp0 = Op0;
3527 TmpOp1 = Op1;
3528 Opc = ISD::OR;
3529 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3530 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3531 break;
3532 }
3533 } else {
3534 // Integer comparisons.
3535 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003536 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003537 case ISD::SETNE: Invert = true;
3538 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3539 case ISD::SETLT: Swap = true;
3540 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3541 case ISD::SETLE: Swap = true;
3542 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3543 case ISD::SETULT: Swap = true;
3544 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3545 case ISD::SETULE: Swap = true;
3546 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3547 }
3548
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003549 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003550 if (Opc == ARMISD::VCEQ) {
3551
3552 SDValue AndOp;
3553 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3554 AndOp = Op0;
3555 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3556 AndOp = Op1;
3557
3558 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003559 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003560 AndOp = AndOp.getOperand(0);
3561
3562 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3563 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003564 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3565 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003566 Invert = !Invert;
3567 }
3568 }
3569 }
3570
3571 if (Swap)
3572 std::swap(Op0, Op1);
3573
Owen Andersonc24cb352010-11-08 23:21:22 +00003574 // If one of the operands is a constant vector zero, attempt to fold the
3575 // comparison to a specialized compare-against-zero form.
3576 SDValue SingleOp;
3577 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3578 SingleOp = Op0;
3579 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3580 if (Opc == ARMISD::VCGE)
3581 Opc = ARMISD::VCLEZ;
3582 else if (Opc == ARMISD::VCGT)
3583 Opc = ARMISD::VCLTZ;
3584 SingleOp = Op1;
3585 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003586
Owen Andersonc24cb352010-11-08 23:21:22 +00003587 SDValue Result;
3588 if (SingleOp.getNode()) {
3589 switch (Opc) {
3590 case ARMISD::VCEQ:
3591 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3592 case ARMISD::VCGE:
3593 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3594 case ARMISD::VCLEZ:
3595 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3596 case ARMISD::VCGT:
3597 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3598 case ARMISD::VCLTZ:
3599 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3600 default:
3601 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3602 }
3603 } else {
3604 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3605 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003606
3607 if (Invert)
3608 Result = DAG.getNOT(dl, Result, VT);
3609
3610 return Result;
3611}
3612
Bob Wilsond3c42842010-06-14 22:19:57 +00003613/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3614/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003615/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003616static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3617 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003618 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003619 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003620
Bob Wilson827b2102010-06-15 19:05:35 +00003621 // SplatBitSize is set to the smallest size that splats the vector, so a
3622 // zero vector will always have SplatBitSize == 8. However, NEON modified
3623 // immediate instructions others than VMOV do not support the 8-bit encoding
3624 // of a zero vector, and the default encoding of zero is supposed to be the
3625 // 32-bit version.
3626 if (SplatBits == 0)
3627 SplatBitSize = 32;
3628
Bob Wilson5bafff32009-06-22 23:27:02 +00003629 switch (SplatBitSize) {
3630 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003631 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003632 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003633 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003634 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003635 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003636 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003637 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003638 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003639
3640 case 16:
3641 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003642 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003643 if ((SplatBits & ~0xff) == 0) {
3644 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003645 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003646 Imm = SplatBits;
3647 break;
3648 }
3649 if ((SplatBits & ~0xff00) == 0) {
3650 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003651 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003652 Imm = SplatBits >> 8;
3653 break;
3654 }
3655 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003656
3657 case 32:
3658 // NEON's 32-bit VMOV supports splat values where:
3659 // * only one byte is nonzero, or
3660 // * the least significant byte is 0xff and the second byte is nonzero, or
3661 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003662 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003663 if ((SplatBits & ~0xff) == 0) {
3664 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003665 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003666 Imm = SplatBits;
3667 break;
3668 }
3669 if ((SplatBits & ~0xff00) == 0) {
3670 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003671 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003672 Imm = SplatBits >> 8;
3673 break;
3674 }
3675 if ((SplatBits & ~0xff0000) == 0) {
3676 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003677 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003678 Imm = SplatBits >> 16;
3679 break;
3680 }
3681 if ((SplatBits & ~0xff000000) == 0) {
3682 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003683 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003684 Imm = SplatBits >> 24;
3685 break;
3686 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003687
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003688 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3689 if (type == OtherModImm) return SDValue();
3690
Bob Wilson5bafff32009-06-22 23:27:02 +00003691 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003692 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3693 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003694 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003695 Imm = SplatBits >> 8;
3696 SplatBits |= 0xff;
3697 break;
3698 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003699
3700 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003701 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3702 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003703 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003704 Imm = SplatBits >> 16;
3705 SplatBits |= 0xffff;
3706 break;
3707 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003708
3709 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3710 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3711 // VMOV.I32. A (very) minor optimization would be to replicate the value
3712 // and fall through here to test for a valid 64-bit splat. But, then the
3713 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003714 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003715
3716 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003717 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003718 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003719 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003720 uint64_t BitMask = 0xff;
3721 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003722 unsigned ImmMask = 1;
3723 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003724 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003725 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003726 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003727 Imm |= ImmMask;
3728 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003729 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003730 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003731 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003732 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003733 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003734 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003735 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003736 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003737 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003738 break;
3739 }
3740
Bob Wilson1a913ed2010-06-11 21:34:50 +00003741 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003742 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003743 return SDValue();
3744 }
3745
Bob Wilsoncba270d2010-07-13 21:16:48 +00003746 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3747 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003748}
3749
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003750static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3751 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003752 unsigned NumElts = VT.getVectorNumElements();
3753 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003754
3755 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3756 if (M[0] < 0)
3757 return false;
3758
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003759 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003760
3761 // If this is a VEXT shuffle, the immediate value is the index of the first
3762 // element. The other shuffle indices must be the successive elements after
3763 // the first one.
3764 unsigned ExpectedElt = Imm;
3765 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003766 // Increment the expected index. If it wraps around, it may still be
3767 // a VEXT but the source vectors must be swapped.
3768 ExpectedElt += 1;
3769 if (ExpectedElt == NumElts * 2) {
3770 ExpectedElt = 0;
3771 ReverseVEXT = true;
3772 }
3773
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003774 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003775 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003776 return false;
3777 }
3778
3779 // Adjust the index value if the source operands will be swapped.
3780 if (ReverseVEXT)
3781 Imm -= NumElts;
3782
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003783 return true;
3784}
3785
Bob Wilson8bb9e482009-07-26 00:39:34 +00003786/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3787/// instruction with the specified blocksize. (The order of the elements
3788/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003789static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3790 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003791 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3792 "Only possible block sizes for VREV are: 16, 32, 64");
3793
Bob Wilson8bb9e482009-07-26 00:39:34 +00003794 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003795 if (EltSz == 64)
3796 return false;
3797
3798 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003799 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003800 // If the first shuffle index is UNDEF, be optimistic.
3801 if (M[0] < 0)
3802 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003803
3804 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3805 return false;
3806
3807 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003808 if (M[i] < 0) continue; // ignore UNDEF indices
3809 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003810 return false;
3811 }
3812
3813 return true;
3814}
3815
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003816static bool isVTBLMask(const SmallVectorImpl<int> &M, EVT VT) {
3817 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3818 // range, then 0 is placed into the resulting vector. So pretty much any mask
3819 // of 8 elements can work here.
3820 return VT == MVT::v8i8 && M.size() == 8;
3821}
3822
Bob Wilsonc692cb72009-08-21 20:54:19 +00003823static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3824 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003825 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3826 if (EltSz == 64)
3827 return false;
3828
Bob Wilsonc692cb72009-08-21 20:54:19 +00003829 unsigned NumElts = VT.getVectorNumElements();
3830 WhichResult = (M[0] == 0 ? 0 : 1);
3831 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003832 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3833 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003834 return false;
3835 }
3836 return true;
3837}
3838
Bob Wilson324f4f12009-12-03 06:40:55 +00003839/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3840/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3841/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3842static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3843 unsigned &WhichResult) {
3844 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
Bob Wilsonc692cb72009-08-21 20:54:19 +00003858static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3859 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003860 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3861 if (EltSz == 64)
3862 return false;
3863
Bob Wilsonc692cb72009-08-21 20:54:19 +00003864 unsigned NumElts = VT.getVectorNumElements();
3865 WhichResult = (M[0] == 0 ? 0 : 1);
3866 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003867 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003868 if ((unsigned) M[i] != 2 * i + WhichResult)
3869 return false;
3870 }
3871
3872 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003873 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003874 return false;
3875
3876 return true;
3877}
3878
Bob Wilson324f4f12009-12-03 06:40:55 +00003879/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3880/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3881/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3882static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3883 unsigned &WhichResult) {
3884 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3885 if (EltSz == 64)
3886 return false;
3887
3888 unsigned Half = VT.getVectorNumElements() / 2;
3889 WhichResult = (M[0] == 0 ? 0 : 1);
3890 for (unsigned j = 0; j != 2; ++j) {
3891 unsigned Idx = WhichResult;
3892 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003893 int MIdx = M[i + j * Half];
3894 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003895 return false;
3896 Idx += 2;
3897 }
3898 }
3899
3900 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3901 if (VT.is64BitVector() && EltSz == 32)
3902 return false;
3903
3904 return true;
3905}
3906
Bob Wilsonc692cb72009-08-21 20:54:19 +00003907static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3908 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003909 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3910 if (EltSz == 64)
3911 return false;
3912
Bob Wilsonc692cb72009-08-21 20:54:19 +00003913 unsigned NumElts = VT.getVectorNumElements();
3914 WhichResult = (M[0] == 0 ? 0 : 1);
3915 unsigned Idx = WhichResult * NumElts / 2;
3916 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003917 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3918 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003919 return false;
3920 Idx += 1;
3921 }
3922
3923 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003924 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003925 return false;
3926
3927 return true;
3928}
3929
Bob Wilson324f4f12009-12-03 06:40:55 +00003930/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3931/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3932/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3933static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3934 unsigned &WhichResult) {
3935 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3936 if (EltSz == 64)
3937 return false;
3938
3939 unsigned NumElts = VT.getVectorNumElements();
3940 WhichResult = (M[0] == 0 ? 0 : 1);
3941 unsigned Idx = WhichResult * NumElts / 2;
3942 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003943 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3944 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003945 return false;
3946 Idx += 1;
3947 }
3948
3949 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3950 if (VT.is64BitVector() && EltSz == 32)
3951 return false;
3952
3953 return true;
3954}
3955
Dale Johannesenf630c712010-07-29 20:10:08 +00003956// If N is an integer constant that can be moved into a register in one
3957// instruction, return an SDValue of such a constant (will become a MOV
3958// instruction). Otherwise return null.
3959static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3960 const ARMSubtarget *ST, DebugLoc dl) {
3961 uint64_t Val;
3962 if (!isa<ConstantSDNode>(N))
3963 return SDValue();
3964 Val = cast<ConstantSDNode>(N)->getZExtValue();
3965
3966 if (ST->isThumb1Only()) {
3967 if (Val <= 255 || ~Val <= 255)
3968 return DAG.getConstant(Val, MVT::i32);
3969 } else {
3970 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3971 return DAG.getConstant(Val, MVT::i32);
3972 }
3973 return SDValue();
3974}
3975
Bob Wilson5bafff32009-06-22 23:27:02 +00003976// If this is a case we can't handle, return null and let the default
3977// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003978SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3979 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003980 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003981 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003982 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003983
3984 APInt SplatBits, SplatUndef;
3985 unsigned SplatBitSize;
3986 bool HasAnyUndefs;
3987 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003988 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003989 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003990 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003991 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003992 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003993 DAG, VmovVT, VT.is128BitVector(),
3994 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003995 if (Val.getNode()) {
3996 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003997 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003998 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003999
4000 // Try an immediate VMVN.
Eli Friedman8e4d0422011-10-13 22:40:23 +00004001 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004002 Val = isNEONModifiedImm(NegatedImm,
4003 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004004 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004005 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004006 if (Val.getNode()) {
4007 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004008 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004009 }
Evan Chengeaa192a2011-11-15 02:12:34 +00004010
4011 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedman2f21e8c2011-12-15 22:56:53 +00004012 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedmaneffab8f2011-12-09 23:54:42 +00004013 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Chengeaa192a2011-11-15 02:12:34 +00004014 if (ImmVal != -1) {
4015 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4016 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4017 }
4018 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004019 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00004020 }
4021
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004022 // Scan through the operands to see if only one value is used.
4023 unsigned NumElts = VT.getVectorNumElements();
4024 bool isOnlyLowElement = true;
4025 bool usesOnlyOneValue = true;
4026 bool isConstant = true;
4027 SDValue Value;
4028 for (unsigned i = 0; i < NumElts; ++i) {
4029 SDValue V = Op.getOperand(i);
4030 if (V.getOpcode() == ISD::UNDEF)
4031 continue;
4032 if (i > 0)
4033 isOnlyLowElement = false;
4034 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4035 isConstant = false;
4036
4037 if (!Value.getNode())
4038 Value = V;
4039 else if (V != Value)
4040 usesOnlyOneValue = false;
4041 }
4042
4043 if (!Value.getNode())
4044 return DAG.getUNDEF(VT);
4045
4046 if (isOnlyLowElement)
4047 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4048
Dale Johannesenf630c712010-07-29 20:10:08 +00004049 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4050
Dale Johannesen575cd142010-10-19 20:00:17 +00004051 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4052 // i32 and try again.
4053 if (usesOnlyOneValue && EltSize <= 32) {
4054 if (!isConstant)
4055 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4056 if (VT.getVectorElementType().isFloatingPoint()) {
4057 SmallVector<SDValue, 8> Ops;
4058 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004059 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00004060 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00004061 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
4062 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00004063 Val = LowerBUILD_VECTOR(Val, DAG, ST);
4064 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004065 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004066 }
Dale Johannesen575cd142010-10-19 20:00:17 +00004067 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
4068 if (Val.getNode())
4069 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004070 }
4071
4072 // If all elements are constants and the case above didn't get hit, fall back
4073 // to the default expansion, which will generate a load from the constant
4074 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004075 if (isConstant)
4076 return SDValue();
4077
Bob Wilson11a1dff2011-01-07 21:37:30 +00004078 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4079 if (NumElts >= 4) {
4080 SDValue shuffle = ReconstructShuffle(Op, DAG);
4081 if (shuffle != SDValue())
4082 return shuffle;
4083 }
4084
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004085 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004086 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4087 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004088 if (EltSize >= 32) {
4089 // Do the expansion with floating-point types, since that is what the VFP
4090 // registers are defined to use, and since i64 is not legal.
4091 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4092 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004093 SmallVector<SDValue, 8> Ops;
4094 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004095 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004096 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004097 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004098 }
4099
4100 return SDValue();
4101}
4102
Bob Wilson11a1dff2011-01-07 21:37:30 +00004103// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004104// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004105SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4106 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004107 DebugLoc dl = Op.getDebugLoc();
4108 EVT VT = Op.getValueType();
4109 unsigned NumElts = VT.getVectorNumElements();
4110
4111 SmallVector<SDValue, 2> SourceVecs;
4112 SmallVector<unsigned, 2> MinElts;
4113 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004114
Bob Wilson11a1dff2011-01-07 21:37:30 +00004115 for (unsigned i = 0; i < NumElts; ++i) {
4116 SDValue V = Op.getOperand(i);
4117 if (V.getOpcode() == ISD::UNDEF)
4118 continue;
4119 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4120 // A shuffle can only come from building a vector from various
4121 // elements of other vectors.
4122 return SDValue();
Eli Friedman46995fa2011-10-14 23:58:49 +00004123 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
4124 VT.getVectorElementType()) {
4125 // This code doesn't know how to handle shuffles where the vector
4126 // element types do not match (this happens because type legalization
4127 // promotes the return type of EXTRACT_VECTOR_ELT).
4128 // FIXME: It might be appropriate to extend this code to handle
4129 // mismatched types.
4130 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004131 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004132
Bob Wilson11a1dff2011-01-07 21:37:30 +00004133 // Record this extraction against the appropriate vector if possible...
4134 SDValue SourceVec = V.getOperand(0);
4135 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4136 bool FoundSource = false;
4137 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4138 if (SourceVecs[j] == SourceVec) {
4139 if (MinElts[j] > EltNo)
4140 MinElts[j] = EltNo;
4141 if (MaxElts[j] < EltNo)
4142 MaxElts[j] = EltNo;
4143 FoundSource = true;
4144 break;
4145 }
4146 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004147
Bob Wilson11a1dff2011-01-07 21:37:30 +00004148 // Or record a new source if not...
4149 if (!FoundSource) {
4150 SourceVecs.push_back(SourceVec);
4151 MinElts.push_back(EltNo);
4152 MaxElts.push_back(EltNo);
4153 }
4154 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004155
Bob Wilson11a1dff2011-01-07 21:37:30 +00004156 // Currently only do something sane when at most two source vectors
4157 // involved.
4158 if (SourceVecs.size() > 2)
4159 return SDValue();
4160
4161 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4162 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004163
Bob Wilson11a1dff2011-01-07 21:37:30 +00004164 // This loop extracts the usage patterns of the source vectors
4165 // and prepares appropriate SDValues for a shuffle if possible.
4166 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4167 if (SourceVecs[i].getValueType() == VT) {
4168 // No VEXT necessary
4169 ShuffleSrcs[i] = SourceVecs[i];
4170 VEXTOffsets[i] = 0;
4171 continue;
4172 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4173 // It probably isn't worth padding out a smaller vector just to
4174 // break it down again in a shuffle.
4175 return SDValue();
4176 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004177
Bob Wilson11a1dff2011-01-07 21:37:30 +00004178 // Since only 64-bit and 128-bit vectors are legal on ARM and
4179 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004180 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4181 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004182
Bob Wilson11a1dff2011-01-07 21:37:30 +00004183 if (MaxElts[i] - MinElts[i] >= NumElts) {
4184 // Span too large for a VEXT to cope
4185 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004186 }
4187
Bob Wilson11a1dff2011-01-07 21:37:30 +00004188 if (MinElts[i] >= NumElts) {
4189 // The extraction can just take the second half
4190 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004191 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4192 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004193 DAG.getIntPtrConstant(NumElts));
4194 } else if (MaxElts[i] < NumElts) {
4195 // The extraction can just take the first half
4196 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004197 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4198 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004199 DAG.getIntPtrConstant(0));
4200 } else {
4201 // An actual VEXT is needed
4202 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004203 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4204 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004205 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004206 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4207 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004208 DAG.getIntPtrConstant(NumElts));
4209 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4210 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4211 }
4212 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004213
Bob Wilson11a1dff2011-01-07 21:37:30 +00004214 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004215
Bob Wilson11a1dff2011-01-07 21:37:30 +00004216 for (unsigned i = 0; i < NumElts; ++i) {
4217 SDValue Entry = Op.getOperand(i);
4218 if (Entry.getOpcode() == ISD::UNDEF) {
4219 Mask.push_back(-1);
4220 continue;
4221 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004222
Bob Wilson11a1dff2011-01-07 21:37:30 +00004223 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004224 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4225 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004226 if (ExtractVec == SourceVecs[0]) {
4227 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4228 } else {
4229 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4230 }
4231 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004232
Bob Wilson11a1dff2011-01-07 21:37:30 +00004233 // Final check before we try to produce nonsense...
4234 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004235 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4236 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004237
Bob Wilson11a1dff2011-01-07 21:37:30 +00004238 return SDValue();
4239}
4240
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004241/// isShuffleMaskLegal - Targets can use this to indicate that they only
4242/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4243/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4244/// are assumed to be legal.
4245bool
4246ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4247 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004248 if (VT.getVectorNumElements() == 4 &&
4249 (VT.is128BitVector() || VT.is64BitVector())) {
4250 unsigned PFIndexes[4];
4251 for (unsigned i = 0; i != 4; ++i) {
4252 if (M[i] < 0)
4253 PFIndexes[i] = 8;
4254 else
4255 PFIndexes[i] = M[i];
4256 }
4257
4258 // Compute the index in the perfect shuffle table.
4259 unsigned PFTableIndex =
4260 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4261 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4262 unsigned Cost = (PFEntry >> 30);
4263
4264 if (Cost <= 4)
4265 return true;
4266 }
4267
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004268 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004269 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004270
Bob Wilson53dd2452010-06-07 23:53:38 +00004271 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4272 return (EltSize >= 32 ||
4273 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004274 isVREVMask(M, VT, 64) ||
4275 isVREVMask(M, VT, 32) ||
4276 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004277 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004278 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004279 isVTRNMask(M, VT, WhichResult) ||
4280 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004281 isVZIPMask(M, VT, WhichResult) ||
4282 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4283 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4284 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004285}
4286
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004287/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4288/// the specified operations to build the shuffle.
4289static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4290 SDValue RHS, SelectionDAG &DAG,
4291 DebugLoc dl) {
4292 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4293 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4294 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4295
4296 enum {
4297 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4298 OP_VREV,
4299 OP_VDUP0,
4300 OP_VDUP1,
4301 OP_VDUP2,
4302 OP_VDUP3,
4303 OP_VEXT1,
4304 OP_VEXT2,
4305 OP_VEXT3,
4306 OP_VUZPL, // VUZP, left result
4307 OP_VUZPR, // VUZP, right result
4308 OP_VZIPL, // VZIP, left result
4309 OP_VZIPR, // VZIP, right result
4310 OP_VTRNL, // VTRN, left result
4311 OP_VTRNR // VTRN, right result
4312 };
4313
4314 if (OpNum == OP_COPY) {
4315 if (LHSID == (1*9+2)*9+3) return LHS;
4316 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4317 return RHS;
4318 }
4319
4320 SDValue OpLHS, OpRHS;
4321 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4322 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4323 EVT VT = OpLHS.getValueType();
4324
4325 switch (OpNum) {
4326 default: llvm_unreachable("Unknown shuffle opcode!");
4327 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004328 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004329 if (VT.getVectorElementType() == MVT::i32 ||
4330 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004331 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4332 // vrev <4 x i16> -> VREV32
4333 if (VT.getVectorElementType() == MVT::i16)
4334 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4335 // vrev <4 x i8> -> VREV16
4336 assert(VT.getVectorElementType() == MVT::i8);
4337 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004338 case OP_VDUP0:
4339 case OP_VDUP1:
4340 case OP_VDUP2:
4341 case OP_VDUP3:
4342 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004343 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004344 case OP_VEXT1:
4345 case OP_VEXT2:
4346 case OP_VEXT3:
4347 return DAG.getNode(ARMISD::VEXT, dl, VT,
4348 OpLHS, OpRHS,
4349 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4350 case OP_VUZPL:
4351 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004352 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004353 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4354 case OP_VZIPL:
4355 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004356 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004357 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4358 case OP_VTRNL:
4359 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004360 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4361 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004362 }
4363}
4364
Bill Wendling69a05a72011-03-14 23:02:38 +00004365static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
4366 SmallVectorImpl<int> &ShuffleMask,
4367 SelectionDAG &DAG) {
4368 // Check to see if we can use the VTBL instruction.
4369 SDValue V1 = Op.getOperand(0);
4370 SDValue V2 = Op.getOperand(1);
4371 DebugLoc DL = Op.getDebugLoc();
4372
4373 SmallVector<SDValue, 8> VTBLMask;
4374 for (SmallVectorImpl<int>::iterator
4375 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4376 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4377
4378 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4379 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4380 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4381 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004382
Owen Anderson76706012011-04-05 21:48:57 +00004383 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004384 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4385 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004386}
4387
Bob Wilson5bafff32009-06-22 23:27:02 +00004388static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004389 SDValue V1 = Op.getOperand(0);
4390 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004391 DebugLoc dl = Op.getDebugLoc();
4392 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004393 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004394 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00004395
Bob Wilson28865062009-08-13 02:13:04 +00004396 // Convert shuffles that are directly supported on NEON to target-specific
4397 // DAG nodes, instead of keeping them as shuffles and matching them again
4398 // during code selection. This is more efficient and avoids the possibility
4399 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004400 // FIXME: floating-point vectors should be canonicalized to integer vectors
4401 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004402 SVN->getMask(ShuffleMask);
4403
Bob Wilson53dd2452010-06-07 23:53:38 +00004404 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4405 if (EltSize <= 32) {
4406 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4407 int Lane = SVN->getSplatIndex();
4408 // If this is undef splat, generate it via "just" vdup, if possible.
4409 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004410
Dan Gohman65fd6562011-11-03 21:49:52 +00004411 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson53dd2452010-06-07 23:53:38 +00004412 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4413 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4414 }
Dan Gohman65fd6562011-11-03 21:49:52 +00004415 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
4416 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
4417 // reaches it).
4418 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
4419 !isa<ConstantSDNode>(V1.getOperand(0))) {
4420 bool IsScalarToVector = true;
4421 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
4422 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
4423 IsScalarToVector = false;
4424 break;
4425 }
4426 if (IsScalarToVector)
4427 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4428 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004429 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4430 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004431 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004432
4433 bool ReverseVEXT;
4434 unsigned Imm;
4435 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4436 if (ReverseVEXT)
4437 std::swap(V1, V2);
4438 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4439 DAG.getConstant(Imm, MVT::i32));
4440 }
4441
4442 if (isVREVMask(ShuffleMask, VT, 64))
4443 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4444 if (isVREVMask(ShuffleMask, VT, 32))
4445 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4446 if (isVREVMask(ShuffleMask, VT, 16))
4447 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4448
4449 // Check for Neon shuffles that modify both input vectors in place.
4450 // If both results are used, i.e., if there are two shuffles with the same
4451 // source operands and with masks corresponding to both results of one of
4452 // these operations, DAG memoization will ensure that a single node is
4453 // used for both shuffles.
4454 unsigned WhichResult;
4455 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4456 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4457 V1, V2).getValue(WhichResult);
4458 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4459 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4460 V1, V2).getValue(WhichResult);
4461 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4462 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4463 V1, V2).getValue(WhichResult);
4464
4465 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4466 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4467 V1, V1).getValue(WhichResult);
4468 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4469 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4470 V1, V1).getValue(WhichResult);
4471 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4472 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4473 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004474 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004475
Bob Wilsonc692cb72009-08-21 20:54:19 +00004476 // If the shuffle is not directly supported and it has 4 elements, use
4477 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004478 unsigned NumElts = VT.getVectorNumElements();
4479 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004480 unsigned PFIndexes[4];
4481 for (unsigned i = 0; i != 4; ++i) {
4482 if (ShuffleMask[i] < 0)
4483 PFIndexes[i] = 8;
4484 else
4485 PFIndexes[i] = ShuffleMask[i];
4486 }
4487
4488 // Compute the index in the perfect shuffle table.
4489 unsigned PFTableIndex =
4490 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004491 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4492 unsigned Cost = (PFEntry >> 30);
4493
4494 if (Cost <= 4)
4495 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4496 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004497
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004498 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004499 if (EltSize >= 32) {
4500 // Do the expansion with floating-point types, since that is what the VFP
4501 // registers are defined to use, and since i64 is not legal.
4502 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4503 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004504 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4505 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004506 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004507 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004508 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004509 Ops.push_back(DAG.getUNDEF(EltVT));
4510 else
4511 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4512 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4513 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4514 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004515 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004516 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004517 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004518 }
4519
Bill Wendling69a05a72011-03-14 23:02:38 +00004520 if (VT == MVT::v8i8) {
4521 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4522 if (NewOp.getNode())
4523 return NewOp;
4524 }
4525
Bob Wilson22cac0d2009-08-14 05:16:33 +00004526 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004527}
4528
Eli Friedman5c89cb82011-10-24 23:08:52 +00004529static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
4530 // INSERT_VECTOR_ELT is legal only for immediate indexes.
4531 SDValue Lane = Op.getOperand(2);
4532 if (!isa<ConstantSDNode>(Lane))
4533 return SDValue();
4534
4535 return Op;
4536}
4537
Bob Wilson5bafff32009-06-22 23:27:02 +00004538static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004539 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004540 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004541 if (!isa<ConstantSDNode>(Lane))
4542 return SDValue();
4543
4544 SDValue Vec = Op.getOperand(0);
4545 if (Op.getValueType() == MVT::i32 &&
4546 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4547 DebugLoc dl = Op.getDebugLoc();
4548 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4549 }
4550
4551 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004552}
4553
Bob Wilsona6d65862009-08-03 20:36:38 +00004554static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4555 // The only time a CONCAT_VECTORS operation can have legal types is when
4556 // two 64-bit vectors are concatenated to a 128-bit vector.
4557 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4558 "unexpected CONCAT_VECTORS");
4559 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004560 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004561 SDValue Op0 = Op.getOperand(0);
4562 SDValue Op1 = Op.getOperand(1);
4563 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004564 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004565 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004566 DAG.getIntPtrConstant(0));
4567 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004568 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004569 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004570 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004571 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004572}
4573
Bob Wilson626613d2010-11-23 19:38:38 +00004574/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4575/// element has been zero/sign-extended, depending on the isSigned parameter,
4576/// from an integer type half its size.
4577static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4578 bool isSigned) {
4579 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4580 EVT VT = N->getValueType(0);
4581 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4582 SDNode *BVN = N->getOperand(0).getNode();
4583 if (BVN->getValueType(0) != MVT::v4i32 ||
4584 BVN->getOpcode() != ISD::BUILD_VECTOR)
4585 return false;
4586 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4587 unsigned HiElt = 1 - LoElt;
4588 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4589 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4590 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4591 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4592 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4593 return false;
4594 if (isSigned) {
4595 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4596 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4597 return true;
4598 } else {
4599 if (Hi0->isNullValue() && Hi1->isNullValue())
4600 return true;
4601 }
4602 return false;
4603 }
4604
4605 if (N->getOpcode() != ISD::BUILD_VECTOR)
4606 return false;
4607
4608 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4609 SDNode *Elt = N->getOperand(i).getNode();
4610 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4611 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4612 unsigned HalfSize = EltSize / 2;
4613 if (isSigned) {
Bob Wilson9d45de22011-10-18 18:46:49 +00004614 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004615 return false;
4616 } else {
Bob Wilson9d45de22011-10-18 18:46:49 +00004617 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004618 return false;
4619 }
4620 continue;
4621 }
4622 return false;
4623 }
4624
4625 return true;
4626}
4627
4628/// isSignExtended - Check if a node is a vector value that is sign-extended
4629/// or a constant BUILD_VECTOR with sign-extended elements.
4630static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4631 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4632 return true;
4633 if (isExtendedBUILD_VECTOR(N, DAG, true))
4634 return true;
4635 return false;
4636}
4637
4638/// isZeroExtended - Check if a node is a vector value that is zero-extended
4639/// or a constant BUILD_VECTOR with zero-extended elements.
4640static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4641 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4642 return true;
4643 if (isExtendedBUILD_VECTOR(N, DAG, false))
4644 return true;
4645 return false;
4646}
4647
4648/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4649/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004650static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4651 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4652 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004653 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4654 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4655 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004656 LD->isNonTemporal(), LD->isInvariant(),
4657 LD->getAlignment());
Bob Wilson626613d2010-11-23 19:38:38 +00004658 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4659 // have been legalized as a BITCAST from v4i32.
4660 if (N->getOpcode() == ISD::BITCAST) {
4661 SDNode *BVN = N->getOperand(0).getNode();
4662 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4663 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4664 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4665 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4666 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4667 }
4668 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4669 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4670 EVT VT = N->getValueType(0);
4671 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4672 unsigned NumElts = VT.getVectorNumElements();
4673 MVT TruncVT = MVT::getIntegerVT(EltSize);
4674 SmallVector<SDValue, 8> Ops;
4675 for (unsigned i = 0; i != NumElts; ++i) {
4676 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4677 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004678 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004679 }
4680 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4681 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004682}
4683
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004684static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4685 unsigned Opcode = N->getOpcode();
4686 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4687 SDNode *N0 = N->getOperand(0).getNode();
4688 SDNode *N1 = N->getOperand(1).getNode();
4689 return N0->hasOneUse() && N1->hasOneUse() &&
4690 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4691 }
4692 return false;
4693}
4694
4695static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4696 unsigned Opcode = N->getOpcode();
4697 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4698 SDNode *N0 = N->getOperand(0).getNode();
4699 SDNode *N1 = N->getOperand(1).getNode();
4700 return N0->hasOneUse() && N1->hasOneUse() &&
4701 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4702 }
4703 return false;
4704}
4705
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004706static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4707 // Multiplications are only custom-lowered for 128-bit vectors so that
4708 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4709 EVT VT = Op.getValueType();
4710 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4711 SDNode *N0 = Op.getOperand(0).getNode();
4712 SDNode *N1 = Op.getOperand(1).getNode();
4713 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004714 bool isMLA = false;
4715 bool isN0SExt = isSignExtended(N0, DAG);
4716 bool isN1SExt = isSignExtended(N1, DAG);
4717 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004718 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004719 else {
4720 bool isN0ZExt = isZeroExtended(N0, DAG);
4721 bool isN1ZExt = isZeroExtended(N1, DAG);
4722 if (isN0ZExt && isN1ZExt)
4723 NewOpc = ARMISD::VMULLu;
4724 else if (isN1SExt || isN1ZExt) {
4725 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4726 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4727 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4728 NewOpc = ARMISD::VMULLs;
4729 isMLA = true;
4730 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4731 NewOpc = ARMISD::VMULLu;
4732 isMLA = true;
4733 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4734 std::swap(N0, N1);
4735 NewOpc = ARMISD::VMULLu;
4736 isMLA = true;
4737 }
4738 }
4739
4740 if (!NewOpc) {
4741 if (VT == MVT::v2i64)
4742 // Fall through to expand this. It is not legal.
4743 return SDValue();
4744 else
4745 // Other vector multiplications are legal.
4746 return Op;
4747 }
4748 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004749
4750 // Legalize to a VMULL instruction.
4751 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004752 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004753 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004754 if (!isMLA) {
4755 Op0 = SkipExtension(N0, DAG);
4756 assert(Op0.getValueType().is64BitVector() &&
4757 Op1.getValueType().is64BitVector() &&
4758 "unexpected types for extended operands to VMULL");
4759 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4760 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004761
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004762 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4763 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4764 // vmull q0, d4, d6
4765 // vmlal q0, d5, d6
4766 // is faster than
4767 // vaddl q0, d4, d5
4768 // vmovl q1, d6
4769 // vmul q0, q0, q1
4770 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4771 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4772 EVT Op1VT = Op1.getValueType();
4773 return DAG.getNode(N0->getOpcode(), DL, VT,
4774 DAG.getNode(NewOpc, DL, VT,
4775 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4776 DAG.getNode(NewOpc, DL, VT,
4777 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004778}
4779
Owen Anderson76706012011-04-05 21:48:57 +00004780static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004781LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4782 // Convert to float
4783 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4784 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4785 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4786 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4787 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4788 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4789 // Get reciprocal estimate.
4790 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004791 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004792 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4793 // Because char has a smaller range than uchar, we can actually get away
4794 // without any newton steps. This requires that we use a weird bias
4795 // of 0xb000, however (again, this has been exhaustively tested).
4796 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4797 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4798 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4799 Y = DAG.getConstant(0xb000, MVT::i32);
4800 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4801 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4802 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4803 // Convert back to short.
4804 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4805 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4806 return X;
4807}
4808
Owen Anderson76706012011-04-05 21:48:57 +00004809static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004810LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4811 SDValue N2;
4812 // Convert to float.
4813 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4814 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4815 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4816 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4817 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4818 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004819
Nate Begeman7973f352011-02-11 20:53:29 +00004820 // Use reciprocal estimate and one refinement step.
4821 // float4 recip = vrecpeq_f32(yf);
4822 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004823 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004824 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004825 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004826 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4827 N1, N2);
4828 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4829 // Because short has a smaller range than ushort, we can actually get away
4830 // with only a single newton step. This requires that we use a weird bias
4831 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004832 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004833 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4834 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004835 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004836 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4837 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4838 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4839 // Convert back to integer and return.
4840 // return vmovn_s32(vcvt_s32_f32(result));
4841 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4842 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4843 return N0;
4844}
4845
4846static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4847 EVT VT = Op.getValueType();
4848 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4849 "unexpected type for custom-lowering ISD::SDIV");
4850
4851 DebugLoc dl = Op.getDebugLoc();
4852 SDValue N0 = Op.getOperand(0);
4853 SDValue N1 = Op.getOperand(1);
4854 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004855
Nate Begeman7973f352011-02-11 20:53:29 +00004856 if (VT == MVT::v8i8) {
4857 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4858 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004859
Nate Begeman7973f352011-02-11 20:53:29 +00004860 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4861 DAG.getIntPtrConstant(4));
4862 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004863 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004864 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4865 DAG.getIntPtrConstant(0));
4866 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4867 DAG.getIntPtrConstant(0));
4868
4869 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4870 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4871
4872 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4873 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004874
Nate Begeman7973f352011-02-11 20:53:29 +00004875 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
4876 return N0;
4877 }
4878 return LowerSDIV_v4i16(N0, N1, dl, DAG);
4879}
4880
4881static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
4882 EVT VT = Op.getValueType();
4883 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4884 "unexpected type for custom-lowering ISD::UDIV");
4885
4886 DebugLoc dl = Op.getDebugLoc();
4887 SDValue N0 = Op.getOperand(0);
4888 SDValue N1 = Op.getOperand(1);
4889 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004890
Nate Begeman7973f352011-02-11 20:53:29 +00004891 if (VT == MVT::v8i8) {
4892 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
4893 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004894
Nate Begeman7973f352011-02-11 20:53:29 +00004895 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4896 DAG.getIntPtrConstant(4));
4897 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004898 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004899 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4900 DAG.getIntPtrConstant(0));
4901 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4902 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00004903
Nate Begeman7973f352011-02-11 20:53:29 +00004904 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
4905 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00004906
Nate Begeman7973f352011-02-11 20:53:29 +00004907 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4908 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004909
4910 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00004911 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
4912 N0);
4913 return N0;
4914 }
Owen Anderson76706012011-04-05 21:48:57 +00004915
Nate Begeman7973f352011-02-11 20:53:29 +00004916 // v4i16 sdiv ... Convert to float.
4917 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
4918 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
4919 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
4920 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
4921 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004922 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00004923
4924 // Use reciprocal estimate and two refinement steps.
4925 // float4 recip = vrecpeq_f32(yf);
4926 // recip *= vrecpsq_f32(yf, recip);
4927 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004928 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004929 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00004930 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004931 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004932 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004933 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00004934 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004935 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004936 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004937 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4938 // Simply multiplying by the reciprocal estimate can leave us a few ulps
4939 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
4940 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004941 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00004942 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4943 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4944 N1 = DAG.getConstant(2, MVT::i32);
4945 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4946 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4947 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4948 // Convert back to integer and return.
4949 // return vmovn_u32(vcvt_s32_f32(result));
4950 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4951 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4952 return N0;
4953}
4954
Evan Cheng342e3162011-08-30 01:34:54 +00004955static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
4956 EVT VT = Op.getNode()->getValueType(0);
4957 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
4958
4959 unsigned Opc;
4960 bool ExtraOp = false;
4961 switch (Op.getOpcode()) {
4962 default: assert(0 && "Invalid code");
4963 case ISD::ADDC: Opc = ARMISD::ADDC; break;
4964 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
4965 case ISD::SUBC: Opc = ARMISD::SUBC; break;
4966 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
4967 }
4968
4969 if (!ExtraOp)
4970 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
4971 Op.getOperand(1));
4972 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
4973 Op.getOperand(1), Op.getOperand(2));
4974}
4975
Eli Friedman74bf18c2011-09-15 22:26:18 +00004976static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedman7cc15662011-09-15 22:18:49 +00004977 // Monotonic load/store is legal for all targets
4978 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
4979 return Op;
4980
4981 // Aquire/Release load/store is not legal for targets without a
4982 // dmb or equivalent available.
4983 return SDValue();
4984}
4985
4986
Eli Friedman2bdffe42011-08-31 00:31:29 +00004987static void
Eli Friedman4d3f3292011-08-31 17:52:22 +00004988ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
4989 SelectionDAG &DAG, unsigned NewOp) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00004990 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +00004991 assert (Node->getValueType(0) == MVT::i64 &&
4992 "Only know how to expand i64 atomics");
Eli Friedman2bdffe42011-08-31 00:31:29 +00004993
Eli Friedman4d3f3292011-08-31 17:52:22 +00004994 SmallVector<SDValue, 6> Ops;
4995 Ops.push_back(Node->getOperand(0)); // Chain
4996 Ops.push_back(Node->getOperand(1)); // Ptr
4997 // Low part of Val1
4998 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4999 Node->getOperand(2), DAG.getIntPtrConstant(0)));
5000 // High part of Val1
5001 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5002 Node->getOperand(2), DAG.getIntPtrConstant(1)));
Andrew Trick3af7a672011-09-20 03:06:13 +00005003 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) {
Eli Friedman4d3f3292011-08-31 17:52:22 +00005004 // High part of Val1
5005 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5006 Node->getOperand(3), DAG.getIntPtrConstant(0)));
5007 // High part of Val2
5008 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5009 Node->getOperand(3), DAG.getIntPtrConstant(1)));
5010 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005011 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
5012 SDValue Result =
Eli Friedman4d3f3292011-08-31 17:52:22 +00005013 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64,
Eli Friedman2bdffe42011-08-31 00:31:29 +00005014 cast<MemSDNode>(Node)->getMemOperand());
Eli Friedman4d3f3292011-08-31 17:52:22 +00005015 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) };
Eli Friedman2bdffe42011-08-31 00:31:29 +00005016 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
5017 Results.push_back(Result.getValue(2));
5018}
5019
Dan Gohmand858e902010-04-17 15:26:15 +00005020SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005021 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005022 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00005023 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00005024 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005025 case ISD::GlobalAddress:
5026 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
5027 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00005028 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00005029 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00005030 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
5031 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005032 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00005033 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00005034 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00005035 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00005036 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00005037 case ISD::SINT_TO_FP:
5038 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
5039 case ISD::FP_TO_SINT:
5040 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005041 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00005042 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00005043 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005044 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00005045 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00005046 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00005047 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
5048 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00005049 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005050 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00005051 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00005052 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00005053 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00005054 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00005055 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00005056 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Duncan Sands28b77e92011-09-06 19:07:46 +00005057 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00005058 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005059 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedman5c89cb82011-10-24 23:08:52 +00005060 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005061 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00005062 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00005063 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005064 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00005065 case ISD::SDIV: return LowerSDIV(Op, DAG);
5066 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00005067 case ISD::ADDC:
5068 case ISD::ADDE:
5069 case ISD::SUBC:
5070 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Eli Friedman7cc15662011-09-15 22:18:49 +00005071 case ISD::ATOMIC_LOAD:
Eli Friedman74bf18c2011-09-15 22:26:18 +00005072 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005073 }
Dan Gohman475871a2008-07-27 21:46:04 +00005074 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00005075}
5076
Duncan Sands1607f052008-12-01 11:39:25 +00005077/// ReplaceNodeResults - Replace the results of node with an illegal result
5078/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00005079void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
5080 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005081 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00005082 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00005083 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00005084 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005085 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00005086 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005087 case ISD::BITCAST:
5088 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005089 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005090 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00005091 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00005092 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005093 break;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005094 case ISD::ATOMIC_LOAD_ADD:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005095 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005096 return;
5097 case ISD::ATOMIC_LOAD_AND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005098 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005099 return;
5100 case ISD::ATOMIC_LOAD_NAND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005101 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005102 return;
5103 case ISD::ATOMIC_LOAD_OR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005104 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005105 return;
5106 case ISD::ATOMIC_LOAD_SUB:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005107 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005108 return;
5109 case ISD::ATOMIC_LOAD_XOR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005110 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005111 return;
5112 case ISD::ATOMIC_SWAP:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005113 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005114 return;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005115 case ISD::ATOMIC_CMP_SWAP:
5116 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG);
5117 return;
Duncan Sands1607f052008-12-01 11:39:25 +00005118 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00005119 if (Res.getNode())
5120 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00005121}
Chris Lattner27a6c732007-11-24 07:07:01 +00005122
Evan Chenga8e29892007-01-19 07:51:42 +00005123//===----------------------------------------------------------------------===//
5124// ARM Scheduler Hooks
5125//===----------------------------------------------------------------------===//
5126
5127MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005128ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
5129 MachineBasicBlock *BB,
5130 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005131 unsigned dest = MI->getOperand(0).getReg();
5132 unsigned ptr = MI->getOperand(1).getReg();
5133 unsigned oldval = MI->getOperand(2).getReg();
5134 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005135 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5136 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005137 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005138
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005139 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5140 unsigned scratch =
Cameron Zwarich141ec632011-05-18 02:29:50 +00005141 MRI.createVirtualRegister(isThumb2 ? ARM::rGPRRegisterClass
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005142 : ARM::GPRRegisterClass);
5143
5144 if (isThumb2) {
Cameron Zwarich141ec632011-05-18 02:29:50 +00005145 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5146 MRI.constrainRegClass(oldval, ARM::rGPRRegisterClass);
5147 MRI.constrainRegClass(newval, ARM::rGPRRegisterClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005148 }
5149
Jim Grosbach5278eb82009-12-11 01:42:04 +00005150 unsigned ldrOpc, strOpc;
5151 switch (Size) {
5152 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005153 case 1:
5154 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00005155 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005156 break;
5157 case 2:
5158 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5159 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5160 break;
5161 case 4:
5162 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5163 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5164 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00005165 }
5166
5167 MachineFunction *MF = BB->getParent();
5168 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5169 MachineFunction::iterator It = BB;
5170 ++It; // insert the new blocks after the current block
5171
5172 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5173 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5174 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5175 MF->insert(It, loop1MBB);
5176 MF->insert(It, loop2MBB);
5177 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005178
5179 // Transfer the remainder of BB and its successor edges to exitMBB.
5180 exitMBB->splice(exitMBB->begin(), BB,
5181 llvm::next(MachineBasicBlock::iterator(MI)),
5182 BB->end());
5183 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005184
5185 // thisMBB:
5186 // ...
5187 // fallthrough --> loop1MBB
5188 BB->addSuccessor(loop1MBB);
5189
5190 // loop1MBB:
5191 // ldrex dest, [ptr]
5192 // cmp dest, oldval
5193 // bne exitMBB
5194 BB = loop1MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005195 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5196 if (ldrOpc == ARM::t2LDREX)
5197 MIB.addImm(0);
5198 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005199 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005200 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005201 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5202 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005203 BB->addSuccessor(loop2MBB);
5204 BB->addSuccessor(exitMBB);
5205
5206 // loop2MBB:
5207 // strex scratch, newval, [ptr]
5208 // cmp scratch, #0
5209 // bne loop1MBB
5210 BB = loop2MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005211 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr);
5212 if (strOpc == ARM::t2STREX)
5213 MIB.addImm(0);
5214 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005215 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005216 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005217 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5218 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005219 BB->addSuccessor(loop1MBB);
5220 BB->addSuccessor(exitMBB);
5221
5222 // exitMBB:
5223 // ...
5224 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005225
Dan Gohman14152b42010-07-06 20:24:04 +00005226 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005227
Jim Grosbach5278eb82009-12-11 01:42:04 +00005228 return BB;
5229}
5230
5231MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005232ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5233 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005234 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5235 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5236
5237 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005238 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005239 MachineFunction::iterator It = BB;
5240 ++It;
5241
5242 unsigned dest = MI->getOperand(0).getReg();
5243 unsigned ptr = MI->getOperand(1).getReg();
5244 unsigned incr = MI->getOperand(2).getReg();
5245 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005246 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005247
5248 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5249 if (isThumb2) {
5250 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5251 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5252 }
5253
Jim Grosbachc3c23542009-12-14 04:22:04 +00005254 unsigned ldrOpc, strOpc;
5255 switch (Size) {
5256 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005257 case 1:
5258 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005259 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005260 break;
5261 case 2:
5262 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5263 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5264 break;
5265 case 4:
5266 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5267 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5268 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005269 }
5270
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005271 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5272 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5273 MF->insert(It, loopMBB);
5274 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005275
5276 // Transfer the remainder of BB and its successor edges to exitMBB.
5277 exitMBB->splice(exitMBB->begin(), BB,
5278 llvm::next(MachineBasicBlock::iterator(MI)),
5279 BB->end());
5280 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005281
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005282 TargetRegisterClass *TRC =
5283 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5284 unsigned scratch = MRI.createVirtualRegister(TRC);
5285 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005286
5287 // thisMBB:
5288 // ...
5289 // fallthrough --> loopMBB
5290 BB->addSuccessor(loopMBB);
5291
5292 // loopMBB:
5293 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005294 // <binop> scratch2, dest, incr
5295 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005296 // cmp scratch, #0
5297 // bne- loopMBB
5298 // fallthrough --> exitMBB
5299 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005300 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5301 if (ldrOpc == ARM::t2LDREX)
5302 MIB.addImm(0);
5303 AddDefaultPred(MIB);
Jim Grosbachc67b5562009-12-15 00:12:35 +00005304 if (BinOpcode) {
5305 // operand order needs to go the other way for NAND
5306 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5307 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5308 addReg(incr).addReg(dest)).addReg(0);
5309 else
5310 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5311 addReg(dest).addReg(incr)).addReg(0);
5312 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005313
Jim Grosbachb6aed502011-09-09 18:37:27 +00005314 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5315 if (strOpc == ARM::t2STREX)
5316 MIB.addImm(0);
5317 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005318 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005319 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005320 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5321 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005322
5323 BB->addSuccessor(loopMBB);
5324 BB->addSuccessor(exitMBB);
5325
5326 // exitMBB:
5327 // ...
5328 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005329
Dan Gohman14152b42010-07-06 20:24:04 +00005330 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005331
Jim Grosbachc3c23542009-12-14 04:22:04 +00005332 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005333}
5334
Jim Grosbachf7da8822011-04-26 19:44:18 +00005335MachineBasicBlock *
5336ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5337 MachineBasicBlock *BB,
5338 unsigned Size,
5339 bool signExtend,
5340 ARMCC::CondCodes Cond) const {
5341 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5342
5343 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5344 MachineFunction *MF = BB->getParent();
5345 MachineFunction::iterator It = BB;
5346 ++It;
5347
5348 unsigned dest = MI->getOperand(0).getReg();
5349 unsigned ptr = MI->getOperand(1).getReg();
5350 unsigned incr = MI->getOperand(2).getReg();
5351 unsigned oldval = dest;
5352 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005353 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005354
5355 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5356 if (isThumb2) {
5357 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5358 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5359 }
5360
Jim Grosbachf7da8822011-04-26 19:44:18 +00005361 unsigned ldrOpc, strOpc, extendOpc;
5362 switch (Size) {
5363 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5364 case 1:
5365 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5366 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005367 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005368 break;
5369 case 2:
5370 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5371 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005372 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005373 break;
5374 case 4:
5375 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5376 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5377 extendOpc = 0;
5378 break;
5379 }
5380
5381 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5382 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5383 MF->insert(It, loopMBB);
5384 MF->insert(It, exitMBB);
5385
5386 // Transfer the remainder of BB and its successor edges to exitMBB.
5387 exitMBB->splice(exitMBB->begin(), BB,
5388 llvm::next(MachineBasicBlock::iterator(MI)),
5389 BB->end());
5390 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5391
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005392 TargetRegisterClass *TRC =
5393 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5394 unsigned scratch = MRI.createVirtualRegister(TRC);
5395 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005396
5397 // thisMBB:
5398 // ...
5399 // fallthrough --> loopMBB
5400 BB->addSuccessor(loopMBB);
5401
5402 // loopMBB:
5403 // ldrex dest, ptr
5404 // (sign extend dest, if required)
5405 // cmp dest, incr
5406 // cmov.cond scratch2, dest, incr
5407 // strex scratch, scratch2, ptr
5408 // cmp scratch, #0
5409 // bne- loopMBB
5410 // fallthrough --> exitMBB
5411 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005412 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5413 if (ldrOpc == ARM::t2LDREX)
5414 MIB.addImm(0);
5415 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005416
5417 // Sign extend the value, if necessary.
5418 if (signExtend && extendOpc) {
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005419 oldval = MRI.createVirtualRegister(ARM::GPRRegisterClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005420 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5421 .addReg(dest)
5422 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005423 }
5424
5425 // Build compare and cmov instructions.
5426 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5427 .addReg(oldval).addReg(incr));
5428 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5429 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5430
Jim Grosbachb6aed502011-09-09 18:37:27 +00005431 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5432 if (strOpc == ARM::t2STREX)
5433 MIB.addImm(0);
5434 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005435 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5436 .addReg(scratch).addImm(0));
5437 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5438 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5439
5440 BB->addSuccessor(loopMBB);
5441 BB->addSuccessor(exitMBB);
5442
5443 // exitMBB:
5444 // ...
5445 BB = exitMBB;
5446
5447 MI->eraseFromParent(); // The instruction is gone now.
5448
5449 return BB;
5450}
5451
Eli Friedman2bdffe42011-08-31 00:31:29 +00005452MachineBasicBlock *
5453ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
5454 unsigned Op1, unsigned Op2,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005455 bool NeedsCarry, bool IsCmpxchg) const {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005456 // This also handles ATOMIC_SWAP, indicated by Op1==0.
5457 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5458
5459 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5460 MachineFunction *MF = BB->getParent();
5461 MachineFunction::iterator It = BB;
5462 ++It;
5463
5464 unsigned destlo = MI->getOperand(0).getReg();
5465 unsigned desthi = MI->getOperand(1).getReg();
5466 unsigned ptr = MI->getOperand(2).getReg();
5467 unsigned vallo = MI->getOperand(3).getReg();
5468 unsigned valhi = MI->getOperand(4).getReg();
5469 DebugLoc dl = MI->getDebugLoc();
5470 bool isThumb2 = Subtarget->isThumb2();
5471
5472 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5473 if (isThumb2) {
5474 MRI.constrainRegClass(destlo, ARM::rGPRRegisterClass);
5475 MRI.constrainRegClass(desthi, ARM::rGPRRegisterClass);
5476 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5477 }
5478
5479 unsigned ldrOpc = isThumb2 ? ARM::t2LDREXD : ARM::LDREXD;
5480 unsigned strOpc = isThumb2 ? ARM::t2STREXD : ARM::STREXD;
5481
5482 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman7df496d2011-09-01 22:27:41 +00005483 MachineBasicBlock *contBB = 0, *cont2BB = 0;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005484 if (IsCmpxchg) {
5485 contBB = MF->CreateMachineBasicBlock(LLVM_BB);
5486 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB);
5487 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005488 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5489 MF->insert(It, loopMBB);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005490 if (IsCmpxchg) {
5491 MF->insert(It, contBB);
5492 MF->insert(It, cont2BB);
5493 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005494 MF->insert(It, exitMBB);
5495
5496 // Transfer the remainder of BB and its successor edges to exitMBB.
5497 exitMBB->splice(exitMBB->begin(), BB,
5498 llvm::next(MachineBasicBlock::iterator(MI)),
5499 BB->end());
5500 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5501
5502 TargetRegisterClass *TRC =
5503 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5504 unsigned storesuccess = MRI.createVirtualRegister(TRC);
5505
5506 // thisMBB:
5507 // ...
5508 // fallthrough --> loopMBB
5509 BB->addSuccessor(loopMBB);
5510
5511 // loopMBB:
5512 // ldrexd r2, r3, ptr
5513 // <binopa> r0, r2, incr
5514 // <binopb> r1, r3, incr
5515 // strexd storesuccess, r0, r1, ptr
5516 // cmp storesuccess, #0
5517 // bne- loopMBB
5518 // fallthrough --> exitMBB
5519 //
5520 // Note that the registers are explicitly specified because there is not any
5521 // way to force the register allocator to allocate a register pair.
5522 //
Andrew Trick3af7a672011-09-20 03:06:13 +00005523 // FIXME: The hardcoded registers are not necessary for Thumb2, but we
Eli Friedman2bdffe42011-08-31 00:31:29 +00005524 // need to properly enforce the restriction that the two output registers
5525 // for ldrexd must be different.
5526 BB = loopMBB;
5527 // Load
5528 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc))
5529 .addReg(ARM::R2, RegState::Define)
5530 .addReg(ARM::R3, RegState::Define).addReg(ptr));
5531 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
5532 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo).addReg(ARM::R2);
5533 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi).addReg(ARM::R3);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005534
5535 if (IsCmpxchg) {
5536 // Add early exit
5537 for (unsigned i = 0; i < 2; i++) {
5538 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr :
5539 ARM::CMPrr))
5540 .addReg(i == 0 ? destlo : desthi)
5541 .addReg(i == 0 ? vallo : valhi));
5542 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5543 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5544 BB->addSuccessor(exitMBB);
5545 BB->addSuccessor(i == 0 ? contBB : cont2BB);
5546 BB = (i == 0 ? contBB : cont2BB);
5547 }
5548
5549 // Copy to physregs for strexd
5550 unsigned setlo = MI->getOperand(5).getReg();
5551 unsigned sethi = MI->getOperand(6).getReg();
5552 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(setlo);
5553 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(sethi);
5554 } else if (Op1) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005555 // Perform binary operation
5556 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), ARM::R0)
5557 .addReg(destlo).addReg(vallo))
5558 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
5559 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), ARM::R1)
5560 .addReg(desthi).addReg(valhi)).addReg(0);
5561 } else {
5562 // Copy to physregs for strexd
5563 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(vallo);
5564 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(valhi);
5565 }
5566
5567 // Store
5568 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), storesuccess)
5569 .addReg(ARM::R0).addReg(ARM::R1).addReg(ptr));
5570 // Cmp+jump
5571 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5572 .addReg(storesuccess).addImm(0));
5573 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5574 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5575
5576 BB->addSuccessor(loopMBB);
5577 BB->addSuccessor(exitMBB);
5578
5579 // exitMBB:
5580 // ...
5581 BB = exitMBB;
5582
5583 MI->eraseFromParent(); // The instruction is gone now.
5584
5585 return BB;
5586}
5587
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005588/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
5589/// registers the function context.
5590void ARMTargetLowering::
5591SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
5592 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005593 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5594 DebugLoc dl = MI->getDebugLoc();
5595 MachineFunction *MF = MBB->getParent();
5596 MachineRegisterInfo *MRI = &MF->getRegInfo();
5597 MachineConstantPool *MCP = MF->getConstantPool();
5598 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5599 const Function *F = MF->getFunction();
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005600
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005601 bool isThumb = Subtarget->isThumb();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005602 bool isThumb2 = Subtarget->isThumb2();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005603
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005604 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005605 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005606 ARMConstantPoolValue *CPV =
5607 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
5608 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
5609
5610 const TargetRegisterClass *TRC =
5611 isThumb ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5612
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005613 // Grab constant pool and fixed stack memory operands.
5614 MachineMemOperand *CPMMO =
5615 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
5616 MachineMemOperand::MOLoad, 4, 4);
5617
5618 MachineMemOperand *FIMMOSt =
5619 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
5620 MachineMemOperand::MOStore, 4, 4);
5621
5622 // Load the address of the dispatch MBB into the jump buffer.
5623 if (isThumb2) {
5624 // Incoming value: jbuf
5625 // ldr.n r5, LCPI1_1
5626 // orr r5, r5, #1
5627 // add r5, pc
5628 // str r5, [$jbuf, #+4] ; &jbuf[1]
5629 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5630 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
5631 .addConstantPoolIndex(CPI)
5632 .addMemOperand(CPMMO));
5633 // Set the low bit because of thumb mode.
5634 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5635 AddDefaultCC(
5636 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
5637 .addReg(NewVReg1, RegState::Kill)
5638 .addImm(0x01)));
5639 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5640 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
5641 .addReg(NewVReg2, RegState::Kill)
5642 .addImm(PCLabelId);
5643 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
5644 .addReg(NewVReg3, RegState::Kill)
5645 .addFrameIndex(FI)
5646 .addImm(36) // &jbuf[1] :: pc
5647 .addMemOperand(FIMMOSt));
5648 } else if (isThumb) {
5649 // Incoming value: jbuf
5650 // ldr.n r1, LCPI1_4
5651 // add r1, pc
5652 // mov r2, #1
5653 // orrs r1, r2
5654 // add r2, $jbuf, #+4 ; &jbuf[1]
5655 // str r1, [r2]
5656 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5657 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
5658 .addConstantPoolIndex(CPI)
5659 .addMemOperand(CPMMO));
5660 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5661 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
5662 .addReg(NewVReg1, RegState::Kill)
5663 .addImm(PCLabelId);
5664 // Set the low bit because of thumb mode.
5665 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5666 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
5667 .addReg(ARM::CPSR, RegState::Define)
5668 .addImm(1));
5669 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5670 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
5671 .addReg(ARM::CPSR, RegState::Define)
5672 .addReg(NewVReg2, RegState::Kill)
5673 .addReg(NewVReg3, RegState::Kill));
5674 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5675 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
5676 .addFrameIndex(FI)
5677 .addImm(36)); // &jbuf[1] :: pc
5678 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
5679 .addReg(NewVReg4, RegState::Kill)
5680 .addReg(NewVReg5, RegState::Kill)
5681 .addImm(0)
5682 .addMemOperand(FIMMOSt));
5683 } else {
5684 // Incoming value: jbuf
5685 // ldr r1, LCPI1_1
5686 // add r1, pc, r1
5687 // str r1, [$jbuf, #+4] ; &jbuf[1]
5688 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5689 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
5690 .addConstantPoolIndex(CPI)
5691 .addImm(0)
5692 .addMemOperand(CPMMO));
5693 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5694 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
5695 .addReg(NewVReg1, RegState::Kill)
5696 .addImm(PCLabelId));
5697 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
5698 .addReg(NewVReg2, RegState::Kill)
5699 .addFrameIndex(FI)
5700 .addImm(36) // &jbuf[1] :: pc
5701 .addMemOperand(FIMMOSt));
5702 }
5703}
5704
5705MachineBasicBlock *ARMTargetLowering::
5706EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
5707 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5708 DebugLoc dl = MI->getDebugLoc();
5709 MachineFunction *MF = MBB->getParent();
5710 MachineRegisterInfo *MRI = &MF->getRegInfo();
5711 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5712 MachineFrameInfo *MFI = MF->getFrameInfo();
5713 int FI = MFI->getFunctionContextIndex();
5714
5715 const TargetRegisterClass *TRC =
5716 Subtarget->isThumb() ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5717
Bill Wendling04f15b42011-10-06 21:29:56 +00005718 // Get a mapping of the call site numbers to all of the landing pads they're
5719 // associated with.
Bill Wendling2a850152011-10-05 00:02:33 +00005720 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
5721 unsigned MaxCSNum = 0;
5722 MachineModuleInfo &MMI = MF->getMMI();
5723 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E; ++BB) {
5724 if (!BB->isLandingPad()) continue;
5725
5726 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
5727 // pad.
5728 for (MachineBasicBlock::iterator
5729 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
5730 if (!II->isEHLabel()) continue;
5731
5732 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendling5cbef192011-10-05 23:28:57 +00005733 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling2a850152011-10-05 00:02:33 +00005734
Bill Wendling5cbef192011-10-05 23:28:57 +00005735 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
5736 for (SmallVectorImpl<unsigned>::iterator
5737 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
5738 CSI != CSE; ++CSI) {
5739 CallSiteNumToLPad[*CSI].push_back(BB);
5740 MaxCSNum = std::max(MaxCSNum, *CSI);
5741 }
Bill Wendling2a850152011-10-05 00:02:33 +00005742 break;
5743 }
5744 }
5745
5746 // Get an ordered list of the machine basic blocks for the jump table.
5747 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling2acf6382011-10-07 23:18:02 +00005748 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling2a850152011-10-05 00:02:33 +00005749 LPadList.reserve(CallSiteNumToLPad.size());
5750 for (unsigned I = 1; I <= MaxCSNum; ++I) {
5751 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
5752 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00005753 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling2a850152011-10-05 00:02:33 +00005754 LPadList.push_back(*II);
Bill Wendling2acf6382011-10-07 23:18:02 +00005755 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
5756 }
Bill Wendling2a850152011-10-05 00:02:33 +00005757 }
5758
Bill Wendling5cbef192011-10-05 23:28:57 +00005759 assert(!LPadList.empty() &&
5760 "No landing pad destinations for the dispatch jump table!");
5761
Bill Wendling04f15b42011-10-06 21:29:56 +00005762 // Create the jump table and associated information.
Bill Wendling2a850152011-10-05 00:02:33 +00005763 MachineJumpTableInfo *JTI =
5764 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
5765 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
5766 unsigned UId = AFI->createJumpTableUId();
5767
Bill Wendling04f15b42011-10-06 21:29:56 +00005768 // Create the MBBs for the dispatch code.
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005769
5770 // Shove the dispatch's address into the return slot in the function context.
5771 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
5772 DispatchBB->setIsLandingPad();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005773
Bill Wendlingbb734682011-10-05 00:39:32 +00005774 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Bill Wendling083a8eb2011-10-06 23:37:36 +00005775 BuildMI(TrapBB, dl, TII->get(Subtarget->isThumb() ? ARM::tTRAP : ARM::TRAP));
Bill Wendlingbb734682011-10-05 00:39:32 +00005776 DispatchBB->addSuccessor(TrapBB);
5777
5778 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
5779 DispatchBB->addSuccessor(DispContBB);
Bill Wendling2a850152011-10-05 00:02:33 +00005780
Bill Wendlinga48ed4f2011-10-17 21:32:56 +00005781 // Insert and MBBs.
Bill Wendling930193c2011-10-06 00:53:33 +00005782 MF->insert(MF->end(), DispatchBB);
5783 MF->insert(MF->end(), DispContBB);
5784 MF->insert(MF->end(), TrapBB);
Bill Wendling930193c2011-10-06 00:53:33 +00005785
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005786 // Insert code into the entry block that creates and registers the function
5787 // context.
5788 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
5789
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005790 MachineMemOperand *FIMMOLd =
Bill Wendling04f15b42011-10-06 21:29:56 +00005791 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendling083a8eb2011-10-06 23:37:36 +00005792 MachineMemOperand::MOLoad |
5793 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling930193c2011-10-06 00:53:33 +00005794
Bob Wilsoneaab6ef2011-11-16 07:11:57 +00005795 BuildMI(DispatchBB, dl, TII->get(ARM::eh_sjlj_dispatchsetup));
5796
Bill Wendling952cb502011-10-18 22:49:07 +00005797 unsigned NumLPads = LPadList.size();
Bill Wendling95ce2e92011-10-06 22:53:00 +00005798 if (Subtarget->isThumb2()) {
5799 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5800 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
5801 .addFrameIndex(FI)
5802 .addImm(4)
5803 .addMemOperand(FIMMOLd));
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005804
Bill Wendling952cb502011-10-18 22:49:07 +00005805 if (NumLPads < 256) {
5806 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
5807 .addReg(NewVReg1)
5808 .addImm(LPadList.size()));
5809 } else {
5810 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5811 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00005812 .addImm(NumLPads & 0xFFFF));
5813
5814 unsigned VReg2 = VReg1;
5815 if ((NumLPads & 0xFFFF0000) != 0) {
5816 VReg2 = MRI->createVirtualRegister(TRC);
5817 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
5818 .addReg(VReg1)
5819 .addImm(NumLPads >> 16));
5820 }
5821
Bill Wendling952cb502011-10-18 22:49:07 +00005822 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
5823 .addReg(NewVReg1)
5824 .addReg(VReg2));
5825 }
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005826
Bill Wendling95ce2e92011-10-06 22:53:00 +00005827 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
5828 .addMBB(TrapBB)
5829 .addImm(ARMCC::HI)
5830 .addReg(ARM::CPSR);
Bill Wendlingbb734682011-10-05 00:39:32 +00005831
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005832 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5833 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005834 .addJumpTableIndex(MJTI)
5835 .addImm(UId));
Bill Wendling2a850152011-10-05 00:02:33 +00005836
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005837 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005838 AddDefaultCC(
5839 AddDefaultPred(
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005840 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
5841 .addReg(NewVReg3, RegState::Kill)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005842 .addReg(NewVReg1)
5843 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
5844
5845 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005846 .addReg(NewVReg4, RegState::Kill)
Bill Wendling2a850152011-10-05 00:02:33 +00005847 .addReg(NewVReg1)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005848 .addJumpTableIndex(MJTI)
5849 .addImm(UId);
5850 } else if (Subtarget->isThumb()) {
Bill Wendling083a8eb2011-10-06 23:37:36 +00005851 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5852 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
5853 .addFrameIndex(FI)
5854 .addImm(1)
5855 .addMemOperand(FIMMOLd));
Bill Wendlingf1083d42011-10-07 22:08:37 +00005856
Bill Wendlinga5871dc2011-10-18 23:11:05 +00005857 if (NumLPads < 256) {
5858 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
5859 .addReg(NewVReg1)
5860 .addImm(NumLPads));
5861 } else {
5862 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling922ad782011-10-19 09:24:02 +00005863 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
5864 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
5865
5866 // MachineConstantPool wants an explicit alignment.
5867 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
5868 if (Align == 0)
5869 Align = getTargetData()->getTypeAllocSize(C->getType());
5870 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendlinga5871dc2011-10-18 23:11:05 +00005871
5872 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5873 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
5874 .addReg(VReg1, RegState::Define)
5875 .addConstantPoolIndex(Idx));
5876 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
5877 .addReg(NewVReg1)
5878 .addReg(VReg1));
5879 }
5880
Bill Wendling083a8eb2011-10-06 23:37:36 +00005881 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
5882 .addMBB(TrapBB)
5883 .addImm(ARMCC::HI)
5884 .addReg(ARM::CPSR);
5885
5886 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5887 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
5888 .addReg(ARM::CPSR, RegState::Define)
5889 .addReg(NewVReg1)
5890 .addImm(2));
5891
5892 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling217f0e92011-10-06 23:41:14 +00005893 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendling083a8eb2011-10-06 23:37:36 +00005894 .addJumpTableIndex(MJTI)
5895 .addImm(UId));
5896
5897 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5898 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
5899 .addReg(ARM::CPSR, RegState::Define)
5900 .addReg(NewVReg2, RegState::Kill)
5901 .addReg(NewVReg3));
5902
5903 MachineMemOperand *JTMMOLd =
5904 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
5905 MachineMemOperand::MOLoad, 4, 4);
5906
5907 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5908 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
5909 .addReg(NewVReg4, RegState::Kill)
5910 .addImm(0)
5911 .addMemOperand(JTMMOLd));
5912
5913 unsigned NewVReg6 = MRI->createVirtualRegister(TRC);
5914 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
5915 .addReg(ARM::CPSR, RegState::Define)
5916 .addReg(NewVReg5, RegState::Kill)
5917 .addReg(NewVReg3));
5918
5919 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
5920 .addReg(NewVReg6, RegState::Kill)
5921 .addJumpTableIndex(MJTI)
5922 .addImm(UId);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005923 } else {
5924 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5925 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
5926 .addFrameIndex(FI)
5927 .addImm(4)
5928 .addMemOperand(FIMMOLd));
Bill Wendling564392b2011-10-18 22:11:18 +00005929
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005930 if (NumLPads < 256) {
5931 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
5932 .addReg(NewVReg1)
5933 .addImm(NumLPads));
Bill Wendling922ad782011-10-19 09:24:02 +00005934 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005935 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5936 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00005937 .addImm(NumLPads & 0xFFFF));
5938
5939 unsigned VReg2 = VReg1;
5940 if ((NumLPads & 0xFFFF0000) != 0) {
5941 VReg2 = MRI->createVirtualRegister(TRC);
5942 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
5943 .addReg(VReg1)
5944 .addImm(NumLPads >> 16));
5945 }
5946
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005947 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
5948 .addReg(NewVReg1)
5949 .addReg(VReg2));
Bill Wendling922ad782011-10-19 09:24:02 +00005950 } else {
5951 MachineConstantPool *ConstantPool = MF->getConstantPool();
5952 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
5953 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
5954
5955 // MachineConstantPool wants an explicit alignment.
5956 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
5957 if (Align == 0)
5958 Align = getTargetData()->getTypeAllocSize(C->getType());
5959 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
5960
5961 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5962 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
5963 .addReg(VReg1, RegState::Define)
Bill Wendling767f8be2011-10-20 20:37:11 +00005964 .addConstantPoolIndex(Idx)
5965 .addImm(0));
Bill Wendling922ad782011-10-19 09:24:02 +00005966 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
5967 .addReg(NewVReg1)
5968 .addReg(VReg1, RegState::Kill));
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005969 }
5970
Bill Wendling95ce2e92011-10-06 22:53:00 +00005971 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
5972 .addMBB(TrapBB)
5973 .addImm(ARMCC::HI)
5974 .addReg(ARM::CPSR);
Bill Wendling2a850152011-10-05 00:02:33 +00005975
Bill Wendling564392b2011-10-18 22:11:18 +00005976 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005977 AddDefaultCC(
Bill Wendling564392b2011-10-18 22:11:18 +00005978 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005979 .addReg(NewVReg1)
5980 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling564392b2011-10-18 22:11:18 +00005981 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5982 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005983 .addJumpTableIndex(MJTI)
5984 .addImm(UId));
5985
5986 MachineMemOperand *JTMMOLd =
5987 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
5988 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling564392b2011-10-18 22:11:18 +00005989 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005990 AddDefaultPred(
Bill Wendling564392b2011-10-18 22:11:18 +00005991 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
5992 .addReg(NewVReg3, RegState::Kill)
5993 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005994 .addImm(0)
5995 .addMemOperand(JTMMOLd));
5996
5997 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
Bill Wendling564392b2011-10-18 22:11:18 +00005998 .addReg(NewVReg5, RegState::Kill)
5999 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006000 .addJumpTableIndex(MJTI)
6001 .addImm(UId);
6002 }
Bill Wendling2a850152011-10-05 00:02:33 +00006003
Bill Wendlingbb734682011-10-05 00:39:32 +00006004 // Add the jump table entries as successors to the MBB.
Bill Wendling2acf6382011-10-07 23:18:02 +00006005 MachineBasicBlock *PrevMBB = 0;
Bill Wendlingbb734682011-10-05 00:39:32 +00006006 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006007 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6008 MachineBasicBlock *CurMBB = *I;
6009 if (PrevMBB != CurMBB)
6010 DispContBB->addSuccessor(CurMBB);
6011 PrevMBB = CurMBB;
6012 }
6013
Bill Wendling24bb9252011-10-17 05:25:09 +00006014 // N.B. the order the invoke BBs are processed in doesn't matter here.
Bill Wendling969c9ef2011-10-14 23:34:37 +00006015 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6016 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
6017 const unsigned *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006018 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling2acf6382011-10-07 23:18:02 +00006019 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6020 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6021 MachineBasicBlock *BB = *I;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006022
6023 // Remove the landing pad successor from the invoke block and replace it
6024 // with the new dispatch block.
Bill Wendlingde39d862011-10-26 07:16:18 +00006025 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6026 BB->succ_end());
6027 while (!Successors.empty()) {
6028 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling2acf6382011-10-07 23:18:02 +00006029 if (SMBB->isLandingPad()) {
6030 BB->removeSuccessor(SMBB);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006031 MBBLPads.push_back(SMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006032 }
6033 }
6034
6035 BB->addSuccessor(DispatchBB);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006036
6037 // Find the invoke call and mark all of the callee-saved registers as
6038 // 'implicit defined' so that they're spilled. This prevents code from
6039 // moving instructions to before the EH block, where they will never be
6040 // executed.
6041 for (MachineBasicBlock::reverse_iterator
6042 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006043 if (!II->isCall()) continue;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006044
6045 DenseMap<unsigned, bool> DefRegs;
6046 for (MachineInstr::mop_iterator
6047 OI = II->operands_begin(), OE = II->operands_end();
6048 OI != OE; ++OI) {
6049 if (!OI->isReg()) continue;
6050 DefRegs[OI->getReg()] = true;
6051 }
6052
6053 MachineInstrBuilder MIB(&*II);
6054
Bill Wendling5d798592011-10-14 23:55:44 +00006055 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006056 unsigned Reg = SavedRegs[i];
6057 if (Subtarget->isThumb2() &&
6058 !ARM::tGPRRegisterClass->contains(Reg) &&
6059 !ARM::hGPRRegisterClass->contains(Reg))
6060 continue;
6061 else if (Subtarget->isThumb1Only() &&
6062 !ARM::tGPRRegisterClass->contains(Reg))
6063 continue;
6064 else if (!Subtarget->isThumb() &&
6065 !ARM::GPRRegisterClass->contains(Reg))
6066 continue;
6067 if (!DefRegs[Reg])
6068 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling5d798592011-10-14 23:55:44 +00006069 }
Bill Wendling969c9ef2011-10-14 23:34:37 +00006070
6071 break;
6072 }
Bill Wendling2acf6382011-10-07 23:18:02 +00006073 }
Bill Wendlingbb734682011-10-05 00:39:32 +00006074
Bill Wendlingf7b02072011-10-18 18:30:49 +00006075 // Mark all former landing pads as non-landing pads. The dispatch is the only
6076 // landing pad now.
6077 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6078 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6079 (*I)->setIsLandingPad(false);
6080
Bill Wendlingbb734682011-10-05 00:39:32 +00006081 // The instruction is gone now.
6082 MI->eraseFromParent();
6083
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006084 return MBB;
6085}
6086
Evan Cheng218977b2010-07-13 19:27:42 +00006087static
6088MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6089 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6090 E = MBB->succ_end(); I != E; ++I)
6091 if (*I != Succ)
6092 return *I;
6093 llvm_unreachable("Expecting a BB with two successors!");
6094}
6095
Jim Grosbache801dc42009-12-12 01:40:06 +00006096MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006097ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00006098 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006099 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00006100 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006101 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00006102 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00006103 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00006104 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00006105 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00006106 }
Jim Grosbachee2c2a42011-09-16 21:55:56 +00006107 // The Thumb2 pre-indexed stores have the same MI operands, they just
6108 // define them differently in the .td files from the isel patterns, so
6109 // they need pseudos.
6110 case ARM::t2STR_preidx:
6111 MI->setDesc(TII->get(ARM::t2STR_PRE));
6112 return BB;
6113 case ARM::t2STRB_preidx:
6114 MI->setDesc(TII->get(ARM::t2STRB_PRE));
6115 return BB;
6116 case ARM::t2STRH_preidx:
6117 MI->setDesc(TII->get(ARM::t2STRH_PRE));
6118 return BB;
6119
Jim Grosbach19dec202011-08-05 20:35:44 +00006120 case ARM::STRi_preidx:
6121 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00006122 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00006123 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
6124 // Decode the offset.
6125 unsigned Offset = MI->getOperand(4).getImm();
6126 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
6127 Offset = ARM_AM::getAM2Offset(Offset);
6128 if (isSub)
6129 Offset = -Offset;
6130
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006131 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00006132 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00006133 .addOperand(MI->getOperand(0)) // Rn_wb
6134 .addOperand(MI->getOperand(1)) // Rt
6135 .addOperand(MI->getOperand(2)) // Rn
6136 .addImm(Offset) // offset (skip GPR==zero_reg)
6137 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006138 .addOperand(MI->getOperand(6))
6139 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00006140 MI->eraseFromParent();
6141 return BB;
6142 }
6143 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00006144 case ARM::STRBr_preidx:
6145 case ARM::STRH_preidx: {
6146 unsigned NewOpc;
6147 switch (MI->getOpcode()) {
6148 default: llvm_unreachable("unexpected opcode!");
6149 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
6150 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
6151 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
6152 }
Jim Grosbach19dec202011-08-05 20:35:44 +00006153 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
6154 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
6155 MIB.addOperand(MI->getOperand(i));
6156 MI->eraseFromParent();
6157 return BB;
6158 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006159 case ARM::ATOMIC_LOAD_ADD_I8:
6160 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6161 case ARM::ATOMIC_LOAD_ADD_I16:
6162 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6163 case ARM::ATOMIC_LOAD_ADD_I32:
6164 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006165
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006166 case ARM::ATOMIC_LOAD_AND_I8:
6167 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6168 case ARM::ATOMIC_LOAD_AND_I16:
6169 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6170 case ARM::ATOMIC_LOAD_AND_I32:
6171 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006172
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006173 case ARM::ATOMIC_LOAD_OR_I8:
6174 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6175 case ARM::ATOMIC_LOAD_OR_I16:
6176 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6177 case ARM::ATOMIC_LOAD_OR_I32:
6178 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006179
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006180 case ARM::ATOMIC_LOAD_XOR_I8:
6181 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6182 case ARM::ATOMIC_LOAD_XOR_I16:
6183 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6184 case ARM::ATOMIC_LOAD_XOR_I32:
6185 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006186
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006187 case ARM::ATOMIC_LOAD_NAND_I8:
6188 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6189 case ARM::ATOMIC_LOAD_NAND_I16:
6190 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6191 case ARM::ATOMIC_LOAD_NAND_I32:
6192 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006193
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006194 case ARM::ATOMIC_LOAD_SUB_I8:
6195 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6196 case ARM::ATOMIC_LOAD_SUB_I16:
6197 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6198 case ARM::ATOMIC_LOAD_SUB_I32:
6199 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006200
Jim Grosbachf7da8822011-04-26 19:44:18 +00006201 case ARM::ATOMIC_LOAD_MIN_I8:
6202 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
6203 case ARM::ATOMIC_LOAD_MIN_I16:
6204 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
6205 case ARM::ATOMIC_LOAD_MIN_I32:
6206 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
6207
6208 case ARM::ATOMIC_LOAD_MAX_I8:
6209 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
6210 case ARM::ATOMIC_LOAD_MAX_I16:
6211 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
6212 case ARM::ATOMIC_LOAD_MAX_I32:
6213 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
6214
6215 case ARM::ATOMIC_LOAD_UMIN_I8:
6216 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
6217 case ARM::ATOMIC_LOAD_UMIN_I16:
6218 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
6219 case ARM::ATOMIC_LOAD_UMIN_I32:
6220 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
6221
6222 case ARM::ATOMIC_LOAD_UMAX_I8:
6223 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
6224 case ARM::ATOMIC_LOAD_UMAX_I16:
6225 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
6226 case ARM::ATOMIC_LOAD_UMAX_I32:
6227 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
6228
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006229 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
6230 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
6231 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00006232
6233 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
6234 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
6235 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006236
Eli Friedman2bdffe42011-08-31 00:31:29 +00006237
6238 case ARM::ATOMADD6432:
6239 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006240 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr,
6241 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006242 case ARM::ATOMSUB6432:
6243 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006244 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6245 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006246 case ARM::ATOMOR6432:
6247 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006248 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006249 case ARM::ATOMXOR6432:
6250 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006251 isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006252 case ARM::ATOMAND6432:
6253 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006254 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006255 case ARM::ATOMSWAP6432:
6256 return EmitAtomicBinary64(MI, BB, 0, 0, false);
Eli Friedman4d3f3292011-08-31 17:52:22 +00006257 case ARM::ATOMCMPXCHG6432:
6258 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
6259 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6260 /*NeedsCarry*/ false, /*IsCmpxchg*/true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006261
Evan Cheng007ea272009-08-12 05:17:19 +00006262 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00006263 // To "insert" a SELECT_CC instruction, we actually have to insert the
6264 // diamond control-flow pattern. The incoming instruction knows the
6265 // destination vreg to set, the condition code register to branch on, the
6266 // true/false values to select between, and a branch opcode to use.
6267 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006268 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00006269 ++It;
6270
6271 // thisMBB:
6272 // ...
6273 // TrueVal = ...
6274 // cmpTY ccX, r1, r2
6275 // bCC copy1MBB
6276 // fallthrough --> copy0MBB
6277 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006278 MachineFunction *F = BB->getParent();
6279 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6280 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00006281 F->insert(It, copy0MBB);
6282 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006283
6284 // Transfer the remainder of BB and its successor edges to sinkMBB.
6285 sinkMBB->splice(sinkMBB->begin(), BB,
6286 llvm::next(MachineBasicBlock::iterator(MI)),
6287 BB->end());
6288 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6289
Dan Gohman258c58c2010-07-06 15:49:48 +00006290 BB->addSuccessor(copy0MBB);
6291 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00006292
Dan Gohman14152b42010-07-06 20:24:04 +00006293 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
6294 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
6295
Evan Chenga8e29892007-01-19 07:51:42 +00006296 // copy0MBB:
6297 // %FalseValue = ...
6298 // # fallthrough to sinkMBB
6299 BB = copy0MBB;
6300
6301 // Update machine-CFG edges
6302 BB->addSuccessor(sinkMBB);
6303
6304 // sinkMBB:
6305 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6306 // ...
6307 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00006308 BuildMI(*BB, BB->begin(), dl,
6309 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00006310 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6311 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6312
Dan Gohman14152b42010-07-06 20:24:04 +00006313 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00006314 return BB;
6315 }
Evan Cheng86198642009-08-07 00:34:42 +00006316
Evan Cheng218977b2010-07-13 19:27:42 +00006317 case ARM::BCCi64:
6318 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00006319 // If there is an unconditional branch to the other successor, remove it.
6320 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00006321
Evan Cheng218977b2010-07-13 19:27:42 +00006322 // Compare both parts that make up the double comparison separately for
6323 // equality.
6324 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
6325
6326 unsigned LHS1 = MI->getOperand(1).getReg();
6327 unsigned LHS2 = MI->getOperand(2).getReg();
6328 if (RHSisZero) {
6329 AddDefaultPred(BuildMI(BB, dl,
6330 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6331 .addReg(LHS1).addImm(0));
6332 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6333 .addReg(LHS2).addImm(0)
6334 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6335 } else {
6336 unsigned RHS1 = MI->getOperand(3).getReg();
6337 unsigned RHS2 = MI->getOperand(4).getReg();
6338 AddDefaultPred(BuildMI(BB, dl,
6339 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6340 .addReg(LHS1).addReg(RHS1));
6341 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6342 .addReg(LHS2).addReg(RHS2)
6343 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6344 }
6345
6346 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
6347 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
6348 if (MI->getOperand(0).getImm() == ARMCC::NE)
6349 std::swap(destMBB, exitMBB);
6350
6351 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6352 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson51f6a7a2011-09-09 21:48:23 +00006353 if (isThumb2)
6354 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
6355 else
6356 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng218977b2010-07-13 19:27:42 +00006357
6358 MI->eraseFromParent(); // The pseudo instruction is gone now.
6359 return BB;
6360 }
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006361
Bill Wendling5bc85282011-10-17 20:37:20 +00006362 case ARM::Int_eh_sjlj_setjmp:
6363 case ARM::Int_eh_sjlj_setjmp_nofp:
6364 case ARM::tInt_eh_sjlj_setjmp:
6365 case ARM::t2Int_eh_sjlj_setjmp:
6366 case ARM::t2Int_eh_sjlj_setjmp_nofp:
6367 EmitSjLjDispatchBlock(MI, BB);
6368 return BB;
6369
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006370 case ARM::ABS:
6371 case ARM::t2ABS: {
6372 // To insert an ABS instruction, we have to insert the
6373 // diamond control-flow pattern. The incoming instruction knows the
6374 // source vreg to test against 0, the destination vreg to set,
6375 // the condition code register to branch on, the
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006376 // true/false values to select between, and a branch opcode to use.
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006377 // It transforms
6378 // V1 = ABS V0
6379 // into
6380 // V2 = MOVS V0
6381 // BCC (branch to SinkBB if V0 >= 0)
6382 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006383 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006384 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6385 MachineFunction::iterator BBI = BB;
6386 ++BBI;
6387 MachineFunction *Fn = BB->getParent();
6388 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6389 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6390 Fn->insert(BBI, RSBBB);
6391 Fn->insert(BBI, SinkBB);
6392
6393 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
6394 unsigned int ABSDstReg = MI->getOperand(0).getReg();
6395 bool isThumb2 = Subtarget->isThumb2();
6396 MachineRegisterInfo &MRI = Fn->getRegInfo();
6397 // In Thumb mode S must not be specified if source register is the SP or
6398 // PC and if destination register is the SP, so restrict register class
6399 unsigned NewMovDstReg = MRI.createVirtualRegister(
6400 isThumb2 ? ARM::rGPRRegisterClass : ARM::GPRRegisterClass);
6401 unsigned NewRsbDstReg = MRI.createVirtualRegister(
6402 isThumb2 ? ARM::rGPRRegisterClass : ARM::GPRRegisterClass);
6403
6404 // Transfer the remainder of BB and its successor edges to sinkMBB.
6405 SinkBB->splice(SinkBB->begin(), BB,
6406 llvm::next(MachineBasicBlock::iterator(MI)),
6407 BB->end());
6408 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
6409
6410 BB->addSuccessor(RSBBB);
6411 BB->addSuccessor(SinkBB);
6412
6413 // fall through to SinkMBB
6414 RSBBB->addSuccessor(SinkBB);
6415
6416 // insert a movs at the end of BB
6417 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVr : ARM::MOVr),
6418 NewMovDstReg)
6419 .addReg(ABSSrcReg, RegState::Kill)
6420 .addImm((unsigned)ARMCC::AL).addReg(0)
6421 .addReg(ARM::CPSR, RegState::Define);
6422
6423 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006424 BuildMI(BB, dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006425 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
6426 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
6427
6428 // insert rsbri in RSBBB
6429 // Note: BCC and rsbri will be converted into predicated rsbmi
6430 // by if-conversion pass
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006431 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006432 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
6433 .addReg(NewMovDstReg, RegState::Kill)
6434 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
6435
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006436 // insert PHI in SinkBB,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006437 // reuse ABSDstReg to not change uses of ABS instruction
6438 BuildMI(*SinkBB, SinkBB->begin(), dl,
6439 TII->get(ARM::PHI), ABSDstReg)
6440 .addReg(NewRsbDstReg).addMBB(RSBBB)
6441 .addReg(NewMovDstReg).addMBB(BB);
6442
6443 // remove ABS instruction
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006444 MI->eraseFromParent();
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006445
6446 // return last added BB
6447 return SinkBB;
6448 }
Evan Chenga8e29892007-01-19 07:51:42 +00006449 }
6450}
6451
Evan Cheng37fefc22011-08-30 19:09:48 +00006452void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
6453 SDNode *Node) const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006454 if (!MI->hasPostISelHook()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006455 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
6456 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
6457 return;
6458 }
6459
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006460 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick4815d562011-09-20 03:17:40 +00006461 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
6462 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
6463 // operand is still set to noreg. If needed, set the optional operand's
6464 // register to CPSR, and remove the redundant implicit def.
Andrew Trick3be654f2011-09-21 02:20:46 +00006465 //
Andrew Trick90b7b122011-10-18 19:18:52 +00006466 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick4815d562011-09-20 03:17:40 +00006467
Andrew Trick3be654f2011-09-21 02:20:46 +00006468 // Rename pseudo opcodes.
6469 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
6470 if (NewOpc) {
6471 const ARMBaseInstrInfo *TII =
6472 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick90b7b122011-10-18 19:18:52 +00006473 MCID = &TII->get(NewOpc);
6474
6475 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
6476 "converted opcode should be the same except for cc_out");
6477
6478 MI->setDesc(*MCID);
6479
6480 // Add the optional cc_out operand
6481 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick3be654f2011-09-21 02:20:46 +00006482 }
Andrew Trick90b7b122011-10-18 19:18:52 +00006483 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick4815d562011-09-20 03:17:40 +00006484
6485 // Any ARM instruction that sets the 's' bit should specify an optional
6486 // "cc_out" operand in the last operand position.
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006487 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006488 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006489 return;
6490 }
Andrew Trick3be654f2011-09-21 02:20:46 +00006491 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
6492 // since we already have an optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006493 bool definesCPSR = false;
6494 bool deadCPSR = false;
Andrew Trick90b7b122011-10-18 19:18:52 +00006495 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick4815d562011-09-20 03:17:40 +00006496 i != e; ++i) {
6497 const MachineOperand &MO = MI->getOperand(i);
6498 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
6499 definesCPSR = true;
6500 if (MO.isDead())
6501 deadCPSR = true;
6502 MI->RemoveOperand(i);
6503 break;
Evan Cheng37fefc22011-08-30 19:09:48 +00006504 }
6505 }
Andrew Trick4815d562011-09-20 03:17:40 +00006506 if (!definesCPSR) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006507 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006508 return;
6509 }
6510 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick3be654f2011-09-21 02:20:46 +00006511 if (deadCPSR) {
6512 assert(!MI->getOperand(ccOutIdx).getReg() &&
6513 "expect uninitialized optional cc_out operand");
Andrew Trick4815d562011-09-20 03:17:40 +00006514 return;
Andrew Trick3be654f2011-09-21 02:20:46 +00006515 }
Andrew Trick4815d562011-09-20 03:17:40 +00006516
Andrew Trick3be654f2011-09-21 02:20:46 +00006517 // If this instruction was defined with an optional CPSR def and its dag node
6518 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006519 MachineOperand &MO = MI->getOperand(ccOutIdx);
6520 MO.setReg(ARM::CPSR);
6521 MO.setIsDef(true);
Evan Cheng37fefc22011-08-30 19:09:48 +00006522}
6523
Evan Chenga8e29892007-01-19 07:51:42 +00006524//===----------------------------------------------------------------------===//
6525// ARM Optimization Hooks
6526//===----------------------------------------------------------------------===//
6527
Chris Lattnerd1980a52009-03-12 06:52:53 +00006528static
6529SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
6530 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00006531 SelectionDAG &DAG = DCI.DAG;
6532 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00006533 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00006534 unsigned Opc = N->getOpcode();
6535 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
6536 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
6537 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
6538 ISD::CondCode CC = ISD::SETCC_INVALID;
6539
6540 if (isSlctCC) {
6541 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
6542 } else {
6543 SDValue CCOp = Slct.getOperand(0);
6544 if (CCOp.getOpcode() == ISD::SETCC)
6545 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
6546 }
6547
6548 bool DoXform = false;
6549 bool InvCC = false;
6550 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
6551 "Bad input!");
6552
6553 if (LHS.getOpcode() == ISD::Constant &&
6554 cast<ConstantSDNode>(LHS)->isNullValue()) {
6555 DoXform = true;
6556 } else if (CC != ISD::SETCC_INVALID &&
6557 RHS.getOpcode() == ISD::Constant &&
6558 cast<ConstantSDNode>(RHS)->isNullValue()) {
6559 std::swap(LHS, RHS);
6560 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006561 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00006562 Op0.getOperand(0).getValueType();
6563 bool isInt = OpVT.isInteger();
6564 CC = ISD::getSetCCInverse(CC, isInt);
6565
6566 if (!TLI.isCondCodeLegal(CC, OpVT))
6567 return SDValue(); // Inverse operator isn't legal.
6568
6569 DoXform = true;
6570 InvCC = true;
6571 }
6572
6573 if (DoXform) {
6574 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
6575 if (isSlctCC)
6576 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
6577 Slct.getOperand(0), Slct.getOperand(1), CC);
6578 SDValue CCOp = Slct.getOperand(0);
6579 if (InvCC)
6580 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
6581 CCOp.getOperand(0), CCOp.getOperand(1), CC);
6582 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
6583 CCOp, OtherOp, Result);
6584 }
6585 return SDValue();
6586}
6587
Eric Christopherfa6f5912011-06-29 21:10:36 +00006588// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00006589// (only after legalization).
6590static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
6591 TargetLowering::DAGCombinerInfo &DCI,
6592 const ARMSubtarget *Subtarget) {
6593
6594 // Only perform optimization if after legalize, and if NEON is available. We
6595 // also expected both operands to be BUILD_VECTORs.
6596 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
6597 || N0.getOpcode() != ISD::BUILD_VECTOR
6598 || N1.getOpcode() != ISD::BUILD_VECTOR)
6599 return SDValue();
6600
6601 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
6602 EVT VT = N->getValueType(0);
6603 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
6604 return SDValue();
6605
6606 // Check that the vector operands are of the right form.
6607 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
6608 // operands, where N is the size of the formed vector.
6609 // Each EXTRACT_VECTOR should have the same input vector and odd or even
6610 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00006611
6612 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00006613 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00006614 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00006615 SDValue Vec = N0->getOperand(0)->getOperand(0);
6616 SDNode *V = Vec.getNode();
6617 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00006618
Eric Christopherfa6f5912011-06-29 21:10:36 +00006619 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00006620 // check to see if each of their operands are an EXTRACT_VECTOR with
6621 // the same vector and appropriate index.
6622 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
6623 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
6624 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00006625
Tanya Lattner189531f2011-06-14 23:48:48 +00006626 SDValue ExtVec0 = N0->getOperand(i);
6627 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00006628
Tanya Lattner189531f2011-06-14 23:48:48 +00006629 // First operand is the vector, verify its the same.
6630 if (V != ExtVec0->getOperand(0).getNode() ||
6631 V != ExtVec1->getOperand(0).getNode())
6632 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00006633
Tanya Lattner189531f2011-06-14 23:48:48 +00006634 // Second is the constant, verify its correct.
6635 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
6636 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00006637
Tanya Lattner189531f2011-06-14 23:48:48 +00006638 // For the constant, we want to see all the even or all the odd.
6639 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
6640 || C1->getZExtValue() != nextIndex+1)
6641 return SDValue();
6642
6643 // Increment index.
6644 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00006645 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00006646 return SDValue();
6647 }
6648
6649 // Create VPADDL node.
6650 SelectionDAG &DAG = DCI.DAG;
6651 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00006652
6653 // Build operand list.
6654 SmallVector<SDValue, 8> Ops;
6655 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
6656 TLI.getPointerTy()));
6657
6658 // Input is the vector.
6659 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00006660
Tanya Lattner189531f2011-06-14 23:48:48 +00006661 // Get widened type and narrowed type.
6662 MVT widenType;
6663 unsigned numElem = VT.getVectorNumElements();
6664 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
6665 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
6666 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
6667 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
6668 default:
6669 assert(0 && "Invalid vector element type for padd optimization.");
6670 }
6671
6672 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
6673 widenType, &Ops[0], Ops.size());
6674 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
6675}
6676
Bob Wilson3d5792a2010-07-29 20:34:14 +00006677/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
6678/// operands N0 and N1. This is a helper for PerformADDCombine that is
6679/// called with the default operands, and if that fails, with commuted
6680/// operands.
6681static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00006682 TargetLowering::DAGCombinerInfo &DCI,
6683 const ARMSubtarget *Subtarget){
6684
6685 // Attempt to create vpaddl for this add.
6686 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
6687 if (Result.getNode())
6688 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00006689
Chris Lattnerd1980a52009-03-12 06:52:53 +00006690 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
6691 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
6692 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
6693 if (Result.getNode()) return Result;
6694 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00006695 return SDValue();
6696}
6697
Bob Wilson3d5792a2010-07-29 20:34:14 +00006698/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
6699///
6700static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00006701 TargetLowering::DAGCombinerInfo &DCI,
6702 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00006703 SDValue N0 = N->getOperand(0);
6704 SDValue N1 = N->getOperand(1);
6705
6706 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00006707 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00006708 if (Result.getNode())
6709 return Result;
6710
6711 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00006712 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00006713}
6714
Chris Lattnerd1980a52009-03-12 06:52:53 +00006715/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00006716///
Chris Lattnerd1980a52009-03-12 06:52:53 +00006717static SDValue PerformSUBCombine(SDNode *N,
6718 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00006719 SDValue N0 = N->getOperand(0);
6720 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00006721
Chris Lattnerd1980a52009-03-12 06:52:53 +00006722 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
6723 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
6724 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
6725 if (Result.getNode()) return Result;
6726 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00006727
Chris Lattnerd1980a52009-03-12 06:52:53 +00006728 return SDValue();
6729}
6730
Evan Cheng463d3582011-03-31 19:38:48 +00006731/// PerformVMULCombine
6732/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
6733/// special multiplier accumulator forwarding.
6734/// vmul d3, d0, d2
6735/// vmla d3, d1, d2
6736/// is faster than
6737/// vadd d3, d0, d1
6738/// vmul d3, d3, d2
6739static SDValue PerformVMULCombine(SDNode *N,
6740 TargetLowering::DAGCombinerInfo &DCI,
6741 const ARMSubtarget *Subtarget) {
6742 if (!Subtarget->hasVMLxForwarding())
6743 return SDValue();
6744
6745 SelectionDAG &DAG = DCI.DAG;
6746 SDValue N0 = N->getOperand(0);
6747 SDValue N1 = N->getOperand(1);
6748 unsigned Opcode = N0.getOpcode();
6749 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
6750 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00006751 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00006752 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
6753 Opcode != ISD::FADD && Opcode != ISD::FSUB)
6754 return SDValue();
6755 std::swap(N0, N1);
6756 }
6757
6758 EVT VT = N->getValueType(0);
6759 DebugLoc DL = N->getDebugLoc();
6760 SDValue N00 = N0->getOperand(0);
6761 SDValue N01 = N0->getOperand(1);
6762 return DAG.getNode(Opcode, DL, VT,
6763 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
6764 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
6765}
6766
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006767static SDValue PerformMULCombine(SDNode *N,
6768 TargetLowering::DAGCombinerInfo &DCI,
6769 const ARMSubtarget *Subtarget) {
6770 SelectionDAG &DAG = DCI.DAG;
6771
6772 if (Subtarget->isThumb1Only())
6773 return SDValue();
6774
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006775 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
6776 return SDValue();
6777
6778 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00006779 if (VT.is64BitVector() || VT.is128BitVector())
6780 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006781 if (VT != MVT::i32)
6782 return SDValue();
6783
6784 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
6785 if (!C)
6786 return SDValue();
6787
6788 uint64_t MulAmt = C->getZExtValue();
6789 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
6790 ShiftAmt = ShiftAmt & (32 - 1);
6791 SDValue V = N->getOperand(0);
6792 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006793
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006794 SDValue Res;
6795 MulAmt >>= ShiftAmt;
6796 if (isPowerOf2_32(MulAmt - 1)) {
6797 // (mul x, 2^N + 1) => (add (shl x, N), x)
6798 Res = DAG.getNode(ISD::ADD, DL, VT,
6799 V, DAG.getNode(ISD::SHL, DL, VT,
6800 V, DAG.getConstant(Log2_32(MulAmt-1),
6801 MVT::i32)));
6802 } else if (isPowerOf2_32(MulAmt + 1)) {
6803 // (mul x, 2^N - 1) => (sub (shl x, N), x)
6804 Res = DAG.getNode(ISD::SUB, DL, VT,
6805 DAG.getNode(ISD::SHL, DL, VT,
6806 V, DAG.getConstant(Log2_32(MulAmt+1),
6807 MVT::i32)),
6808 V);
6809 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006810 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006811
6812 if (ShiftAmt != 0)
6813 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
6814 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006815
6816 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006817 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006818 return SDValue();
6819}
6820
Owen Anderson080c0922010-11-05 19:27:46 +00006821static SDValue PerformANDCombine(SDNode *N,
6822 TargetLowering::DAGCombinerInfo &DCI) {
Owen Anderson76706012011-04-05 21:48:57 +00006823
Owen Anderson080c0922010-11-05 19:27:46 +00006824 // Attempt to use immediate-form VBIC
6825 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
6826 DebugLoc dl = N->getDebugLoc();
6827 EVT VT = N->getValueType(0);
6828 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006829
Tanya Lattner0433b212011-04-07 15:24:20 +00006830 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
6831 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00006832
Owen Anderson080c0922010-11-05 19:27:46 +00006833 APInt SplatBits, SplatUndef;
6834 unsigned SplatBitSize;
6835 bool HasAnyUndefs;
6836 if (BVN &&
6837 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
6838 if (SplatBitSize <= 64) {
6839 EVT VbicVT;
6840 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
6841 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006842 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00006843 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00006844 if (Val.getNode()) {
6845 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006846 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00006847 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006848 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00006849 }
6850 }
6851 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006852
Owen Anderson080c0922010-11-05 19:27:46 +00006853 return SDValue();
6854}
6855
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006856/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
6857static SDValue PerformORCombine(SDNode *N,
6858 TargetLowering::DAGCombinerInfo &DCI,
6859 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00006860 // Attempt to use immediate-form VORR
6861 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
6862 DebugLoc dl = N->getDebugLoc();
6863 EVT VT = N->getValueType(0);
6864 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006865
Tanya Lattner0433b212011-04-07 15:24:20 +00006866 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
6867 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00006868
Owen Anderson60f48702010-11-03 23:15:26 +00006869 APInt SplatBits, SplatUndef;
6870 unsigned SplatBitSize;
6871 bool HasAnyUndefs;
6872 if (BVN && Subtarget->hasNEON() &&
6873 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
6874 if (SplatBitSize <= 64) {
6875 EVT VorrVT;
6876 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
6877 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00006878 DAG, VorrVT, VT.is128BitVector(),
6879 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00006880 if (Val.getNode()) {
6881 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006882 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00006883 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006884 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00006885 }
6886 }
6887 }
6888
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00006889 SDValue N0 = N->getOperand(0);
6890 if (N0.getOpcode() != ISD::AND)
6891 return SDValue();
6892 SDValue N1 = N->getOperand(1);
6893
6894 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
6895 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
6896 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
6897 APInt SplatUndef;
6898 unsigned SplatBitSize;
6899 bool HasAnyUndefs;
6900
6901 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
6902 APInt SplatBits0;
6903 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
6904 HasAnyUndefs) && !HasAnyUndefs) {
6905 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
6906 APInt SplatBits1;
6907 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
6908 HasAnyUndefs) && !HasAnyUndefs &&
6909 SplatBits0 == ~SplatBits1) {
6910 // Canonicalize the vector type to make instruction selection simpler.
6911 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
6912 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
6913 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00006914 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00006915 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
6916 }
6917 }
6918 }
6919
Jim Grosbach54238562010-07-17 03:30:54 +00006920 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
6921 // reasonable.
6922
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006923 // BFI is only available on V6T2+
6924 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
6925 return SDValue();
6926
Jim Grosbach54238562010-07-17 03:30:54 +00006927 DebugLoc DL = N->getDebugLoc();
6928 // 1) or (and A, mask), val => ARMbfi A, val, mask
6929 // iff (val & mask) == val
6930 //
6931 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
6932 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00006933 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00006934 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00006935 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00006936 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006937
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006938 if (VT != MVT::i32)
6939 return SDValue();
6940
Evan Cheng30fb13f2010-12-13 20:32:54 +00006941 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00006942
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006943 // The value and the mask need to be constants so we can verify this is
6944 // actually a bitfield set. If the mask is 0xffff, we can do better
6945 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00006946 SDValue MaskOp = N0.getOperand(1);
6947 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
6948 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006949 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00006950 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006951 if (Mask == 0xffff)
6952 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006953 SDValue Res;
6954 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00006955 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
6956 if (N1C) {
6957 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00006958 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00006959 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006960
Evan Chenga9688c42010-12-11 04:11:38 +00006961 if (ARM::isBitFieldInvertedMask(Mask)) {
6962 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006963
Evan Cheng30fb13f2010-12-13 20:32:54 +00006964 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00006965 DAG.getConstant(Val, MVT::i32),
6966 DAG.getConstant(Mask, MVT::i32));
6967
6968 // Do not add new nodes to DAG combiner worklist.
6969 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00006970 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00006971 }
Jim Grosbach54238562010-07-17 03:30:54 +00006972 } else if (N1.getOpcode() == ISD::AND) {
6973 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00006974 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
6975 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00006976 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00006977 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006978
Eric Christopher29aeed12011-03-26 01:21:03 +00006979 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
6980 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00006981 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00006982 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00006983 // The pack halfword instruction works better for masks that fit it,
6984 // so use that when it's available.
6985 if (Subtarget->hasT2ExtractPack() &&
6986 (Mask == 0xffff || Mask == 0xffff0000))
6987 return SDValue();
6988 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00006989 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00006990 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00006991 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00006992 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00006993 DAG.getConstant(Mask, MVT::i32));
6994 // Do not add new nodes to DAG combiner worklist.
6995 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00006996 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006997 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00006998 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00006999 // The pack halfword instruction works better for masks that fit it,
7000 // so use that when it's available.
7001 if (Subtarget->hasT2ExtractPack() &&
7002 (Mask2 == 0xffff || Mask2 == 0xffff0000))
7003 return SDValue();
7004 // 2b
7005 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007006 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00007007 DAG.getConstant(lsb, MVT::i32));
7008 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00007009 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00007010 // Do not add new nodes to DAG combiner worklist.
7011 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007012 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007013 }
7014 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007015
Evan Cheng30fb13f2010-12-13 20:32:54 +00007016 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
7017 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
7018 ARM::isBitFieldInvertedMask(~Mask)) {
7019 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
7020 // where lsb(mask) == #shamt and masked bits of B are known zero.
7021 SDValue ShAmt = N00.getOperand(1);
7022 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
7023 unsigned LSB = CountTrailingZeros_32(Mask);
7024 if (ShAmtC != LSB)
7025 return SDValue();
7026
7027 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
7028 DAG.getConstant(~Mask, MVT::i32));
7029
7030 // Do not add new nodes to DAG combiner worklist.
7031 DCI.CombineTo(N, Res, false);
7032 }
7033
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007034 return SDValue();
7035}
7036
Evan Chengbf188ae2011-06-15 01:12:31 +00007037/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
7038/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00007039static SDValue PerformBFICombine(SDNode *N,
7040 TargetLowering::DAGCombinerInfo &DCI) {
7041 SDValue N1 = N->getOperand(1);
7042 if (N1.getOpcode() == ISD::AND) {
7043 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7044 if (!N11C)
7045 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007046 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
7047 unsigned LSB = CountTrailingZeros_32(~InvMask);
7048 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
7049 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00007050 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007051 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00007052 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
7053 N->getOperand(0), N1.getOperand(0),
7054 N->getOperand(2));
7055 }
7056 return SDValue();
7057}
7058
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007059/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
7060/// ARMISD::VMOVRRD.
7061static SDValue PerformVMOVRRDCombine(SDNode *N,
7062 TargetLowering::DAGCombinerInfo &DCI) {
7063 // vmovrrd(vmovdrr x, y) -> x,y
7064 SDValue InDouble = N->getOperand(0);
7065 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
7066 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00007067
7068 // vmovrrd(load f64) -> (load i32), (load i32)
7069 SDNode *InNode = InDouble.getNode();
7070 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
7071 InNode->getValueType(0) == MVT::f64 &&
7072 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
7073 !cast<LoadSDNode>(InNode)->isVolatile()) {
7074 // TODO: Should this be done for non-FrameIndex operands?
7075 LoadSDNode *LD = cast<LoadSDNode>(InNode);
7076
7077 SelectionDAG &DAG = DCI.DAG;
7078 DebugLoc DL = LD->getDebugLoc();
7079 SDValue BasePtr = LD->getBasePtr();
7080 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
7081 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007082 LD->isNonTemporal(), LD->isInvariant(),
7083 LD->getAlignment());
Cameron Zwarich4071a712011-04-02 02:40:43 +00007084
7085 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7086 DAG.getConstant(4, MVT::i32));
7087 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
7088 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007089 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich4071a712011-04-02 02:40:43 +00007090 std::min(4U, LD->getAlignment() / 2));
7091
7092 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
7093 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
7094 DCI.RemoveFromWorklist(LD);
7095 DAG.DeleteNode(LD);
7096 return Result;
7097 }
7098
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007099 return SDValue();
7100}
7101
7102/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
7103/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
7104static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
7105 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
7106 SDValue Op0 = N->getOperand(0);
7107 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007108 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007109 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007110 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007111 Op1 = Op1.getOperand(0);
7112 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
7113 Op0.getNode() == Op1.getNode() &&
7114 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007115 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007116 N->getValueType(0), Op0.getOperand(0));
7117 return SDValue();
7118}
7119
Bob Wilson31600902010-12-21 06:43:19 +00007120/// PerformSTORECombine - Target-specific dag combine xforms for
7121/// ISD::STORE.
7122static SDValue PerformSTORECombine(SDNode *N,
7123 TargetLowering::DAGCombinerInfo &DCI) {
7124 // Bitcast an i64 store extracted from a vector to f64.
7125 // Otherwise, the i64 value will be legalized to a pair of i32 values.
7126 StoreSDNode *St = cast<StoreSDNode>(N);
7127 SDValue StVal = St->getValue();
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007128 if (!ISD::isNormalStore(St) || St->isVolatile())
7129 return SDValue();
7130
7131 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
7132 StVal.getNode()->hasOneUse() && !St->isVolatile()) {
7133 SelectionDAG &DAG = DCI.DAG;
7134 DebugLoc DL = St->getDebugLoc();
7135 SDValue BasePtr = St->getBasePtr();
7136 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
7137 StVal.getNode()->getOperand(0), BasePtr,
7138 St->getPointerInfo(), St->isVolatile(),
7139 St->isNonTemporal(), St->getAlignment());
7140
7141 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7142 DAG.getConstant(4, MVT::i32));
7143 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
7144 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
7145 St->isNonTemporal(),
7146 std::min(4U, St->getAlignment() / 2));
7147 }
7148
7149 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00007150 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
7151 return SDValue();
7152
7153 SelectionDAG &DAG = DCI.DAG;
7154 DebugLoc dl = StVal.getDebugLoc();
7155 SDValue IntVec = StVal.getOperand(0);
7156 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7157 IntVec.getValueType().getVectorNumElements());
7158 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
7159 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
7160 Vec, StVal.getOperand(1));
7161 dl = N->getDebugLoc();
7162 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
7163 // Make the DAGCombiner fold the bitcasts.
7164 DCI.AddToWorklist(Vec.getNode());
7165 DCI.AddToWorklist(ExtElt.getNode());
7166 DCI.AddToWorklist(V.getNode());
7167 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
7168 St->getPointerInfo(), St->isVolatile(),
7169 St->isNonTemporal(), St->getAlignment(),
7170 St->getTBAAInfo());
7171}
7172
7173/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
7174/// are normal, non-volatile loads. If so, it is profitable to bitcast an
7175/// i64 vector to have f64 elements, since the value can then be loaded
7176/// directly into a VFP register.
7177static bool hasNormalLoadOperand(SDNode *N) {
7178 unsigned NumElts = N->getValueType(0).getVectorNumElements();
7179 for (unsigned i = 0; i < NumElts; ++i) {
7180 SDNode *Elt = N->getOperand(i).getNode();
7181 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
7182 return true;
7183 }
7184 return false;
7185}
7186
Bob Wilson75f02882010-09-17 22:59:05 +00007187/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
7188/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00007189static SDValue PerformBUILD_VECTORCombine(SDNode *N,
7190 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00007191 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
7192 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
7193 // into a pair of GPRs, which is fine when the value is used as a scalar,
7194 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00007195 SelectionDAG &DAG = DCI.DAG;
7196 if (N->getNumOperands() == 2) {
7197 SDValue RV = PerformVMOVDRRCombine(N, DAG);
7198 if (RV.getNode())
7199 return RV;
7200 }
Bob Wilson75f02882010-09-17 22:59:05 +00007201
Bob Wilson31600902010-12-21 06:43:19 +00007202 // Load i64 elements as f64 values so that type legalization does not split
7203 // them up into i32 values.
7204 EVT VT = N->getValueType(0);
7205 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
7206 return SDValue();
7207 DebugLoc dl = N->getDebugLoc();
7208 SmallVector<SDValue, 8> Ops;
7209 unsigned NumElts = VT.getVectorNumElements();
7210 for (unsigned i = 0; i < NumElts; ++i) {
7211 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
7212 Ops.push_back(V);
7213 // Make the DAGCombiner fold the bitcast.
7214 DCI.AddToWorklist(V.getNode());
7215 }
7216 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
7217 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
7218 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
7219}
7220
7221/// PerformInsertEltCombine - Target-specific dag combine xforms for
7222/// ISD::INSERT_VECTOR_ELT.
7223static SDValue PerformInsertEltCombine(SDNode *N,
7224 TargetLowering::DAGCombinerInfo &DCI) {
7225 // Bitcast an i64 load inserted into a vector to f64.
7226 // Otherwise, the i64 value will be legalized to a pair of i32 values.
7227 EVT VT = N->getValueType(0);
7228 SDNode *Elt = N->getOperand(1).getNode();
7229 if (VT.getVectorElementType() != MVT::i64 ||
7230 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
7231 return SDValue();
7232
7233 SelectionDAG &DAG = DCI.DAG;
7234 DebugLoc dl = N->getDebugLoc();
7235 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7236 VT.getVectorNumElements());
7237 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
7238 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
7239 // Make the DAGCombiner fold the bitcasts.
7240 DCI.AddToWorklist(Vec.getNode());
7241 DCI.AddToWorklist(V.getNode());
7242 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
7243 Vec, V, N->getOperand(2));
7244 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00007245}
7246
Bob Wilsonf20700c2010-10-27 20:38:28 +00007247/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
7248/// ISD::VECTOR_SHUFFLE.
7249static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
7250 // The LLVM shufflevector instruction does not require the shuffle mask
7251 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
7252 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
7253 // operands do not match the mask length, they are extended by concatenating
7254 // them with undef vectors. That is probably the right thing for other
7255 // targets, but for NEON it is better to concatenate two double-register
7256 // size vector operands into a single quad-register size vector. Do that
7257 // transformation here:
7258 // shuffle(concat(v1, undef), concat(v2, undef)) ->
7259 // shuffle(concat(v1, v2), undef)
7260 SDValue Op0 = N->getOperand(0);
7261 SDValue Op1 = N->getOperand(1);
7262 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
7263 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
7264 Op0.getNumOperands() != 2 ||
7265 Op1.getNumOperands() != 2)
7266 return SDValue();
7267 SDValue Concat0Op1 = Op0.getOperand(1);
7268 SDValue Concat1Op1 = Op1.getOperand(1);
7269 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
7270 Concat1Op1.getOpcode() != ISD::UNDEF)
7271 return SDValue();
7272 // Skip the transformation if any of the types are illegal.
7273 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7274 EVT VT = N->getValueType(0);
7275 if (!TLI.isTypeLegal(VT) ||
7276 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
7277 !TLI.isTypeLegal(Concat1Op1.getValueType()))
7278 return SDValue();
7279
7280 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
7281 Op0.getOperand(0), Op1.getOperand(0));
7282 // Translate the shuffle mask.
7283 SmallVector<int, 16> NewMask;
7284 unsigned NumElts = VT.getVectorNumElements();
7285 unsigned HalfElts = NumElts/2;
7286 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
7287 for (unsigned n = 0; n < NumElts; ++n) {
7288 int MaskElt = SVN->getMaskElt(n);
7289 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007290 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00007291 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007292 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00007293 NewElt = HalfElts + MaskElt - NumElts;
7294 NewMask.push_back(NewElt);
7295 }
7296 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
7297 DAG.getUNDEF(VT), NewMask.data());
7298}
7299
Bob Wilson1c3ef902011-02-07 17:43:21 +00007300/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
7301/// NEON load/store intrinsics to merge base address updates.
7302static SDValue CombineBaseUpdate(SDNode *N,
7303 TargetLowering::DAGCombinerInfo &DCI) {
7304 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7305 return SDValue();
7306
7307 SelectionDAG &DAG = DCI.DAG;
7308 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
7309 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
7310 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
7311 SDValue Addr = N->getOperand(AddrOpIdx);
7312
7313 // Search for a use of the address operand that is an increment.
7314 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
7315 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
7316 SDNode *User = *UI;
7317 if (User->getOpcode() != ISD::ADD ||
7318 UI.getUse().getResNo() != Addr.getResNo())
7319 continue;
7320
7321 // Check that the add is independent of the load/store. Otherwise, folding
7322 // it would create a cycle.
7323 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
7324 continue;
7325
7326 // Find the new opcode for the updating load/store.
7327 bool isLoad = true;
7328 bool isLaneOp = false;
7329 unsigned NewOpc = 0;
7330 unsigned NumVecs = 0;
7331 if (isIntrinsic) {
7332 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
7333 switch (IntNo) {
7334 default: assert(0 && "unexpected intrinsic for Neon base update");
7335 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
7336 NumVecs = 1; break;
7337 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
7338 NumVecs = 2; break;
7339 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
7340 NumVecs = 3; break;
7341 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
7342 NumVecs = 4; break;
7343 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
7344 NumVecs = 2; isLaneOp = true; break;
7345 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
7346 NumVecs = 3; isLaneOp = true; break;
7347 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
7348 NumVecs = 4; isLaneOp = true; break;
7349 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
7350 NumVecs = 1; isLoad = false; break;
7351 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
7352 NumVecs = 2; isLoad = false; break;
7353 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
7354 NumVecs = 3; isLoad = false; break;
7355 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
7356 NumVecs = 4; isLoad = false; break;
7357 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
7358 NumVecs = 2; isLoad = false; isLaneOp = true; break;
7359 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
7360 NumVecs = 3; isLoad = false; isLaneOp = true; break;
7361 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
7362 NumVecs = 4; isLoad = false; isLaneOp = true; break;
7363 }
7364 } else {
7365 isLaneOp = true;
7366 switch (N->getOpcode()) {
7367 default: assert(0 && "unexpected opcode for Neon base update");
7368 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
7369 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
7370 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
7371 }
7372 }
7373
7374 // Find the size of memory referenced by the load/store.
7375 EVT VecTy;
7376 if (isLoad)
7377 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00007378 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00007379 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
7380 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
7381 if (isLaneOp)
7382 NumBytes /= VecTy.getVectorNumElements();
7383
7384 // If the increment is a constant, it must match the memory ref size.
7385 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
7386 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
7387 uint64_t IncVal = CInc->getZExtValue();
7388 if (IncVal != NumBytes)
7389 continue;
7390 } else if (NumBytes >= 3 * 16) {
7391 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
7392 // separate instructions that make it harder to use a non-constant update.
7393 continue;
7394 }
7395
7396 // Create the new updating load/store node.
7397 EVT Tys[6];
7398 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
7399 unsigned n;
7400 for (n = 0; n < NumResultVecs; ++n)
7401 Tys[n] = VecTy;
7402 Tys[n++] = MVT::i32;
7403 Tys[n] = MVT::Other;
7404 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
7405 SmallVector<SDValue, 8> Ops;
7406 Ops.push_back(N->getOperand(0)); // incoming chain
7407 Ops.push_back(N->getOperand(AddrOpIdx));
7408 Ops.push_back(Inc);
7409 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
7410 Ops.push_back(N->getOperand(i));
7411 }
7412 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
7413 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
7414 Ops.data(), Ops.size(),
7415 MemInt->getMemoryVT(),
7416 MemInt->getMemOperand());
7417
7418 // Update the uses.
7419 std::vector<SDValue> NewResults;
7420 for (unsigned i = 0; i < NumResultVecs; ++i) {
7421 NewResults.push_back(SDValue(UpdN.getNode(), i));
7422 }
7423 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
7424 DCI.CombineTo(N, NewResults);
7425 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
7426
7427 break;
Owen Anderson76706012011-04-05 21:48:57 +00007428 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00007429 return SDValue();
7430}
7431
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007432/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
7433/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
7434/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
7435/// return true.
7436static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
7437 SelectionDAG &DAG = DCI.DAG;
7438 EVT VT = N->getValueType(0);
7439 // vldN-dup instructions only support 64-bit vectors for N > 1.
7440 if (!VT.is64BitVector())
7441 return false;
7442
7443 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
7444 SDNode *VLD = N->getOperand(0).getNode();
7445 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
7446 return false;
7447 unsigned NumVecs = 0;
7448 unsigned NewOpc = 0;
7449 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
7450 if (IntNo == Intrinsic::arm_neon_vld2lane) {
7451 NumVecs = 2;
7452 NewOpc = ARMISD::VLD2DUP;
7453 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
7454 NumVecs = 3;
7455 NewOpc = ARMISD::VLD3DUP;
7456 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
7457 NumVecs = 4;
7458 NewOpc = ARMISD::VLD4DUP;
7459 } else {
7460 return false;
7461 }
7462
7463 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
7464 // numbers match the load.
7465 unsigned VLDLaneNo =
7466 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
7467 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
7468 UI != UE; ++UI) {
7469 // Ignore uses of the chain result.
7470 if (UI.getUse().getResNo() == NumVecs)
7471 continue;
7472 SDNode *User = *UI;
7473 if (User->getOpcode() != ARMISD::VDUPLANE ||
7474 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
7475 return false;
7476 }
7477
7478 // Create the vldN-dup node.
7479 EVT Tys[5];
7480 unsigned n;
7481 for (n = 0; n < NumVecs; ++n)
7482 Tys[n] = VT;
7483 Tys[n] = MVT::Other;
7484 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
7485 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
7486 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
7487 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
7488 Ops, 2, VLDMemInt->getMemoryVT(),
7489 VLDMemInt->getMemOperand());
7490
7491 // Update the uses.
7492 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
7493 UI != UE; ++UI) {
7494 unsigned ResNo = UI.getUse().getResNo();
7495 // Ignore uses of the chain result.
7496 if (ResNo == NumVecs)
7497 continue;
7498 SDNode *User = *UI;
7499 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
7500 }
7501
7502 // Now the vldN-lane intrinsic is dead except for its chain result.
7503 // Update uses of the chain.
7504 std::vector<SDValue> VLDDupResults;
7505 for (unsigned n = 0; n < NumVecs; ++n)
7506 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
7507 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
7508 DCI.CombineTo(VLD, VLDDupResults);
7509
7510 return true;
7511}
7512
Bob Wilson9e82bf12010-07-14 01:22:12 +00007513/// PerformVDUPLANECombine - Target-specific dag combine xforms for
7514/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007515static SDValue PerformVDUPLANECombine(SDNode *N,
7516 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00007517 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007518
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007519 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
7520 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
7521 if (CombineVLDDUP(N, DCI))
7522 return SDValue(N, 0);
7523
7524 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
7525 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007526 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00007527 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00007528 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00007529 return SDValue();
7530
7531 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
7532 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
7533 // The canonical VMOV for a zero vector uses a 32-bit element size.
7534 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7535 unsigned EltBits;
7536 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
7537 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007538 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007539 if (EltSize > VT.getVectorElementType().getSizeInBits())
7540 return SDValue();
7541
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007542 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007543}
7544
Eric Christopherfa6f5912011-06-29 21:10:36 +00007545// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00007546// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
7547static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
7548{
Chad Rosier118c9a02011-06-28 17:26:57 +00007549 integerPart cN;
7550 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00007551 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
7552 I != E; I++) {
7553 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
7554 if (!C)
7555 return false;
7556
Eric Christopherfa6f5912011-06-29 21:10:36 +00007557 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00007558 APFloat APF = C->getValueAPF();
7559 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
7560 != APFloat::opOK || !isExact)
7561 return false;
7562
7563 c0 = (I == 0) ? cN : c0;
7564 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
7565 return false;
7566 }
7567 C = c0;
7568 return true;
7569}
7570
7571/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
7572/// can replace combinations of VMUL and VCVT (floating-point to integer)
7573/// when the VMUL has a constant operand that is a power of 2.
7574///
7575/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
7576/// vmul.f32 d16, d17, d16
7577/// vcvt.s32.f32 d16, d16
7578/// becomes:
7579/// vcvt.s32.f32 d16, d16, #3
7580static SDValue PerformVCVTCombine(SDNode *N,
7581 TargetLowering::DAGCombinerInfo &DCI,
7582 const ARMSubtarget *Subtarget) {
7583 SelectionDAG &DAG = DCI.DAG;
7584 SDValue Op = N->getOperand(0);
7585
7586 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
7587 Op.getOpcode() != ISD::FMUL)
7588 return SDValue();
7589
7590 uint64_t C;
7591 SDValue N0 = Op->getOperand(0);
7592 SDValue ConstVec = Op->getOperand(1);
7593 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
7594
Eric Christopherfa6f5912011-06-29 21:10:36 +00007595 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00007596 !isConstVecPow2(ConstVec, isSigned, C))
7597 return SDValue();
7598
7599 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
7600 Intrinsic::arm_neon_vcvtfp2fxu;
7601 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7602 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00007603 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00007604 DAG.getConstant(Log2_64(C), MVT::i32));
7605}
7606
7607/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
7608/// can replace combinations of VCVT (integer to floating-point) and VDIV
7609/// when the VDIV has a constant operand that is a power of 2.
7610///
7611/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
7612/// vcvt.f32.s32 d16, d16
7613/// vdiv.f32 d16, d17, d16
7614/// becomes:
7615/// vcvt.f32.s32 d16, d16, #3
7616static SDValue PerformVDIVCombine(SDNode *N,
7617 TargetLowering::DAGCombinerInfo &DCI,
7618 const ARMSubtarget *Subtarget) {
7619 SelectionDAG &DAG = DCI.DAG;
7620 SDValue Op = N->getOperand(0);
7621 unsigned OpOpcode = Op.getNode()->getOpcode();
7622
7623 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
7624 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
7625 return SDValue();
7626
7627 uint64_t C;
7628 SDValue ConstVec = N->getOperand(1);
7629 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
7630
7631 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
7632 !isConstVecPow2(ConstVec, isSigned, C))
7633 return SDValue();
7634
Eric Christopherfa6f5912011-06-29 21:10:36 +00007635 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00007636 Intrinsic::arm_neon_vcvtfxu2fp;
7637 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7638 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00007639 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00007640 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
7641}
7642
7643/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00007644/// operand of a vector shift operation, where all the elements of the
7645/// build_vector must have the same constant integer value.
7646static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
7647 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007648 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00007649 Op = Op.getOperand(0);
7650 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
7651 APInt SplatBits, SplatUndef;
7652 unsigned SplatBitSize;
7653 bool HasAnyUndefs;
7654 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
7655 HasAnyUndefs, ElementBits) ||
7656 SplatBitSize > ElementBits)
7657 return false;
7658 Cnt = SplatBits.getSExtValue();
7659 return true;
7660}
7661
7662/// isVShiftLImm - Check if this is a valid build_vector for the immediate
7663/// operand of a vector shift left operation. That value must be in the range:
7664/// 0 <= Value < ElementBits for a left shift; or
7665/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00007666static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00007667 assert(VT.isVector() && "vector shift count is not a vector type");
7668 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
7669 if (! getVShiftImm(Op, ElementBits, Cnt))
7670 return false;
7671 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
7672}
7673
7674/// isVShiftRImm - Check if this is a valid build_vector for the immediate
7675/// operand of a vector shift right operation. For a shift opcode, the value
7676/// is positive, but for an intrinsic the value count must be negative. The
7677/// absolute value must be in the range:
7678/// 1 <= |Value| <= ElementBits for a right shift; or
7679/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00007680static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00007681 int64_t &Cnt) {
7682 assert(VT.isVector() && "vector shift count is not a vector type");
7683 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
7684 if (! getVShiftImm(Op, ElementBits, Cnt))
7685 return false;
7686 if (isIntrinsic)
7687 Cnt = -Cnt;
7688 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
7689}
7690
7691/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
7692static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
7693 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
7694 switch (IntNo) {
7695 default:
7696 // Don't do anything for most intrinsics.
7697 break;
7698
7699 // Vector shifts: check for immediate versions and lower them.
7700 // Note: This is done during DAG combining instead of DAG legalizing because
7701 // the build_vectors for 64-bit vector element shift counts are generally
7702 // not legal, and it is hard to see their values after they get legalized to
7703 // loads from a constant pool.
7704 case Intrinsic::arm_neon_vshifts:
7705 case Intrinsic::arm_neon_vshiftu:
7706 case Intrinsic::arm_neon_vshiftls:
7707 case Intrinsic::arm_neon_vshiftlu:
7708 case Intrinsic::arm_neon_vshiftn:
7709 case Intrinsic::arm_neon_vrshifts:
7710 case Intrinsic::arm_neon_vrshiftu:
7711 case Intrinsic::arm_neon_vrshiftn:
7712 case Intrinsic::arm_neon_vqshifts:
7713 case Intrinsic::arm_neon_vqshiftu:
7714 case Intrinsic::arm_neon_vqshiftsu:
7715 case Intrinsic::arm_neon_vqshiftns:
7716 case Intrinsic::arm_neon_vqshiftnu:
7717 case Intrinsic::arm_neon_vqshiftnsu:
7718 case Intrinsic::arm_neon_vqrshiftns:
7719 case Intrinsic::arm_neon_vqrshiftnu:
7720 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00007721 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007722 int64_t Cnt;
7723 unsigned VShiftOpc = 0;
7724
7725 switch (IntNo) {
7726 case Intrinsic::arm_neon_vshifts:
7727 case Intrinsic::arm_neon_vshiftu:
7728 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
7729 VShiftOpc = ARMISD::VSHL;
7730 break;
7731 }
7732 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
7733 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
7734 ARMISD::VSHRs : ARMISD::VSHRu);
7735 break;
7736 }
7737 return SDValue();
7738
7739 case Intrinsic::arm_neon_vshiftls:
7740 case Intrinsic::arm_neon_vshiftlu:
7741 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
7742 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007743 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007744
7745 case Intrinsic::arm_neon_vrshifts:
7746 case Intrinsic::arm_neon_vrshiftu:
7747 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
7748 break;
7749 return SDValue();
7750
7751 case Intrinsic::arm_neon_vqshifts:
7752 case Intrinsic::arm_neon_vqshiftu:
7753 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
7754 break;
7755 return SDValue();
7756
7757 case Intrinsic::arm_neon_vqshiftsu:
7758 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
7759 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007760 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007761
7762 case Intrinsic::arm_neon_vshiftn:
7763 case Intrinsic::arm_neon_vrshiftn:
7764 case Intrinsic::arm_neon_vqshiftns:
7765 case Intrinsic::arm_neon_vqshiftnu:
7766 case Intrinsic::arm_neon_vqshiftnsu:
7767 case Intrinsic::arm_neon_vqrshiftns:
7768 case Intrinsic::arm_neon_vqrshiftnu:
7769 case Intrinsic::arm_neon_vqrshiftnsu:
7770 // Narrowing shifts require an immediate right shift.
7771 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
7772 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00007773 llvm_unreachable("invalid shift count for narrowing vector shift "
7774 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007775
7776 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00007777 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00007778 }
7779
7780 switch (IntNo) {
7781 case Intrinsic::arm_neon_vshifts:
7782 case Intrinsic::arm_neon_vshiftu:
7783 // Opcode already set above.
7784 break;
7785 case Intrinsic::arm_neon_vshiftls:
7786 case Intrinsic::arm_neon_vshiftlu:
7787 if (Cnt == VT.getVectorElementType().getSizeInBits())
7788 VShiftOpc = ARMISD::VSHLLi;
7789 else
7790 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
7791 ARMISD::VSHLLs : ARMISD::VSHLLu);
7792 break;
7793 case Intrinsic::arm_neon_vshiftn:
7794 VShiftOpc = ARMISD::VSHRN; break;
7795 case Intrinsic::arm_neon_vrshifts:
7796 VShiftOpc = ARMISD::VRSHRs; break;
7797 case Intrinsic::arm_neon_vrshiftu:
7798 VShiftOpc = ARMISD::VRSHRu; break;
7799 case Intrinsic::arm_neon_vrshiftn:
7800 VShiftOpc = ARMISD::VRSHRN; break;
7801 case Intrinsic::arm_neon_vqshifts:
7802 VShiftOpc = ARMISD::VQSHLs; break;
7803 case Intrinsic::arm_neon_vqshiftu:
7804 VShiftOpc = ARMISD::VQSHLu; break;
7805 case Intrinsic::arm_neon_vqshiftsu:
7806 VShiftOpc = ARMISD::VQSHLsu; break;
7807 case Intrinsic::arm_neon_vqshiftns:
7808 VShiftOpc = ARMISD::VQSHRNs; break;
7809 case Intrinsic::arm_neon_vqshiftnu:
7810 VShiftOpc = ARMISD::VQSHRNu; break;
7811 case Intrinsic::arm_neon_vqshiftnsu:
7812 VShiftOpc = ARMISD::VQSHRNsu; break;
7813 case Intrinsic::arm_neon_vqrshiftns:
7814 VShiftOpc = ARMISD::VQRSHRNs; break;
7815 case Intrinsic::arm_neon_vqrshiftnu:
7816 VShiftOpc = ARMISD::VQRSHRNu; break;
7817 case Intrinsic::arm_neon_vqrshiftnsu:
7818 VShiftOpc = ARMISD::VQRSHRNsu; break;
7819 }
7820
7821 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007822 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007823 }
7824
7825 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00007826 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007827 int64_t Cnt;
7828 unsigned VShiftOpc = 0;
7829
7830 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
7831 VShiftOpc = ARMISD::VSLI;
7832 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
7833 VShiftOpc = ARMISD::VSRI;
7834 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00007835 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007836 }
7837
7838 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
7839 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00007840 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007841 }
7842
7843 case Intrinsic::arm_neon_vqrshifts:
7844 case Intrinsic::arm_neon_vqrshiftu:
7845 // No immediate versions of these to check for.
7846 break;
7847 }
7848
7849 return SDValue();
7850}
7851
7852/// PerformShiftCombine - Checks for immediate versions of vector shifts and
7853/// lowers them. As with the vector shift intrinsics, this is done during DAG
7854/// combining instead of DAG legalizing because the build_vectors for 64-bit
7855/// vector element shift counts are generally not legal, and it is hard to see
7856/// their values after they get legalized to loads from a constant pool.
7857static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
7858 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00007859 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00007860
7861 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00007862 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7863 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00007864 return SDValue();
7865
7866 assert(ST->hasNEON() && "unexpected vector shift");
7867 int64_t Cnt;
7868
7869 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007870 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00007871
7872 case ISD::SHL:
7873 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
7874 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007875 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007876 break;
7877
7878 case ISD::SRA:
7879 case ISD::SRL:
7880 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
7881 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
7882 ARMISD::VSHRs : ARMISD::VSHRu);
7883 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007884 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007885 }
7886 }
7887 return SDValue();
7888}
7889
7890/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
7891/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
7892static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
7893 const ARMSubtarget *ST) {
7894 SDValue N0 = N->getOperand(0);
7895
7896 // Check for sign- and zero-extensions of vector extract operations of 8-
7897 // and 16-bit vector elements. NEON supports these directly. They are
7898 // handled during DAG combining because type legalization will promote them
7899 // to 32-bit types and it is messy to recognize the operations after that.
7900 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
7901 SDValue Vec = N0.getOperand(0);
7902 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00007903 EVT VT = N->getValueType(0);
7904 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007905 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7906
Owen Anderson825b72b2009-08-11 20:47:22 +00007907 if (VT == MVT::i32 &&
7908 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00007909 TLI.isTypeLegal(Vec.getValueType()) &&
7910 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00007911
7912 unsigned Opc = 0;
7913 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007914 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00007915 case ISD::SIGN_EXTEND:
7916 Opc = ARMISD::VGETLANEs;
7917 break;
7918 case ISD::ZERO_EXTEND:
7919 case ISD::ANY_EXTEND:
7920 Opc = ARMISD::VGETLANEu;
7921 break;
7922 }
7923 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
7924 }
7925 }
7926
7927 return SDValue();
7928}
7929
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007930/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
7931/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
7932static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
7933 const ARMSubtarget *ST) {
7934 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00007935 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007936 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
7937 // a NaN; only do the transformation when it matches that behavior.
7938
7939 // For now only do this when using NEON for FP operations; if using VFP, it
7940 // is not obvious that the benefit outweighs the cost of switching to the
7941 // NEON pipeline.
7942 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
7943 N->getValueType(0) != MVT::f32)
7944 return SDValue();
7945
7946 SDValue CondLHS = N->getOperand(0);
7947 SDValue CondRHS = N->getOperand(1);
7948 SDValue LHS = N->getOperand(2);
7949 SDValue RHS = N->getOperand(3);
7950 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
7951
7952 unsigned Opcode = 0;
7953 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00007954 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007955 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00007956 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007957 IsReversed = true ; // x CC y ? y : x
7958 } else {
7959 return SDValue();
7960 }
7961
Bob Wilsone742bb52010-02-24 22:15:53 +00007962 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007963 switch (CC) {
7964 default: break;
7965 case ISD::SETOLT:
7966 case ISD::SETOLE:
7967 case ISD::SETLT:
7968 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007969 case ISD::SETULT:
7970 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00007971 // If LHS is NaN, an ordered comparison will be false and the result will
7972 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
7973 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
7974 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
7975 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
7976 break;
7977 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
7978 // will return -0, so vmin can only be used for unsafe math or if one of
7979 // the operands is known to be nonzero.
7980 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00007981 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00007982 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
7983 break;
7984 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007985 break;
7986
7987 case ISD::SETOGT:
7988 case ISD::SETOGE:
7989 case ISD::SETGT:
7990 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007991 case ISD::SETUGT:
7992 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00007993 // If LHS is NaN, an ordered comparison will be false and the result will
7994 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
7995 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
7996 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
7997 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
7998 break;
7999 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
8000 // will return +0, so vmax can only be used for unsafe math or if one of
8001 // the operands is known to be nonzero.
8002 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008003 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00008004 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8005 break;
8006 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008007 break;
8008 }
8009
8010 if (!Opcode)
8011 return SDValue();
8012 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
8013}
8014
Evan Chenge721f5c2011-07-13 00:42:17 +00008015/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
8016SDValue
8017ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
8018 SDValue Cmp = N->getOperand(4);
8019 if (Cmp.getOpcode() != ARMISD::CMPZ)
8020 // Only looking at EQ and NE cases.
8021 return SDValue();
8022
8023 EVT VT = N->getValueType(0);
8024 DebugLoc dl = N->getDebugLoc();
8025 SDValue LHS = Cmp.getOperand(0);
8026 SDValue RHS = Cmp.getOperand(1);
8027 SDValue FalseVal = N->getOperand(0);
8028 SDValue TrueVal = N->getOperand(1);
8029 SDValue ARMcc = N->getOperand(2);
Jim Grosbachb04546f2011-09-13 20:30:37 +00008030 ARMCC::CondCodes CC =
8031 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chenge721f5c2011-07-13 00:42:17 +00008032
8033 // Simplify
8034 // mov r1, r0
8035 // cmp r1, x
8036 // mov r0, y
8037 // moveq r0, x
8038 // to
8039 // cmp r0, x
8040 // movne r0, y
8041 //
8042 // mov r1, r0
8043 // cmp r1, x
8044 // mov r0, x
8045 // movne r0, y
8046 // to
8047 // cmp r0, x
8048 // movne r0, y
8049 /// FIXME: Turn this into a target neutral optimization?
8050 SDValue Res;
Evan Cheng9b88d2d2011-09-28 23:16:31 +00008051 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chenge721f5c2011-07-13 00:42:17 +00008052 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
8053 N->getOperand(3), Cmp);
8054 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
8055 SDValue ARMcc;
8056 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
8057 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
8058 N->getOperand(3), NewCmp);
8059 }
8060
8061 if (Res.getNode()) {
8062 APInt KnownZero, KnownOne;
8063 APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits());
8064 DAG.ComputeMaskedBits(SDValue(N,0), Mask, KnownZero, KnownOne);
8065 // Capture demanded bits information that would be otherwise lost.
8066 if (KnownZero == 0xfffffffe)
8067 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8068 DAG.getValueType(MVT::i1));
8069 else if (KnownZero == 0xffffff00)
8070 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8071 DAG.getValueType(MVT::i8));
8072 else if (KnownZero == 0xffff0000)
8073 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8074 DAG.getValueType(MVT::i16));
8075 }
8076
8077 return Res;
8078}
8079
Dan Gohman475871a2008-07-27 21:46:04 +00008080SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008081 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008082 switch (N->getOpcode()) {
8083 default: break;
Tanya Lattner189531f2011-06-14 23:48:48 +00008084 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008085 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00008086 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008087 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00008088 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00008089 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00008090 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008091 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00008092 case ISD::STORE: return PerformSTORECombine(N, DCI);
8093 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
8094 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00008095 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008096 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00008097 case ISD::FP_TO_SINT:
8098 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
8099 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008100 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00008101 case ISD::SHL:
8102 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008103 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00008104 case ISD::SIGN_EXTEND:
8105 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008106 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
8107 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00008108 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00008109 case ARMISD::VLD2DUP:
8110 case ARMISD::VLD3DUP:
8111 case ARMISD::VLD4DUP:
8112 return CombineBaseUpdate(N, DCI);
8113 case ISD::INTRINSIC_VOID:
8114 case ISD::INTRINSIC_W_CHAIN:
8115 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
8116 case Intrinsic::arm_neon_vld1:
8117 case Intrinsic::arm_neon_vld2:
8118 case Intrinsic::arm_neon_vld3:
8119 case Intrinsic::arm_neon_vld4:
8120 case Intrinsic::arm_neon_vld2lane:
8121 case Intrinsic::arm_neon_vld3lane:
8122 case Intrinsic::arm_neon_vld4lane:
8123 case Intrinsic::arm_neon_vst1:
8124 case Intrinsic::arm_neon_vst2:
8125 case Intrinsic::arm_neon_vst3:
8126 case Intrinsic::arm_neon_vst4:
8127 case Intrinsic::arm_neon_vst2lane:
8128 case Intrinsic::arm_neon_vst3lane:
8129 case Intrinsic::arm_neon_vst4lane:
8130 return CombineBaseUpdate(N, DCI);
8131 default: break;
8132 }
8133 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008134 }
Dan Gohman475871a2008-07-27 21:46:04 +00008135 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008136}
8137
Evan Cheng31959b12011-02-02 01:06:55 +00008138bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
8139 EVT VT) const {
8140 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
8141}
8142
Bill Wendlingaf566342009-08-15 21:21:19 +00008143bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00008144 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00008145 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00008146
8147 switch (VT.getSimpleVT().SimpleTy) {
8148 default:
8149 return false;
8150 case MVT::i8:
8151 case MVT::i16:
8152 case MVT::i32:
8153 return true;
8154 // FIXME: VLD1 etc with standard alignment is legal.
8155 }
8156}
8157
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008158static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
8159 unsigned AlignCheck) {
8160 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
8161 (DstAlign == 0 || DstAlign % AlignCheck == 0));
8162}
8163
8164EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
8165 unsigned DstAlign, unsigned SrcAlign,
Lang Hamesa1e78882011-11-02 23:37:04 +00008166 bool IsZeroVal,
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008167 bool MemcpyStrSrc,
8168 MachineFunction &MF) const {
8169 const Function *F = MF.getFunction();
8170
8171 // See if we can use NEON instructions for this...
Lang Hamesa1e78882011-11-02 23:37:04 +00008172 if (IsZeroVal &&
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008173 !F->hasFnAttr(Attribute::NoImplicitFloat) &&
8174 Subtarget->hasNEON()) {
8175 if (memOpAlign(SrcAlign, DstAlign, 16) && Size >= 16) {
8176 return MVT::v4i32;
8177 } else if (memOpAlign(SrcAlign, DstAlign, 8) && Size >= 8) {
8178 return MVT::v2i32;
8179 }
8180 }
8181
Lang Hames5207bf22011-11-08 18:56:23 +00008182 // Lowering to i32/i16 if the size permits.
8183 if (Size >= 4) {
8184 return MVT::i32;
8185 } else if (Size >= 2) {
8186 return MVT::i16;
8187 }
8188
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008189 // Let the target-independent logic figure it out.
8190 return MVT::Other;
8191}
8192
Evan Chenge6c835f2009-08-14 20:09:37 +00008193static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
8194 if (V < 0)
8195 return false;
8196
8197 unsigned Scale = 1;
8198 switch (VT.getSimpleVT().SimpleTy) {
8199 default: return false;
8200 case MVT::i1:
8201 case MVT::i8:
8202 // Scale == 1;
8203 break;
8204 case MVT::i16:
8205 // Scale == 2;
8206 Scale = 2;
8207 break;
8208 case MVT::i32:
8209 // Scale == 4;
8210 Scale = 4;
8211 break;
8212 }
8213
8214 if ((V & (Scale - 1)) != 0)
8215 return false;
8216 V /= Scale;
8217 return V == (V & ((1LL << 5) - 1));
8218}
8219
8220static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
8221 const ARMSubtarget *Subtarget) {
8222 bool isNeg = false;
8223 if (V < 0) {
8224 isNeg = true;
8225 V = - V;
8226 }
8227
8228 switch (VT.getSimpleVT().SimpleTy) {
8229 default: return false;
8230 case MVT::i1:
8231 case MVT::i8:
8232 case MVT::i16:
8233 case MVT::i32:
8234 // + imm12 or - imm8
8235 if (isNeg)
8236 return V == (V & ((1LL << 8) - 1));
8237 return V == (V & ((1LL << 12) - 1));
8238 case MVT::f32:
8239 case MVT::f64:
8240 // Same as ARM mode. FIXME: NEON?
8241 if (!Subtarget->hasVFP2())
8242 return false;
8243 if ((V & 3) != 0)
8244 return false;
8245 V >>= 2;
8246 return V == (V & ((1LL << 8) - 1));
8247 }
8248}
8249
Evan Chengb01fad62007-03-12 23:30:29 +00008250/// isLegalAddressImmediate - Return true if the integer value can be used
8251/// as the offset of the target addressing mode for load / store of the
8252/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00008253static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00008254 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00008255 if (V == 0)
8256 return true;
8257
Evan Cheng65011532009-03-09 19:15:00 +00008258 if (!VT.isSimple())
8259 return false;
8260
Evan Chenge6c835f2009-08-14 20:09:37 +00008261 if (Subtarget->isThumb1Only())
8262 return isLegalT1AddressImmediate(V, VT);
8263 else if (Subtarget->isThumb2())
8264 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00008265
Evan Chenge6c835f2009-08-14 20:09:37 +00008266 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00008267 if (V < 0)
8268 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00008269 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00008270 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008271 case MVT::i1:
8272 case MVT::i8:
8273 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00008274 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008275 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008276 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00008277 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008278 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008279 case MVT::f32:
8280 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00008281 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00008282 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00008283 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00008284 return false;
8285 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008286 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00008287 }
Evan Chenga8e29892007-01-19 07:51:42 +00008288}
8289
Evan Chenge6c835f2009-08-14 20:09:37 +00008290bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
8291 EVT VT) const {
8292 int Scale = AM.Scale;
8293 if (Scale < 0)
8294 return false;
8295
8296 switch (VT.getSimpleVT().SimpleTy) {
8297 default: return false;
8298 case MVT::i1:
8299 case MVT::i8:
8300 case MVT::i16:
8301 case MVT::i32:
8302 if (Scale == 1)
8303 return true;
8304 // r + r << imm
8305 Scale = Scale & ~1;
8306 return Scale == 2 || Scale == 4 || Scale == 8;
8307 case MVT::i64:
8308 // r + r
8309 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
8310 return true;
8311 return false;
8312 case MVT::isVoid:
8313 // Note, we allow "void" uses (basically, uses that aren't loads or
8314 // stores), because arm allows folding a scale into many arithmetic
8315 // operations. This should be made more precise and revisited later.
8316
8317 // Allow r << imm, but the imm has to be a multiple of two.
8318 if (Scale & 1) return false;
8319 return isPowerOf2_32(Scale);
8320 }
8321}
8322
Chris Lattner37caf8c2007-04-09 23:33:39 +00008323/// isLegalAddressingMode - Return true if the addressing mode represented
8324/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008325bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008326 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008327 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00008328 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00008329 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008330
Chris Lattner37caf8c2007-04-09 23:33:39 +00008331 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008332 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008333 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008334
Chris Lattner37caf8c2007-04-09 23:33:39 +00008335 switch (AM.Scale) {
8336 case 0: // no scale reg, must be "r+i" or "r", or "i".
8337 break;
8338 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00008339 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00008340 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008341 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00008342 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008343 // ARM doesn't support any R+R*scale+imm addr modes.
8344 if (AM.BaseOffs)
8345 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008346
Bob Wilson2c7dab12009-04-08 17:55:28 +00008347 if (!VT.isSimple())
8348 return false;
8349
Evan Chenge6c835f2009-08-14 20:09:37 +00008350 if (Subtarget->isThumb2())
8351 return isLegalT2ScaledAddressingMode(AM, VT);
8352
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008353 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00008354 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00008355 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008356 case MVT::i1:
8357 case MVT::i8:
8358 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008359 if (Scale < 0) Scale = -Scale;
8360 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008361 return true;
8362 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00008363 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008364 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00008365 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00008366 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008367 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008368 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00008369 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008370
Owen Anderson825b72b2009-08-11 20:47:22 +00008371 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00008372 // Note, we allow "void" uses (basically, uses that aren't loads or
8373 // stores), because arm allows folding a scale into many arithmetic
8374 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008375
Chris Lattner37caf8c2007-04-09 23:33:39 +00008376 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00008377 if (Scale & 1) return false;
8378 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00008379 }
8380 break;
Evan Chengb01fad62007-03-12 23:30:29 +00008381 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00008382 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00008383}
8384
Evan Cheng77e47512009-11-11 19:05:52 +00008385/// isLegalICmpImmediate - Return true if the specified immediate is legal
8386/// icmp immediate, that is the target has icmp instructions which can compare
8387/// a register against the immediate without having to materialize the
8388/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00008389bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00008390 if (!Subtarget->isThumb())
8391 return ARM_AM::getSOImmVal(Imm) != -1;
8392 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00008393 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00008394 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00008395}
8396
Dan Gohmancca82142011-05-03 00:46:49 +00008397/// isLegalAddImmediate - Return true if the specified immediate is legal
8398/// add immediate, that is the target has add instructions which can add
8399/// a register with the immediate without having to materialize the
8400/// immediate into a register.
8401bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
8402 return ARM_AM::getSOImmVal(Imm) != -1;
8403}
8404
Owen Andersone50ed302009-08-10 22:56:29 +00008405static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00008406 bool isSEXTLoad, SDValue &Base,
8407 SDValue &Offset, bool &isInc,
8408 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00008409 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
8410 return false;
8411
Owen Anderson825b72b2009-08-11 20:47:22 +00008412 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00008413 // AddressingMode 3
8414 Base = Ptr->getOperand(0);
8415 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008416 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00008417 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008418 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00008419 isInc = false;
8420 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8421 return true;
8422 }
8423 }
8424 isInc = (Ptr->getOpcode() == ISD::ADD);
8425 Offset = Ptr->getOperand(1);
8426 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00008427 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00008428 // AddressingMode 2
8429 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008430 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00008431 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008432 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00008433 isInc = false;
8434 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8435 Base = Ptr->getOperand(0);
8436 return true;
8437 }
8438 }
8439
8440 if (Ptr->getOpcode() == ISD::ADD) {
8441 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00008442 ARM_AM::ShiftOpc ShOpcVal=
8443 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00008444 if (ShOpcVal != ARM_AM::no_shift) {
8445 Base = Ptr->getOperand(1);
8446 Offset = Ptr->getOperand(0);
8447 } else {
8448 Base = Ptr->getOperand(0);
8449 Offset = Ptr->getOperand(1);
8450 }
8451 return true;
8452 }
8453
8454 isInc = (Ptr->getOpcode() == ISD::ADD);
8455 Base = Ptr->getOperand(0);
8456 Offset = Ptr->getOperand(1);
8457 return true;
8458 }
8459
Jim Grosbache5165492009-11-09 00:11:35 +00008460 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00008461 return false;
8462}
8463
Owen Andersone50ed302009-08-10 22:56:29 +00008464static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00008465 bool isSEXTLoad, SDValue &Base,
8466 SDValue &Offset, bool &isInc,
8467 SelectionDAG &DAG) {
8468 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
8469 return false;
8470
8471 Base = Ptr->getOperand(0);
8472 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
8473 int RHSC = (int)RHS->getZExtValue();
8474 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
8475 assert(Ptr->getOpcode() == ISD::ADD);
8476 isInc = false;
8477 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8478 return true;
8479 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
8480 isInc = Ptr->getOpcode() == ISD::ADD;
8481 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
8482 return true;
8483 }
8484 }
8485
8486 return false;
8487}
8488
Evan Chenga8e29892007-01-19 07:51:42 +00008489/// getPreIndexedAddressParts - returns true by value, base pointer and
8490/// offset pointer and addressing mode by reference if the node's address
8491/// can be legally represented as pre-indexed load / store address.
8492bool
Dan Gohman475871a2008-07-27 21:46:04 +00008493ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
8494 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00008495 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00008496 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008497 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00008498 return false;
8499
Owen Andersone50ed302009-08-10 22:56:29 +00008500 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00008501 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00008502 bool isSEXTLoad = false;
8503 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
8504 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008505 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00008506 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
8507 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
8508 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008509 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00008510 } else
8511 return false;
8512
8513 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00008514 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00008515 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00008516 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
8517 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00008518 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00008519 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00008520 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00008521 if (!isLegal)
8522 return false;
8523
8524 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
8525 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00008526}
8527
8528/// getPostIndexedAddressParts - returns true by value, base pointer and
8529/// offset pointer and addressing mode by reference if this node can be
8530/// combined with a load / store to form a post-indexed load / store.
8531bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00008532 SDValue &Base,
8533 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00008534 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00008535 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008536 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00008537 return false;
8538
Owen Andersone50ed302009-08-10 22:56:29 +00008539 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00008540 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00008541 bool isSEXTLoad = false;
8542 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008543 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00008544 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00008545 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
8546 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008547 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00008548 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00008549 } else
8550 return false;
8551
8552 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00008553 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00008554 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00008555 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00008556 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00008557 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00008558 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
8559 isInc, DAG);
8560 if (!isLegal)
8561 return false;
8562
Evan Cheng28dad2a2010-05-18 21:31:17 +00008563 if (Ptr != Base) {
8564 // Swap base ptr and offset to catch more post-index load / store when
8565 // it's legal. In Thumb2 mode, offset must be an immediate.
8566 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
8567 !Subtarget->isThumb2())
8568 std::swap(Base, Offset);
8569
8570 // Post-indexed load / store update the base pointer.
8571 if (Ptr != Base)
8572 return false;
8573 }
8574
Evan Chenge88d5ce2009-07-02 07:28:31 +00008575 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
8576 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00008577}
8578
Dan Gohman475871a2008-07-27 21:46:04 +00008579void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00008580 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008581 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008582 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008583 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00008584 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008585 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00008586 switch (Op.getOpcode()) {
8587 default: break;
8588 case ARMISD::CMOV: {
8589 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00008590 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00008591 if (KnownZero == 0 && KnownOne == 0) return;
8592
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008593 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00008594 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
8595 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00008596 KnownZero &= KnownZeroRHS;
8597 KnownOne &= KnownOneRHS;
8598 return;
8599 }
8600 }
8601}
8602
8603//===----------------------------------------------------------------------===//
8604// ARM Inline Assembly Support
8605//===----------------------------------------------------------------------===//
8606
Evan Cheng55d42002011-01-08 01:24:27 +00008607bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
8608 // Looking for "rev" which is V6+.
8609 if (!Subtarget->hasV6Ops())
8610 return false;
8611
8612 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
8613 std::string AsmStr = IA->getAsmString();
8614 SmallVector<StringRef, 4> AsmPieces;
8615 SplitString(AsmStr, AsmPieces, ";\n");
8616
8617 switch (AsmPieces.size()) {
8618 default: return false;
8619 case 1:
8620 AsmStr = AsmPieces[0];
8621 AsmPieces.clear();
8622 SplitString(AsmStr, AsmPieces, " \t,");
8623
8624 // rev $0, $1
8625 if (AsmPieces.size() == 3 &&
8626 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
8627 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008628 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00008629 if (Ty && Ty->getBitWidth() == 32)
8630 return IntrinsicLowering::LowerToByteSwap(CI);
8631 }
8632 break;
8633 }
8634
8635 return false;
8636}
8637
Evan Chenga8e29892007-01-19 07:51:42 +00008638/// getConstraintType - Given a constraint letter, return the type of
8639/// constraint it is for this target.
8640ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00008641ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
8642 if (Constraint.size() == 1) {
8643 switch (Constraint[0]) {
8644 default: break;
8645 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008646 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00008647 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00008648 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008649 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00008650 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00008651 // An address with a single base register. Due to the way we
8652 // currently handle addresses it is the same as an 'r' memory constraint.
8653 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00008654 }
Eric Christopher1312ca82011-06-21 22:10:57 +00008655 } else if (Constraint.size() == 2) {
8656 switch (Constraint[0]) {
8657 default: break;
8658 // All 'U+' constraints are addresses.
8659 case 'U': return C_Memory;
8660 }
Evan Chenga8e29892007-01-19 07:51:42 +00008661 }
Chris Lattner4234f572007-03-25 02:14:49 +00008662 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00008663}
8664
John Thompson44ab89e2010-10-29 17:29:13 +00008665/// Examine constraint type and operand type and determine a weight value.
8666/// This object must already have been set up with the operand type
8667/// and the current alternative constraint selected.
8668TargetLowering::ConstraintWeight
8669ARMTargetLowering::getSingleConstraintMatchWeight(
8670 AsmOperandInfo &info, const char *constraint) const {
8671 ConstraintWeight weight = CW_Invalid;
8672 Value *CallOperandVal = info.CallOperandVal;
8673 // If we don't have a value, we can't do a match,
8674 // but allow it at the lowest weight.
8675 if (CallOperandVal == NULL)
8676 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008677 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00008678 // Look at the constraint type.
8679 switch (*constraint) {
8680 default:
8681 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
8682 break;
8683 case 'l':
8684 if (type->isIntegerTy()) {
8685 if (Subtarget->isThumb())
8686 weight = CW_SpecificReg;
8687 else
8688 weight = CW_Register;
8689 }
8690 break;
8691 case 'w':
8692 if (type->isFloatingPointTy())
8693 weight = CW_Register;
8694 break;
8695 }
8696 return weight;
8697}
8698
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008699typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
8700RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00008701ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00008702 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00008703 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00008704 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00008705 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00008706 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00008707 if (Subtarget->isThumb())
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008708 return RCPair(0U, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +00008709 else
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008710 return RCPair(0U, ARM::GPRRegisterClass);
Eric Christopher73744df2011-06-30 23:23:01 +00008711 case 'h': // High regs or no regs.
8712 if (Subtarget->isThumb())
Andrew Trick3af7a672011-09-20 03:06:13 +00008713 return RCPair(0U, ARM::hGPRRegisterClass);
Eric Christopher1070f822011-07-01 00:19:27 +00008714 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008715 case 'r':
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008716 return RCPair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008717 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00008718 if (VT == MVT::f32)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008719 return RCPair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00008720 if (VT.getSizeInBits() == 64)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008721 return RCPair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00008722 if (VT.getSizeInBits() == 128)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008723 return RCPair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008724 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00008725 case 'x':
8726 if (VT == MVT::f32)
Andrew Trick3af7a672011-09-20 03:06:13 +00008727 return RCPair(0U, ARM::SPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008728 if (VT.getSizeInBits() == 64)
Andrew Trick3af7a672011-09-20 03:06:13 +00008729 return RCPair(0U, ARM::DPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008730 if (VT.getSizeInBits() == 128)
Andrew Trick3af7a672011-09-20 03:06:13 +00008731 return RCPair(0U, ARM::QPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008732 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008733 case 't':
8734 if (VT == MVT::f32)
Andrew Trick3af7a672011-09-20 03:06:13 +00008735 return RCPair(0U, ARM::SPRRegisterClass);
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008736 break;
Evan Chenga8e29892007-01-19 07:51:42 +00008737 }
8738 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00008739 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00008740 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00008741
Evan Chenga8e29892007-01-19 07:51:42 +00008742 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
8743}
8744
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008745/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
8746/// vector. If it is invalid, don't add anything to Ops.
8747void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00008748 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008749 std::vector<SDValue>&Ops,
8750 SelectionDAG &DAG) const {
8751 SDValue Result(0, 0);
8752
Eric Christopher100c8332011-06-02 23:16:42 +00008753 // Currently only support length 1 constraints.
8754 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00008755
Eric Christopher100c8332011-06-02 23:16:42 +00008756 char ConstraintLetter = Constraint[0];
8757 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008758 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00008759 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008760 case 'I': case 'J': case 'K': case 'L':
8761 case 'M': case 'N': case 'O':
8762 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
8763 if (!C)
8764 return;
8765
8766 int64_t CVal64 = C->getSExtValue();
8767 int CVal = (int) CVal64;
8768 // None of these constraints allow values larger than 32 bits. Check
8769 // that the value fits in an int.
8770 if (CVal != CVal64)
8771 return;
8772
Eric Christopher100c8332011-06-02 23:16:42 +00008773 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00008774 case 'j':
Andrew Trick3af7a672011-09-20 03:06:13 +00008775 // Constant suitable for movw, must be between 0 and
8776 // 65535.
8777 if (Subtarget->hasV6T2Ops())
8778 if (CVal >= 0 && CVal <= 65535)
8779 break;
8780 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008781 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008782 if (Subtarget->isThumb1Only()) {
8783 // This must be a constant between 0 and 255, for ADD
8784 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008785 if (CVal >= 0 && CVal <= 255)
8786 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008787 } else if (Subtarget->isThumb2()) {
8788 // A constant that can be used as an immediate value in a
8789 // data-processing instruction.
8790 if (ARM_AM::getT2SOImmVal(CVal) != -1)
8791 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008792 } else {
8793 // A constant that can be used as an immediate value in a
8794 // data-processing instruction.
8795 if (ARM_AM::getSOImmVal(CVal) != -1)
8796 break;
8797 }
8798 return;
8799
8800 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008801 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008802 // This must be a constant between -255 and -1, for negated ADD
8803 // immediates. This can be used in GCC with an "n" modifier that
8804 // prints the negated value, for use with SUB instructions. It is
8805 // not useful otherwise but is implemented for compatibility.
8806 if (CVal >= -255 && CVal <= -1)
8807 break;
8808 } else {
8809 // This must be a constant between -4095 and 4095. It is not clear
8810 // what this constraint is intended for. Implemented for
8811 // compatibility with GCC.
8812 if (CVal >= -4095 && CVal <= 4095)
8813 break;
8814 }
8815 return;
8816
8817 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008818 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008819 // A 32-bit value where only one byte has a nonzero value. Exclude
8820 // zero to match GCC. This constraint is used by GCC internally for
8821 // constants that can be loaded with a move/shift combination.
8822 // It is not useful otherwise but is implemented for compatibility.
8823 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
8824 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008825 } else if (Subtarget->isThumb2()) {
8826 // A constant whose bitwise inverse can be used as an immediate
8827 // value in a data-processing instruction. This can be used in GCC
8828 // with a "B" modifier that prints the inverted value, for use with
8829 // BIC and MVN instructions. It is not useful otherwise but is
8830 // implemented for compatibility.
8831 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
8832 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008833 } else {
8834 // A constant whose bitwise inverse can be used as an immediate
8835 // value in a data-processing instruction. This can be used in GCC
8836 // with a "B" modifier that prints the inverted value, for use with
8837 // BIC and MVN instructions. It is not useful otherwise but is
8838 // implemented for compatibility.
8839 if (ARM_AM::getSOImmVal(~CVal) != -1)
8840 break;
8841 }
8842 return;
8843
8844 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008845 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008846 // This must be a constant between -7 and 7,
8847 // for 3-operand ADD/SUB immediate instructions.
8848 if (CVal >= -7 && CVal < 7)
8849 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008850 } else if (Subtarget->isThumb2()) {
8851 // A constant whose negation can be used as an immediate value in a
8852 // data-processing instruction. This can be used in GCC with an "n"
8853 // modifier that prints the negated value, for use with SUB
8854 // instructions. It is not useful otherwise but is implemented for
8855 // compatibility.
8856 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
8857 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008858 } else {
8859 // A constant whose negation can be used as an immediate value in a
8860 // data-processing instruction. This can be used in GCC with an "n"
8861 // modifier that prints the negated value, for use with SUB
8862 // instructions. It is not useful otherwise but is implemented for
8863 // compatibility.
8864 if (ARM_AM::getSOImmVal(-CVal) != -1)
8865 break;
8866 }
8867 return;
8868
8869 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008870 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008871 // This must be a multiple of 4 between 0 and 1020, for
8872 // ADD sp + immediate.
8873 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
8874 break;
8875 } else {
8876 // A power of two or a constant between 0 and 32. This is used in
8877 // GCC for the shift amount on shifted register operands, but it is
8878 // useful in general for any shift amounts.
8879 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
8880 break;
8881 }
8882 return;
8883
8884 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008885 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008886 // This must be a constant between 0 and 31, for shift amounts.
8887 if (CVal >= 0 && CVal <= 31)
8888 break;
8889 }
8890 return;
8891
8892 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008893 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008894 // This must be a multiple of 4 between -508 and 508, for
8895 // ADD/SUB sp = sp + immediate.
8896 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
8897 break;
8898 }
8899 return;
8900 }
8901 Result = DAG.getTargetConstant(CVal, Op.getValueType());
8902 break;
8903 }
8904
8905 if (Result.getNode()) {
8906 Ops.push_back(Result);
8907 return;
8908 }
Dale Johannesen1784d162010-06-25 21:55:36 +00008909 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008910}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00008911
8912bool
8913ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
8914 // The ARM target isn't yet aware of offsets.
8915 return false;
8916}
Evan Cheng39382422009-10-28 01:44:26 +00008917
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008918bool ARM::isBitFieldInvertedMask(unsigned v) {
8919 if (v == 0xffffffff)
8920 return 0;
8921 // there can be 1's on either or both "outsides", all the "inside"
8922 // bits must be 0's
8923 unsigned int lsb = 0, msb = 31;
8924 while (v & (1 << msb)) --msb;
8925 while (v & (1 << lsb)) ++lsb;
8926 for (unsigned int i = lsb; i <= msb; ++i) {
8927 if (v & (1 << i))
8928 return 0;
8929 }
8930 return 1;
8931}
8932
Evan Cheng39382422009-10-28 01:44:26 +00008933/// isFPImmLegal - Returns true if the target can instruction select the
8934/// specified FP immediate natively. If false, the legalizer will
8935/// materialize the FP immediate as a load from a constant pool.
8936bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
8937 if (!Subtarget->hasVFP3())
8938 return false;
8939 if (VT == MVT::f32)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00008940 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00008941 if (VT == MVT::f64)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00008942 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00008943 return false;
8944}
Bob Wilson65ffec42010-09-21 17:56:22 +00008945
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008946/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00008947/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
8948/// specified in the intrinsic calls.
8949bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
8950 const CallInst &I,
8951 unsigned Intrinsic) const {
8952 switch (Intrinsic) {
8953 case Intrinsic::arm_neon_vld1:
8954 case Intrinsic::arm_neon_vld2:
8955 case Intrinsic::arm_neon_vld3:
8956 case Intrinsic::arm_neon_vld4:
8957 case Intrinsic::arm_neon_vld2lane:
8958 case Intrinsic::arm_neon_vld3lane:
8959 case Intrinsic::arm_neon_vld4lane: {
8960 Info.opc = ISD::INTRINSIC_W_CHAIN;
8961 // Conservatively set memVT to the entire set of vectors loaded.
8962 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
8963 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
8964 Info.ptrVal = I.getArgOperand(0);
8965 Info.offset = 0;
8966 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
8967 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
8968 Info.vol = false; // volatile loads with NEON intrinsics not supported
8969 Info.readMem = true;
8970 Info.writeMem = false;
8971 return true;
8972 }
8973 case Intrinsic::arm_neon_vst1:
8974 case Intrinsic::arm_neon_vst2:
8975 case Intrinsic::arm_neon_vst3:
8976 case Intrinsic::arm_neon_vst4:
8977 case Intrinsic::arm_neon_vst2lane:
8978 case Intrinsic::arm_neon_vst3lane:
8979 case Intrinsic::arm_neon_vst4lane: {
8980 Info.opc = ISD::INTRINSIC_VOID;
8981 // Conservatively set memVT to the entire set of vectors stored.
8982 unsigned NumElts = 0;
8983 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008984 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +00008985 if (!ArgTy->isVectorTy())
8986 break;
8987 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
8988 }
8989 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
8990 Info.ptrVal = I.getArgOperand(0);
8991 Info.offset = 0;
8992 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
8993 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
8994 Info.vol = false; // volatile stores with NEON intrinsics not supported
8995 Info.readMem = false;
8996 Info.writeMem = true;
8997 return true;
8998 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00008999 case Intrinsic::arm_strexd: {
9000 Info.opc = ISD::INTRINSIC_W_CHAIN;
9001 Info.memVT = MVT::i64;
9002 Info.ptrVal = I.getArgOperand(2);
9003 Info.offset = 0;
9004 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009005 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009006 Info.readMem = false;
9007 Info.writeMem = true;
9008 return true;
9009 }
9010 case Intrinsic::arm_ldrexd: {
9011 Info.opc = ISD::INTRINSIC_W_CHAIN;
9012 Info.memVT = MVT::i64;
9013 Info.ptrVal = I.getArgOperand(0);
9014 Info.offset = 0;
9015 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009016 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009017 Info.readMem = true;
9018 Info.writeMem = false;
9019 return true;
9020 }
Bob Wilson65ffec42010-09-21 17:56:22 +00009021 default:
9022 break;
9023 }
9024
9025 return false;
9026}