blob: 8783ac09ee4abd0a8e91254733ea44308ba4d433 [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
Cameron Zwaricha86686e2011-06-10 20:59:24 +000075namespace llvm {
76 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);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000435 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000436 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000437 if (!Subtarget->isFPOnlySP())
438 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000439
Owen Anderson825b72b2009-08-11 20:47:22 +0000440 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000441 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000442
Eli Friedman9f1f26a2011-11-08 01:43:53 +0000443 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
444 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
445 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
446 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
447 setTruncStoreAction((MVT::SimpleValueType)VT,
448 (MVT::SimpleValueType)InnerVT, Expand);
449 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
450 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
451 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
452 }
453
Bob Wilson5bafff32009-06-22 23:27:02 +0000454 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000455 addDRTypeForNEON(MVT::v2f32);
456 addDRTypeForNEON(MVT::v8i8);
457 addDRTypeForNEON(MVT::v4i16);
458 addDRTypeForNEON(MVT::v2i32);
459 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000460
Owen Anderson825b72b2009-08-11 20:47:22 +0000461 addQRTypeForNEON(MVT::v4f32);
462 addQRTypeForNEON(MVT::v2f64);
463 addQRTypeForNEON(MVT::v16i8);
464 addQRTypeForNEON(MVT::v8i16);
465 addQRTypeForNEON(MVT::v4i32);
466 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000467
Bob Wilson74dc72e2009-09-15 23:55:57 +0000468 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
469 // neither Neon nor VFP support any arithmetic operations on it.
470 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
471 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
472 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
473 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
474 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
475 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000476 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Bob Wilson74dc72e2009-09-15 23:55:57 +0000477 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
478 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
479 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
480 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
481 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
482 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
483 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
484 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
485 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
486 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
487 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
488 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
489 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
490 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
491 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
492 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
493 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
494
Bob Wilson642b3292009-09-16 00:32:15 +0000495 // Neon does not support some operations on v1i64 and v2i64 types.
496 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000497 // Custom handling for some quad-vector types to detect VMULL.
498 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
499 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
500 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begeman7973f352011-02-11 20:53:29 +0000501 // Custom handling for some vector types to avoid expensive expansions
502 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
503 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
504 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
505 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000506 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
507 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000508 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
509 // a destination type that is wider than the source.
510 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
511 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000512
Bob Wilson1c3ef902011-02-07 17:43:21 +0000513 setTargetDAGCombine(ISD::INTRINSIC_VOID);
514 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000515 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
516 setTargetDAGCombine(ISD::SHL);
517 setTargetDAGCombine(ISD::SRL);
518 setTargetDAGCombine(ISD::SRA);
519 setTargetDAGCombine(ISD::SIGN_EXTEND);
520 setTargetDAGCombine(ISD::ZERO_EXTEND);
521 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000522 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000523 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000524 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000525 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
526 setTargetDAGCombine(ISD::STORE);
Chad Rosieref01edf2011-06-24 19:23:04 +0000527 setTargetDAGCombine(ISD::FP_TO_SINT);
528 setTargetDAGCombine(ISD::FP_TO_UINT);
529 setTargetDAGCombine(ISD::FDIV);
Nadav Rotem004a24b2011-10-15 20:03:12 +0000530
531 setLoadExtAction(ISD::EXTLOAD, MVT::v4i8, Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000532 }
533
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000534 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000535
536 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000537 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000538
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000539 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000540 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000541
Evan Chenga8e29892007-01-19 07:51:42 +0000542 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000543 if (!Subtarget->isThumb1Only()) {
544 for (unsigned im = (unsigned)ISD::PRE_INC;
545 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000546 setIndexedLoadAction(im, MVT::i1, Legal);
547 setIndexedLoadAction(im, MVT::i8, Legal);
548 setIndexedLoadAction(im, MVT::i16, Legal);
549 setIndexedLoadAction(im, MVT::i32, Legal);
550 setIndexedStoreAction(im, MVT::i1, Legal);
551 setIndexedStoreAction(im, MVT::i8, Legal);
552 setIndexedStoreAction(im, MVT::i16, Legal);
553 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000554 }
Evan Chenga8e29892007-01-19 07:51:42 +0000555 }
556
557 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000558 setOperationAction(ISD::MUL, MVT::i64, Expand);
559 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000560 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000561 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
562 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000563 }
Jim Grosbacha7603982011-07-01 21:12:19 +0000564 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
565 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopher2cc40132011-04-19 18:49:19 +0000566 setOperationAction(ISD::MULHS, MVT::i32, Expand);
567
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000568 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000569 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000570 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000571 setOperationAction(ISD::SRL, MVT::i64, Custom);
572 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000573
Evan Cheng342e3162011-08-30 01:34:54 +0000574 if (!Subtarget->isThumb1Only()) {
575 // FIXME: We should do this for Thumb1 as well.
576 setOperationAction(ISD::ADDC, MVT::i32, Custom);
577 setOperationAction(ISD::ADDE, MVT::i32, Custom);
578 setOperationAction(ISD::SUBC, MVT::i32, Custom);
579 setOperationAction(ISD::SUBE, MVT::i32, Custom);
580 }
581
Evan Chenga8e29892007-01-19 07:51:42 +0000582 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000583 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000584 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000585 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000586 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000587 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000588
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000589 // Only ARMv6 has BSWAP.
590 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000591 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000592
Evan Chenga8e29892007-01-19 07:51:42 +0000593 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000594 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000595 // v7M has a hardware divider
596 setOperationAction(ISD::SDIV, MVT::i32, Expand);
597 setOperationAction(ISD::UDIV, MVT::i32, Expand);
598 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000599 setOperationAction(ISD::SREM, MVT::i32, Expand);
600 setOperationAction(ISD::UREM, MVT::i32, Expand);
601 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
602 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000603
Owen Anderson825b72b2009-08-11 20:47:22 +0000604 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
605 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
606 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
607 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000608 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000609
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000610 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000611
Evan Chenga8e29892007-01-19 07:51:42 +0000612 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000613 setOperationAction(ISD::VASTART, MVT::Other, Custom);
614 setOperationAction(ISD::VAARG, MVT::Other, Expand);
615 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
616 setOperationAction(ISD::VAEND, MVT::Other, Expand);
617 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
618 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000619 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000620 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
621 setExceptionPointerRegister(ARM::R0);
622 setExceptionSelectorRegister(ARM::R1);
623
Evan Cheng3a1588a2010-04-15 22:20:34 +0000624 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000625 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
626 // the default expansion.
Eli Friedman4db5aca2011-08-29 18:23:02 +0000627 // FIXME: This should be checking for v6k, not just v6.
Evan Cheng11db0682010-08-11 06:22:01 +0000628 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000629 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000630 // membarrier needs custom lowering; the rest are legal and handled
631 // normally.
632 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000633 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Eli Friedman2bdffe42011-08-31 00:31:29 +0000634 // Custom lowering for 64-bit ops
635 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
636 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
637 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
638 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
639 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
640 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Eli Friedman4d3f3292011-08-31 17:52:22 +0000641 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Eli Friedman26689ac2011-08-03 21:06:02 +0000642 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
643 setInsertFencesForAtomic(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000644 } else {
645 // Set them all for expansion, which will force libcalls.
646 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000647 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000648 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000649 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000650 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000651 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000652 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000653 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000654 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000655 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000656 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000657 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000658 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000659 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedman7cc15662011-09-15 22:18:49 +0000660 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
661 // Unordered/Monotonic case.
662 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
663 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000664 // Since the libcalls include locking, fold in the fences
665 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000666 }
Evan Chenga8e29892007-01-19 07:51:42 +0000667
Evan Cheng416941d2010-11-04 05:19:35 +0000668 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000669
Eli Friedmana2c6f452010-06-26 04:36:50 +0000670 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
671 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000672 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
673 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000674 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000675 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000676
Nate Begemand1fb5832010-08-03 21:31:55 +0000677 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000678 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
679 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000680 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000681 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
682 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000683
684 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000685 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000686 if (Subtarget->isTargetDarwin()) {
687 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
688 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Jim Grosbache4ad3872010-10-19 23:27:08 +0000689 setOperationAction(ISD::EH_SJLJ_DISPATCHSETUP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000690 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000691 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000692
Owen Anderson825b72b2009-08-11 20:47:22 +0000693 setOperationAction(ISD::SETCC, MVT::i32, Expand);
694 setOperationAction(ISD::SETCC, MVT::f32, Expand);
695 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000696 setOperationAction(ISD::SELECT, MVT::i32, Custom);
697 setOperationAction(ISD::SELECT, MVT::f32, Custom);
698 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000699 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
700 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
701 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000702
Owen Anderson825b72b2009-08-11 20:47:22 +0000703 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
704 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
705 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
706 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
707 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000708
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000709 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000710 setOperationAction(ISD::FSIN, MVT::f64, Expand);
711 setOperationAction(ISD::FSIN, MVT::f32, Expand);
712 setOperationAction(ISD::FCOS, MVT::f32, Expand);
713 setOperationAction(ISD::FCOS, MVT::f64, Expand);
714 setOperationAction(ISD::FREM, MVT::f64, Expand);
715 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000716 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000717 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
718 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000719 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000720 setOperationAction(ISD::FPOW, MVT::f64, Expand);
721 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000722
Cameron Zwarich33390842011-07-08 21:39:21 +0000723 setOperationAction(ISD::FMA, MVT::f64, Expand);
724 setOperationAction(ISD::FMA, MVT::f32, Expand);
725
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000726 // Various VFP goodness
727 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000728 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
729 if (Subtarget->hasVFP2()) {
730 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
731 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
732 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
733 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
734 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000735 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000736 if (!Subtarget->hasFP16()) {
737 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
738 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000739 }
Evan Cheng110cf482008-04-01 01:50:16 +0000740 }
Evan Chenga8e29892007-01-19 07:51:42 +0000741
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000742 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000743 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000744 setTargetDAGCombine(ISD::ADD);
745 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000746 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000747
Owen Anderson080c0922010-11-05 19:27:46 +0000748 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON())
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000749 setTargetDAGCombine(ISD::OR);
Owen Anderson080c0922010-11-05 19:27:46 +0000750 if (Subtarget->hasNEON())
751 setTargetDAGCombine(ISD::AND);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000752
Evan Chenga8e29892007-01-19 07:51:42 +0000753 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000754
Evan Chengf7d87ee2010-05-21 00:43:17 +0000755 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
756 setSchedulingPreference(Sched::RegPressure);
757 else
758 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000759
Evan Cheng05219282011-01-06 06:52:41 +0000760 //// temporary - rewrite interface to use type
761 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Lang Hames75757f92011-10-26 20:56:52 +0000762 maxStoresPerMemset = 16;
763 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengf6799392010-06-26 01:52:05 +0000764
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000765 // On ARM arguments smaller than 4 bytes are extended, so all arguments
766 // are at least 4 bytes aligned.
767 setMinStackArgumentAlignment(4);
768
Evan Chengfff606d2010-09-24 19:07:23 +0000769 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000770
771 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000772}
773
Andrew Trick32cec0a2011-01-19 02:35:27 +0000774// FIXME: It might make sense to define the representative register class as the
775// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
776// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
777// SPR's representative would be DPR_VFP2. This should work well if register
778// pressure tracking were modified such that a register use would increment the
779// pressure of the register class's representative and all of it's super
780// classes' representatives transitively. We have not implemented this because
781// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000782// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000783// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000784std::pair<const TargetRegisterClass*, uint8_t>
785ARMTargetLowering::findRepresentativeClass(EVT VT) const{
786 const TargetRegisterClass *RRC = 0;
787 uint8_t Cost = 1;
788 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000789 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000790 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000791 // Use DPR as representative register class for all floating point
792 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
793 // the cost is 1 for both f32 and f64.
794 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000795 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000796 RRC = ARM::DPRRegisterClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000797 // When NEON is used for SP, only half of the register file is available
798 // because operations that define both SP and DP results will be constrained
799 // to the VFP2 class (D0-D15). We currently model this constraint prior to
800 // coalescing by double-counting the SP regs. See the FIXME above.
801 if (Subtarget->useNEONForSinglePrecisionFP())
802 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000803 break;
804 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
805 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000806 RRC = ARM::DPRRegisterClass;
807 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000808 break;
809 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000810 RRC = ARM::DPRRegisterClass;
811 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000812 break;
813 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000814 RRC = ARM::DPRRegisterClass;
815 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000816 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000817 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000818 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000819}
820
Evan Chenga8e29892007-01-19 07:51:42 +0000821const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
822 switch (Opcode) {
823 default: return 0;
824 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000825 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000826 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000827 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
828 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000829 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000830 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
831 case ARMISD::tCALL: return "ARMISD::tCALL";
832 case ARMISD::BRCOND: return "ARMISD::BRCOND";
833 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000834 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000835 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
836 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
837 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000838 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000839 case ARMISD::CMPFP: return "ARMISD::CMPFP";
840 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000841 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000842 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
843 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000844
Jim Grosbach3482c802010-01-18 19:58:49 +0000845 case ARMISD::RBIT: return "ARMISD::RBIT";
846
Bob Wilson76a312b2010-03-19 22:51:32 +0000847 case ARMISD::FTOSI: return "ARMISD::FTOSI";
848 case ARMISD::FTOUI: return "ARMISD::FTOUI";
849 case ARMISD::SITOF: return "ARMISD::SITOF";
850 case ARMISD::UITOF: return "ARMISD::UITOF";
851
Evan Chenga8e29892007-01-19 07:51:42 +0000852 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
853 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
854 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000855
Evan Cheng342e3162011-08-30 01:34:54 +0000856 case ARMISD::ADDC: return "ARMISD::ADDC";
857 case ARMISD::ADDE: return "ARMISD::ADDE";
858 case ARMISD::SUBC: return "ARMISD::SUBC";
859 case ARMISD::SUBE: return "ARMISD::SUBE";
860
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000861 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
862 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000863
Evan Chengc5942082009-10-28 06:55:03 +0000864 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
865 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
Jim Grosbache4ad3872010-10-19 23:27:08 +0000866 case ARMISD::EH_SJLJ_DISPATCHSETUP:return "ARMISD::EH_SJLJ_DISPATCHSETUP";
Evan Chengc5942082009-10-28 06:55:03 +0000867
Dale Johannesen51e28e62010-06-03 21:09:53 +0000868 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000869
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000870 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000871
Evan Cheng86198642009-08-07 00:34:42 +0000872 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
873
Jim Grosbach3728e962009-12-10 00:11:09 +0000874 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000875 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000876
Evan Chengdfed19f2010-11-03 06:34:55 +0000877 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
878
Bob Wilson5bafff32009-06-22 23:27:02 +0000879 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000880 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000881 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000882 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
883 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000884 case ARMISD::VCGEU: return "ARMISD::VCGEU";
885 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000886 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
887 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000888 case ARMISD::VCGTU: return "ARMISD::VCGTU";
889 case ARMISD::VTST: return "ARMISD::VTST";
890
891 case ARMISD::VSHL: return "ARMISD::VSHL";
892 case ARMISD::VSHRs: return "ARMISD::VSHRs";
893 case ARMISD::VSHRu: return "ARMISD::VSHRu";
894 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
895 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
896 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
897 case ARMISD::VSHRN: return "ARMISD::VSHRN";
898 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
899 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
900 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
901 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
902 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
903 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
904 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
905 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
906 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
907 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
908 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
909 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
910 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
911 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000912 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000913 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Chengeaa192a2011-11-15 02:12:34 +0000914 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000915 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000916 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000917 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000918 case ARMISD::VREV64: return "ARMISD::VREV64";
919 case ARMISD::VREV32: return "ARMISD::VREV32";
920 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000921 case ARMISD::VZIP: return "ARMISD::VZIP";
922 case ARMISD::VUZP: return "ARMISD::VUZP";
923 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000924 case ARMISD::VTBL1: return "ARMISD::VTBL1";
925 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000926 case ARMISD::VMULLs: return "ARMISD::VMULLs";
927 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000928 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000929 case ARMISD::FMAX: return "ARMISD::FMAX";
930 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000931 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000932 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
933 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000934 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000935 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
936 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
937 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +0000938 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
939 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
940 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
941 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
942 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
943 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
944 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
945 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
946 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
947 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
948 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
949 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
950 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
951 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
952 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
953 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
954 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +0000955 }
956}
957
Duncan Sands28b77e92011-09-06 19:07:46 +0000958EVT ARMTargetLowering::getSetCCResultType(EVT VT) const {
959 if (!VT.isVector()) return getPointerTy();
960 return VT.changeVectorElementTypeToInteger();
961}
962
Evan Cheng06b666c2010-05-15 02:18:07 +0000963/// getRegClassFor - Return the register class that should be used for the
964/// specified value type.
965TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
966 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
967 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
968 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000969 if (Subtarget->hasNEON()) {
970 if (VT == MVT::v4i64)
971 return ARM::QQPRRegisterClass;
972 else if (VT == MVT::v8i64)
973 return ARM::QQQQPRRegisterClass;
974 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000975 return TargetLowering::getRegClassFor(VT);
976}
977
Eric Christopherab695882010-07-21 22:26:11 +0000978// Create a fast isel object.
979FastISel *
980ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
981 return ARM::createFastISel(funcInfo);
982}
983
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000984/// getMaximalGlobalOffset - Returns the maximal possible offset which can
985/// be used for loads / stores from the global.
986unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
987 return (Subtarget->isThumb1Only() ? 127 : 4095);
988}
989
Evan Cheng1cc39842010-05-20 23:26:43 +0000990Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000991 unsigned NumVals = N->getNumValues();
992 if (!NumVals)
993 return Sched::RegPressure;
994
995 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000996 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000997 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +0000998 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +0000999 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman692c1d82011-10-24 17:55:11 +00001000 return Sched::ILP;
Evan Cheng1cc39842010-05-20 23:26:43 +00001001 }
Evan Chengc10f5432010-05-28 23:25:23 +00001002
1003 if (!N->isMachineOpcode())
1004 return Sched::RegPressure;
1005
1006 // Load are scheduled for latency even if there instruction itinerary
1007 // is not available.
1008 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +00001009 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +00001010
Evan Chenge837dea2011-06-28 19:10:37 +00001011 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +00001012 return Sched::RegPressure;
1013 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +00001014 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman692c1d82011-10-24 17:55:11 +00001015 return Sched::ILP;
Evan Chengc10f5432010-05-28 23:25:23 +00001016
Evan Cheng1cc39842010-05-20 23:26:43 +00001017 return Sched::RegPressure;
1018}
1019
Evan Chenga8e29892007-01-19 07:51:42 +00001020//===----------------------------------------------------------------------===//
1021// Lowering Code
1022//===----------------------------------------------------------------------===//
1023
Evan Chenga8e29892007-01-19 07:51:42 +00001024/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1025static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1026 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001027 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +00001028 case ISD::SETNE: return ARMCC::NE;
1029 case ISD::SETEQ: return ARMCC::EQ;
1030 case ISD::SETGT: return ARMCC::GT;
1031 case ISD::SETGE: return ARMCC::GE;
1032 case ISD::SETLT: return ARMCC::LT;
1033 case ISD::SETLE: return ARMCC::LE;
1034 case ISD::SETUGT: return ARMCC::HI;
1035 case ISD::SETUGE: return ARMCC::HS;
1036 case ISD::SETULT: return ARMCC::LO;
1037 case ISD::SETULE: return ARMCC::LS;
1038 }
1039}
1040
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001041/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1042static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001043 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001044 CondCode2 = ARMCC::AL;
1045 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001046 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001047 case ISD::SETEQ:
1048 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1049 case ISD::SETGT:
1050 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1051 case ISD::SETGE:
1052 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1053 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001054 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001055 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1056 case ISD::SETO: CondCode = ARMCC::VC; break;
1057 case ISD::SETUO: CondCode = ARMCC::VS; break;
1058 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1059 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1060 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1061 case ISD::SETLT:
1062 case ISD::SETULT: CondCode = ARMCC::LT; break;
1063 case ISD::SETLE:
1064 case ISD::SETULE: CondCode = ARMCC::LE; break;
1065 case ISD::SETNE:
1066 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1067 }
Evan Chenga8e29892007-01-19 07:51:42 +00001068}
1069
Bob Wilson1f595bb2009-04-17 19:07:39 +00001070//===----------------------------------------------------------------------===//
1071// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001072//===----------------------------------------------------------------------===//
1073
1074#include "ARMGenCallingConv.inc"
1075
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001076/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1077/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001078CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001079 bool Return,
1080 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001081 switch (CC) {
1082 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001083 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001084 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001085 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001086 if (!Subtarget->isAAPCS_ABI())
1087 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1088 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1089 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1090 }
1091 // Fallthrough
1092 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001093 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001094 if (!Subtarget->isAAPCS_ABI())
1095 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1096 else if (Subtarget->hasVFP2() &&
1097 FloatABIType == FloatABI::Hard && !isVarArg)
1098 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1099 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1100 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001101 case CallingConv::ARM_AAPCS_VFP:
Evan Cheng76f920d2010-10-22 18:23:05 +00001102 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001103 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001104 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001105 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001106 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001107 }
1108}
1109
Dan Gohman98ca4f22009-08-05 01:29:28 +00001110/// LowerCallResult - Lower the result values of a call into the
1111/// appropriate copies out of appropriate physical registers.
1112SDValue
1113ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001114 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001115 const SmallVectorImpl<ISD::InputArg> &Ins,
1116 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001117 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001118
Bob Wilson1f595bb2009-04-17 19:07:39 +00001119 // Assign locations to each value returned by this call.
1120 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001121 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1122 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001123 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001124 CCAssignFnForNode(CallConv, /* Return*/ true,
1125 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001126
1127 // Copy all of the result registers out of their specified physreg.
1128 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1129 CCValAssign VA = RVLocs[i];
1130
Bob Wilson80915242009-04-25 00:33:20 +00001131 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001132 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001133 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001134 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001135 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001136 Chain = Lo.getValue(1);
1137 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001138 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001139 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001140 InFlag);
1141 Chain = Hi.getValue(1);
1142 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001143 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001144
Owen Anderson825b72b2009-08-11 20:47:22 +00001145 if (VA.getLocVT() == MVT::v2f64) {
1146 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1147 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1148 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001149
1150 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001151 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001152 Chain = Lo.getValue(1);
1153 InFlag = Lo.getValue(2);
1154 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001155 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001156 Chain = Hi.getValue(1);
1157 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001158 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001159 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1160 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001161 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001162 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001163 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1164 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001165 Chain = Val.getValue(1);
1166 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001167 }
Bob Wilson80915242009-04-25 00:33:20 +00001168
1169 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001170 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001171 case CCValAssign::Full: break;
1172 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001173 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001174 break;
1175 }
1176
Dan Gohman98ca4f22009-08-05 01:29:28 +00001177 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001178 }
1179
Dan Gohman98ca4f22009-08-05 01:29:28 +00001180 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001181}
1182
Bob Wilsondee46d72009-04-17 20:35:10 +00001183/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001184SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001185ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1186 SDValue StackPtr, SDValue Arg,
1187 DebugLoc dl, SelectionDAG &DAG,
1188 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001189 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001190 unsigned LocMemOffset = VA.getLocMemOffset();
1191 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1192 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001193 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001194 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001195 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001196}
1197
Dan Gohman98ca4f22009-08-05 01:29:28 +00001198void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001199 SDValue Chain, SDValue &Arg,
1200 RegsToPassVector &RegsToPass,
1201 CCValAssign &VA, CCValAssign &NextVA,
1202 SDValue &StackPtr,
1203 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001204 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001205
Jim Grosbache5165492009-11-09 00:11:35 +00001206 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001207 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001208 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1209
1210 if (NextVA.isRegLoc())
1211 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1212 else {
1213 assert(NextVA.isMemLoc());
1214 if (StackPtr.getNode() == 0)
1215 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1216
Dan Gohman98ca4f22009-08-05 01:29:28 +00001217 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1218 dl, DAG, NextVA,
1219 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001220 }
1221}
1222
Dan Gohman98ca4f22009-08-05 01:29:28 +00001223/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001224/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1225/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001226SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001227ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001228 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001229 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001230 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001231 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001232 const SmallVectorImpl<ISD::InputArg> &Ins,
1233 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001234 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001235 MachineFunction &MF = DAG.getMachineFunction();
1236 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1237 bool IsSibCall = false;
Bob Wilson6d2f9ce2011-10-07 17:17:49 +00001238 // Disable tail calls if they're not supported.
1239 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Bob Wilson703af3a2010-08-13 22:43:33 +00001240 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001241 if (isTailCall) {
1242 // Check if it's really possible to do a tail call.
1243 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1244 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001245 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001246 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1247 // detected sibcalls.
1248 if (isTailCall) {
1249 ++NumTailCalls;
1250 IsSibCall = true;
1251 }
1252 }
Evan Chenga8e29892007-01-19 07:51:42 +00001253
Bob Wilson1f595bb2009-04-17 19:07:39 +00001254 // Analyze operands of the call, assigning locations to each operand.
1255 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001256 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1257 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001258 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001259 CCAssignFnForNode(CallConv, /* Return*/ false,
1260 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001261
Bob Wilson1f595bb2009-04-17 19:07:39 +00001262 // Get a count of how many bytes are to be pushed on the stack.
1263 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001264
Dale Johannesen51e28e62010-06-03 21:09:53 +00001265 // For tail calls, memory operands are available in our caller's stack.
1266 if (IsSibCall)
1267 NumBytes = 0;
1268
Evan Chenga8e29892007-01-19 07:51:42 +00001269 // Adjust the stack pointer for the new arguments...
1270 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001271 if (!IsSibCall)
1272 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001273
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001274 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001275
Bob Wilson5bafff32009-06-22 23:27:02 +00001276 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001277 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001278
Bob Wilson1f595bb2009-04-17 19:07:39 +00001279 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001280 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001281 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1282 i != e;
1283 ++i, ++realArgIdx) {
1284 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001285 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001286 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001287 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001288
Bob Wilson1f595bb2009-04-17 19:07:39 +00001289 // Promote the value if needed.
1290 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001291 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001292 case CCValAssign::Full: break;
1293 case CCValAssign::SExt:
1294 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1295 break;
1296 case CCValAssign::ZExt:
1297 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1298 break;
1299 case CCValAssign::AExt:
1300 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1301 break;
1302 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001303 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001304 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001305 }
1306
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001307 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001308 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001309 if (VA.getLocVT() == MVT::v2f64) {
1310 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1311 DAG.getConstant(0, MVT::i32));
1312 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1313 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001314
Dan Gohman98ca4f22009-08-05 01:29:28 +00001315 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001316 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1317
1318 VA = ArgLocs[++i]; // skip ahead to next loc
1319 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001320 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001321 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1322 } else {
1323 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001324
Dan Gohman98ca4f22009-08-05 01:29:28 +00001325 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1326 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001327 }
1328 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001329 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001330 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001331 }
1332 } else if (VA.isRegLoc()) {
1333 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001334 } else if (isByVal) {
1335 assert(VA.isMemLoc());
1336 unsigned offset = 0;
1337
1338 // True if this byval aggregate will be split between registers
1339 // and memory.
1340 if (CCInfo.isFirstByValRegValid()) {
1341 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1342 unsigned int i, j;
1343 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1344 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1345 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1346 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1347 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001348 false, false, false, 0);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001349 MemOpChains.push_back(Load.getValue(1));
1350 RegsToPass.push_back(std::make_pair(j, Load));
1351 }
1352 offset = ARM::R4 - CCInfo.getFirstByValReg();
1353 CCInfo.clearFirstByValReg();
1354 }
1355
1356 unsigned LocMemOffset = VA.getLocMemOffset();
1357 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1358 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1359 StkPtrOff);
1360 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1361 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1362 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1363 MVT::i32);
1364 MemOpChains.push_back(DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode,
1365 Flags.getByValAlign(),
1366 /*isVolatile=*/false,
Dan Gohman65fd6562011-11-03 21:49:52 +00001367 /*AlwaysInline=*/false,
Stuart Hastingsc7315872011-04-20 16:47:52 +00001368 MachinePointerInfo(0),
1369 MachinePointerInfo(0)));
1370
1371 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001372 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001373
Dan Gohman98ca4f22009-08-05 01:29:28 +00001374 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1375 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001376 }
Evan Chenga8e29892007-01-19 07:51:42 +00001377 }
1378
1379 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001380 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001381 &MemOpChains[0], MemOpChains.size());
1382
1383 // Build a sequence of copy-to-reg nodes chained together with token chain
1384 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001385 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001386 // Tail call byval lowering might overwrite argument registers so in case of
1387 // tail call optimization the copies to registers are lowered later.
1388 if (!isTailCall)
1389 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1390 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1391 RegsToPass[i].second, InFlag);
1392 InFlag = Chain.getValue(1);
1393 }
Evan Chenga8e29892007-01-19 07:51:42 +00001394
Dale Johannesen51e28e62010-06-03 21:09:53 +00001395 // For tail calls lower the arguments to the 'real' stack slot.
1396 if (isTailCall) {
1397 // Force all the incoming stack arguments to be loaded from the stack
1398 // before any new outgoing arguments are stored to the stack, because the
1399 // outgoing stack slots may alias the incoming argument stack slots, and
1400 // the alias isn't otherwise explicit. This is slightly more conservative
1401 // than necessary, because it means that each store effectively depends
1402 // on every argument instead of just those arguments it would clobber.
1403
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001404 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001405 InFlag = SDValue();
1406 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1407 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1408 RegsToPass[i].second, InFlag);
1409 InFlag = Chain.getValue(1);
1410 }
1411 InFlag =SDValue();
1412 }
1413
Bill Wendling056292f2008-09-16 21:48:12 +00001414 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1415 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1416 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001417 bool isDirect = false;
1418 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001419 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001420 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001421
1422 if (EnableARMLongCalls) {
1423 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1424 && "long-calls with non-static relocation model!");
1425 // Handle a global address or an external symbol. If it's not one of
1426 // those, the target's already in a register, so we don't need to do
1427 // anything extra.
1428 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001429 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001430 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001431 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001432 ARMConstantPoolValue *CPV =
1433 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1434
Jim Grosbache7b52522010-04-14 22:28:31 +00001435 // Get the address of the callee into a register
1436 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1437 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1438 Callee = DAG.getLoad(getPointerTy(), dl,
1439 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001440 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001441 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001442 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1443 const char *Sym = S->getSymbol();
1444
1445 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001446 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001447 ARMConstantPoolValue *CPV =
1448 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1449 ARMPCLabelIndex, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001450 // Get the address of the callee into a register
1451 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1452 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1453 Callee = DAG.getLoad(getPointerTy(), dl,
1454 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001455 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001456 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001457 }
1458 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001459 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001460 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001461 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001462 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001463 getTargetMachine().getRelocationModel() != Reloc::Static;
1464 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001465 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001466 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001467 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001468 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001469 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001470 ARMConstantPoolValue *CPV =
1471 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001472 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001473 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001474 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001475 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001476 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001477 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001478 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001479 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001480 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001481 } else {
1482 // On ELF targets for PIC code, direct calls should go through the PLT
1483 unsigned OpFlags = 0;
1484 if (Subtarget->isTargetELF() &&
1485 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1486 OpFlags = ARMII::MO_PLT;
1487 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1488 }
Bill Wendling056292f2008-09-16 21:48:12 +00001489 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001490 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001491 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001492 getTargetMachine().getRelocationModel() != Reloc::Static;
1493 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001494 // tBX takes a register source operand.
1495 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001496 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001497 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001498 ARMConstantPoolValue *CPV =
1499 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1500 ARMPCLabelIndex, 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);
Dale Johannesen33c960f2009-02-04 20:06:27 +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 unsigned OpFlags = 0;
1512 // On ELF targets for PIC code, direct calls should go through the PLT
1513 if (Subtarget->isTargetELF() &&
1514 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1515 OpFlags = ARMII::MO_PLT;
1516 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1517 }
Evan Chenga8e29892007-01-19 07:51:42 +00001518 }
1519
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001520 // FIXME: handle tail calls differently.
1521 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001522 if (Subtarget->isThumb()) {
1523 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001524 CallOpc = ARMISD::CALL_NOLINK;
1525 else
1526 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1527 } else {
1528 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001529 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1530 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001531 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001532
Dan Gohman475871a2008-07-27 21:46:04 +00001533 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001534 Ops.push_back(Chain);
1535 Ops.push_back(Callee);
1536
1537 // Add argument registers to the end of the list so that they are known live
1538 // into the call.
1539 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1540 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1541 RegsToPass[i].second.getValueType()));
1542
Gabor Greifba36cb52008-08-28 21:40:38 +00001543 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001544 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001545
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001546 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001547 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001548 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001549
Duncan Sands4bdcb612008-07-02 17:40:58 +00001550 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001551 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001552 InFlag = Chain.getValue(1);
1553
Chris Lattnere563bbc2008-10-11 22:08:30 +00001554 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1555 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001556 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001557 InFlag = Chain.getValue(1);
1558
Bob Wilson1f595bb2009-04-17 19:07:39 +00001559 // Handle result values, copying them out of physregs into vregs that we
1560 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001561 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1562 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001563}
1564
Stuart Hastingsf222e592011-02-28 17:17:53 +00001565/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001566/// on the stack. Remember the next parameter register to allocate,
1567/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001568/// this.
1569void
Stuart Hastingsc7315872011-04-20 16:47:52 +00001570llvm::ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
1571 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1572 assert((State->getCallOrPrologue() == Prologue ||
1573 State->getCallOrPrologue() == Call) &&
1574 "unhandled ParmContext");
1575 if ((!State->isFirstByValRegValid()) &&
1576 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1577 State->setFirstByValReg(reg);
1578 // At a call site, a byval parameter that is split between
1579 // registers and memory needs its size truncated here. In a
1580 // function prologue, such byval parameters are reassembled in
1581 // memory, and are not truncated.
1582 if (State->getCallOrPrologue() == Call) {
1583 unsigned excess = 4 * (ARM::R4 - reg);
1584 assert(size >= excess && "expected larger existing stack allocation");
1585 size -= excess;
1586 }
1587 }
1588 // Confiscate any remaining parameter registers to preclude their
1589 // assignment to subsequent parameters.
1590 while (State->AllocateReg(GPRArgRegs, 4))
1591 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001592}
1593
Dale Johannesen51e28e62010-06-03 21:09:53 +00001594/// MatchingStackOffset - Return true if the given stack call argument is
1595/// already available in the same position (relatively) of the caller's
1596/// incoming argument stack.
1597static
1598bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1599 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1600 const ARMInstrInfo *TII) {
1601 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1602 int FI = INT_MAX;
1603 if (Arg.getOpcode() == ISD::CopyFromReg) {
1604 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001605 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001606 return false;
1607 MachineInstr *Def = MRI->getVRegDef(VR);
1608 if (!Def)
1609 return false;
1610 if (!Flags.isByVal()) {
1611 if (!TII->isLoadFromStackSlot(Def, FI))
1612 return false;
1613 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001614 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001615 }
1616 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1617 if (Flags.isByVal())
1618 // ByVal argument is passed in as a pointer but it's now being
1619 // dereferenced. e.g.
1620 // define @foo(%struct.X* %A) {
1621 // tail call @bar(%struct.X* byval %A)
1622 // }
1623 return false;
1624 SDValue Ptr = Ld->getBasePtr();
1625 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1626 if (!FINode)
1627 return false;
1628 FI = FINode->getIndex();
1629 } else
1630 return false;
1631
1632 assert(FI != INT_MAX);
1633 if (!MFI->isFixedObjectIndex(FI))
1634 return false;
1635 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1636}
1637
1638/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1639/// for tail call optimization. Targets which want to do tail call
1640/// optimization should implement this function.
1641bool
1642ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1643 CallingConv::ID CalleeCC,
1644 bool isVarArg,
1645 bool isCalleeStructRet,
1646 bool isCallerStructRet,
1647 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001648 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001649 const SmallVectorImpl<ISD::InputArg> &Ins,
1650 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001651 const Function *CallerF = DAG.getMachineFunction().getFunction();
1652 CallingConv::ID CallerCC = CallerF->getCallingConv();
1653 bool CCMatch = CallerCC == CalleeCC;
1654
1655 // Look for obvious safe cases to perform tail call optimization that do not
1656 // require ABI changes. This is what gcc calls sibcall.
1657
Jim Grosbach7616b642010-06-16 23:45:49 +00001658 // Do not sibcall optimize vararg calls unless the call site is not passing
1659 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001660 if (isVarArg && !Outs.empty())
1661 return false;
1662
1663 // Also avoid sibcall optimization if either caller or callee uses struct
1664 // return semantics.
1665 if (isCalleeStructRet || isCallerStructRet)
1666 return false;
1667
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001668 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001669 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1670 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1671 // support in the assembler and linker to be used. This would need to be
1672 // fixed to fully support tail calls in Thumb1.
1673 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001674 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1675 // LR. This means if we need to reload LR, it takes an extra instructions,
1676 // which outweighs the value of the tail call; but here we don't know yet
1677 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001678 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001679 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001680
1681 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1682 // but we need to make sure there are enough registers; the only valid
1683 // registers are the 4 used for parameters. We don't currently do this
1684 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001685 if (Subtarget->isThumb1Only())
1686 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001687
Dale Johannesen51e28e62010-06-03 21:09:53 +00001688 // If the calling conventions do not match, then we'd better make sure the
1689 // results are returned in the same way as what the caller expects.
1690 if (!CCMatch) {
1691 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001692 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1693 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001694 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1695
1696 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001697 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1698 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001699 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1700
1701 if (RVLocs1.size() != RVLocs2.size())
1702 return false;
1703 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1704 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1705 return false;
1706 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1707 return false;
1708 if (RVLocs1[i].isRegLoc()) {
1709 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1710 return false;
1711 } else {
1712 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1713 return false;
1714 }
1715 }
1716 }
1717
1718 // If the callee takes no arguments then go on to check the results of the
1719 // call.
1720 if (!Outs.empty()) {
1721 // Check if stack adjustment is needed. For now, do not do this if any
1722 // argument is passed on the stack.
1723 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001724 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1725 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001726 CCInfo.AnalyzeCallOperands(Outs,
1727 CCAssignFnForNode(CalleeCC, false, isVarArg));
1728 if (CCInfo.getNextStackOffset()) {
1729 MachineFunction &MF = DAG.getMachineFunction();
1730
1731 // Check if the arguments are already laid out in the right way as
1732 // the caller's fixed stack objects.
1733 MachineFrameInfo *MFI = MF.getFrameInfo();
1734 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1735 const ARMInstrInfo *TII =
1736 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001737 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1738 i != e;
1739 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001740 CCValAssign &VA = ArgLocs[i];
1741 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001742 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001743 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001744 if (VA.getLocInfo() == CCValAssign::Indirect)
1745 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001746 if (VA.needsCustom()) {
1747 // f64 and vector types are split into multiple registers or
1748 // register/stack-slot combinations. The types will not match
1749 // the registers; give up on memory f64 refs until we figure
1750 // out what to do about this.
1751 if (!VA.isRegLoc())
1752 return false;
1753 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001754 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001755 if (RegVT == MVT::v2f64) {
1756 if (!ArgLocs[++i].isRegLoc())
1757 return false;
1758 if (!ArgLocs[++i].isRegLoc())
1759 return false;
1760 }
1761 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001762 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1763 MFI, MRI, TII))
1764 return false;
1765 }
1766 }
1767 }
1768 }
1769
1770 return true;
1771}
1772
Dan Gohman98ca4f22009-08-05 01:29:28 +00001773SDValue
1774ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001775 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001776 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001777 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001778 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001779
Bob Wilsondee46d72009-04-17 20:35:10 +00001780 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001781 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001782
Bob Wilsondee46d72009-04-17 20:35:10 +00001783 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001784 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1785 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001786
Dan Gohman98ca4f22009-08-05 01:29:28 +00001787 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001788 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1789 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001790
1791 // If this is the first return lowered for this function, add
1792 // the regs to the liveout set for the function.
1793 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1794 for (unsigned i = 0; i != RVLocs.size(); ++i)
1795 if (RVLocs[i].isRegLoc())
1796 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001797 }
1798
Bob Wilson1f595bb2009-04-17 19:07:39 +00001799 SDValue Flag;
1800
1801 // Copy the result values into the output registers.
1802 for (unsigned i = 0, realRVLocIdx = 0;
1803 i != RVLocs.size();
1804 ++i, ++realRVLocIdx) {
1805 CCValAssign &VA = RVLocs[i];
1806 assert(VA.isRegLoc() && "Can only return in registers!");
1807
Dan Gohmanc9403652010-07-07 15:54:55 +00001808 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001809
1810 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001811 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001812 case CCValAssign::Full: break;
1813 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001814 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001815 break;
1816 }
1817
Bob Wilson1f595bb2009-04-17 19:07:39 +00001818 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001819 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001820 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001821 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1822 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001823 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001824 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001825
1826 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1827 Flag = Chain.getValue(1);
1828 VA = RVLocs[++i]; // skip ahead to next loc
1829 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1830 HalfGPRs.getValue(1), Flag);
1831 Flag = Chain.getValue(1);
1832 VA = RVLocs[++i]; // skip ahead to next loc
1833
1834 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001835 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1836 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001837 }
1838 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1839 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001840 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001841 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001842 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001843 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001844 VA = RVLocs[++i]; // skip ahead to next loc
1845 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1846 Flag);
1847 } else
1848 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1849
Bob Wilsondee46d72009-04-17 20:35:10 +00001850 // Guarantee that all emitted copies are
1851 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001852 Flag = Chain.getValue(1);
1853 }
1854
1855 SDValue result;
1856 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001857 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001858 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001859 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001860
1861 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001862}
1863
Evan Cheng3d2125c2010-11-30 23:55:39 +00001864bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1865 if (N->getNumValues() != 1)
1866 return false;
1867 if (!N->hasNUsesOfValue(1, 0))
1868 return false;
1869
1870 unsigned NumCopies = 0;
1871 SDNode* Copies[2];
1872 SDNode *Use = *N->use_begin();
1873 if (Use->getOpcode() == ISD::CopyToReg) {
1874 Copies[NumCopies++] = Use;
1875 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1876 // f64 returned in a pair of GPRs.
1877 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1878 UI != UE; ++UI) {
1879 if (UI->getOpcode() != ISD::CopyToReg)
1880 return false;
1881 Copies[UI.getUse().getResNo()] = *UI;
1882 ++NumCopies;
1883 }
1884 } else if (Use->getOpcode() == ISD::BITCAST) {
1885 // f32 returned in a single GPR.
1886 if (!Use->hasNUsesOfValue(1, 0))
1887 return false;
1888 Use = *Use->use_begin();
1889 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1890 return false;
1891 Copies[NumCopies++] = Use;
1892 } else {
1893 return false;
1894 }
1895
1896 if (NumCopies != 1 && NumCopies != 2)
1897 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001898
1899 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001900 for (unsigned i = 0; i < NumCopies; ++i) {
1901 SDNode *Copy = Copies[i];
1902 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1903 UI != UE; ++UI) {
1904 if (UI->getOpcode() == ISD::CopyToReg) {
1905 SDNode *Use = *UI;
1906 if (Use == Copies[0] || Use == Copies[1])
1907 continue;
1908 return false;
1909 }
1910 if (UI->getOpcode() != ARMISD::RET_FLAG)
1911 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001912 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001913 }
1914 }
1915
Evan Cheng1bf891a2010-12-01 22:59:46 +00001916 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001917}
1918
Evan Cheng485fafc2011-03-21 01:19:09 +00001919bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1920 if (!EnableARMTailCalls)
1921 return false;
1922
1923 if (!CI->isTailCall())
1924 return false;
1925
1926 return !Subtarget->isThumb1Only();
1927}
1928
Bob Wilsonb62d2572009-11-03 00:02:05 +00001929// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1930// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1931// one of the above mentioned nodes. It has to be wrapped because otherwise
1932// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1933// be used to form addressing mode. These wrapped nodes will be selected
1934// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001935static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001936 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001937 // FIXME there is no actual debug info here
1938 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001939 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001940 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001941 if (CP->isMachineConstantPoolEntry())
1942 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1943 CP->getAlignment());
1944 else
1945 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1946 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001947 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001948}
1949
Jim Grosbache1102ca2010-07-19 17:20:38 +00001950unsigned ARMTargetLowering::getJumpTableEncoding() const {
1951 return MachineJumpTableInfo::EK_Inline;
1952}
1953
Dan Gohmand858e902010-04-17 15:26:15 +00001954SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1955 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001956 MachineFunction &MF = DAG.getMachineFunction();
1957 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1958 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001959 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001960 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001961 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001962 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1963 SDValue CPAddr;
1964 if (RelocM == Reloc::Static) {
1965 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1966 } else {
1967 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001968 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001969 ARMConstantPoolValue *CPV =
1970 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
1971 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson907eebd2009-11-02 20:59:23 +00001972 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1973 }
1974 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1975 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001976 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001977 false, false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001978 if (RelocM == Reloc::Static)
1979 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001980 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001981 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001982}
1983
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001984// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001985SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001986ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001987 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001988 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001989 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001990 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001991 MachineFunction &MF = DAG.getMachineFunction();
1992 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001993 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001994 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00001995 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
1996 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001997 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001998 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001999 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002000 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002001 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002002 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002003
Evan Chenge7e0d622009-11-06 22:24:13 +00002004 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002005 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002006
2007 // call __tls_get_addr.
2008 ArgListTy Args;
2009 ArgListEntry Entry;
2010 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002011 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002012 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002013 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00002014 std::pair<SDValue, SDValue> CallResult =
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002015 LowerCallTo(Chain, (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00002016 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002017 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002018 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002019 return CallResult.first;
2020}
2021
2022// Lower ISD::GlobalTLSAddress using the "initial exec" or
2023// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00002024SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002025ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002026 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002027 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002028 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002029 SDValue Offset;
2030 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002031 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002032 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002033 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002034
Chris Lattner4fb63d02009-07-15 04:12:33 +00002035 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002036 MachineFunction &MF = DAG.getMachineFunction();
2037 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002038 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002039 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002040 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2041 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002042 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2043 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2044 true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002045 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002046 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002047 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002048 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002049 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002050 Chain = Offset.getValue(1);
2051
Evan Chenge7e0d622009-11-06 22:24:13 +00002052 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002053 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002054
Evan Cheng9eda6892009-10-31 03:39:36 +00002055 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002056 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002057 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002058 } else {
2059 // local exec model
Bill Wendling5bb77992011-10-01 08:00:54 +00002060 ARMConstantPoolValue *CPV =
2061 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002062 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002063 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002064 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002065 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002066 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002067 }
2068
2069 // The address of the thread local variable is the add of the thread
2070 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002071 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002072}
2073
Dan Gohman475871a2008-07-27 21:46:04 +00002074SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002075ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002076 // TODO: implement the "local dynamic" model
2077 assert(Subtarget->isTargetELF() &&
2078 "TLS not implemented for non-ELF targets");
2079 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2080 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
2081 // otherwise use the "Local Exec" TLS Model
2082 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
2083 return LowerToTLSGeneralDynamicModel(GA, DAG);
2084 else
2085 return LowerToTLSExecModels(GA, DAG);
2086}
2087
Dan Gohman475871a2008-07-27 21:46:04 +00002088SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002089 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002090 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002091 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002092 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002093 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2094 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002095 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002096 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002097 ARMConstantPoolConstant::Create(GV,
2098 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002099 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002100 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002101 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002102 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002103 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002104 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002105 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002106 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002107 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002108 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002109 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002110 MachinePointerInfo::getGOT(),
2111 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002112 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002113 }
2114
2115 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloy015cca62011-10-26 08:53:19 +00002116 // pair. This is always cheaper.
2117 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002118 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002119 // FIXME: Once remat is capable of dealing with instructions with register
2120 // operands, expand this into two nodes.
2121 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2122 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002123 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002124 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2125 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2126 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2127 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002128 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002129 }
2130}
2131
Dan Gohman475871a2008-07-27 21:46:04 +00002132SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002133 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002134 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002135 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002136 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002137 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002138 MachineFunction &MF = DAG.getMachineFunction();
2139 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2140
Evan Cheng4abce0c2011-05-27 20:11:27 +00002141 // FIXME: Enable this for static codegen when tool issues are fixed.
Evan Chengf31151f2011-10-26 01:17:44 +00002142 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002143 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002144 // FIXME: Once remat is capable of dealing with instructions with register
2145 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002146 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002147 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2148 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2149
Evan Cheng53519f02011-01-21 18:55:51 +00002150 unsigned Wrapper = (RelocM == Reloc::PIC_)
2151 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2152 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002153 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002154 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2155 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002156 MachinePointerInfo::getGOT(),
2157 false, false, false, 0);
Evan Chengfc8475b2011-01-19 02:16:49 +00002158 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002159 }
2160
2161 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002162 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002163 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002164 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002165 } else {
2166 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002167 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2168 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002169 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue,
2170 PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002171 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002172 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002173 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002174
Evan Cheng9eda6892009-10-31 03:39:36 +00002175 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002176 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002177 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002178 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002179
2180 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002181 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002182 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002183 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002184
Evan Cheng63476a82009-09-03 07:04:02 +00002185 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002186 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002187 false, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002188
2189 return Result;
2190}
2191
Dan Gohman475871a2008-07-27 21:46:04 +00002192SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002193 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002194 assert(Subtarget->isTargetELF() &&
2195 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002196 MachineFunction &MF = DAG.getMachineFunction();
2197 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002198 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002199 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002200 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002201 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingfe31e672011-10-01 08:58:29 +00002202 ARMConstantPoolValue *CPV =
2203 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2204 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002205 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002206 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002207 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002208 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002209 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002210 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002211 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002212}
2213
Jim Grosbach0e0da732009-05-12 23:59:14 +00002214SDValue
Jim Grosbache4ad3872010-10-19 23:27:08 +00002215ARMTargetLowering::LowerEH_SJLJ_DISPATCHSETUP(SDValue Op, SelectionDAG &DAG)
2216 const {
2217 DebugLoc dl = Op.getDebugLoc();
2218 return DAG.getNode(ARMISD::EH_SJLJ_DISPATCHSETUP, dl, MVT::Other,
Bill Wendling61512ba2011-05-11 01:11:55 +00002219 Op.getOperand(0), Op.getOperand(1));
Jim Grosbache4ad3872010-10-19 23:27:08 +00002220}
2221
2222SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002223ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2224 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002225 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendlingce370cf2011-10-07 21:25:38 +00002226 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2227 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002228 Op.getOperand(1), Val);
2229}
2230
2231SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002232ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2233 DebugLoc dl = Op.getDebugLoc();
2234 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2235 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2236}
2237
2238SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002239ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002240 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002241 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002242 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002243 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002244 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002245 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002246 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002247 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2248 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002249 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002250 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002251 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002252 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002253 EVT PtrVT = getPointerTy();
2254 DebugLoc dl = Op.getDebugLoc();
2255 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2256 SDValue CPAddr;
2257 unsigned PCAdj = (RelocM != Reloc::PIC_)
2258 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002259 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002260 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2261 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002262 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002263 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002264 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002265 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002266 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002267 false, false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002268
2269 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002270 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002271 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2272 }
2273 return Result;
2274 }
Evan Cheng92e39162011-03-29 23:06:19 +00002275 case Intrinsic::arm_neon_vmulls:
2276 case Intrinsic::arm_neon_vmullu: {
2277 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2278 ? ARMISD::VMULLs : ARMISD::VMULLu;
2279 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2280 Op.getOperand(1), Op.getOperand(2));
2281 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002282 }
2283}
2284
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002285static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002286 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002287 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002288 if (!Subtarget->hasDataBarrier()) {
2289 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2290 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2291 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002292 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002293 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002294 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002295 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002296 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002297
2298 SDValue Op5 = Op.getOperand(5);
2299 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2300 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2301 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2302 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2303
2304 ARM_MB::MemBOpt DMBOpt;
2305 if (isDeviceBarrier)
2306 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2307 else
2308 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2309 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2310 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002311}
2312
Eli Friedman26689ac2011-08-03 21:06:02 +00002313
2314static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2315 const ARMSubtarget *Subtarget) {
2316 // FIXME: handle "fence singlethread" more efficiently.
2317 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002318 if (!Subtarget->hasDataBarrier()) {
2319 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2320 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2321 // here.
2322 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2323 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002324 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002325 DAG.getConstant(0, MVT::i32));
2326 }
2327
Eli Friedman26689ac2011-08-03 21:06:02 +00002328 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002329 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002330}
2331
Evan Chengdfed19f2010-11-03 06:34:55 +00002332static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2333 const ARMSubtarget *Subtarget) {
2334 // ARM pre v5TE and Thumb1 does not have preload instructions.
2335 if (!(Subtarget->isThumb2() ||
2336 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2337 // Just preserve the chain.
2338 return Op.getOperand(0);
2339
2340 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002341 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2342 if (!isRead &&
2343 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2344 // ARMv7 with MP extension has PLDW.
2345 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002346
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002347 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2348 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002349 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002350 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002351 isData = ~isData & 1;
2352 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002353
2354 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002355 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2356 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002357}
2358
Dan Gohman1e93df62010-04-17 14:41:14 +00002359static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2360 MachineFunction &MF = DAG.getMachineFunction();
2361 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2362
Evan Chenga8e29892007-01-19 07:51:42 +00002363 // vastart just stores the address of the VarArgsFrameIndex slot into the
2364 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002365 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002366 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002367 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002368 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002369 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2370 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002371}
2372
Dan Gohman475871a2008-07-27 21:46:04 +00002373SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002374ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2375 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002376 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002377 MachineFunction &MF = DAG.getMachineFunction();
2378 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2379
2380 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002381 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002382 RC = ARM::tGPRRegisterClass;
2383 else
2384 RC = ARM::GPRRegisterClass;
2385
2386 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002387 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002388 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002389
2390 SDValue ArgValue2;
2391 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002392 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002393 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002394
2395 // Create load node to retrieve arguments from the stack.
2396 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002397 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002398 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002399 false, false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002400 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002401 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002402 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002403 }
2404
Jim Grosbache5165492009-11-09 00:11:35 +00002405 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002406}
2407
Stuart Hastingsc7315872011-04-20 16:47:52 +00002408void
2409ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2410 unsigned &VARegSize, unsigned &VARegSaveSize)
2411 const {
2412 unsigned NumGPRs;
2413 if (CCInfo.isFirstByValRegValid())
2414 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2415 else {
2416 unsigned int firstUnalloced;
2417 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2418 sizeof(GPRArgRegs) /
2419 sizeof(GPRArgRegs[0]));
2420 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2421 }
2422
2423 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2424 VARegSize = NumGPRs * 4;
2425 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2426}
2427
2428// The remaining GPRs hold either the beginning of variable-argument
2429// data, or the beginning of an aggregate passed by value (usuall
2430// byval). Either way, we allocate stack slots adjacent to the data
2431// provided by our caller, and store the unallocated registers there.
2432// If this is a variadic function, the va_list pointer will begin with
2433// these values; otherwise, this reassembles a (byval) structure that
2434// was split between registers and memory.
2435void
2436ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2437 DebugLoc dl, SDValue &Chain,
2438 unsigned ArgOffset) const {
2439 MachineFunction &MF = DAG.getMachineFunction();
2440 MachineFrameInfo *MFI = MF.getFrameInfo();
2441 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2442 unsigned firstRegToSaveIndex;
2443 if (CCInfo.isFirstByValRegValid())
2444 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2445 else {
2446 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2447 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2448 }
2449
2450 unsigned VARegSize, VARegSaveSize;
2451 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2452 if (VARegSaveSize) {
2453 // If this function is vararg, store any remaining integer argument regs
2454 // to their spots on the stack so that they may be loaded by deferencing
2455 // the result of va_next.
2456 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002457 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2458 ArgOffset + VARegSaveSize
2459 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002460 false));
2461 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2462 getPointerTy());
2463
2464 SmallVector<SDValue, 4> MemOps;
2465 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
2466 TargetRegisterClass *RC;
2467 if (AFI->isThumb1OnlyFunction())
2468 RC = ARM::tGPRRegisterClass;
2469 else
2470 RC = ARM::GPRRegisterClass;
2471
2472 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2473 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2474 SDValue Store =
2475 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002476 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002477 false, false, 0);
2478 MemOps.push_back(Store);
2479 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2480 DAG.getConstant(4, getPointerTy()));
2481 }
2482 if (!MemOps.empty())
2483 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2484 &MemOps[0], MemOps.size());
2485 } else
2486 // This will point to the next argument passed via stack.
2487 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2488}
2489
Bob Wilson5bafff32009-06-22 23:27:02 +00002490SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002491ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002492 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002493 const SmallVectorImpl<ISD::InputArg>
2494 &Ins,
2495 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002496 SmallVectorImpl<SDValue> &InVals)
2497 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002498 MachineFunction &MF = DAG.getMachineFunction();
2499 MachineFrameInfo *MFI = MF.getFrameInfo();
2500
Bob Wilson1f595bb2009-04-17 19:07:39 +00002501 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2502
2503 // Assign locations to all of the incoming arguments.
2504 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002505 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2506 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002507 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002508 CCAssignFnForNode(CallConv, /* Return*/ false,
2509 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002510
2511 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002512 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002513
Stuart Hastingsf222e592011-02-28 17:17:53 +00002514 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002515 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2516 CCValAssign &VA = ArgLocs[i];
2517
Bob Wilsondee46d72009-04-17 20:35:10 +00002518 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002519 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002520 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002521
Bob Wilson1f595bb2009-04-17 19:07:39 +00002522 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002523 // f64 and vector types are split up into multiple registers or
2524 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002525 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002526 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002527 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002528 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002529 SDValue ArgValue2;
2530 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002531 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002532 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2533 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002534 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002535 false, false, false, 0);
Bob Wilson6a234f02010-04-13 22:03:22 +00002536 } else {
2537 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2538 Chain, DAG, dl);
2539 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002540 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2541 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002542 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002543 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002544 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2545 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002546 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002547
Bob Wilson5bafff32009-06-22 23:27:02 +00002548 } else {
2549 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002550
Owen Anderson825b72b2009-08-11 20:47:22 +00002551 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002552 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002553 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002554 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002555 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002556 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002557 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002558 RC = (AFI->isThumb1OnlyFunction() ?
2559 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002560 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002561 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002562
2563 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002564 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002565 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002566 }
2567
2568 // If this is an 8 or 16-bit value, it is really passed promoted
2569 // to 32 bits. Insert an assert[sz]ext to capture this, then
2570 // truncate to the right size.
2571 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002572 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002573 case CCValAssign::Full: break;
2574 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002575 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002576 break;
2577 case CCValAssign::SExt:
2578 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2579 DAG.getValueType(VA.getValVT()));
2580 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2581 break;
2582 case CCValAssign::ZExt:
2583 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2584 DAG.getValueType(VA.getValVT()));
2585 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2586 break;
2587 }
2588
Dan Gohman98ca4f22009-08-05 01:29:28 +00002589 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002590
2591 } else { // VA.isRegLoc()
2592
2593 // sanity check
2594 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002595 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002596
Stuart Hastingsf222e592011-02-28 17:17:53 +00002597 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002598
Stuart Hastingsf222e592011-02-28 17:17:53 +00002599 // Some Ins[] entries become multiple ArgLoc[] entries.
2600 // Process them only once.
2601 if (index != lastInsIndex)
2602 {
2603 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002604 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002605 // This can be changed with more analysis.
2606 // In case of tail call optimization mark all arguments mutable.
2607 // Since they could be overwritten by lowering of arguments in case of
2608 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002609 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002610 unsigned VARegSize, VARegSaveSize;
2611 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2612 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2613 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002614 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002615 int FI = MFI->CreateFixedObject(Bytes,
2616 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002617 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2618 } else {
2619 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2620 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002621
Stuart Hastingsf222e592011-02-28 17:17:53 +00002622 // Create load nodes to retrieve arguments from the stack.
2623 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2624 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2625 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002626 false, false, false, 0));
Stuart Hastingsf222e592011-02-28 17:17:53 +00002627 }
2628 lastInsIndex = index;
2629 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002630 }
2631 }
2632
2633 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002634 if (isVarArg)
2635 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002636
Dan Gohman98ca4f22009-08-05 01:29:28 +00002637 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002638}
2639
2640/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002641static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002642 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002643 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002644 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002645 // Maybe this has already been legalized into the constant pool?
2646 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002647 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002648 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002649 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002650 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002651 }
2652 }
2653 return false;
2654}
2655
Evan Chenga8e29892007-01-19 07:51:42 +00002656/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2657/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002658SDValue
2659ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002660 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002661 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002662 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002663 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002664 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002665 // Constant does not fit, try adjusting it by one?
2666 switch (CC) {
2667 default: break;
2668 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002669 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002670 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002671 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002672 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002673 }
2674 break;
2675 case ISD::SETULT:
2676 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002677 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002678 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002679 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002680 }
2681 break;
2682 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002683 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002684 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002685 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002686 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002687 }
2688 break;
2689 case ISD::SETULE:
2690 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002691 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002692 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002693 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002694 }
2695 break;
2696 }
2697 }
2698 }
2699
2700 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002701 ARMISD::NodeType CompareType;
2702 switch (CondCode) {
2703 default:
2704 CompareType = ARMISD::CMP;
2705 break;
2706 case ARMCC::EQ:
2707 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002708 // Uses only Z Flag
2709 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002710 break;
2711 }
Evan Cheng218977b2010-07-13 19:27:42 +00002712 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002713 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002714}
2715
2716/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002717SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002718ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002719 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002720 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002721 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002722 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002723 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002724 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2725 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002726}
2727
Bob Wilson79f56c92011-03-08 01:17:20 +00002728/// duplicateCmp - Glue values can have only one use, so this function
2729/// duplicates a comparison node.
2730SDValue
2731ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2732 unsigned Opc = Cmp.getOpcode();
2733 DebugLoc DL = Cmp.getDebugLoc();
2734 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2735 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2736
2737 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2738 Cmp = Cmp.getOperand(0);
2739 Opc = Cmp.getOpcode();
2740 if (Opc == ARMISD::CMPFP)
2741 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2742 else {
2743 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2744 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2745 }
2746 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2747}
2748
Bill Wendlingde2b1512010-08-11 08:43:16 +00002749SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2750 SDValue Cond = Op.getOperand(0);
2751 SDValue SelectTrue = Op.getOperand(1);
2752 SDValue SelectFalse = Op.getOperand(2);
2753 DebugLoc dl = Op.getDebugLoc();
2754
2755 // Convert:
2756 //
2757 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2758 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2759 //
2760 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2761 const ConstantSDNode *CMOVTrue =
2762 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2763 const ConstantSDNode *CMOVFalse =
2764 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2765
2766 if (CMOVTrue && CMOVFalse) {
2767 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2768 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2769
2770 SDValue True;
2771 SDValue False;
2772 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2773 True = SelectTrue;
2774 False = SelectFalse;
2775 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2776 True = SelectFalse;
2777 False = SelectTrue;
2778 }
2779
2780 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002781 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002782 SDValue ARMcc = Cond.getOperand(2);
2783 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002784 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002785 assert(True.getValueType() == VT);
2786 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002787 }
2788 }
2789 }
2790
2791 return DAG.getSelectCC(dl, Cond,
2792 DAG.getConstant(0, Cond.getValueType()),
2793 SelectTrue, SelectFalse, ISD::SETNE);
2794}
2795
Dan Gohmand858e902010-04-17 15:26:15 +00002796SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002797 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002798 SDValue LHS = Op.getOperand(0);
2799 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002800 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002801 SDValue TrueVal = Op.getOperand(2);
2802 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002803 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002804
Owen Anderson825b72b2009-08-11 20:47:22 +00002805 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002806 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002807 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002808 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Jim Grosbachb04546f2011-09-13 20:30:37 +00002809 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002810 }
2811
2812 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002813 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002814
Evan Cheng218977b2010-07-13 19:27:42 +00002815 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2816 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002817 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002818 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002819 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002820 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002821 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002822 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002823 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002824 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002825 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002826 }
2827 return Result;
2828}
2829
Evan Cheng218977b2010-07-13 19:27:42 +00002830/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2831/// to morph to an integer compare sequence.
2832static bool canChangeToInt(SDValue Op, bool &SeenZero,
2833 const ARMSubtarget *Subtarget) {
2834 SDNode *N = Op.getNode();
2835 if (!N->hasOneUse())
2836 // Otherwise it requires moving the value from fp to integer registers.
2837 return false;
2838 if (!N->getNumValues())
2839 return false;
2840 EVT VT = Op.getValueType();
2841 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2842 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2843 // vmrs are very slow, e.g. cortex-a8.
2844 return false;
2845
2846 if (isFloatingPointZero(Op)) {
2847 SeenZero = true;
2848 return true;
2849 }
2850 return ISD::isNormalLoad(N);
2851}
2852
2853static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2854 if (isFloatingPointZero(Op))
2855 return DAG.getConstant(0, MVT::i32);
2856
2857 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2858 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002859 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002860 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002861 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002862
2863 llvm_unreachable("Unknown VFP cmp argument!");
2864}
2865
2866static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2867 SDValue &RetVal1, SDValue &RetVal2) {
2868 if (isFloatingPointZero(Op)) {
2869 RetVal1 = DAG.getConstant(0, MVT::i32);
2870 RetVal2 = DAG.getConstant(0, MVT::i32);
2871 return;
2872 }
2873
2874 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2875 SDValue Ptr = Ld->getBasePtr();
2876 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2877 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002878 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002879 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002880 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002881
2882 EVT PtrType = Ptr.getValueType();
2883 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2884 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2885 PtrType, Ptr, DAG.getConstant(4, PtrType));
2886 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2887 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002888 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002889 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002890 Ld->isInvariant(), NewAlign);
Evan Cheng218977b2010-07-13 19:27:42 +00002891 return;
2892 }
2893
2894 llvm_unreachable("Unknown VFP cmp argument!");
2895}
2896
2897/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2898/// f32 and even f64 comparisons to integer ones.
2899SDValue
2900ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2901 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002902 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002903 SDValue LHS = Op.getOperand(2);
2904 SDValue RHS = Op.getOperand(3);
2905 SDValue Dest = Op.getOperand(4);
2906 DebugLoc dl = Op.getDebugLoc();
2907
2908 bool SeenZero = false;
2909 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2910 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002911 // If one of the operand is zero, it's safe to ignore the NaN case since
2912 // we only care about equality comparisons.
2913 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Bob Wilson1b772f92011-03-08 01:17:16 +00002914 // If unsafe fp math optimization is enabled and there are no other uses of
2915 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00002916 // to an integer comparison.
2917 if (CC == ISD::SETOEQ)
2918 CC = ISD::SETEQ;
2919 else if (CC == ISD::SETUNE)
2920 CC = ISD::SETNE;
2921
2922 SDValue ARMcc;
2923 if (LHS.getValueType() == MVT::f32) {
2924 LHS = bitcastf32Toi32(LHS, DAG);
2925 RHS = bitcastf32Toi32(RHS, DAG);
2926 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2927 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2928 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2929 Chain, Dest, ARMcc, CCR, Cmp);
2930 }
2931
2932 SDValue LHS1, LHS2;
2933 SDValue RHS1, RHS2;
2934 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2935 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2936 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2937 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002938 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002939 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2940 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2941 }
2942
2943 return SDValue();
2944}
2945
2946SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2947 SDValue Chain = Op.getOperand(0);
2948 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2949 SDValue LHS = Op.getOperand(2);
2950 SDValue RHS = Op.getOperand(3);
2951 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002952 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002953
Owen Anderson825b72b2009-08-11 20:47:22 +00002954 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002955 SDValue ARMcc;
2956 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002957 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002958 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002959 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002960 }
2961
Owen Anderson825b72b2009-08-11 20:47:22 +00002962 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002963
2964 if (UnsafeFPMath &&
2965 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2966 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2967 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2968 if (Result.getNode())
2969 return Result;
2970 }
2971
Evan Chenga8e29892007-01-19 07:51:42 +00002972 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002973 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002974
Evan Cheng218977b2010-07-13 19:27:42 +00002975 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2976 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002977 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002978 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002979 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002980 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002981 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002982 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2983 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002984 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002985 }
2986 return Res;
2987}
2988
Dan Gohmand858e902010-04-17 15:26:15 +00002989SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002990 SDValue Chain = Op.getOperand(0);
2991 SDValue Table = Op.getOperand(1);
2992 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002993 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002994
Owen Andersone50ed302009-08-10 22:56:29 +00002995 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002996 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2997 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002998 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002999 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00003000 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00003001 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3002 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00003003 if (Subtarget->isThumb2()) {
3004 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3005 // which does another jump to the destination. This also makes it easier
3006 // to translate it to TBB / TBH later.
3007 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00003008 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00003009 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003010 }
Evan Cheng66ac5312009-07-25 00:33:29 +00003011 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00003012 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003013 MachinePointerInfo::getJumpTable(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003014 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003015 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003016 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00003017 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003018 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00003019 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003020 MachinePointerInfo::getJumpTable(),
3021 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003022 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003023 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003024 }
Evan Chenga8e29892007-01-19 07:51:42 +00003025}
3026
Eli Friedman14e809c2011-11-09 23:36:02 +00003027static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
3028 EVT VT = Op.getValueType();
3029 assert(VT.getVectorElementType() == MVT::i32 && "Unexpected custom lowering");
3030
3031 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3032 return Op;
3033 return DAG.UnrollVectorOp(Op.getNode());
3034}
3035
Bob Wilson76a312b2010-03-19 22:51:32 +00003036static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman14e809c2011-11-09 23:36:02 +00003037 EVT VT = Op.getValueType();
3038 if (VT.isVector())
3039 return LowerVectorFP_TO_INT(Op, DAG);
3040
Bob Wilson76a312b2010-03-19 22:51:32 +00003041 DebugLoc dl = Op.getDebugLoc();
3042 unsigned Opc;
3043
3044 switch (Op.getOpcode()) {
3045 default:
3046 assert(0 && "Invalid opcode!");
3047 case ISD::FP_TO_SINT:
3048 Opc = ARMISD::FTOSI;
3049 break;
3050 case ISD::FP_TO_UINT:
3051 Opc = ARMISD::FTOUI;
3052 break;
3053 }
3054 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003055 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003056}
3057
Cameron Zwarich3007d332011-03-29 21:41:55 +00003058static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3059 EVT VT = Op.getValueType();
3060 DebugLoc dl = Op.getDebugLoc();
3061
Eli Friedman14e809c2011-11-09 23:36:02 +00003062 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3063 if (VT.getVectorElementType() == MVT::f32)
3064 return Op;
3065 return DAG.UnrollVectorOp(Op.getNode());
3066 }
3067
Duncan Sands1f6a3292011-08-12 14:54:45 +00003068 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3069 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003070 if (VT != MVT::v4f32)
3071 return DAG.UnrollVectorOp(Op.getNode());
3072
3073 unsigned CastOpc;
3074 unsigned Opc;
3075 switch (Op.getOpcode()) {
3076 default:
3077 assert(0 && "Invalid opcode!");
3078 case ISD::SINT_TO_FP:
3079 CastOpc = ISD::SIGN_EXTEND;
3080 Opc = ISD::SINT_TO_FP;
3081 break;
3082 case ISD::UINT_TO_FP:
3083 CastOpc = ISD::ZERO_EXTEND;
3084 Opc = ISD::UINT_TO_FP;
3085 break;
3086 }
3087
3088 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3089 return DAG.getNode(Opc, dl, VT, Op);
3090}
3091
Bob Wilson76a312b2010-03-19 22:51:32 +00003092static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3093 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003094 if (VT.isVector())
3095 return LowerVectorINT_TO_FP(Op, DAG);
3096
Bob Wilson76a312b2010-03-19 22:51:32 +00003097 DebugLoc dl = Op.getDebugLoc();
3098 unsigned Opc;
3099
3100 switch (Op.getOpcode()) {
3101 default:
3102 assert(0 && "Invalid opcode!");
3103 case ISD::SINT_TO_FP:
3104 Opc = ARMISD::SITOF;
3105 break;
3106 case ISD::UINT_TO_FP:
3107 Opc = ARMISD::UITOF;
3108 break;
3109 }
3110
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003111 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003112 return DAG.getNode(Opc, dl, VT, Op);
3113}
3114
Evan Cheng515fe3a2010-07-08 02:08:50 +00003115SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003116 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003117 SDValue Tmp0 = Op.getOperand(0);
3118 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003119 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003120 EVT VT = Op.getValueType();
3121 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003122 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3123 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3124 bool UseNEON = !InGPR && Subtarget->hasNEON();
3125
3126 if (UseNEON) {
3127 // Use VBSL to copy the sign bit.
3128 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3129 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3130 DAG.getTargetConstant(EncodedVal, MVT::i32));
3131 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3132 if (VT == MVT::f64)
3133 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3134 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3135 DAG.getConstant(32, MVT::i32));
3136 else /*if (VT == MVT::f32)*/
3137 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3138 if (SrcVT == MVT::f32) {
3139 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3140 if (VT == MVT::f64)
3141 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3142 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3143 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003144 } else if (VT == MVT::f32)
3145 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3146 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3147 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003148 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3149 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3150
3151 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3152 MVT::i32);
3153 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3154 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3155 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003156
Evan Chenge573fb32011-02-23 02:24:55 +00003157 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3158 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3159 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003160 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003161 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3162 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3163 DAG.getConstant(0, MVT::i32));
3164 } else {
3165 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3166 }
3167
3168 return Res;
3169 }
Evan Chengc143dd42011-02-11 02:28:55 +00003170
3171 // Bitcast operand 1 to i32.
3172 if (SrcVT == MVT::f64)
3173 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3174 &Tmp1, 1).getValue(1);
3175 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3176
Evan Chenge573fb32011-02-23 02:24:55 +00003177 // Or in the signbit with integer operations.
3178 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3179 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3180 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3181 if (VT == MVT::f32) {
3182 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3183 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3184 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3185 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003186 }
3187
Evan Chenge573fb32011-02-23 02:24:55 +00003188 // f64: Or the high part with signbit and then combine two parts.
3189 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3190 &Tmp0, 1);
3191 SDValue Lo = Tmp0.getValue(0);
3192 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3193 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3194 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003195}
3196
Evan Cheng2457f2c2010-05-22 01:47:14 +00003197SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3198 MachineFunction &MF = DAG.getMachineFunction();
3199 MachineFrameInfo *MFI = MF.getFrameInfo();
3200 MFI->setReturnAddressIsTaken(true);
3201
3202 EVT VT = Op.getValueType();
3203 DebugLoc dl = Op.getDebugLoc();
3204 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3205 if (Depth) {
3206 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3207 SDValue Offset = DAG.getConstant(4, MVT::i32);
3208 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3209 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003210 MachinePointerInfo(), false, false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003211 }
3212
3213 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003214 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003215 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3216}
3217
Dan Gohmand858e902010-04-17 15:26:15 +00003218SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003219 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3220 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003221
Owen Andersone50ed302009-08-10 22:56:29 +00003222 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003223 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3224 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003225 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003226 ? ARM::R7 : ARM::R11;
3227 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3228 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003229 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3230 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003231 false, false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003232 return FrameAddr;
3233}
3234
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003235/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003236/// expand a bit convert where either the source or destination type is i64 to
3237/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3238/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3239/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003240static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003241 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3242 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003243 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003244
Bob Wilson9f3f0612010-04-17 05:30:19 +00003245 // This function is only supposed to be called for i64 types, either as the
3246 // source or destination of the bit convert.
3247 EVT SrcVT = Op.getValueType();
3248 EVT DstVT = N->getValueType(0);
3249 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003250 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003251
Bob Wilson9f3f0612010-04-17 05:30:19 +00003252 // Turn i64->f64 into VMOVDRR.
3253 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003254 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3255 DAG.getConstant(0, MVT::i32));
3256 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3257 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003258 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003259 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003260 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003261
Jim Grosbache5165492009-11-09 00:11:35 +00003262 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003263 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3264 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3265 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3266 // Merge the pieces into a single i64 value.
3267 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3268 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003269
Bob Wilson9f3f0612010-04-17 05:30:19 +00003270 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003271}
3272
Bob Wilson5bafff32009-06-22 23:27:02 +00003273/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003274/// Zero vectors are used to represent vector negation and in those cases
3275/// will be implemented with the NEON VNEG instruction. However, VNEG does
3276/// not support i64 elements, so sometimes the zero vectors will need to be
3277/// explicitly constructed. Regardless, use a canonical VMOV to create the
3278/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003279static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003280 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003281 // The canonical modified immediate encoding of a zero vector is....0!
3282 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3283 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3284 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003285 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003286}
3287
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003288/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3289/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003290SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3291 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003292 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3293 EVT VT = Op.getValueType();
3294 unsigned VTBits = VT.getSizeInBits();
3295 DebugLoc dl = Op.getDebugLoc();
3296 SDValue ShOpLo = Op.getOperand(0);
3297 SDValue ShOpHi = Op.getOperand(1);
3298 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003299 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003300 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003301
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003302 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3303
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003304 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3305 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3306 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3307 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3308 DAG.getConstant(VTBits, MVT::i32));
3309 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3310 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003311 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003312
3313 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3314 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003315 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003316 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003317 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003318 CCR, Cmp);
3319
3320 SDValue Ops[2] = { Lo, Hi };
3321 return DAG.getMergeValues(Ops, 2, dl);
3322}
3323
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003324/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3325/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003326SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3327 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003328 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3329 EVT VT = Op.getValueType();
3330 unsigned VTBits = VT.getSizeInBits();
3331 DebugLoc dl = Op.getDebugLoc();
3332 SDValue ShOpLo = Op.getOperand(0);
3333 SDValue ShOpHi = Op.getOperand(1);
3334 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003335 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003336
3337 assert(Op.getOpcode() == ISD::SHL_PARTS);
3338 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3339 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3340 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3341 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3342 DAG.getConstant(VTBits, MVT::i32));
3343 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3344 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3345
3346 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3347 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3348 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003349 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003350 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003351 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003352 CCR, Cmp);
3353
3354 SDValue Ops[2] = { Lo, Hi };
3355 return DAG.getMergeValues(Ops, 2, dl);
3356}
3357
Jim Grosbach4725ca72010-09-08 03:54:02 +00003358SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003359 SelectionDAG &DAG) const {
3360 // The rounding mode is in bits 23:22 of the FPSCR.
3361 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3362 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3363 // so that the shift + and get folded into a bitfield extract.
3364 DebugLoc dl = Op.getDebugLoc();
3365 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3366 DAG.getConstant(Intrinsic::arm_get_fpscr,
3367 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003368 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003369 DAG.getConstant(1U << 22, MVT::i32));
3370 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3371 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003372 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003373 DAG.getConstant(3, MVT::i32));
3374}
3375
Jim Grosbach3482c802010-01-18 19:58:49 +00003376static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3377 const ARMSubtarget *ST) {
3378 EVT VT = N->getValueType(0);
3379 DebugLoc dl = N->getDebugLoc();
3380
3381 if (!ST->hasV6T2Ops())
3382 return SDValue();
3383
3384 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3385 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3386}
3387
Bob Wilson5bafff32009-06-22 23:27:02 +00003388static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3389 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003390 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003391 DebugLoc dl = N->getDebugLoc();
3392
Bob Wilsond5448bb2010-11-18 21:16:28 +00003393 if (!VT.isVector())
3394 return SDValue();
3395
Bob Wilson5bafff32009-06-22 23:27:02 +00003396 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003397 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003398
Bob Wilsond5448bb2010-11-18 21:16:28 +00003399 // Left shifts translate directly to the vshiftu intrinsic.
3400 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003401 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003402 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3403 N->getOperand(0), N->getOperand(1));
3404
3405 assert((N->getOpcode() == ISD::SRA ||
3406 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3407
3408 // NEON uses the same intrinsics for both left and right shifts. For
3409 // right shifts, the shift amounts are negative, so negate the vector of
3410 // shift amounts.
3411 EVT ShiftVT = N->getOperand(1).getValueType();
3412 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3413 getZeroVector(ShiftVT, DAG, dl),
3414 N->getOperand(1));
3415 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3416 Intrinsic::arm_neon_vshifts :
3417 Intrinsic::arm_neon_vshiftu);
3418 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3419 DAG.getConstant(vshiftInt, MVT::i32),
3420 N->getOperand(0), NegatedCount);
3421}
3422
3423static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3424 const ARMSubtarget *ST) {
3425 EVT VT = N->getValueType(0);
3426 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003427
Eli Friedmance392eb2009-08-22 03:13:10 +00003428 // We can get here for a node like i32 = ISD::SHL i32, i64
3429 if (VT != MVT::i64)
3430 return SDValue();
3431
3432 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003433 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003434
Chris Lattner27a6c732007-11-24 07:07:01 +00003435 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3436 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003437 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003438 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003439
Chris Lattner27a6c732007-11-24 07:07:01 +00003440 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003441 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003442
Chris Lattner27a6c732007-11-24 07:07:01 +00003443 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003444 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003445 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003446 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003447 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003448
Chris Lattner27a6c732007-11-24 07:07:01 +00003449 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3450 // captures the result into a carry flag.
3451 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003452 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003453
Chris Lattner27a6c732007-11-24 07:07:01 +00003454 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003455 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003456
Chris Lattner27a6c732007-11-24 07:07:01 +00003457 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003458 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003459}
3460
Bob Wilson5bafff32009-06-22 23:27:02 +00003461static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3462 SDValue TmpOp0, TmpOp1;
3463 bool Invert = false;
3464 bool Swap = false;
3465 unsigned Opc = 0;
3466
3467 SDValue Op0 = Op.getOperand(0);
3468 SDValue Op1 = Op.getOperand(1);
3469 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003470 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003471 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3472 DebugLoc dl = Op.getDebugLoc();
3473
3474 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3475 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003476 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003477 case ISD::SETUNE:
3478 case ISD::SETNE: Invert = true; // Fallthrough
3479 case ISD::SETOEQ:
3480 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3481 case ISD::SETOLT:
3482 case ISD::SETLT: Swap = true; // Fallthrough
3483 case ISD::SETOGT:
3484 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3485 case ISD::SETOLE:
3486 case ISD::SETLE: Swap = true; // Fallthrough
3487 case ISD::SETOGE:
3488 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3489 case ISD::SETUGE: Swap = true; // Fallthrough
3490 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3491 case ISD::SETUGT: Swap = true; // Fallthrough
3492 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3493 case ISD::SETUEQ: Invert = true; // Fallthrough
3494 case ISD::SETONE:
3495 // Expand this to (OLT | OGT).
3496 TmpOp0 = Op0;
3497 TmpOp1 = Op1;
3498 Opc = ISD::OR;
3499 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3500 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3501 break;
3502 case ISD::SETUO: Invert = true; // Fallthrough
3503 case ISD::SETO:
3504 // Expand this to (OLT | OGE).
3505 TmpOp0 = Op0;
3506 TmpOp1 = Op1;
3507 Opc = ISD::OR;
3508 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3509 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3510 break;
3511 }
3512 } else {
3513 // Integer comparisons.
3514 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003515 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003516 case ISD::SETNE: Invert = true;
3517 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3518 case ISD::SETLT: Swap = true;
3519 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3520 case ISD::SETLE: Swap = true;
3521 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3522 case ISD::SETULT: Swap = true;
3523 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3524 case ISD::SETULE: Swap = true;
3525 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3526 }
3527
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003528 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003529 if (Opc == ARMISD::VCEQ) {
3530
3531 SDValue AndOp;
3532 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3533 AndOp = Op0;
3534 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3535 AndOp = Op1;
3536
3537 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003538 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003539 AndOp = AndOp.getOperand(0);
3540
3541 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3542 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003543 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3544 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003545 Invert = !Invert;
3546 }
3547 }
3548 }
3549
3550 if (Swap)
3551 std::swap(Op0, Op1);
3552
Owen Andersonc24cb352010-11-08 23:21:22 +00003553 // If one of the operands is a constant vector zero, attempt to fold the
3554 // comparison to a specialized compare-against-zero form.
3555 SDValue SingleOp;
3556 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3557 SingleOp = Op0;
3558 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3559 if (Opc == ARMISD::VCGE)
3560 Opc = ARMISD::VCLEZ;
3561 else if (Opc == ARMISD::VCGT)
3562 Opc = ARMISD::VCLTZ;
3563 SingleOp = Op1;
3564 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003565
Owen Andersonc24cb352010-11-08 23:21:22 +00003566 SDValue Result;
3567 if (SingleOp.getNode()) {
3568 switch (Opc) {
3569 case ARMISD::VCEQ:
3570 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3571 case ARMISD::VCGE:
3572 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3573 case ARMISD::VCLEZ:
3574 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3575 case ARMISD::VCGT:
3576 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3577 case ARMISD::VCLTZ:
3578 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3579 default:
3580 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3581 }
3582 } else {
3583 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3584 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003585
3586 if (Invert)
3587 Result = DAG.getNOT(dl, Result, VT);
3588
3589 return Result;
3590}
3591
Bob Wilsond3c42842010-06-14 22:19:57 +00003592/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3593/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003594/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003595static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3596 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003597 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003598 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003599
Bob Wilson827b2102010-06-15 19:05:35 +00003600 // SplatBitSize is set to the smallest size that splats the vector, so a
3601 // zero vector will always have SplatBitSize == 8. However, NEON modified
3602 // immediate instructions others than VMOV do not support the 8-bit encoding
3603 // of a zero vector, and the default encoding of zero is supposed to be the
3604 // 32-bit version.
3605 if (SplatBits == 0)
3606 SplatBitSize = 32;
3607
Bob Wilson5bafff32009-06-22 23:27:02 +00003608 switch (SplatBitSize) {
3609 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003610 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003611 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003612 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003613 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003614 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003615 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003616 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003617 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003618
3619 case 16:
3620 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003621 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003622 if ((SplatBits & ~0xff) == 0) {
3623 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003624 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003625 Imm = SplatBits;
3626 break;
3627 }
3628 if ((SplatBits & ~0xff00) == 0) {
3629 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003630 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003631 Imm = SplatBits >> 8;
3632 break;
3633 }
3634 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003635
3636 case 32:
3637 // NEON's 32-bit VMOV supports splat values where:
3638 // * only one byte is nonzero, or
3639 // * the least significant byte is 0xff and the second byte is nonzero, or
3640 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003641 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003642 if ((SplatBits & ~0xff) == 0) {
3643 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003644 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003645 Imm = SplatBits;
3646 break;
3647 }
3648 if ((SplatBits & ~0xff00) == 0) {
3649 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003650 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003651 Imm = SplatBits >> 8;
3652 break;
3653 }
3654 if ((SplatBits & ~0xff0000) == 0) {
3655 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003656 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003657 Imm = SplatBits >> 16;
3658 break;
3659 }
3660 if ((SplatBits & ~0xff000000) == 0) {
3661 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003662 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003663 Imm = SplatBits >> 24;
3664 break;
3665 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003666
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003667 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3668 if (type == OtherModImm) return SDValue();
3669
Bob Wilson5bafff32009-06-22 23:27:02 +00003670 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003671 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3672 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003673 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003674 Imm = SplatBits >> 8;
3675 SplatBits |= 0xff;
3676 break;
3677 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003678
3679 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003680 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3681 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003682 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003683 Imm = SplatBits >> 16;
3684 SplatBits |= 0xffff;
3685 break;
3686 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003687
3688 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3689 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3690 // VMOV.I32. A (very) minor optimization would be to replicate the value
3691 // and fall through here to test for a valid 64-bit splat. But, then the
3692 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003693 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003694
3695 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003696 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003697 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003698 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003699 uint64_t BitMask = 0xff;
3700 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003701 unsigned ImmMask = 1;
3702 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003703 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003704 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003705 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003706 Imm |= ImmMask;
3707 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003708 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003709 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003710 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003711 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003712 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003713 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003714 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003715 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003716 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003717 break;
3718 }
3719
Bob Wilson1a913ed2010-06-11 21:34:50 +00003720 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003721 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003722 return SDValue();
3723 }
3724
Bob Wilsoncba270d2010-07-13 21:16:48 +00003725 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3726 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003727}
3728
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003729static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3730 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003731 unsigned NumElts = VT.getVectorNumElements();
3732 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003733
3734 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3735 if (M[0] < 0)
3736 return false;
3737
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003738 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003739
3740 // If this is a VEXT shuffle, the immediate value is the index of the first
3741 // element. The other shuffle indices must be the successive elements after
3742 // the first one.
3743 unsigned ExpectedElt = Imm;
3744 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003745 // Increment the expected index. If it wraps around, it may still be
3746 // a VEXT but the source vectors must be swapped.
3747 ExpectedElt += 1;
3748 if (ExpectedElt == NumElts * 2) {
3749 ExpectedElt = 0;
3750 ReverseVEXT = true;
3751 }
3752
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003753 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003754 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003755 return false;
3756 }
3757
3758 // Adjust the index value if the source operands will be swapped.
3759 if (ReverseVEXT)
3760 Imm -= NumElts;
3761
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003762 return true;
3763}
3764
Bob Wilson8bb9e482009-07-26 00:39:34 +00003765/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3766/// instruction with the specified blocksize. (The order of the elements
3767/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003768static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3769 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003770 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3771 "Only possible block sizes for VREV are: 16, 32, 64");
3772
Bob Wilson8bb9e482009-07-26 00:39:34 +00003773 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003774 if (EltSz == 64)
3775 return false;
3776
3777 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003778 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003779 // If the first shuffle index is UNDEF, be optimistic.
3780 if (M[0] < 0)
3781 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003782
3783 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3784 return false;
3785
3786 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003787 if (M[i] < 0) continue; // ignore UNDEF indices
3788 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003789 return false;
3790 }
3791
3792 return true;
3793}
3794
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003795static bool isVTBLMask(const SmallVectorImpl<int> &M, EVT VT) {
3796 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3797 // range, then 0 is placed into the resulting vector. So pretty much any mask
3798 // of 8 elements can work here.
3799 return VT == MVT::v8i8 && M.size() == 8;
3800}
3801
Bob Wilsonc692cb72009-08-21 20:54:19 +00003802static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3803 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003804 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3805 if (EltSz == 64)
3806 return false;
3807
Bob Wilsonc692cb72009-08-21 20:54:19 +00003808 unsigned NumElts = VT.getVectorNumElements();
3809 WhichResult = (M[0] == 0 ? 0 : 1);
3810 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003811 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3812 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003813 return false;
3814 }
3815 return true;
3816}
3817
Bob Wilson324f4f12009-12-03 06:40:55 +00003818/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3819/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3820/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3821static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3822 unsigned &WhichResult) {
3823 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3824 if (EltSz == 64)
3825 return false;
3826
3827 unsigned NumElts = VT.getVectorNumElements();
3828 WhichResult = (M[0] == 0 ? 0 : 1);
3829 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003830 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3831 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003832 return false;
3833 }
3834 return true;
3835}
3836
Bob Wilsonc692cb72009-08-21 20:54:19 +00003837static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3838 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003839 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3840 if (EltSz == 64)
3841 return false;
3842
Bob Wilsonc692cb72009-08-21 20:54:19 +00003843 unsigned NumElts = VT.getVectorNumElements();
3844 WhichResult = (M[0] == 0 ? 0 : 1);
3845 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003846 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003847 if ((unsigned) M[i] != 2 * i + WhichResult)
3848 return false;
3849 }
3850
3851 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003852 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003853 return false;
3854
3855 return true;
3856}
3857
Bob Wilson324f4f12009-12-03 06:40:55 +00003858/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3859/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3860/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3861static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3862 unsigned &WhichResult) {
3863 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3864 if (EltSz == 64)
3865 return false;
3866
3867 unsigned Half = VT.getVectorNumElements() / 2;
3868 WhichResult = (M[0] == 0 ? 0 : 1);
3869 for (unsigned j = 0; j != 2; ++j) {
3870 unsigned Idx = WhichResult;
3871 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003872 int MIdx = M[i + j * Half];
3873 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003874 return false;
3875 Idx += 2;
3876 }
3877 }
3878
3879 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3880 if (VT.is64BitVector() && EltSz == 32)
3881 return false;
3882
3883 return true;
3884}
3885
Bob Wilsonc692cb72009-08-21 20:54:19 +00003886static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3887 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003888 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3889 if (EltSz == 64)
3890 return false;
3891
Bob Wilsonc692cb72009-08-21 20:54:19 +00003892 unsigned NumElts = VT.getVectorNumElements();
3893 WhichResult = (M[0] == 0 ? 0 : 1);
3894 unsigned Idx = WhichResult * NumElts / 2;
3895 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003896 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3897 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003898 return false;
3899 Idx += 1;
3900 }
3901
3902 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003903 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003904 return false;
3905
3906 return true;
3907}
3908
Bob Wilson324f4f12009-12-03 06:40:55 +00003909/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3910/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3911/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3912static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3913 unsigned &WhichResult) {
3914 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3915 if (EltSz == 64)
3916 return false;
3917
3918 unsigned NumElts = VT.getVectorNumElements();
3919 WhichResult = (M[0] == 0 ? 0 : 1);
3920 unsigned Idx = WhichResult * NumElts / 2;
3921 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003922 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3923 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003924 return false;
3925 Idx += 1;
3926 }
3927
3928 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3929 if (VT.is64BitVector() && EltSz == 32)
3930 return false;
3931
3932 return true;
3933}
3934
Dale Johannesenf630c712010-07-29 20:10:08 +00003935// If N is an integer constant that can be moved into a register in one
3936// instruction, return an SDValue of such a constant (will become a MOV
3937// instruction). Otherwise return null.
3938static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3939 const ARMSubtarget *ST, DebugLoc dl) {
3940 uint64_t Val;
3941 if (!isa<ConstantSDNode>(N))
3942 return SDValue();
3943 Val = cast<ConstantSDNode>(N)->getZExtValue();
3944
3945 if (ST->isThumb1Only()) {
3946 if (Val <= 255 || ~Val <= 255)
3947 return DAG.getConstant(Val, MVT::i32);
3948 } else {
3949 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3950 return DAG.getConstant(Val, MVT::i32);
3951 }
3952 return SDValue();
3953}
3954
Bob Wilson5bafff32009-06-22 23:27:02 +00003955// If this is a case we can't handle, return null and let the default
3956// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003957SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3958 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003959 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003960 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003961 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003962
3963 APInt SplatBits, SplatUndef;
3964 unsigned SplatBitSize;
3965 bool HasAnyUndefs;
3966 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003967 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003968 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003969 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003970 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003971 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003972 DAG, VmovVT, VT.is128BitVector(),
3973 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003974 if (Val.getNode()) {
3975 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003976 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003977 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003978
3979 // Try an immediate VMVN.
Eli Friedman8e4d0422011-10-13 22:40:23 +00003980 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003981 Val = isNEONModifiedImm(NegatedImm,
3982 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003983 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003984 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003985 if (Val.getNode()) {
3986 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003987 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003988 }
Evan Chengeaa192a2011-11-15 02:12:34 +00003989
3990 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
3991 if (VT == MVT::v2f32 || VT == MVT::v4f32) {
3992 ConstantFPSDNode *C = cast<ConstantFPSDNode>(Op.getOperand(0));
3993 int ImmVal = ARM_AM::getFP32Imm(C->getValueAPF());
3994 if (ImmVal != -1) {
3995 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
3996 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
3997 }
3998 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003999 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00004000 }
4001
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004002 // Scan through the operands to see if only one value is used.
4003 unsigned NumElts = VT.getVectorNumElements();
4004 bool isOnlyLowElement = true;
4005 bool usesOnlyOneValue = true;
4006 bool isConstant = true;
4007 SDValue Value;
4008 for (unsigned i = 0; i < NumElts; ++i) {
4009 SDValue V = Op.getOperand(i);
4010 if (V.getOpcode() == ISD::UNDEF)
4011 continue;
4012 if (i > 0)
4013 isOnlyLowElement = false;
4014 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4015 isConstant = false;
4016
4017 if (!Value.getNode())
4018 Value = V;
4019 else if (V != Value)
4020 usesOnlyOneValue = false;
4021 }
4022
4023 if (!Value.getNode())
4024 return DAG.getUNDEF(VT);
4025
4026 if (isOnlyLowElement)
4027 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4028
Dale Johannesenf630c712010-07-29 20:10:08 +00004029 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4030
Dale Johannesen575cd142010-10-19 20:00:17 +00004031 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4032 // i32 and try again.
4033 if (usesOnlyOneValue && EltSize <= 32) {
4034 if (!isConstant)
4035 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4036 if (VT.getVectorElementType().isFloatingPoint()) {
4037 SmallVector<SDValue, 8> Ops;
4038 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004039 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00004040 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00004041 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
4042 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00004043 Val = LowerBUILD_VECTOR(Val, DAG, ST);
4044 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004045 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004046 }
Dale Johannesen575cd142010-10-19 20:00:17 +00004047 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
4048 if (Val.getNode())
4049 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004050 }
4051
4052 // If all elements are constants and the case above didn't get hit, fall back
4053 // to the default expansion, which will generate a load from the constant
4054 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004055 if (isConstant)
4056 return SDValue();
4057
Bob Wilson11a1dff2011-01-07 21:37:30 +00004058 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4059 if (NumElts >= 4) {
4060 SDValue shuffle = ReconstructShuffle(Op, DAG);
4061 if (shuffle != SDValue())
4062 return shuffle;
4063 }
4064
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004065 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004066 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4067 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004068 if (EltSize >= 32) {
4069 // Do the expansion with floating-point types, since that is what the VFP
4070 // registers are defined to use, and since i64 is not legal.
4071 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4072 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004073 SmallVector<SDValue, 8> Ops;
4074 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004075 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004076 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004077 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004078 }
4079
4080 return SDValue();
4081}
4082
Bob Wilson11a1dff2011-01-07 21:37:30 +00004083// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004084// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004085SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4086 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004087 DebugLoc dl = Op.getDebugLoc();
4088 EVT VT = Op.getValueType();
4089 unsigned NumElts = VT.getVectorNumElements();
4090
4091 SmallVector<SDValue, 2> SourceVecs;
4092 SmallVector<unsigned, 2> MinElts;
4093 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004094
Bob Wilson11a1dff2011-01-07 21:37:30 +00004095 for (unsigned i = 0; i < NumElts; ++i) {
4096 SDValue V = Op.getOperand(i);
4097 if (V.getOpcode() == ISD::UNDEF)
4098 continue;
4099 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4100 // A shuffle can only come from building a vector from various
4101 // elements of other vectors.
4102 return SDValue();
Eli Friedman46995fa2011-10-14 23:58:49 +00004103 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
4104 VT.getVectorElementType()) {
4105 // This code doesn't know how to handle shuffles where the vector
4106 // element types do not match (this happens because type legalization
4107 // promotes the return type of EXTRACT_VECTOR_ELT).
4108 // FIXME: It might be appropriate to extend this code to handle
4109 // mismatched types.
4110 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004111 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004112
Bob Wilson11a1dff2011-01-07 21:37:30 +00004113 // Record this extraction against the appropriate vector if possible...
4114 SDValue SourceVec = V.getOperand(0);
4115 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4116 bool FoundSource = false;
4117 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4118 if (SourceVecs[j] == SourceVec) {
4119 if (MinElts[j] > EltNo)
4120 MinElts[j] = EltNo;
4121 if (MaxElts[j] < EltNo)
4122 MaxElts[j] = EltNo;
4123 FoundSource = true;
4124 break;
4125 }
4126 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004127
Bob Wilson11a1dff2011-01-07 21:37:30 +00004128 // Or record a new source if not...
4129 if (!FoundSource) {
4130 SourceVecs.push_back(SourceVec);
4131 MinElts.push_back(EltNo);
4132 MaxElts.push_back(EltNo);
4133 }
4134 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004135
Bob Wilson11a1dff2011-01-07 21:37:30 +00004136 // Currently only do something sane when at most two source vectors
4137 // involved.
4138 if (SourceVecs.size() > 2)
4139 return SDValue();
4140
4141 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4142 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004143
Bob Wilson11a1dff2011-01-07 21:37:30 +00004144 // This loop extracts the usage patterns of the source vectors
4145 // and prepares appropriate SDValues for a shuffle if possible.
4146 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4147 if (SourceVecs[i].getValueType() == VT) {
4148 // No VEXT necessary
4149 ShuffleSrcs[i] = SourceVecs[i];
4150 VEXTOffsets[i] = 0;
4151 continue;
4152 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4153 // It probably isn't worth padding out a smaller vector just to
4154 // break it down again in a shuffle.
4155 return SDValue();
4156 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004157
Bob Wilson11a1dff2011-01-07 21:37:30 +00004158 // Since only 64-bit and 128-bit vectors are legal on ARM and
4159 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004160 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4161 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004162
Bob Wilson11a1dff2011-01-07 21:37:30 +00004163 if (MaxElts[i] - MinElts[i] >= NumElts) {
4164 // Span too large for a VEXT to cope
4165 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004166 }
4167
Bob Wilson11a1dff2011-01-07 21:37:30 +00004168 if (MinElts[i] >= NumElts) {
4169 // The extraction can just take the second half
4170 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004171 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4172 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004173 DAG.getIntPtrConstant(NumElts));
4174 } else if (MaxElts[i] < NumElts) {
4175 // The extraction can just take the first half
4176 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004177 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4178 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004179 DAG.getIntPtrConstant(0));
4180 } else {
4181 // An actual VEXT is needed
4182 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004183 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4184 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004185 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004186 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4187 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004188 DAG.getIntPtrConstant(NumElts));
4189 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4190 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4191 }
4192 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004193
Bob Wilson11a1dff2011-01-07 21:37:30 +00004194 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004195
Bob Wilson11a1dff2011-01-07 21:37:30 +00004196 for (unsigned i = 0; i < NumElts; ++i) {
4197 SDValue Entry = Op.getOperand(i);
4198 if (Entry.getOpcode() == ISD::UNDEF) {
4199 Mask.push_back(-1);
4200 continue;
4201 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004202
Bob Wilson11a1dff2011-01-07 21:37:30 +00004203 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004204 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4205 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004206 if (ExtractVec == SourceVecs[0]) {
4207 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4208 } else {
4209 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4210 }
4211 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004212
Bob Wilson11a1dff2011-01-07 21:37:30 +00004213 // Final check before we try to produce nonsense...
4214 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004215 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4216 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004217
Bob Wilson11a1dff2011-01-07 21:37:30 +00004218 return SDValue();
4219}
4220
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004221/// isShuffleMaskLegal - Targets can use this to indicate that they only
4222/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4223/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4224/// are assumed to be legal.
4225bool
4226ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4227 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004228 if (VT.getVectorNumElements() == 4 &&
4229 (VT.is128BitVector() || VT.is64BitVector())) {
4230 unsigned PFIndexes[4];
4231 for (unsigned i = 0; i != 4; ++i) {
4232 if (M[i] < 0)
4233 PFIndexes[i] = 8;
4234 else
4235 PFIndexes[i] = M[i];
4236 }
4237
4238 // Compute the index in the perfect shuffle table.
4239 unsigned PFTableIndex =
4240 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4241 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4242 unsigned Cost = (PFEntry >> 30);
4243
4244 if (Cost <= 4)
4245 return true;
4246 }
4247
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004248 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004249 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004250
Bob Wilson53dd2452010-06-07 23:53:38 +00004251 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4252 return (EltSize >= 32 ||
4253 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004254 isVREVMask(M, VT, 64) ||
4255 isVREVMask(M, VT, 32) ||
4256 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004257 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004258 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004259 isVTRNMask(M, VT, WhichResult) ||
4260 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004261 isVZIPMask(M, VT, WhichResult) ||
4262 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4263 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4264 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004265}
4266
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004267/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4268/// the specified operations to build the shuffle.
4269static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4270 SDValue RHS, SelectionDAG &DAG,
4271 DebugLoc dl) {
4272 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4273 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4274 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4275
4276 enum {
4277 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4278 OP_VREV,
4279 OP_VDUP0,
4280 OP_VDUP1,
4281 OP_VDUP2,
4282 OP_VDUP3,
4283 OP_VEXT1,
4284 OP_VEXT2,
4285 OP_VEXT3,
4286 OP_VUZPL, // VUZP, left result
4287 OP_VUZPR, // VUZP, right result
4288 OP_VZIPL, // VZIP, left result
4289 OP_VZIPR, // VZIP, right result
4290 OP_VTRNL, // VTRN, left result
4291 OP_VTRNR // VTRN, right result
4292 };
4293
4294 if (OpNum == OP_COPY) {
4295 if (LHSID == (1*9+2)*9+3) return LHS;
4296 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4297 return RHS;
4298 }
4299
4300 SDValue OpLHS, OpRHS;
4301 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4302 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4303 EVT VT = OpLHS.getValueType();
4304
4305 switch (OpNum) {
4306 default: llvm_unreachable("Unknown shuffle opcode!");
4307 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004308 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004309 if (VT.getVectorElementType() == MVT::i32 ||
4310 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004311 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4312 // vrev <4 x i16> -> VREV32
4313 if (VT.getVectorElementType() == MVT::i16)
4314 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4315 // vrev <4 x i8> -> VREV16
4316 assert(VT.getVectorElementType() == MVT::i8);
4317 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004318 case OP_VDUP0:
4319 case OP_VDUP1:
4320 case OP_VDUP2:
4321 case OP_VDUP3:
4322 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004323 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004324 case OP_VEXT1:
4325 case OP_VEXT2:
4326 case OP_VEXT3:
4327 return DAG.getNode(ARMISD::VEXT, dl, VT,
4328 OpLHS, OpRHS,
4329 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4330 case OP_VUZPL:
4331 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004332 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004333 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4334 case OP_VZIPL:
4335 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004336 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004337 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4338 case OP_VTRNL:
4339 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004340 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4341 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004342 }
4343}
4344
Bill Wendling69a05a72011-03-14 23:02:38 +00004345static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
4346 SmallVectorImpl<int> &ShuffleMask,
4347 SelectionDAG &DAG) {
4348 // Check to see if we can use the VTBL instruction.
4349 SDValue V1 = Op.getOperand(0);
4350 SDValue V2 = Op.getOperand(1);
4351 DebugLoc DL = Op.getDebugLoc();
4352
4353 SmallVector<SDValue, 8> VTBLMask;
4354 for (SmallVectorImpl<int>::iterator
4355 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4356 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4357
4358 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4359 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4360 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4361 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004362
Owen Anderson76706012011-04-05 21:48:57 +00004363 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004364 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4365 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004366}
4367
Bob Wilson5bafff32009-06-22 23:27:02 +00004368static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004369 SDValue V1 = Op.getOperand(0);
4370 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004371 DebugLoc dl = Op.getDebugLoc();
4372 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004373 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004374 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00004375
Bob Wilson28865062009-08-13 02:13:04 +00004376 // Convert shuffles that are directly supported on NEON to target-specific
4377 // DAG nodes, instead of keeping them as shuffles and matching them again
4378 // during code selection. This is more efficient and avoids the possibility
4379 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004380 // FIXME: floating-point vectors should be canonicalized to integer vectors
4381 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004382 SVN->getMask(ShuffleMask);
4383
Bob Wilson53dd2452010-06-07 23:53:38 +00004384 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4385 if (EltSize <= 32) {
4386 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4387 int Lane = SVN->getSplatIndex();
4388 // If this is undef splat, generate it via "just" vdup, if possible.
4389 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004390
Dan Gohman65fd6562011-11-03 21:49:52 +00004391 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson53dd2452010-06-07 23:53:38 +00004392 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4393 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4394 }
Dan Gohman65fd6562011-11-03 21:49:52 +00004395 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
4396 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
4397 // reaches it).
4398 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
4399 !isa<ConstantSDNode>(V1.getOperand(0))) {
4400 bool IsScalarToVector = true;
4401 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
4402 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
4403 IsScalarToVector = false;
4404 break;
4405 }
4406 if (IsScalarToVector)
4407 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4408 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004409 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4410 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004411 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004412
4413 bool ReverseVEXT;
4414 unsigned Imm;
4415 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4416 if (ReverseVEXT)
4417 std::swap(V1, V2);
4418 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4419 DAG.getConstant(Imm, MVT::i32));
4420 }
4421
4422 if (isVREVMask(ShuffleMask, VT, 64))
4423 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4424 if (isVREVMask(ShuffleMask, VT, 32))
4425 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4426 if (isVREVMask(ShuffleMask, VT, 16))
4427 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4428
4429 // Check for Neon shuffles that modify both input vectors in place.
4430 // If both results are used, i.e., if there are two shuffles with the same
4431 // source operands and with masks corresponding to both results of one of
4432 // these operations, DAG memoization will ensure that a single node is
4433 // used for both shuffles.
4434 unsigned WhichResult;
4435 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4436 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4437 V1, V2).getValue(WhichResult);
4438 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4439 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4440 V1, V2).getValue(WhichResult);
4441 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4442 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4443 V1, V2).getValue(WhichResult);
4444
4445 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4446 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4447 V1, V1).getValue(WhichResult);
4448 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4449 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4450 V1, V1).getValue(WhichResult);
4451 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4452 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4453 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004454 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004455
Bob Wilsonc692cb72009-08-21 20:54:19 +00004456 // If the shuffle is not directly supported and it has 4 elements, use
4457 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004458 unsigned NumElts = VT.getVectorNumElements();
4459 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004460 unsigned PFIndexes[4];
4461 for (unsigned i = 0; i != 4; ++i) {
4462 if (ShuffleMask[i] < 0)
4463 PFIndexes[i] = 8;
4464 else
4465 PFIndexes[i] = ShuffleMask[i];
4466 }
4467
4468 // Compute the index in the perfect shuffle table.
4469 unsigned PFTableIndex =
4470 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004471 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4472 unsigned Cost = (PFEntry >> 30);
4473
4474 if (Cost <= 4)
4475 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4476 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004477
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004478 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004479 if (EltSize >= 32) {
4480 // Do the expansion with floating-point types, since that is what the VFP
4481 // registers are defined to use, and since i64 is not legal.
4482 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4483 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004484 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4485 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004486 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004487 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004488 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004489 Ops.push_back(DAG.getUNDEF(EltVT));
4490 else
4491 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4492 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4493 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4494 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004495 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004496 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004497 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004498 }
4499
Bill Wendling69a05a72011-03-14 23:02:38 +00004500 if (VT == MVT::v8i8) {
4501 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4502 if (NewOp.getNode())
4503 return NewOp;
4504 }
4505
Bob Wilson22cac0d2009-08-14 05:16:33 +00004506 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004507}
4508
Eli Friedman5c89cb82011-10-24 23:08:52 +00004509static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
4510 // INSERT_VECTOR_ELT is legal only for immediate indexes.
4511 SDValue Lane = Op.getOperand(2);
4512 if (!isa<ConstantSDNode>(Lane))
4513 return SDValue();
4514
4515 return Op;
4516}
4517
Bob Wilson5bafff32009-06-22 23:27:02 +00004518static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004519 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004520 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004521 if (!isa<ConstantSDNode>(Lane))
4522 return SDValue();
4523
4524 SDValue Vec = Op.getOperand(0);
4525 if (Op.getValueType() == MVT::i32 &&
4526 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4527 DebugLoc dl = Op.getDebugLoc();
4528 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4529 }
4530
4531 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004532}
4533
Bob Wilsona6d65862009-08-03 20:36:38 +00004534static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4535 // The only time a CONCAT_VECTORS operation can have legal types is when
4536 // two 64-bit vectors are concatenated to a 128-bit vector.
4537 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4538 "unexpected CONCAT_VECTORS");
4539 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004540 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004541 SDValue Op0 = Op.getOperand(0);
4542 SDValue Op1 = Op.getOperand(1);
4543 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004544 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004545 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004546 DAG.getIntPtrConstant(0));
4547 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004548 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004549 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004550 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004551 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004552}
4553
Bob Wilson626613d2010-11-23 19:38:38 +00004554/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4555/// element has been zero/sign-extended, depending on the isSigned parameter,
4556/// from an integer type half its size.
4557static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4558 bool isSigned) {
4559 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4560 EVT VT = N->getValueType(0);
4561 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4562 SDNode *BVN = N->getOperand(0).getNode();
4563 if (BVN->getValueType(0) != MVT::v4i32 ||
4564 BVN->getOpcode() != ISD::BUILD_VECTOR)
4565 return false;
4566 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4567 unsigned HiElt = 1 - LoElt;
4568 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4569 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4570 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4571 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4572 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4573 return false;
4574 if (isSigned) {
4575 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4576 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4577 return true;
4578 } else {
4579 if (Hi0->isNullValue() && Hi1->isNullValue())
4580 return true;
4581 }
4582 return false;
4583 }
4584
4585 if (N->getOpcode() != ISD::BUILD_VECTOR)
4586 return false;
4587
4588 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4589 SDNode *Elt = N->getOperand(i).getNode();
4590 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4591 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4592 unsigned HalfSize = EltSize / 2;
4593 if (isSigned) {
Bob Wilson9d45de22011-10-18 18:46:49 +00004594 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004595 return false;
4596 } else {
Bob Wilson9d45de22011-10-18 18:46:49 +00004597 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004598 return false;
4599 }
4600 continue;
4601 }
4602 return false;
4603 }
4604
4605 return true;
4606}
4607
4608/// isSignExtended - Check if a node is a vector value that is sign-extended
4609/// or a constant BUILD_VECTOR with sign-extended elements.
4610static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4611 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4612 return true;
4613 if (isExtendedBUILD_VECTOR(N, DAG, true))
4614 return true;
4615 return false;
4616}
4617
4618/// isZeroExtended - Check if a node is a vector value that is zero-extended
4619/// or a constant BUILD_VECTOR with zero-extended elements.
4620static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4621 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4622 return true;
4623 if (isExtendedBUILD_VECTOR(N, DAG, false))
4624 return true;
4625 return false;
4626}
4627
4628/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4629/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004630static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4631 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4632 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004633 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4634 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4635 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004636 LD->isNonTemporal(), LD->isInvariant(),
4637 LD->getAlignment());
Bob Wilson626613d2010-11-23 19:38:38 +00004638 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4639 // have been legalized as a BITCAST from v4i32.
4640 if (N->getOpcode() == ISD::BITCAST) {
4641 SDNode *BVN = N->getOperand(0).getNode();
4642 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4643 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4644 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4645 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4646 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4647 }
4648 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4649 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4650 EVT VT = N->getValueType(0);
4651 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4652 unsigned NumElts = VT.getVectorNumElements();
4653 MVT TruncVT = MVT::getIntegerVT(EltSize);
4654 SmallVector<SDValue, 8> Ops;
4655 for (unsigned i = 0; i != NumElts; ++i) {
4656 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4657 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004658 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004659 }
4660 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4661 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004662}
4663
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004664static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4665 unsigned Opcode = N->getOpcode();
4666 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4667 SDNode *N0 = N->getOperand(0).getNode();
4668 SDNode *N1 = N->getOperand(1).getNode();
4669 return N0->hasOneUse() && N1->hasOneUse() &&
4670 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4671 }
4672 return false;
4673}
4674
4675static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4676 unsigned Opcode = N->getOpcode();
4677 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4678 SDNode *N0 = N->getOperand(0).getNode();
4679 SDNode *N1 = N->getOperand(1).getNode();
4680 return N0->hasOneUse() && N1->hasOneUse() &&
4681 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4682 }
4683 return false;
4684}
4685
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004686static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4687 // Multiplications are only custom-lowered for 128-bit vectors so that
4688 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4689 EVT VT = Op.getValueType();
4690 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4691 SDNode *N0 = Op.getOperand(0).getNode();
4692 SDNode *N1 = Op.getOperand(1).getNode();
4693 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004694 bool isMLA = false;
4695 bool isN0SExt = isSignExtended(N0, DAG);
4696 bool isN1SExt = isSignExtended(N1, DAG);
4697 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004698 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004699 else {
4700 bool isN0ZExt = isZeroExtended(N0, DAG);
4701 bool isN1ZExt = isZeroExtended(N1, DAG);
4702 if (isN0ZExt && isN1ZExt)
4703 NewOpc = ARMISD::VMULLu;
4704 else if (isN1SExt || isN1ZExt) {
4705 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4706 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4707 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4708 NewOpc = ARMISD::VMULLs;
4709 isMLA = true;
4710 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4711 NewOpc = ARMISD::VMULLu;
4712 isMLA = true;
4713 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4714 std::swap(N0, N1);
4715 NewOpc = ARMISD::VMULLu;
4716 isMLA = true;
4717 }
4718 }
4719
4720 if (!NewOpc) {
4721 if (VT == MVT::v2i64)
4722 // Fall through to expand this. It is not legal.
4723 return SDValue();
4724 else
4725 // Other vector multiplications are legal.
4726 return Op;
4727 }
4728 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004729
4730 // Legalize to a VMULL instruction.
4731 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004732 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004733 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004734 if (!isMLA) {
4735 Op0 = SkipExtension(N0, DAG);
4736 assert(Op0.getValueType().is64BitVector() &&
4737 Op1.getValueType().is64BitVector() &&
4738 "unexpected types for extended operands to VMULL");
4739 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4740 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004741
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004742 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4743 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4744 // vmull q0, d4, d6
4745 // vmlal q0, d5, d6
4746 // is faster than
4747 // vaddl q0, d4, d5
4748 // vmovl q1, d6
4749 // vmul q0, q0, q1
4750 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4751 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4752 EVT Op1VT = Op1.getValueType();
4753 return DAG.getNode(N0->getOpcode(), DL, VT,
4754 DAG.getNode(NewOpc, DL, VT,
4755 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4756 DAG.getNode(NewOpc, DL, VT,
4757 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004758}
4759
Owen Anderson76706012011-04-05 21:48:57 +00004760static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004761LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4762 // Convert to float
4763 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4764 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4765 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4766 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4767 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4768 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4769 // Get reciprocal estimate.
4770 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004771 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004772 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4773 // Because char has a smaller range than uchar, we can actually get away
4774 // without any newton steps. This requires that we use a weird bias
4775 // of 0xb000, however (again, this has been exhaustively tested).
4776 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4777 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4778 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4779 Y = DAG.getConstant(0xb000, MVT::i32);
4780 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4781 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4782 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4783 // Convert back to short.
4784 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4785 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4786 return X;
4787}
4788
Owen Anderson76706012011-04-05 21:48:57 +00004789static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004790LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4791 SDValue N2;
4792 // Convert to float.
4793 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4794 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4795 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4796 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4797 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4798 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004799
Nate Begeman7973f352011-02-11 20:53:29 +00004800 // Use reciprocal estimate and one refinement step.
4801 // float4 recip = vrecpeq_f32(yf);
4802 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004803 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004804 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004805 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004806 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4807 N1, N2);
4808 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4809 // Because short has a smaller range than ushort, we can actually get away
4810 // with only a single newton step. This requires that we use a weird bias
4811 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004812 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004813 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4814 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004815 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004816 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4817 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4818 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4819 // Convert back to integer and return.
4820 // return vmovn_s32(vcvt_s32_f32(result));
4821 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4822 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4823 return N0;
4824}
4825
4826static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4827 EVT VT = Op.getValueType();
4828 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4829 "unexpected type for custom-lowering ISD::SDIV");
4830
4831 DebugLoc dl = Op.getDebugLoc();
4832 SDValue N0 = Op.getOperand(0);
4833 SDValue N1 = Op.getOperand(1);
4834 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004835
Nate Begeman7973f352011-02-11 20:53:29 +00004836 if (VT == MVT::v8i8) {
4837 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4838 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004839
Nate Begeman7973f352011-02-11 20:53:29 +00004840 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4841 DAG.getIntPtrConstant(4));
4842 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004843 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004844 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4845 DAG.getIntPtrConstant(0));
4846 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4847 DAG.getIntPtrConstant(0));
4848
4849 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4850 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4851
4852 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4853 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004854
Nate Begeman7973f352011-02-11 20:53:29 +00004855 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
4856 return N0;
4857 }
4858 return LowerSDIV_v4i16(N0, N1, dl, DAG);
4859}
4860
4861static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
4862 EVT VT = Op.getValueType();
4863 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4864 "unexpected type for custom-lowering ISD::UDIV");
4865
4866 DebugLoc dl = Op.getDebugLoc();
4867 SDValue N0 = Op.getOperand(0);
4868 SDValue N1 = Op.getOperand(1);
4869 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004870
Nate Begeman7973f352011-02-11 20:53:29 +00004871 if (VT == MVT::v8i8) {
4872 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
4873 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004874
Nate Begeman7973f352011-02-11 20:53:29 +00004875 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4876 DAG.getIntPtrConstant(4));
4877 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004878 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004879 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4880 DAG.getIntPtrConstant(0));
4881 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4882 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00004883
Nate Begeman7973f352011-02-11 20:53:29 +00004884 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
4885 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00004886
Nate Begeman7973f352011-02-11 20:53:29 +00004887 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4888 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004889
4890 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00004891 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
4892 N0);
4893 return N0;
4894 }
Owen Anderson76706012011-04-05 21:48:57 +00004895
Nate Begeman7973f352011-02-11 20:53:29 +00004896 // v4i16 sdiv ... Convert to float.
4897 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
4898 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
4899 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
4900 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
4901 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004902 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00004903
4904 // Use reciprocal estimate and two refinement steps.
4905 // float4 recip = vrecpeq_f32(yf);
4906 // recip *= vrecpsq_f32(yf, recip);
4907 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004908 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004909 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00004910 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004911 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004912 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004913 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00004914 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004915 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004916 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004917 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4918 // Simply multiplying by the reciprocal estimate can leave us a few ulps
4919 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
4920 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004921 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00004922 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4923 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4924 N1 = DAG.getConstant(2, MVT::i32);
4925 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4926 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4927 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4928 // Convert back to integer and return.
4929 // return vmovn_u32(vcvt_s32_f32(result));
4930 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4931 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4932 return N0;
4933}
4934
Evan Cheng342e3162011-08-30 01:34:54 +00004935static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
4936 EVT VT = Op.getNode()->getValueType(0);
4937 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
4938
4939 unsigned Opc;
4940 bool ExtraOp = false;
4941 switch (Op.getOpcode()) {
4942 default: assert(0 && "Invalid code");
4943 case ISD::ADDC: Opc = ARMISD::ADDC; break;
4944 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
4945 case ISD::SUBC: Opc = ARMISD::SUBC; break;
4946 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
4947 }
4948
4949 if (!ExtraOp)
4950 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
4951 Op.getOperand(1));
4952 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
4953 Op.getOperand(1), Op.getOperand(2));
4954}
4955
Eli Friedman74bf18c2011-09-15 22:26:18 +00004956static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedman7cc15662011-09-15 22:18:49 +00004957 // Monotonic load/store is legal for all targets
4958 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
4959 return Op;
4960
4961 // Aquire/Release load/store is not legal for targets without a
4962 // dmb or equivalent available.
4963 return SDValue();
4964}
4965
4966
Eli Friedman2bdffe42011-08-31 00:31:29 +00004967static void
Eli Friedman4d3f3292011-08-31 17:52:22 +00004968ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
4969 SelectionDAG &DAG, unsigned NewOp) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00004970 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +00004971 assert (Node->getValueType(0) == MVT::i64 &&
4972 "Only know how to expand i64 atomics");
Eli Friedman2bdffe42011-08-31 00:31:29 +00004973
Eli Friedman4d3f3292011-08-31 17:52:22 +00004974 SmallVector<SDValue, 6> Ops;
4975 Ops.push_back(Node->getOperand(0)); // Chain
4976 Ops.push_back(Node->getOperand(1)); // Ptr
4977 // Low part of Val1
4978 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4979 Node->getOperand(2), DAG.getIntPtrConstant(0)));
4980 // High part of Val1
4981 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4982 Node->getOperand(2), DAG.getIntPtrConstant(1)));
Andrew Trick3af7a672011-09-20 03:06:13 +00004983 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) {
Eli Friedman4d3f3292011-08-31 17:52:22 +00004984 // High part of Val1
4985 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4986 Node->getOperand(3), DAG.getIntPtrConstant(0)));
4987 // High part of Val2
4988 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4989 Node->getOperand(3), DAG.getIntPtrConstant(1)));
4990 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00004991 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
4992 SDValue Result =
Eli Friedman4d3f3292011-08-31 17:52:22 +00004993 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64,
Eli Friedman2bdffe42011-08-31 00:31:29 +00004994 cast<MemSDNode>(Node)->getMemOperand());
Eli Friedman4d3f3292011-08-31 17:52:22 +00004995 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) };
Eli Friedman2bdffe42011-08-31 00:31:29 +00004996 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
4997 Results.push_back(Result.getValue(2));
4998}
4999
Dan Gohmand858e902010-04-17 15:26:15 +00005000SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005001 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005002 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00005003 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00005004 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005005 case ISD::GlobalAddress:
5006 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
5007 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00005008 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00005009 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00005010 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
5011 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005012 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00005013 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00005014 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00005015 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00005016 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00005017 case ISD::SINT_TO_FP:
5018 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
5019 case ISD::FP_TO_SINT:
5020 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005021 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00005022 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00005023 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005024 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00005025 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00005026 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbache4ad3872010-10-19 23:27:08 +00005027 case ISD::EH_SJLJ_DISPATCHSETUP: return LowerEH_SJLJ_DISPATCHSETUP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00005028 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
5029 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00005030 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005031 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00005032 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00005033 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00005034 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00005035 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00005036 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00005037 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Duncan Sands28b77e92011-09-06 19:07:46 +00005038 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00005039 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005040 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedman5c89cb82011-10-24 23:08:52 +00005041 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005042 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00005043 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00005044 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005045 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00005046 case ISD::SDIV: return LowerSDIV(Op, DAG);
5047 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00005048 case ISD::ADDC:
5049 case ISD::ADDE:
5050 case ISD::SUBC:
5051 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Eli Friedman7cc15662011-09-15 22:18:49 +00005052 case ISD::ATOMIC_LOAD:
Eli Friedman74bf18c2011-09-15 22:26:18 +00005053 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005054 }
Dan Gohman475871a2008-07-27 21:46:04 +00005055 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00005056}
5057
Duncan Sands1607f052008-12-01 11:39:25 +00005058/// ReplaceNodeResults - Replace the results of node with an illegal result
5059/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00005060void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
5061 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005062 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00005063 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00005064 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00005065 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005066 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00005067 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005068 case ISD::BITCAST:
5069 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005070 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005071 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00005072 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00005073 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005074 break;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005075 case ISD::ATOMIC_LOAD_ADD:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005076 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005077 return;
5078 case ISD::ATOMIC_LOAD_AND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005079 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005080 return;
5081 case ISD::ATOMIC_LOAD_NAND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005082 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005083 return;
5084 case ISD::ATOMIC_LOAD_OR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005085 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005086 return;
5087 case ISD::ATOMIC_LOAD_SUB:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005088 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005089 return;
5090 case ISD::ATOMIC_LOAD_XOR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005091 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005092 return;
5093 case ISD::ATOMIC_SWAP:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005094 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005095 return;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005096 case ISD::ATOMIC_CMP_SWAP:
5097 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG);
5098 return;
Duncan Sands1607f052008-12-01 11:39:25 +00005099 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00005100 if (Res.getNode())
5101 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00005102}
Chris Lattner27a6c732007-11-24 07:07:01 +00005103
Evan Chenga8e29892007-01-19 07:51:42 +00005104//===----------------------------------------------------------------------===//
5105// ARM Scheduler Hooks
5106//===----------------------------------------------------------------------===//
5107
5108MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005109ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
5110 MachineBasicBlock *BB,
5111 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005112 unsigned dest = MI->getOperand(0).getReg();
5113 unsigned ptr = MI->getOperand(1).getReg();
5114 unsigned oldval = MI->getOperand(2).getReg();
5115 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005116 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5117 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005118 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005119
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005120 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5121 unsigned scratch =
Cameron Zwarich141ec632011-05-18 02:29:50 +00005122 MRI.createVirtualRegister(isThumb2 ? ARM::rGPRRegisterClass
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005123 : ARM::GPRRegisterClass);
5124
5125 if (isThumb2) {
Cameron Zwarich141ec632011-05-18 02:29:50 +00005126 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5127 MRI.constrainRegClass(oldval, ARM::rGPRRegisterClass);
5128 MRI.constrainRegClass(newval, ARM::rGPRRegisterClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005129 }
5130
Jim Grosbach5278eb82009-12-11 01:42:04 +00005131 unsigned ldrOpc, strOpc;
5132 switch (Size) {
5133 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005134 case 1:
5135 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00005136 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005137 break;
5138 case 2:
5139 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5140 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5141 break;
5142 case 4:
5143 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5144 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5145 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00005146 }
5147
5148 MachineFunction *MF = BB->getParent();
5149 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5150 MachineFunction::iterator It = BB;
5151 ++It; // insert the new blocks after the current block
5152
5153 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5154 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5155 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5156 MF->insert(It, loop1MBB);
5157 MF->insert(It, loop2MBB);
5158 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005159
5160 // Transfer the remainder of BB and its successor edges to exitMBB.
5161 exitMBB->splice(exitMBB->begin(), BB,
5162 llvm::next(MachineBasicBlock::iterator(MI)),
5163 BB->end());
5164 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005165
5166 // thisMBB:
5167 // ...
5168 // fallthrough --> loop1MBB
5169 BB->addSuccessor(loop1MBB);
5170
5171 // loop1MBB:
5172 // ldrex dest, [ptr]
5173 // cmp dest, oldval
5174 // bne exitMBB
5175 BB = loop1MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005176 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5177 if (ldrOpc == ARM::t2LDREX)
5178 MIB.addImm(0);
5179 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005180 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005181 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005182 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5183 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005184 BB->addSuccessor(loop2MBB);
5185 BB->addSuccessor(exitMBB);
5186
5187 // loop2MBB:
5188 // strex scratch, newval, [ptr]
5189 // cmp scratch, #0
5190 // bne loop1MBB
5191 BB = loop2MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005192 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr);
5193 if (strOpc == ARM::t2STREX)
5194 MIB.addImm(0);
5195 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005196 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005197 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005198 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5199 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005200 BB->addSuccessor(loop1MBB);
5201 BB->addSuccessor(exitMBB);
5202
5203 // exitMBB:
5204 // ...
5205 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005206
Dan Gohman14152b42010-07-06 20:24:04 +00005207 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005208
Jim Grosbach5278eb82009-12-11 01:42:04 +00005209 return BB;
5210}
5211
5212MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005213ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5214 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005215 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5216 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5217
5218 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005219 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005220 MachineFunction::iterator It = BB;
5221 ++It;
5222
5223 unsigned dest = MI->getOperand(0).getReg();
5224 unsigned ptr = MI->getOperand(1).getReg();
5225 unsigned incr = MI->getOperand(2).getReg();
5226 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005227 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005228
5229 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5230 if (isThumb2) {
5231 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5232 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5233 }
5234
Jim Grosbachc3c23542009-12-14 04:22:04 +00005235 unsigned ldrOpc, strOpc;
5236 switch (Size) {
5237 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005238 case 1:
5239 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005240 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005241 break;
5242 case 2:
5243 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5244 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5245 break;
5246 case 4:
5247 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5248 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5249 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005250 }
5251
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005252 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5253 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5254 MF->insert(It, loopMBB);
5255 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005256
5257 // Transfer the remainder of BB and its successor edges to exitMBB.
5258 exitMBB->splice(exitMBB->begin(), BB,
5259 llvm::next(MachineBasicBlock::iterator(MI)),
5260 BB->end());
5261 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005262
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005263 TargetRegisterClass *TRC =
5264 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5265 unsigned scratch = MRI.createVirtualRegister(TRC);
5266 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005267
5268 // thisMBB:
5269 // ...
5270 // fallthrough --> loopMBB
5271 BB->addSuccessor(loopMBB);
5272
5273 // loopMBB:
5274 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005275 // <binop> scratch2, dest, incr
5276 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005277 // cmp scratch, #0
5278 // bne- loopMBB
5279 // fallthrough --> exitMBB
5280 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005281 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5282 if (ldrOpc == ARM::t2LDREX)
5283 MIB.addImm(0);
5284 AddDefaultPred(MIB);
Jim Grosbachc67b5562009-12-15 00:12:35 +00005285 if (BinOpcode) {
5286 // operand order needs to go the other way for NAND
5287 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5288 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5289 addReg(incr).addReg(dest)).addReg(0);
5290 else
5291 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5292 addReg(dest).addReg(incr)).addReg(0);
5293 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005294
Jim Grosbachb6aed502011-09-09 18:37:27 +00005295 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5296 if (strOpc == ARM::t2STREX)
5297 MIB.addImm(0);
5298 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005299 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005300 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005301 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5302 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005303
5304 BB->addSuccessor(loopMBB);
5305 BB->addSuccessor(exitMBB);
5306
5307 // exitMBB:
5308 // ...
5309 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005310
Dan Gohman14152b42010-07-06 20:24:04 +00005311 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005312
Jim Grosbachc3c23542009-12-14 04:22:04 +00005313 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005314}
5315
Jim Grosbachf7da8822011-04-26 19:44:18 +00005316MachineBasicBlock *
5317ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5318 MachineBasicBlock *BB,
5319 unsigned Size,
5320 bool signExtend,
5321 ARMCC::CondCodes Cond) const {
5322 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5323
5324 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5325 MachineFunction *MF = BB->getParent();
5326 MachineFunction::iterator It = BB;
5327 ++It;
5328
5329 unsigned dest = MI->getOperand(0).getReg();
5330 unsigned ptr = MI->getOperand(1).getReg();
5331 unsigned incr = MI->getOperand(2).getReg();
5332 unsigned oldval = dest;
5333 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005334 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005335
5336 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5337 if (isThumb2) {
5338 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5339 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5340 }
5341
Jim Grosbachf7da8822011-04-26 19:44:18 +00005342 unsigned ldrOpc, strOpc, extendOpc;
5343 switch (Size) {
5344 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5345 case 1:
5346 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5347 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005348 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005349 break;
5350 case 2:
5351 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5352 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005353 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005354 break;
5355 case 4:
5356 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5357 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5358 extendOpc = 0;
5359 break;
5360 }
5361
5362 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5363 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5364 MF->insert(It, loopMBB);
5365 MF->insert(It, exitMBB);
5366
5367 // Transfer the remainder of BB and its successor edges to exitMBB.
5368 exitMBB->splice(exitMBB->begin(), BB,
5369 llvm::next(MachineBasicBlock::iterator(MI)),
5370 BB->end());
5371 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5372
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005373 TargetRegisterClass *TRC =
5374 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5375 unsigned scratch = MRI.createVirtualRegister(TRC);
5376 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005377
5378 // thisMBB:
5379 // ...
5380 // fallthrough --> loopMBB
5381 BB->addSuccessor(loopMBB);
5382
5383 // loopMBB:
5384 // ldrex dest, ptr
5385 // (sign extend dest, if required)
5386 // cmp dest, incr
5387 // cmov.cond scratch2, dest, incr
5388 // strex scratch, scratch2, ptr
5389 // cmp scratch, #0
5390 // bne- loopMBB
5391 // fallthrough --> exitMBB
5392 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005393 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5394 if (ldrOpc == ARM::t2LDREX)
5395 MIB.addImm(0);
5396 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005397
5398 // Sign extend the value, if necessary.
5399 if (signExtend && extendOpc) {
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005400 oldval = MRI.createVirtualRegister(ARM::GPRRegisterClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005401 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5402 .addReg(dest)
5403 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005404 }
5405
5406 // Build compare and cmov instructions.
5407 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5408 .addReg(oldval).addReg(incr));
5409 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5410 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5411
Jim Grosbachb6aed502011-09-09 18:37:27 +00005412 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5413 if (strOpc == ARM::t2STREX)
5414 MIB.addImm(0);
5415 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005416 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5417 .addReg(scratch).addImm(0));
5418 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5419 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5420
5421 BB->addSuccessor(loopMBB);
5422 BB->addSuccessor(exitMBB);
5423
5424 // exitMBB:
5425 // ...
5426 BB = exitMBB;
5427
5428 MI->eraseFromParent(); // The instruction is gone now.
5429
5430 return BB;
5431}
5432
Eli Friedman2bdffe42011-08-31 00:31:29 +00005433MachineBasicBlock *
5434ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
5435 unsigned Op1, unsigned Op2,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005436 bool NeedsCarry, bool IsCmpxchg) const {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005437 // This also handles ATOMIC_SWAP, indicated by Op1==0.
5438 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5439
5440 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5441 MachineFunction *MF = BB->getParent();
5442 MachineFunction::iterator It = BB;
5443 ++It;
5444
5445 unsigned destlo = MI->getOperand(0).getReg();
5446 unsigned desthi = MI->getOperand(1).getReg();
5447 unsigned ptr = MI->getOperand(2).getReg();
5448 unsigned vallo = MI->getOperand(3).getReg();
5449 unsigned valhi = MI->getOperand(4).getReg();
5450 DebugLoc dl = MI->getDebugLoc();
5451 bool isThumb2 = Subtarget->isThumb2();
5452
5453 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5454 if (isThumb2) {
5455 MRI.constrainRegClass(destlo, ARM::rGPRRegisterClass);
5456 MRI.constrainRegClass(desthi, ARM::rGPRRegisterClass);
5457 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5458 }
5459
5460 unsigned ldrOpc = isThumb2 ? ARM::t2LDREXD : ARM::LDREXD;
5461 unsigned strOpc = isThumb2 ? ARM::t2STREXD : ARM::STREXD;
5462
5463 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman7df496d2011-09-01 22:27:41 +00005464 MachineBasicBlock *contBB = 0, *cont2BB = 0;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005465 if (IsCmpxchg) {
5466 contBB = MF->CreateMachineBasicBlock(LLVM_BB);
5467 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB);
5468 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005469 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5470 MF->insert(It, loopMBB);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005471 if (IsCmpxchg) {
5472 MF->insert(It, contBB);
5473 MF->insert(It, cont2BB);
5474 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005475 MF->insert(It, exitMBB);
5476
5477 // Transfer the remainder of BB and its successor edges to exitMBB.
5478 exitMBB->splice(exitMBB->begin(), BB,
5479 llvm::next(MachineBasicBlock::iterator(MI)),
5480 BB->end());
5481 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5482
5483 TargetRegisterClass *TRC =
5484 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5485 unsigned storesuccess = MRI.createVirtualRegister(TRC);
5486
5487 // thisMBB:
5488 // ...
5489 // fallthrough --> loopMBB
5490 BB->addSuccessor(loopMBB);
5491
5492 // loopMBB:
5493 // ldrexd r2, r3, ptr
5494 // <binopa> r0, r2, incr
5495 // <binopb> r1, r3, incr
5496 // strexd storesuccess, r0, r1, ptr
5497 // cmp storesuccess, #0
5498 // bne- loopMBB
5499 // fallthrough --> exitMBB
5500 //
5501 // Note that the registers are explicitly specified because there is not any
5502 // way to force the register allocator to allocate a register pair.
5503 //
Andrew Trick3af7a672011-09-20 03:06:13 +00005504 // FIXME: The hardcoded registers are not necessary for Thumb2, but we
Eli Friedman2bdffe42011-08-31 00:31:29 +00005505 // need to properly enforce the restriction that the two output registers
5506 // for ldrexd must be different.
5507 BB = loopMBB;
5508 // Load
5509 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc))
5510 .addReg(ARM::R2, RegState::Define)
5511 .addReg(ARM::R3, RegState::Define).addReg(ptr));
5512 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
5513 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo).addReg(ARM::R2);
5514 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi).addReg(ARM::R3);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005515
5516 if (IsCmpxchg) {
5517 // Add early exit
5518 for (unsigned i = 0; i < 2; i++) {
5519 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr :
5520 ARM::CMPrr))
5521 .addReg(i == 0 ? destlo : desthi)
5522 .addReg(i == 0 ? vallo : valhi));
5523 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5524 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5525 BB->addSuccessor(exitMBB);
5526 BB->addSuccessor(i == 0 ? contBB : cont2BB);
5527 BB = (i == 0 ? contBB : cont2BB);
5528 }
5529
5530 // Copy to physregs for strexd
5531 unsigned setlo = MI->getOperand(5).getReg();
5532 unsigned sethi = MI->getOperand(6).getReg();
5533 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(setlo);
5534 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(sethi);
5535 } else if (Op1) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005536 // Perform binary operation
5537 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), ARM::R0)
5538 .addReg(destlo).addReg(vallo))
5539 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
5540 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), ARM::R1)
5541 .addReg(desthi).addReg(valhi)).addReg(0);
5542 } else {
5543 // Copy to physregs for strexd
5544 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(vallo);
5545 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(valhi);
5546 }
5547
5548 // Store
5549 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), storesuccess)
5550 .addReg(ARM::R0).addReg(ARM::R1).addReg(ptr));
5551 // Cmp+jump
5552 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5553 .addReg(storesuccess).addImm(0));
5554 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5555 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5556
5557 BB->addSuccessor(loopMBB);
5558 BB->addSuccessor(exitMBB);
5559
5560 // exitMBB:
5561 // ...
5562 BB = exitMBB;
5563
5564 MI->eraseFromParent(); // The instruction is gone now.
5565
5566 return BB;
5567}
5568
Bill Wendlingf1083d42011-10-07 22:08:37 +00005569/// EmitBasePointerRecalculation - For functions using a base pointer, we
5570/// rematerialize it (via the frame pointer).
5571void ARMTargetLowering::
5572EmitBasePointerRecalculation(MachineInstr *MI, MachineBasicBlock *MBB,
5573 MachineBasicBlock *DispatchBB) const {
5574 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5575 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
5576 MachineFunction &MF = *MI->getParent()->getParent();
5577 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
5578 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
5579
5580 if (!RI.hasBasePointer(MF)) return;
5581
5582 MachineBasicBlock::iterator MBBI = MI;
5583
5584 int32_t NumBytes = AFI->getFramePtrSpillOffset();
5585 unsigned FramePtr = RI.getFrameRegister(MF);
5586 assert(MF.getTarget().getFrameLowering()->hasFP(MF) &&
5587 "Base pointer without frame pointer?");
5588
5589 if (AFI->isThumb2Function())
5590 llvm::emitT2RegPlusImmediate(*MBB, MBBI, MI->getDebugLoc(), ARM::R6,
5591 FramePtr, -NumBytes, ARMCC::AL, 0, *AII);
5592 else if (AFI->isThumbFunction())
5593 llvm::emitThumbRegPlusImmediate(*MBB, MBBI, MI->getDebugLoc(), ARM::R6,
5594 FramePtr, -NumBytes, *AII, RI);
5595 else
5596 llvm::emitARMRegPlusImmediate(*MBB, MBBI, MI->getDebugLoc(), ARM::R6,
5597 FramePtr, -NumBytes, ARMCC::AL, 0, *AII);
5598
5599 if (!RI.needsStackRealignment(MF)) return;
5600
5601 // If there's dynamic realignment, adjust for it.
5602 MachineFrameInfo *MFI = MF.getFrameInfo();
5603 unsigned MaxAlign = MFI->getMaxAlignment();
5604 assert(!AFI->isThumb1OnlyFunction());
5605
5606 // Emit bic r6, r6, MaxAlign
5607 unsigned bicOpc = AFI->isThumbFunction() ? ARM::t2BICri : ARM::BICri;
5608 AddDefaultCC(
5609 AddDefaultPred(
5610 BuildMI(*MBB, MBBI, MI->getDebugLoc(), TII->get(bicOpc), ARM::R6)
5611 .addReg(ARM::R6, RegState::Kill)
5612 .addImm(MaxAlign - 1)));
5613}
5614
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005615/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
5616/// registers the function context.
5617void ARMTargetLowering::
5618SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
5619 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005620 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5621 DebugLoc dl = MI->getDebugLoc();
5622 MachineFunction *MF = MBB->getParent();
5623 MachineRegisterInfo *MRI = &MF->getRegInfo();
5624 MachineConstantPool *MCP = MF->getConstantPool();
5625 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5626 const Function *F = MF->getFunction();
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005627
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005628 bool isThumb = Subtarget->isThumb();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005629 bool isThumb2 = Subtarget->isThumb2();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005630
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005631 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005632 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005633 ARMConstantPoolValue *CPV =
5634 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
5635 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
5636
5637 const TargetRegisterClass *TRC =
5638 isThumb ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5639
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005640 // Grab constant pool and fixed stack memory operands.
5641 MachineMemOperand *CPMMO =
5642 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
5643 MachineMemOperand::MOLoad, 4, 4);
5644
5645 MachineMemOperand *FIMMOSt =
5646 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
5647 MachineMemOperand::MOStore, 4, 4);
5648
Bill Wendlingf1083d42011-10-07 22:08:37 +00005649 EmitBasePointerRecalculation(MI, MBB, DispatchBB);
5650
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005651 // Load the address of the dispatch MBB into the jump buffer.
5652 if (isThumb2) {
5653 // Incoming value: jbuf
5654 // ldr.n r5, LCPI1_1
5655 // orr r5, r5, #1
5656 // add r5, pc
5657 // str r5, [$jbuf, #+4] ; &jbuf[1]
5658 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5659 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
5660 .addConstantPoolIndex(CPI)
5661 .addMemOperand(CPMMO));
5662 // Set the low bit because of thumb mode.
5663 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5664 AddDefaultCC(
5665 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
5666 .addReg(NewVReg1, RegState::Kill)
5667 .addImm(0x01)));
5668 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5669 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
5670 .addReg(NewVReg2, RegState::Kill)
5671 .addImm(PCLabelId);
5672 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
5673 .addReg(NewVReg3, RegState::Kill)
5674 .addFrameIndex(FI)
5675 .addImm(36) // &jbuf[1] :: pc
5676 .addMemOperand(FIMMOSt));
5677 } else if (isThumb) {
5678 // Incoming value: jbuf
5679 // ldr.n r1, LCPI1_4
5680 // add r1, pc
5681 // mov r2, #1
5682 // orrs r1, r2
5683 // add r2, $jbuf, #+4 ; &jbuf[1]
5684 // str r1, [r2]
5685 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5686 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
5687 .addConstantPoolIndex(CPI)
5688 .addMemOperand(CPMMO));
5689 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5690 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
5691 .addReg(NewVReg1, RegState::Kill)
5692 .addImm(PCLabelId);
5693 // Set the low bit because of thumb mode.
5694 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5695 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
5696 .addReg(ARM::CPSR, RegState::Define)
5697 .addImm(1));
5698 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5699 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
5700 .addReg(ARM::CPSR, RegState::Define)
5701 .addReg(NewVReg2, RegState::Kill)
5702 .addReg(NewVReg3, RegState::Kill));
5703 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5704 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
5705 .addFrameIndex(FI)
5706 .addImm(36)); // &jbuf[1] :: pc
5707 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
5708 .addReg(NewVReg4, RegState::Kill)
5709 .addReg(NewVReg5, RegState::Kill)
5710 .addImm(0)
5711 .addMemOperand(FIMMOSt));
5712 } else {
5713 // Incoming value: jbuf
5714 // ldr r1, LCPI1_1
5715 // add r1, pc, r1
5716 // str r1, [$jbuf, #+4] ; &jbuf[1]
5717 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5718 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
5719 .addConstantPoolIndex(CPI)
5720 .addImm(0)
5721 .addMemOperand(CPMMO));
5722 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5723 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
5724 .addReg(NewVReg1, RegState::Kill)
5725 .addImm(PCLabelId));
5726 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
5727 .addReg(NewVReg2, RegState::Kill)
5728 .addFrameIndex(FI)
5729 .addImm(36) // &jbuf[1] :: pc
5730 .addMemOperand(FIMMOSt));
5731 }
5732}
5733
5734MachineBasicBlock *ARMTargetLowering::
5735EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
5736 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5737 DebugLoc dl = MI->getDebugLoc();
5738 MachineFunction *MF = MBB->getParent();
5739 MachineRegisterInfo *MRI = &MF->getRegInfo();
5740 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5741 MachineFrameInfo *MFI = MF->getFrameInfo();
5742 int FI = MFI->getFunctionContextIndex();
5743
5744 const TargetRegisterClass *TRC =
5745 Subtarget->isThumb() ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5746
Bill Wendling04f15b42011-10-06 21:29:56 +00005747 // Get a mapping of the call site numbers to all of the landing pads they're
5748 // associated with.
Bill Wendling2a850152011-10-05 00:02:33 +00005749 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
5750 unsigned MaxCSNum = 0;
5751 MachineModuleInfo &MMI = MF->getMMI();
5752 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E; ++BB) {
5753 if (!BB->isLandingPad()) continue;
5754
5755 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
5756 // pad.
5757 for (MachineBasicBlock::iterator
5758 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
5759 if (!II->isEHLabel()) continue;
5760
5761 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendling5cbef192011-10-05 23:28:57 +00005762 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling2a850152011-10-05 00:02:33 +00005763
Bill Wendling5cbef192011-10-05 23:28:57 +00005764 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
5765 for (SmallVectorImpl<unsigned>::iterator
5766 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
5767 CSI != CSE; ++CSI) {
5768 CallSiteNumToLPad[*CSI].push_back(BB);
5769 MaxCSNum = std::max(MaxCSNum, *CSI);
5770 }
Bill Wendling2a850152011-10-05 00:02:33 +00005771 break;
5772 }
5773 }
5774
5775 // Get an ordered list of the machine basic blocks for the jump table.
5776 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling2acf6382011-10-07 23:18:02 +00005777 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling2a850152011-10-05 00:02:33 +00005778 LPadList.reserve(CallSiteNumToLPad.size());
5779 for (unsigned I = 1; I <= MaxCSNum; ++I) {
5780 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
5781 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00005782 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling2a850152011-10-05 00:02:33 +00005783 LPadList.push_back(*II);
Bill Wendling2acf6382011-10-07 23:18:02 +00005784 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
5785 }
Bill Wendling2a850152011-10-05 00:02:33 +00005786 }
5787
Bill Wendling5cbef192011-10-05 23:28:57 +00005788 assert(!LPadList.empty() &&
5789 "No landing pad destinations for the dispatch jump table!");
5790
Bill Wendling04f15b42011-10-06 21:29:56 +00005791 // Create the jump table and associated information.
Bill Wendling2a850152011-10-05 00:02:33 +00005792 MachineJumpTableInfo *JTI =
5793 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
5794 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
5795 unsigned UId = AFI->createJumpTableUId();
5796
Bill Wendling04f15b42011-10-06 21:29:56 +00005797 // Create the MBBs for the dispatch code.
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005798
5799 // Shove the dispatch's address into the return slot in the function context.
5800 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
5801 DispatchBB->setIsLandingPad();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005802
Bill Wendlingbb734682011-10-05 00:39:32 +00005803 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Bill Wendling083a8eb2011-10-06 23:37:36 +00005804 BuildMI(TrapBB, dl, TII->get(Subtarget->isThumb() ? ARM::tTRAP : ARM::TRAP));
Bill Wendlingbb734682011-10-05 00:39:32 +00005805 DispatchBB->addSuccessor(TrapBB);
5806
5807 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
5808 DispatchBB->addSuccessor(DispContBB);
Bill Wendling2a850152011-10-05 00:02:33 +00005809
Bill Wendlinga48ed4f2011-10-17 21:32:56 +00005810 // Insert and MBBs.
Bill Wendling930193c2011-10-06 00:53:33 +00005811 MF->insert(MF->end(), DispatchBB);
5812 MF->insert(MF->end(), DispContBB);
5813 MF->insert(MF->end(), TrapBB);
Bill Wendling930193c2011-10-06 00:53:33 +00005814
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005815 // Insert code into the entry block that creates and registers the function
5816 // context.
5817 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
5818
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005819 MachineMemOperand *FIMMOLd =
Bill Wendling04f15b42011-10-06 21:29:56 +00005820 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendling083a8eb2011-10-06 23:37:36 +00005821 MachineMemOperand::MOLoad |
5822 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling930193c2011-10-06 00:53:33 +00005823
Bill Wendling952cb502011-10-18 22:49:07 +00005824 unsigned NumLPads = LPadList.size();
Bill Wendling95ce2e92011-10-06 22:53:00 +00005825 if (Subtarget->isThumb2()) {
5826 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5827 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
5828 .addFrameIndex(FI)
5829 .addImm(4)
5830 .addMemOperand(FIMMOLd));
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005831
Bill Wendling952cb502011-10-18 22:49:07 +00005832 if (NumLPads < 256) {
5833 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
5834 .addReg(NewVReg1)
5835 .addImm(LPadList.size()));
5836 } else {
5837 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5838 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00005839 .addImm(NumLPads & 0xFFFF));
5840
5841 unsigned VReg2 = VReg1;
5842 if ((NumLPads & 0xFFFF0000) != 0) {
5843 VReg2 = MRI->createVirtualRegister(TRC);
5844 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
5845 .addReg(VReg1)
5846 .addImm(NumLPads >> 16));
5847 }
5848
Bill Wendling952cb502011-10-18 22:49:07 +00005849 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
5850 .addReg(NewVReg1)
5851 .addReg(VReg2));
5852 }
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005853
Bill Wendling95ce2e92011-10-06 22:53:00 +00005854 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
5855 .addMBB(TrapBB)
5856 .addImm(ARMCC::HI)
5857 .addReg(ARM::CPSR);
Bill Wendlingbb734682011-10-05 00:39:32 +00005858
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005859 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5860 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005861 .addJumpTableIndex(MJTI)
5862 .addImm(UId));
Bill Wendling2a850152011-10-05 00:02:33 +00005863
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005864 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005865 AddDefaultCC(
5866 AddDefaultPred(
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005867 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
5868 .addReg(NewVReg3, RegState::Kill)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005869 .addReg(NewVReg1)
5870 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
5871
5872 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005873 .addReg(NewVReg4, RegState::Kill)
Bill Wendling2a850152011-10-05 00:02:33 +00005874 .addReg(NewVReg1)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005875 .addJumpTableIndex(MJTI)
5876 .addImm(UId);
5877 } else if (Subtarget->isThumb()) {
Bill Wendling083a8eb2011-10-06 23:37:36 +00005878 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5879 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
5880 .addFrameIndex(FI)
5881 .addImm(1)
5882 .addMemOperand(FIMMOLd));
Bill Wendlingf1083d42011-10-07 22:08:37 +00005883
Bill Wendlinga5871dc2011-10-18 23:11:05 +00005884 if (NumLPads < 256) {
5885 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
5886 .addReg(NewVReg1)
5887 .addImm(NumLPads));
5888 } else {
5889 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling922ad782011-10-19 09:24:02 +00005890 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
5891 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
5892
5893 // MachineConstantPool wants an explicit alignment.
5894 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
5895 if (Align == 0)
5896 Align = getTargetData()->getTypeAllocSize(C->getType());
5897 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendlinga5871dc2011-10-18 23:11:05 +00005898
5899 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5900 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
5901 .addReg(VReg1, RegState::Define)
5902 .addConstantPoolIndex(Idx));
5903 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
5904 .addReg(NewVReg1)
5905 .addReg(VReg1));
5906 }
5907
Bill Wendling083a8eb2011-10-06 23:37:36 +00005908 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
5909 .addMBB(TrapBB)
5910 .addImm(ARMCC::HI)
5911 .addReg(ARM::CPSR);
5912
5913 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5914 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
5915 .addReg(ARM::CPSR, RegState::Define)
5916 .addReg(NewVReg1)
5917 .addImm(2));
5918
5919 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling217f0e92011-10-06 23:41:14 +00005920 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendling083a8eb2011-10-06 23:37:36 +00005921 .addJumpTableIndex(MJTI)
5922 .addImm(UId));
5923
5924 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5925 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
5926 .addReg(ARM::CPSR, RegState::Define)
5927 .addReg(NewVReg2, RegState::Kill)
5928 .addReg(NewVReg3));
5929
5930 MachineMemOperand *JTMMOLd =
5931 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
5932 MachineMemOperand::MOLoad, 4, 4);
5933
5934 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5935 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
5936 .addReg(NewVReg4, RegState::Kill)
5937 .addImm(0)
5938 .addMemOperand(JTMMOLd));
5939
5940 unsigned NewVReg6 = MRI->createVirtualRegister(TRC);
5941 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
5942 .addReg(ARM::CPSR, RegState::Define)
5943 .addReg(NewVReg5, RegState::Kill)
5944 .addReg(NewVReg3));
5945
5946 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
5947 .addReg(NewVReg6, RegState::Kill)
5948 .addJumpTableIndex(MJTI)
5949 .addImm(UId);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005950 } else {
5951 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5952 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
5953 .addFrameIndex(FI)
5954 .addImm(4)
5955 .addMemOperand(FIMMOLd));
Bill Wendling564392b2011-10-18 22:11:18 +00005956
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005957 if (NumLPads < 256) {
5958 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
5959 .addReg(NewVReg1)
5960 .addImm(NumLPads));
Bill Wendling922ad782011-10-19 09:24:02 +00005961 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005962 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5963 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00005964 .addImm(NumLPads & 0xFFFF));
5965
5966 unsigned VReg2 = VReg1;
5967 if ((NumLPads & 0xFFFF0000) != 0) {
5968 VReg2 = MRI->createVirtualRegister(TRC);
5969 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
5970 .addReg(VReg1)
5971 .addImm(NumLPads >> 16));
5972 }
5973
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005974 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
5975 .addReg(NewVReg1)
5976 .addReg(VReg2));
Bill Wendling922ad782011-10-19 09:24:02 +00005977 } else {
5978 MachineConstantPool *ConstantPool = MF->getConstantPool();
5979 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
5980 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
5981
5982 // MachineConstantPool wants an explicit alignment.
5983 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
5984 if (Align == 0)
5985 Align = getTargetData()->getTypeAllocSize(C->getType());
5986 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
5987
5988 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5989 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
5990 .addReg(VReg1, RegState::Define)
Bill Wendling767f8be2011-10-20 20:37:11 +00005991 .addConstantPoolIndex(Idx)
5992 .addImm(0));
Bill Wendling922ad782011-10-19 09:24:02 +00005993 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
5994 .addReg(NewVReg1)
5995 .addReg(VReg1, RegState::Kill));
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005996 }
5997
Bill Wendling95ce2e92011-10-06 22:53:00 +00005998 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
5999 .addMBB(TrapBB)
6000 .addImm(ARMCC::HI)
6001 .addReg(ARM::CPSR);
Bill Wendling2a850152011-10-05 00:02:33 +00006002
Bill Wendling564392b2011-10-18 22:11:18 +00006003 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006004 AddDefaultCC(
Bill Wendling564392b2011-10-18 22:11:18 +00006005 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006006 .addReg(NewVReg1)
6007 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling564392b2011-10-18 22:11:18 +00006008 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6009 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006010 .addJumpTableIndex(MJTI)
6011 .addImm(UId));
6012
6013 MachineMemOperand *JTMMOLd =
6014 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6015 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling564392b2011-10-18 22:11:18 +00006016 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006017 AddDefaultPred(
Bill Wendling564392b2011-10-18 22:11:18 +00006018 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6019 .addReg(NewVReg3, RegState::Kill)
6020 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006021 .addImm(0)
6022 .addMemOperand(JTMMOLd));
6023
6024 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
Bill Wendling564392b2011-10-18 22:11:18 +00006025 .addReg(NewVReg5, RegState::Kill)
6026 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006027 .addJumpTableIndex(MJTI)
6028 .addImm(UId);
6029 }
Bill Wendling2a850152011-10-05 00:02:33 +00006030
Bill Wendlingbb734682011-10-05 00:39:32 +00006031 // Add the jump table entries as successors to the MBB.
Bill Wendling2acf6382011-10-07 23:18:02 +00006032 MachineBasicBlock *PrevMBB = 0;
Bill Wendlingbb734682011-10-05 00:39:32 +00006033 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006034 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6035 MachineBasicBlock *CurMBB = *I;
6036 if (PrevMBB != CurMBB)
6037 DispContBB->addSuccessor(CurMBB);
6038 PrevMBB = CurMBB;
6039 }
6040
Bill Wendling24bb9252011-10-17 05:25:09 +00006041 // N.B. the order the invoke BBs are processed in doesn't matter here.
Bill Wendling969c9ef2011-10-14 23:34:37 +00006042 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6043 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
6044 const unsigned *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006045 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling2acf6382011-10-07 23:18:02 +00006046 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6047 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6048 MachineBasicBlock *BB = *I;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006049
6050 // Remove the landing pad successor from the invoke block and replace it
6051 // with the new dispatch block.
Bill Wendlingde39d862011-10-26 07:16:18 +00006052 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6053 BB->succ_end());
6054 while (!Successors.empty()) {
6055 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling2acf6382011-10-07 23:18:02 +00006056 if (SMBB->isLandingPad()) {
6057 BB->removeSuccessor(SMBB);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006058 MBBLPads.push_back(SMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006059 }
6060 }
6061
6062 BB->addSuccessor(DispatchBB);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006063
6064 // Find the invoke call and mark all of the callee-saved registers as
6065 // 'implicit defined' so that they're spilled. This prevents code from
6066 // moving instructions to before the EH block, where they will never be
6067 // executed.
6068 for (MachineBasicBlock::reverse_iterator
6069 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
6070 if (!II->getDesc().isCall()) continue;
6071
6072 DenseMap<unsigned, bool> DefRegs;
6073 for (MachineInstr::mop_iterator
6074 OI = II->operands_begin(), OE = II->operands_end();
6075 OI != OE; ++OI) {
6076 if (!OI->isReg()) continue;
6077 DefRegs[OI->getReg()] = true;
6078 }
6079
6080 MachineInstrBuilder MIB(&*II);
6081
Bill Wendling5d798592011-10-14 23:55:44 +00006082 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006083 unsigned Reg = SavedRegs[i];
6084 if (Subtarget->isThumb2() &&
6085 !ARM::tGPRRegisterClass->contains(Reg) &&
6086 !ARM::hGPRRegisterClass->contains(Reg))
6087 continue;
6088 else if (Subtarget->isThumb1Only() &&
6089 !ARM::tGPRRegisterClass->contains(Reg))
6090 continue;
6091 else if (!Subtarget->isThumb() &&
6092 !ARM::GPRRegisterClass->contains(Reg))
6093 continue;
6094 if (!DefRegs[Reg])
6095 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling5d798592011-10-14 23:55:44 +00006096 }
Bill Wendling969c9ef2011-10-14 23:34:37 +00006097
6098 break;
6099 }
Bill Wendling2acf6382011-10-07 23:18:02 +00006100 }
Bill Wendlingbb734682011-10-05 00:39:32 +00006101
Bill Wendlingf7b02072011-10-18 18:30:49 +00006102 // Mark all former landing pads as non-landing pads. The dispatch is the only
6103 // landing pad now.
6104 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6105 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6106 (*I)->setIsLandingPad(false);
6107
Bill Wendlingbb734682011-10-05 00:39:32 +00006108 // The instruction is gone now.
6109 MI->eraseFromParent();
6110
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006111 return MBB;
6112}
6113
Evan Cheng218977b2010-07-13 19:27:42 +00006114static
6115MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6116 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6117 E = MBB->succ_end(); I != E; ++I)
6118 if (*I != Succ)
6119 return *I;
6120 llvm_unreachable("Expecting a BB with two successors!");
6121}
6122
Jim Grosbache801dc42009-12-12 01:40:06 +00006123MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006124ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00006125 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006126 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00006127 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006128 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00006129 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00006130 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00006131 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00006132 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00006133 }
Jim Grosbachee2c2a42011-09-16 21:55:56 +00006134 // The Thumb2 pre-indexed stores have the same MI operands, they just
6135 // define them differently in the .td files from the isel patterns, so
6136 // they need pseudos.
6137 case ARM::t2STR_preidx:
6138 MI->setDesc(TII->get(ARM::t2STR_PRE));
6139 return BB;
6140 case ARM::t2STRB_preidx:
6141 MI->setDesc(TII->get(ARM::t2STRB_PRE));
6142 return BB;
6143 case ARM::t2STRH_preidx:
6144 MI->setDesc(TII->get(ARM::t2STRH_PRE));
6145 return BB;
6146
Jim Grosbach19dec202011-08-05 20:35:44 +00006147 case ARM::STRi_preidx:
6148 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00006149 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00006150 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
6151 // Decode the offset.
6152 unsigned Offset = MI->getOperand(4).getImm();
6153 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
6154 Offset = ARM_AM::getAM2Offset(Offset);
6155 if (isSub)
6156 Offset = -Offset;
6157
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006158 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00006159 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00006160 .addOperand(MI->getOperand(0)) // Rn_wb
6161 .addOperand(MI->getOperand(1)) // Rt
6162 .addOperand(MI->getOperand(2)) // Rn
6163 .addImm(Offset) // offset (skip GPR==zero_reg)
6164 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006165 .addOperand(MI->getOperand(6))
6166 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00006167 MI->eraseFromParent();
6168 return BB;
6169 }
6170 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00006171 case ARM::STRBr_preidx:
6172 case ARM::STRH_preidx: {
6173 unsigned NewOpc;
6174 switch (MI->getOpcode()) {
6175 default: llvm_unreachable("unexpected opcode!");
6176 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
6177 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
6178 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
6179 }
Jim Grosbach19dec202011-08-05 20:35:44 +00006180 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
6181 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
6182 MIB.addOperand(MI->getOperand(i));
6183 MI->eraseFromParent();
6184 return BB;
6185 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006186 case ARM::ATOMIC_LOAD_ADD_I8:
6187 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6188 case ARM::ATOMIC_LOAD_ADD_I16:
6189 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6190 case ARM::ATOMIC_LOAD_ADD_I32:
6191 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006192
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006193 case ARM::ATOMIC_LOAD_AND_I8:
6194 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6195 case ARM::ATOMIC_LOAD_AND_I16:
6196 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6197 case ARM::ATOMIC_LOAD_AND_I32:
6198 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006199
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006200 case ARM::ATOMIC_LOAD_OR_I8:
6201 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6202 case ARM::ATOMIC_LOAD_OR_I16:
6203 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6204 case ARM::ATOMIC_LOAD_OR_I32:
6205 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006206
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006207 case ARM::ATOMIC_LOAD_XOR_I8:
6208 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6209 case ARM::ATOMIC_LOAD_XOR_I16:
6210 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6211 case ARM::ATOMIC_LOAD_XOR_I32:
6212 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006213
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006214 case ARM::ATOMIC_LOAD_NAND_I8:
6215 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6216 case ARM::ATOMIC_LOAD_NAND_I16:
6217 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6218 case ARM::ATOMIC_LOAD_NAND_I32:
6219 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006220
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006221 case ARM::ATOMIC_LOAD_SUB_I8:
6222 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6223 case ARM::ATOMIC_LOAD_SUB_I16:
6224 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6225 case ARM::ATOMIC_LOAD_SUB_I32:
6226 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006227
Jim Grosbachf7da8822011-04-26 19:44:18 +00006228 case ARM::ATOMIC_LOAD_MIN_I8:
6229 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
6230 case ARM::ATOMIC_LOAD_MIN_I16:
6231 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
6232 case ARM::ATOMIC_LOAD_MIN_I32:
6233 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
6234
6235 case ARM::ATOMIC_LOAD_MAX_I8:
6236 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
6237 case ARM::ATOMIC_LOAD_MAX_I16:
6238 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
6239 case ARM::ATOMIC_LOAD_MAX_I32:
6240 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
6241
6242 case ARM::ATOMIC_LOAD_UMIN_I8:
6243 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
6244 case ARM::ATOMIC_LOAD_UMIN_I16:
6245 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
6246 case ARM::ATOMIC_LOAD_UMIN_I32:
6247 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
6248
6249 case ARM::ATOMIC_LOAD_UMAX_I8:
6250 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
6251 case ARM::ATOMIC_LOAD_UMAX_I16:
6252 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
6253 case ARM::ATOMIC_LOAD_UMAX_I32:
6254 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
6255
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006256 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
6257 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
6258 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00006259
6260 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
6261 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
6262 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006263
Eli Friedman2bdffe42011-08-31 00:31:29 +00006264
6265 case ARM::ATOMADD6432:
6266 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006267 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr,
6268 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006269 case ARM::ATOMSUB6432:
6270 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006271 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6272 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006273 case ARM::ATOMOR6432:
6274 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006275 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006276 case ARM::ATOMXOR6432:
6277 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006278 isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006279 case ARM::ATOMAND6432:
6280 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006281 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006282 case ARM::ATOMSWAP6432:
6283 return EmitAtomicBinary64(MI, BB, 0, 0, false);
Eli Friedman4d3f3292011-08-31 17:52:22 +00006284 case ARM::ATOMCMPXCHG6432:
6285 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
6286 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6287 /*NeedsCarry*/ false, /*IsCmpxchg*/true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006288
Evan Cheng007ea272009-08-12 05:17:19 +00006289 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00006290 // To "insert" a SELECT_CC instruction, we actually have to insert the
6291 // diamond control-flow pattern. The incoming instruction knows the
6292 // destination vreg to set, the condition code register to branch on, the
6293 // true/false values to select between, and a branch opcode to use.
6294 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006295 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00006296 ++It;
6297
6298 // thisMBB:
6299 // ...
6300 // TrueVal = ...
6301 // cmpTY ccX, r1, r2
6302 // bCC copy1MBB
6303 // fallthrough --> copy0MBB
6304 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006305 MachineFunction *F = BB->getParent();
6306 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6307 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00006308 F->insert(It, copy0MBB);
6309 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006310
6311 // Transfer the remainder of BB and its successor edges to sinkMBB.
6312 sinkMBB->splice(sinkMBB->begin(), BB,
6313 llvm::next(MachineBasicBlock::iterator(MI)),
6314 BB->end());
6315 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6316
Dan Gohman258c58c2010-07-06 15:49:48 +00006317 BB->addSuccessor(copy0MBB);
6318 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00006319
Dan Gohman14152b42010-07-06 20:24:04 +00006320 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
6321 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
6322
Evan Chenga8e29892007-01-19 07:51:42 +00006323 // copy0MBB:
6324 // %FalseValue = ...
6325 // # fallthrough to sinkMBB
6326 BB = copy0MBB;
6327
6328 // Update machine-CFG edges
6329 BB->addSuccessor(sinkMBB);
6330
6331 // sinkMBB:
6332 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6333 // ...
6334 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00006335 BuildMI(*BB, BB->begin(), dl,
6336 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00006337 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6338 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6339
Dan Gohman14152b42010-07-06 20:24:04 +00006340 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00006341 return BB;
6342 }
Evan Cheng86198642009-08-07 00:34:42 +00006343
Evan Cheng218977b2010-07-13 19:27:42 +00006344 case ARM::BCCi64:
6345 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00006346 // If there is an unconditional branch to the other successor, remove it.
6347 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00006348
Evan Cheng218977b2010-07-13 19:27:42 +00006349 // Compare both parts that make up the double comparison separately for
6350 // equality.
6351 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
6352
6353 unsigned LHS1 = MI->getOperand(1).getReg();
6354 unsigned LHS2 = MI->getOperand(2).getReg();
6355 if (RHSisZero) {
6356 AddDefaultPred(BuildMI(BB, dl,
6357 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6358 .addReg(LHS1).addImm(0));
6359 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6360 .addReg(LHS2).addImm(0)
6361 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6362 } else {
6363 unsigned RHS1 = MI->getOperand(3).getReg();
6364 unsigned RHS2 = MI->getOperand(4).getReg();
6365 AddDefaultPred(BuildMI(BB, dl,
6366 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6367 .addReg(LHS1).addReg(RHS1));
6368 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6369 .addReg(LHS2).addReg(RHS2)
6370 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6371 }
6372
6373 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
6374 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
6375 if (MI->getOperand(0).getImm() == ARMCC::NE)
6376 std::swap(destMBB, exitMBB);
6377
6378 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6379 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson51f6a7a2011-09-09 21:48:23 +00006380 if (isThumb2)
6381 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
6382 else
6383 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng218977b2010-07-13 19:27:42 +00006384
6385 MI->eraseFromParent(); // The pseudo instruction is gone now.
6386 return BB;
6387 }
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006388
Bill Wendling5bc85282011-10-17 20:37:20 +00006389 case ARM::Int_eh_sjlj_setjmp:
6390 case ARM::Int_eh_sjlj_setjmp_nofp:
6391 case ARM::tInt_eh_sjlj_setjmp:
6392 case ARM::t2Int_eh_sjlj_setjmp:
6393 case ARM::t2Int_eh_sjlj_setjmp_nofp:
6394 EmitSjLjDispatchBlock(MI, BB);
6395 return BB;
6396
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006397 case ARM::ABS:
6398 case ARM::t2ABS: {
6399 // To insert an ABS instruction, we have to insert the
6400 // diamond control-flow pattern. The incoming instruction knows the
6401 // source vreg to test against 0, the destination vreg to set,
6402 // the condition code register to branch on, the
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006403 // true/false values to select between, and a branch opcode to use.
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006404 // It transforms
6405 // V1 = ABS V0
6406 // into
6407 // V2 = MOVS V0
6408 // BCC (branch to SinkBB if V0 >= 0)
6409 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006410 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006411 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6412 MachineFunction::iterator BBI = BB;
6413 ++BBI;
6414 MachineFunction *Fn = BB->getParent();
6415 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6416 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6417 Fn->insert(BBI, RSBBB);
6418 Fn->insert(BBI, SinkBB);
6419
6420 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
6421 unsigned int ABSDstReg = MI->getOperand(0).getReg();
6422 bool isThumb2 = Subtarget->isThumb2();
6423 MachineRegisterInfo &MRI = Fn->getRegInfo();
6424 // In Thumb mode S must not be specified if source register is the SP or
6425 // PC and if destination register is the SP, so restrict register class
6426 unsigned NewMovDstReg = MRI.createVirtualRegister(
6427 isThumb2 ? ARM::rGPRRegisterClass : ARM::GPRRegisterClass);
6428 unsigned NewRsbDstReg = MRI.createVirtualRegister(
6429 isThumb2 ? ARM::rGPRRegisterClass : ARM::GPRRegisterClass);
6430
6431 // Transfer the remainder of BB and its successor edges to sinkMBB.
6432 SinkBB->splice(SinkBB->begin(), BB,
6433 llvm::next(MachineBasicBlock::iterator(MI)),
6434 BB->end());
6435 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
6436
6437 BB->addSuccessor(RSBBB);
6438 BB->addSuccessor(SinkBB);
6439
6440 // fall through to SinkMBB
6441 RSBBB->addSuccessor(SinkBB);
6442
6443 // insert a movs at the end of BB
6444 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVr : ARM::MOVr),
6445 NewMovDstReg)
6446 .addReg(ABSSrcReg, RegState::Kill)
6447 .addImm((unsigned)ARMCC::AL).addReg(0)
6448 .addReg(ARM::CPSR, RegState::Define);
6449
6450 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006451 BuildMI(BB, dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006452 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
6453 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
6454
6455 // insert rsbri in RSBBB
6456 // Note: BCC and rsbri will be converted into predicated rsbmi
6457 // by if-conversion pass
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006458 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006459 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
6460 .addReg(NewMovDstReg, RegState::Kill)
6461 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
6462
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006463 // insert PHI in SinkBB,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006464 // reuse ABSDstReg to not change uses of ABS instruction
6465 BuildMI(*SinkBB, SinkBB->begin(), dl,
6466 TII->get(ARM::PHI), ABSDstReg)
6467 .addReg(NewRsbDstReg).addMBB(RSBBB)
6468 .addReg(NewMovDstReg).addMBB(BB);
6469
6470 // remove ABS instruction
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006471 MI->eraseFromParent();
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006472
6473 // return last added BB
6474 return SinkBB;
6475 }
Evan Chenga8e29892007-01-19 07:51:42 +00006476 }
6477}
6478
Evan Cheng37fefc22011-08-30 19:09:48 +00006479void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
6480 SDNode *Node) const {
Andrew Trick90b7b122011-10-18 19:18:52 +00006481 const MCInstrDesc *MCID = &MI->getDesc();
6482 if (!MCID->hasPostISelHook()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006483 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
6484 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
6485 return;
6486 }
6487
Andrew Trick4815d562011-09-20 03:17:40 +00006488 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
6489 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
6490 // operand is still set to noreg. If needed, set the optional operand's
6491 // register to CPSR, and remove the redundant implicit def.
Andrew Trick3be654f2011-09-21 02:20:46 +00006492 //
Andrew Trick90b7b122011-10-18 19:18:52 +00006493 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick4815d562011-09-20 03:17:40 +00006494
Andrew Trick3be654f2011-09-21 02:20:46 +00006495 // Rename pseudo opcodes.
6496 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
6497 if (NewOpc) {
6498 const ARMBaseInstrInfo *TII =
6499 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick90b7b122011-10-18 19:18:52 +00006500 MCID = &TII->get(NewOpc);
6501
6502 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
6503 "converted opcode should be the same except for cc_out");
6504
6505 MI->setDesc(*MCID);
6506
6507 // Add the optional cc_out operand
6508 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick3be654f2011-09-21 02:20:46 +00006509 }
Andrew Trick90b7b122011-10-18 19:18:52 +00006510 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick4815d562011-09-20 03:17:40 +00006511
6512 // Any ARM instruction that sets the 's' bit should specify an optional
6513 // "cc_out" operand in the last operand position.
Andrew Trick90b7b122011-10-18 19:18:52 +00006514 if (!MCID->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006515 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006516 return;
6517 }
Andrew Trick3be654f2011-09-21 02:20:46 +00006518 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
6519 // since we already have an optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006520 bool definesCPSR = false;
6521 bool deadCPSR = false;
Andrew Trick90b7b122011-10-18 19:18:52 +00006522 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick4815d562011-09-20 03:17:40 +00006523 i != e; ++i) {
6524 const MachineOperand &MO = MI->getOperand(i);
6525 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
6526 definesCPSR = true;
6527 if (MO.isDead())
6528 deadCPSR = true;
6529 MI->RemoveOperand(i);
6530 break;
Evan Cheng37fefc22011-08-30 19:09:48 +00006531 }
6532 }
Andrew Trick4815d562011-09-20 03:17:40 +00006533 if (!definesCPSR) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006534 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006535 return;
6536 }
6537 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick3be654f2011-09-21 02:20:46 +00006538 if (deadCPSR) {
6539 assert(!MI->getOperand(ccOutIdx).getReg() &&
6540 "expect uninitialized optional cc_out operand");
Andrew Trick4815d562011-09-20 03:17:40 +00006541 return;
Andrew Trick3be654f2011-09-21 02:20:46 +00006542 }
Andrew Trick4815d562011-09-20 03:17:40 +00006543
Andrew Trick3be654f2011-09-21 02:20:46 +00006544 // If this instruction was defined with an optional CPSR def and its dag node
6545 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006546 MachineOperand &MO = MI->getOperand(ccOutIdx);
6547 MO.setReg(ARM::CPSR);
6548 MO.setIsDef(true);
Evan Cheng37fefc22011-08-30 19:09:48 +00006549}
6550
Evan Chenga8e29892007-01-19 07:51:42 +00006551//===----------------------------------------------------------------------===//
6552// ARM Optimization Hooks
6553//===----------------------------------------------------------------------===//
6554
Chris Lattnerd1980a52009-03-12 06:52:53 +00006555static
6556SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
6557 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00006558 SelectionDAG &DAG = DCI.DAG;
6559 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00006560 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00006561 unsigned Opc = N->getOpcode();
6562 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
6563 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
6564 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
6565 ISD::CondCode CC = ISD::SETCC_INVALID;
6566
6567 if (isSlctCC) {
6568 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
6569 } else {
6570 SDValue CCOp = Slct.getOperand(0);
6571 if (CCOp.getOpcode() == ISD::SETCC)
6572 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
6573 }
6574
6575 bool DoXform = false;
6576 bool InvCC = false;
6577 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
6578 "Bad input!");
6579
6580 if (LHS.getOpcode() == ISD::Constant &&
6581 cast<ConstantSDNode>(LHS)->isNullValue()) {
6582 DoXform = true;
6583 } else if (CC != ISD::SETCC_INVALID &&
6584 RHS.getOpcode() == ISD::Constant &&
6585 cast<ConstantSDNode>(RHS)->isNullValue()) {
6586 std::swap(LHS, RHS);
6587 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006588 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00006589 Op0.getOperand(0).getValueType();
6590 bool isInt = OpVT.isInteger();
6591 CC = ISD::getSetCCInverse(CC, isInt);
6592
6593 if (!TLI.isCondCodeLegal(CC, OpVT))
6594 return SDValue(); // Inverse operator isn't legal.
6595
6596 DoXform = true;
6597 InvCC = true;
6598 }
6599
6600 if (DoXform) {
6601 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
6602 if (isSlctCC)
6603 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
6604 Slct.getOperand(0), Slct.getOperand(1), CC);
6605 SDValue CCOp = Slct.getOperand(0);
6606 if (InvCC)
6607 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
6608 CCOp.getOperand(0), CCOp.getOperand(1), CC);
6609 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
6610 CCOp, OtherOp, Result);
6611 }
6612 return SDValue();
6613}
6614
Eric Christopherfa6f5912011-06-29 21:10:36 +00006615// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00006616// (only after legalization).
6617static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
6618 TargetLowering::DAGCombinerInfo &DCI,
6619 const ARMSubtarget *Subtarget) {
6620
6621 // Only perform optimization if after legalize, and if NEON is available. We
6622 // also expected both operands to be BUILD_VECTORs.
6623 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
6624 || N0.getOpcode() != ISD::BUILD_VECTOR
6625 || N1.getOpcode() != ISD::BUILD_VECTOR)
6626 return SDValue();
6627
6628 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
6629 EVT VT = N->getValueType(0);
6630 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
6631 return SDValue();
6632
6633 // Check that the vector operands are of the right form.
6634 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
6635 // operands, where N is the size of the formed vector.
6636 // Each EXTRACT_VECTOR should have the same input vector and odd or even
6637 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00006638
6639 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00006640 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00006641 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00006642 SDValue Vec = N0->getOperand(0)->getOperand(0);
6643 SDNode *V = Vec.getNode();
6644 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00006645
Eric Christopherfa6f5912011-06-29 21:10:36 +00006646 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00006647 // check to see if each of their operands are an EXTRACT_VECTOR with
6648 // the same vector and appropriate index.
6649 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
6650 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
6651 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00006652
Tanya Lattner189531f2011-06-14 23:48:48 +00006653 SDValue ExtVec0 = N0->getOperand(i);
6654 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00006655
Tanya Lattner189531f2011-06-14 23:48:48 +00006656 // First operand is the vector, verify its the same.
6657 if (V != ExtVec0->getOperand(0).getNode() ||
6658 V != ExtVec1->getOperand(0).getNode())
6659 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00006660
Tanya Lattner189531f2011-06-14 23:48:48 +00006661 // Second is the constant, verify its correct.
6662 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
6663 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00006664
Tanya Lattner189531f2011-06-14 23:48:48 +00006665 // For the constant, we want to see all the even or all the odd.
6666 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
6667 || C1->getZExtValue() != nextIndex+1)
6668 return SDValue();
6669
6670 // Increment index.
6671 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00006672 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00006673 return SDValue();
6674 }
6675
6676 // Create VPADDL node.
6677 SelectionDAG &DAG = DCI.DAG;
6678 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00006679
6680 // Build operand list.
6681 SmallVector<SDValue, 8> Ops;
6682 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
6683 TLI.getPointerTy()));
6684
6685 // Input is the vector.
6686 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00006687
Tanya Lattner189531f2011-06-14 23:48:48 +00006688 // Get widened type and narrowed type.
6689 MVT widenType;
6690 unsigned numElem = VT.getVectorNumElements();
6691 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
6692 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
6693 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
6694 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
6695 default:
6696 assert(0 && "Invalid vector element type for padd optimization.");
6697 }
6698
6699 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
6700 widenType, &Ops[0], Ops.size());
6701 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
6702}
6703
Bob Wilson3d5792a2010-07-29 20:34:14 +00006704/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
6705/// operands N0 and N1. This is a helper for PerformADDCombine that is
6706/// called with the default operands, and if that fails, with commuted
6707/// operands.
6708static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00006709 TargetLowering::DAGCombinerInfo &DCI,
6710 const ARMSubtarget *Subtarget){
6711
6712 // Attempt to create vpaddl for this add.
6713 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
6714 if (Result.getNode())
6715 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00006716
Chris Lattnerd1980a52009-03-12 06:52:53 +00006717 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
6718 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
6719 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
6720 if (Result.getNode()) return Result;
6721 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00006722 return SDValue();
6723}
6724
Bob Wilson3d5792a2010-07-29 20:34:14 +00006725/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
6726///
6727static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00006728 TargetLowering::DAGCombinerInfo &DCI,
6729 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00006730 SDValue N0 = N->getOperand(0);
6731 SDValue N1 = N->getOperand(1);
6732
6733 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00006734 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00006735 if (Result.getNode())
6736 return Result;
6737
6738 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00006739 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00006740}
6741
Chris Lattnerd1980a52009-03-12 06:52:53 +00006742/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00006743///
Chris Lattnerd1980a52009-03-12 06:52:53 +00006744static SDValue PerformSUBCombine(SDNode *N,
6745 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00006746 SDValue N0 = N->getOperand(0);
6747 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00006748
Chris Lattnerd1980a52009-03-12 06:52:53 +00006749 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
6750 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
6751 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
6752 if (Result.getNode()) return Result;
6753 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00006754
Chris Lattnerd1980a52009-03-12 06:52:53 +00006755 return SDValue();
6756}
6757
Evan Cheng463d3582011-03-31 19:38:48 +00006758/// PerformVMULCombine
6759/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
6760/// special multiplier accumulator forwarding.
6761/// vmul d3, d0, d2
6762/// vmla d3, d1, d2
6763/// is faster than
6764/// vadd d3, d0, d1
6765/// vmul d3, d3, d2
6766static SDValue PerformVMULCombine(SDNode *N,
6767 TargetLowering::DAGCombinerInfo &DCI,
6768 const ARMSubtarget *Subtarget) {
6769 if (!Subtarget->hasVMLxForwarding())
6770 return SDValue();
6771
6772 SelectionDAG &DAG = DCI.DAG;
6773 SDValue N0 = N->getOperand(0);
6774 SDValue N1 = N->getOperand(1);
6775 unsigned Opcode = N0.getOpcode();
6776 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
6777 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00006778 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00006779 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
6780 Opcode != ISD::FADD && Opcode != ISD::FSUB)
6781 return SDValue();
6782 std::swap(N0, N1);
6783 }
6784
6785 EVT VT = N->getValueType(0);
6786 DebugLoc DL = N->getDebugLoc();
6787 SDValue N00 = N0->getOperand(0);
6788 SDValue N01 = N0->getOperand(1);
6789 return DAG.getNode(Opcode, DL, VT,
6790 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
6791 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
6792}
6793
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006794static SDValue PerformMULCombine(SDNode *N,
6795 TargetLowering::DAGCombinerInfo &DCI,
6796 const ARMSubtarget *Subtarget) {
6797 SelectionDAG &DAG = DCI.DAG;
6798
6799 if (Subtarget->isThumb1Only())
6800 return SDValue();
6801
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006802 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
6803 return SDValue();
6804
6805 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00006806 if (VT.is64BitVector() || VT.is128BitVector())
6807 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006808 if (VT != MVT::i32)
6809 return SDValue();
6810
6811 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
6812 if (!C)
6813 return SDValue();
6814
6815 uint64_t MulAmt = C->getZExtValue();
6816 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
6817 ShiftAmt = ShiftAmt & (32 - 1);
6818 SDValue V = N->getOperand(0);
6819 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006820
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006821 SDValue Res;
6822 MulAmt >>= ShiftAmt;
6823 if (isPowerOf2_32(MulAmt - 1)) {
6824 // (mul x, 2^N + 1) => (add (shl x, N), x)
6825 Res = DAG.getNode(ISD::ADD, DL, VT,
6826 V, DAG.getNode(ISD::SHL, DL, VT,
6827 V, DAG.getConstant(Log2_32(MulAmt-1),
6828 MVT::i32)));
6829 } else if (isPowerOf2_32(MulAmt + 1)) {
6830 // (mul x, 2^N - 1) => (sub (shl x, N), x)
6831 Res = DAG.getNode(ISD::SUB, DL, VT,
6832 DAG.getNode(ISD::SHL, DL, VT,
6833 V, DAG.getConstant(Log2_32(MulAmt+1),
6834 MVT::i32)),
6835 V);
6836 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006837 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006838
6839 if (ShiftAmt != 0)
6840 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
6841 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006842
6843 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006844 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006845 return SDValue();
6846}
6847
Owen Anderson080c0922010-11-05 19:27:46 +00006848static SDValue PerformANDCombine(SDNode *N,
6849 TargetLowering::DAGCombinerInfo &DCI) {
Owen Anderson76706012011-04-05 21:48:57 +00006850
Owen Anderson080c0922010-11-05 19:27:46 +00006851 // Attempt to use immediate-form VBIC
6852 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
6853 DebugLoc dl = N->getDebugLoc();
6854 EVT VT = N->getValueType(0);
6855 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006856
Tanya Lattner0433b212011-04-07 15:24:20 +00006857 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
6858 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00006859
Owen Anderson080c0922010-11-05 19:27:46 +00006860 APInt SplatBits, SplatUndef;
6861 unsigned SplatBitSize;
6862 bool HasAnyUndefs;
6863 if (BVN &&
6864 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
6865 if (SplatBitSize <= 64) {
6866 EVT VbicVT;
6867 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
6868 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006869 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00006870 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00006871 if (Val.getNode()) {
6872 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006873 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00006874 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006875 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00006876 }
6877 }
6878 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006879
Owen Anderson080c0922010-11-05 19:27:46 +00006880 return SDValue();
6881}
6882
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006883/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
6884static SDValue PerformORCombine(SDNode *N,
6885 TargetLowering::DAGCombinerInfo &DCI,
6886 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00006887 // Attempt to use immediate-form VORR
6888 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
6889 DebugLoc dl = N->getDebugLoc();
6890 EVT VT = N->getValueType(0);
6891 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006892
Tanya Lattner0433b212011-04-07 15:24:20 +00006893 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
6894 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00006895
Owen Anderson60f48702010-11-03 23:15:26 +00006896 APInt SplatBits, SplatUndef;
6897 unsigned SplatBitSize;
6898 bool HasAnyUndefs;
6899 if (BVN && Subtarget->hasNEON() &&
6900 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
6901 if (SplatBitSize <= 64) {
6902 EVT VorrVT;
6903 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
6904 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00006905 DAG, VorrVT, VT.is128BitVector(),
6906 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00006907 if (Val.getNode()) {
6908 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006909 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00006910 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006911 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00006912 }
6913 }
6914 }
6915
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00006916 SDValue N0 = N->getOperand(0);
6917 if (N0.getOpcode() != ISD::AND)
6918 return SDValue();
6919 SDValue N1 = N->getOperand(1);
6920
6921 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
6922 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
6923 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
6924 APInt SplatUndef;
6925 unsigned SplatBitSize;
6926 bool HasAnyUndefs;
6927
6928 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
6929 APInt SplatBits0;
6930 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
6931 HasAnyUndefs) && !HasAnyUndefs) {
6932 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
6933 APInt SplatBits1;
6934 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
6935 HasAnyUndefs) && !HasAnyUndefs &&
6936 SplatBits0 == ~SplatBits1) {
6937 // Canonicalize the vector type to make instruction selection simpler.
6938 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
6939 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
6940 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00006941 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00006942 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
6943 }
6944 }
6945 }
6946
Jim Grosbach54238562010-07-17 03:30:54 +00006947 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
6948 // reasonable.
6949
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006950 // BFI is only available on V6T2+
6951 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
6952 return SDValue();
6953
Jim Grosbach54238562010-07-17 03:30:54 +00006954 DebugLoc DL = N->getDebugLoc();
6955 // 1) or (and A, mask), val => ARMbfi A, val, mask
6956 // iff (val & mask) == val
6957 //
6958 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
6959 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00006960 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00006961 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00006962 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00006963 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006964
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006965 if (VT != MVT::i32)
6966 return SDValue();
6967
Evan Cheng30fb13f2010-12-13 20:32:54 +00006968 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00006969
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006970 // The value and the mask need to be constants so we can verify this is
6971 // actually a bitfield set. If the mask is 0xffff, we can do better
6972 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00006973 SDValue MaskOp = N0.getOperand(1);
6974 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
6975 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006976 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00006977 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006978 if (Mask == 0xffff)
6979 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006980 SDValue Res;
6981 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00006982 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
6983 if (N1C) {
6984 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00006985 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00006986 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006987
Evan Chenga9688c42010-12-11 04:11:38 +00006988 if (ARM::isBitFieldInvertedMask(Mask)) {
6989 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006990
Evan Cheng30fb13f2010-12-13 20:32:54 +00006991 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00006992 DAG.getConstant(Val, MVT::i32),
6993 DAG.getConstant(Mask, MVT::i32));
6994
6995 // Do not add new nodes to DAG combiner worklist.
6996 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00006997 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00006998 }
Jim Grosbach54238562010-07-17 03:30:54 +00006999 } else if (N1.getOpcode() == ISD::AND) {
7000 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00007001 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7002 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00007003 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00007004 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007005
Eric Christopher29aeed12011-03-26 01:21:03 +00007006 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
7007 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00007008 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007009 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007010 // The pack halfword instruction works better for masks that fit it,
7011 // so use that when it's available.
7012 if (Subtarget->hasT2ExtractPack() &&
7013 (Mask == 0xffff || Mask == 0xffff0000))
7014 return SDValue();
7015 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00007016 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00007017 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00007018 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00007019 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00007020 DAG.getConstant(Mask, MVT::i32));
7021 // Do not add new nodes to DAG combiner worklist.
7022 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007023 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007024 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007025 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007026 // The pack halfword instruction works better for masks that fit it,
7027 // so use that when it's available.
7028 if (Subtarget->hasT2ExtractPack() &&
7029 (Mask2 == 0xffff || Mask2 == 0xffff0000))
7030 return SDValue();
7031 // 2b
7032 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007033 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00007034 DAG.getConstant(lsb, MVT::i32));
7035 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00007036 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00007037 // Do not add new nodes to DAG combiner worklist.
7038 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007039 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007040 }
7041 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007042
Evan Cheng30fb13f2010-12-13 20:32:54 +00007043 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
7044 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
7045 ARM::isBitFieldInvertedMask(~Mask)) {
7046 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
7047 // where lsb(mask) == #shamt and masked bits of B are known zero.
7048 SDValue ShAmt = N00.getOperand(1);
7049 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
7050 unsigned LSB = CountTrailingZeros_32(Mask);
7051 if (ShAmtC != LSB)
7052 return SDValue();
7053
7054 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
7055 DAG.getConstant(~Mask, MVT::i32));
7056
7057 // Do not add new nodes to DAG combiner worklist.
7058 DCI.CombineTo(N, Res, false);
7059 }
7060
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007061 return SDValue();
7062}
7063
Evan Chengbf188ae2011-06-15 01:12:31 +00007064/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
7065/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00007066static SDValue PerformBFICombine(SDNode *N,
7067 TargetLowering::DAGCombinerInfo &DCI) {
7068 SDValue N1 = N->getOperand(1);
7069 if (N1.getOpcode() == ISD::AND) {
7070 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7071 if (!N11C)
7072 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007073 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
7074 unsigned LSB = CountTrailingZeros_32(~InvMask);
7075 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
7076 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00007077 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007078 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00007079 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
7080 N->getOperand(0), N1.getOperand(0),
7081 N->getOperand(2));
7082 }
7083 return SDValue();
7084}
7085
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007086/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
7087/// ARMISD::VMOVRRD.
7088static SDValue PerformVMOVRRDCombine(SDNode *N,
7089 TargetLowering::DAGCombinerInfo &DCI) {
7090 // vmovrrd(vmovdrr x, y) -> x,y
7091 SDValue InDouble = N->getOperand(0);
7092 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
7093 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00007094
7095 // vmovrrd(load f64) -> (load i32), (load i32)
7096 SDNode *InNode = InDouble.getNode();
7097 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
7098 InNode->getValueType(0) == MVT::f64 &&
7099 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
7100 !cast<LoadSDNode>(InNode)->isVolatile()) {
7101 // TODO: Should this be done for non-FrameIndex operands?
7102 LoadSDNode *LD = cast<LoadSDNode>(InNode);
7103
7104 SelectionDAG &DAG = DCI.DAG;
7105 DebugLoc DL = LD->getDebugLoc();
7106 SDValue BasePtr = LD->getBasePtr();
7107 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
7108 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007109 LD->isNonTemporal(), LD->isInvariant(),
7110 LD->getAlignment());
Cameron Zwarich4071a712011-04-02 02:40:43 +00007111
7112 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7113 DAG.getConstant(4, MVT::i32));
7114 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
7115 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007116 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich4071a712011-04-02 02:40:43 +00007117 std::min(4U, LD->getAlignment() / 2));
7118
7119 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
7120 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
7121 DCI.RemoveFromWorklist(LD);
7122 DAG.DeleteNode(LD);
7123 return Result;
7124 }
7125
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007126 return SDValue();
7127}
7128
7129/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
7130/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
7131static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
7132 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
7133 SDValue Op0 = N->getOperand(0);
7134 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007135 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007136 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007137 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007138 Op1 = Op1.getOperand(0);
7139 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
7140 Op0.getNode() == Op1.getNode() &&
7141 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007142 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007143 N->getValueType(0), Op0.getOperand(0));
7144 return SDValue();
7145}
7146
Bob Wilson31600902010-12-21 06:43:19 +00007147/// PerformSTORECombine - Target-specific dag combine xforms for
7148/// ISD::STORE.
7149static SDValue PerformSTORECombine(SDNode *N,
7150 TargetLowering::DAGCombinerInfo &DCI) {
7151 // Bitcast an i64 store extracted from a vector to f64.
7152 // Otherwise, the i64 value will be legalized to a pair of i32 values.
7153 StoreSDNode *St = cast<StoreSDNode>(N);
7154 SDValue StVal = St->getValue();
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007155 if (!ISD::isNormalStore(St) || St->isVolatile())
7156 return SDValue();
7157
7158 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
7159 StVal.getNode()->hasOneUse() && !St->isVolatile()) {
7160 SelectionDAG &DAG = DCI.DAG;
7161 DebugLoc DL = St->getDebugLoc();
7162 SDValue BasePtr = St->getBasePtr();
7163 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
7164 StVal.getNode()->getOperand(0), BasePtr,
7165 St->getPointerInfo(), St->isVolatile(),
7166 St->isNonTemporal(), St->getAlignment());
7167
7168 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7169 DAG.getConstant(4, MVT::i32));
7170 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
7171 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
7172 St->isNonTemporal(),
7173 std::min(4U, St->getAlignment() / 2));
7174 }
7175
7176 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00007177 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
7178 return SDValue();
7179
7180 SelectionDAG &DAG = DCI.DAG;
7181 DebugLoc dl = StVal.getDebugLoc();
7182 SDValue IntVec = StVal.getOperand(0);
7183 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7184 IntVec.getValueType().getVectorNumElements());
7185 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
7186 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
7187 Vec, StVal.getOperand(1));
7188 dl = N->getDebugLoc();
7189 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
7190 // Make the DAGCombiner fold the bitcasts.
7191 DCI.AddToWorklist(Vec.getNode());
7192 DCI.AddToWorklist(ExtElt.getNode());
7193 DCI.AddToWorklist(V.getNode());
7194 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
7195 St->getPointerInfo(), St->isVolatile(),
7196 St->isNonTemporal(), St->getAlignment(),
7197 St->getTBAAInfo());
7198}
7199
7200/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
7201/// are normal, non-volatile loads. If so, it is profitable to bitcast an
7202/// i64 vector to have f64 elements, since the value can then be loaded
7203/// directly into a VFP register.
7204static bool hasNormalLoadOperand(SDNode *N) {
7205 unsigned NumElts = N->getValueType(0).getVectorNumElements();
7206 for (unsigned i = 0; i < NumElts; ++i) {
7207 SDNode *Elt = N->getOperand(i).getNode();
7208 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
7209 return true;
7210 }
7211 return false;
7212}
7213
Bob Wilson75f02882010-09-17 22:59:05 +00007214/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
7215/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00007216static SDValue PerformBUILD_VECTORCombine(SDNode *N,
7217 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00007218 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
7219 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
7220 // into a pair of GPRs, which is fine when the value is used as a scalar,
7221 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00007222 SelectionDAG &DAG = DCI.DAG;
7223 if (N->getNumOperands() == 2) {
7224 SDValue RV = PerformVMOVDRRCombine(N, DAG);
7225 if (RV.getNode())
7226 return RV;
7227 }
Bob Wilson75f02882010-09-17 22:59:05 +00007228
Bob Wilson31600902010-12-21 06:43:19 +00007229 // Load i64 elements as f64 values so that type legalization does not split
7230 // them up into i32 values.
7231 EVT VT = N->getValueType(0);
7232 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
7233 return SDValue();
7234 DebugLoc dl = N->getDebugLoc();
7235 SmallVector<SDValue, 8> Ops;
7236 unsigned NumElts = VT.getVectorNumElements();
7237 for (unsigned i = 0; i < NumElts; ++i) {
7238 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
7239 Ops.push_back(V);
7240 // Make the DAGCombiner fold the bitcast.
7241 DCI.AddToWorklist(V.getNode());
7242 }
7243 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
7244 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
7245 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
7246}
7247
7248/// PerformInsertEltCombine - Target-specific dag combine xforms for
7249/// ISD::INSERT_VECTOR_ELT.
7250static SDValue PerformInsertEltCombine(SDNode *N,
7251 TargetLowering::DAGCombinerInfo &DCI) {
7252 // Bitcast an i64 load inserted into a vector to f64.
7253 // Otherwise, the i64 value will be legalized to a pair of i32 values.
7254 EVT VT = N->getValueType(0);
7255 SDNode *Elt = N->getOperand(1).getNode();
7256 if (VT.getVectorElementType() != MVT::i64 ||
7257 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
7258 return SDValue();
7259
7260 SelectionDAG &DAG = DCI.DAG;
7261 DebugLoc dl = N->getDebugLoc();
7262 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7263 VT.getVectorNumElements());
7264 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
7265 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
7266 // Make the DAGCombiner fold the bitcasts.
7267 DCI.AddToWorklist(Vec.getNode());
7268 DCI.AddToWorklist(V.getNode());
7269 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
7270 Vec, V, N->getOperand(2));
7271 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00007272}
7273
Bob Wilsonf20700c2010-10-27 20:38:28 +00007274/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
7275/// ISD::VECTOR_SHUFFLE.
7276static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
7277 // The LLVM shufflevector instruction does not require the shuffle mask
7278 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
7279 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
7280 // operands do not match the mask length, they are extended by concatenating
7281 // them with undef vectors. That is probably the right thing for other
7282 // targets, but for NEON it is better to concatenate two double-register
7283 // size vector operands into a single quad-register size vector. Do that
7284 // transformation here:
7285 // shuffle(concat(v1, undef), concat(v2, undef)) ->
7286 // shuffle(concat(v1, v2), undef)
7287 SDValue Op0 = N->getOperand(0);
7288 SDValue Op1 = N->getOperand(1);
7289 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
7290 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
7291 Op0.getNumOperands() != 2 ||
7292 Op1.getNumOperands() != 2)
7293 return SDValue();
7294 SDValue Concat0Op1 = Op0.getOperand(1);
7295 SDValue Concat1Op1 = Op1.getOperand(1);
7296 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
7297 Concat1Op1.getOpcode() != ISD::UNDEF)
7298 return SDValue();
7299 // Skip the transformation if any of the types are illegal.
7300 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7301 EVT VT = N->getValueType(0);
7302 if (!TLI.isTypeLegal(VT) ||
7303 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
7304 !TLI.isTypeLegal(Concat1Op1.getValueType()))
7305 return SDValue();
7306
7307 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
7308 Op0.getOperand(0), Op1.getOperand(0));
7309 // Translate the shuffle mask.
7310 SmallVector<int, 16> NewMask;
7311 unsigned NumElts = VT.getVectorNumElements();
7312 unsigned HalfElts = NumElts/2;
7313 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
7314 for (unsigned n = 0; n < NumElts; ++n) {
7315 int MaskElt = SVN->getMaskElt(n);
7316 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007317 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00007318 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007319 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00007320 NewElt = HalfElts + MaskElt - NumElts;
7321 NewMask.push_back(NewElt);
7322 }
7323 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
7324 DAG.getUNDEF(VT), NewMask.data());
7325}
7326
Bob Wilson1c3ef902011-02-07 17:43:21 +00007327/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
7328/// NEON load/store intrinsics to merge base address updates.
7329static SDValue CombineBaseUpdate(SDNode *N,
7330 TargetLowering::DAGCombinerInfo &DCI) {
7331 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7332 return SDValue();
7333
7334 SelectionDAG &DAG = DCI.DAG;
7335 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
7336 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
7337 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
7338 SDValue Addr = N->getOperand(AddrOpIdx);
7339
7340 // Search for a use of the address operand that is an increment.
7341 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
7342 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
7343 SDNode *User = *UI;
7344 if (User->getOpcode() != ISD::ADD ||
7345 UI.getUse().getResNo() != Addr.getResNo())
7346 continue;
7347
7348 // Check that the add is independent of the load/store. Otherwise, folding
7349 // it would create a cycle.
7350 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
7351 continue;
7352
7353 // Find the new opcode for the updating load/store.
7354 bool isLoad = true;
7355 bool isLaneOp = false;
7356 unsigned NewOpc = 0;
7357 unsigned NumVecs = 0;
7358 if (isIntrinsic) {
7359 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
7360 switch (IntNo) {
7361 default: assert(0 && "unexpected intrinsic for Neon base update");
7362 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
7363 NumVecs = 1; break;
7364 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
7365 NumVecs = 2; break;
7366 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
7367 NumVecs = 3; break;
7368 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
7369 NumVecs = 4; break;
7370 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
7371 NumVecs = 2; isLaneOp = true; break;
7372 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
7373 NumVecs = 3; isLaneOp = true; break;
7374 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
7375 NumVecs = 4; isLaneOp = true; break;
7376 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
7377 NumVecs = 1; isLoad = false; break;
7378 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
7379 NumVecs = 2; isLoad = false; break;
7380 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
7381 NumVecs = 3; isLoad = false; break;
7382 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
7383 NumVecs = 4; isLoad = false; break;
7384 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
7385 NumVecs = 2; isLoad = false; isLaneOp = true; break;
7386 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
7387 NumVecs = 3; isLoad = false; isLaneOp = true; break;
7388 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
7389 NumVecs = 4; isLoad = false; isLaneOp = true; break;
7390 }
7391 } else {
7392 isLaneOp = true;
7393 switch (N->getOpcode()) {
7394 default: assert(0 && "unexpected opcode for Neon base update");
7395 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
7396 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
7397 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
7398 }
7399 }
7400
7401 // Find the size of memory referenced by the load/store.
7402 EVT VecTy;
7403 if (isLoad)
7404 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00007405 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00007406 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
7407 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
7408 if (isLaneOp)
7409 NumBytes /= VecTy.getVectorNumElements();
7410
7411 // If the increment is a constant, it must match the memory ref size.
7412 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
7413 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
7414 uint64_t IncVal = CInc->getZExtValue();
7415 if (IncVal != NumBytes)
7416 continue;
7417 } else if (NumBytes >= 3 * 16) {
7418 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
7419 // separate instructions that make it harder to use a non-constant update.
7420 continue;
7421 }
7422
7423 // Create the new updating load/store node.
7424 EVT Tys[6];
7425 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
7426 unsigned n;
7427 for (n = 0; n < NumResultVecs; ++n)
7428 Tys[n] = VecTy;
7429 Tys[n++] = MVT::i32;
7430 Tys[n] = MVT::Other;
7431 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
7432 SmallVector<SDValue, 8> Ops;
7433 Ops.push_back(N->getOperand(0)); // incoming chain
7434 Ops.push_back(N->getOperand(AddrOpIdx));
7435 Ops.push_back(Inc);
7436 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
7437 Ops.push_back(N->getOperand(i));
7438 }
7439 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
7440 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
7441 Ops.data(), Ops.size(),
7442 MemInt->getMemoryVT(),
7443 MemInt->getMemOperand());
7444
7445 // Update the uses.
7446 std::vector<SDValue> NewResults;
7447 for (unsigned i = 0; i < NumResultVecs; ++i) {
7448 NewResults.push_back(SDValue(UpdN.getNode(), i));
7449 }
7450 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
7451 DCI.CombineTo(N, NewResults);
7452 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
7453
7454 break;
Owen Anderson76706012011-04-05 21:48:57 +00007455 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00007456 return SDValue();
7457}
7458
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007459/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
7460/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
7461/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
7462/// return true.
7463static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
7464 SelectionDAG &DAG = DCI.DAG;
7465 EVT VT = N->getValueType(0);
7466 // vldN-dup instructions only support 64-bit vectors for N > 1.
7467 if (!VT.is64BitVector())
7468 return false;
7469
7470 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
7471 SDNode *VLD = N->getOperand(0).getNode();
7472 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
7473 return false;
7474 unsigned NumVecs = 0;
7475 unsigned NewOpc = 0;
7476 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
7477 if (IntNo == Intrinsic::arm_neon_vld2lane) {
7478 NumVecs = 2;
7479 NewOpc = ARMISD::VLD2DUP;
7480 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
7481 NumVecs = 3;
7482 NewOpc = ARMISD::VLD3DUP;
7483 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
7484 NumVecs = 4;
7485 NewOpc = ARMISD::VLD4DUP;
7486 } else {
7487 return false;
7488 }
7489
7490 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
7491 // numbers match the load.
7492 unsigned VLDLaneNo =
7493 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
7494 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
7495 UI != UE; ++UI) {
7496 // Ignore uses of the chain result.
7497 if (UI.getUse().getResNo() == NumVecs)
7498 continue;
7499 SDNode *User = *UI;
7500 if (User->getOpcode() != ARMISD::VDUPLANE ||
7501 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
7502 return false;
7503 }
7504
7505 // Create the vldN-dup node.
7506 EVT Tys[5];
7507 unsigned n;
7508 for (n = 0; n < NumVecs; ++n)
7509 Tys[n] = VT;
7510 Tys[n] = MVT::Other;
7511 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
7512 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
7513 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
7514 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
7515 Ops, 2, VLDMemInt->getMemoryVT(),
7516 VLDMemInt->getMemOperand());
7517
7518 // Update the uses.
7519 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
7520 UI != UE; ++UI) {
7521 unsigned ResNo = UI.getUse().getResNo();
7522 // Ignore uses of the chain result.
7523 if (ResNo == NumVecs)
7524 continue;
7525 SDNode *User = *UI;
7526 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
7527 }
7528
7529 // Now the vldN-lane intrinsic is dead except for its chain result.
7530 // Update uses of the chain.
7531 std::vector<SDValue> VLDDupResults;
7532 for (unsigned n = 0; n < NumVecs; ++n)
7533 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
7534 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
7535 DCI.CombineTo(VLD, VLDDupResults);
7536
7537 return true;
7538}
7539
Bob Wilson9e82bf12010-07-14 01:22:12 +00007540/// PerformVDUPLANECombine - Target-specific dag combine xforms for
7541/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007542static SDValue PerformVDUPLANECombine(SDNode *N,
7543 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00007544 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007545
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007546 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
7547 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
7548 if (CombineVLDDUP(N, DCI))
7549 return SDValue(N, 0);
7550
7551 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
7552 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007553 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00007554 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00007555 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00007556 return SDValue();
7557
7558 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
7559 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
7560 // The canonical VMOV for a zero vector uses a 32-bit element size.
7561 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7562 unsigned EltBits;
7563 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
7564 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007565 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007566 if (EltSize > VT.getVectorElementType().getSizeInBits())
7567 return SDValue();
7568
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007569 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007570}
7571
Eric Christopherfa6f5912011-06-29 21:10:36 +00007572// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00007573// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
7574static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
7575{
Chad Rosier118c9a02011-06-28 17:26:57 +00007576 integerPart cN;
7577 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00007578 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
7579 I != E; I++) {
7580 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
7581 if (!C)
7582 return false;
7583
Eric Christopherfa6f5912011-06-29 21:10:36 +00007584 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00007585 APFloat APF = C->getValueAPF();
7586 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
7587 != APFloat::opOK || !isExact)
7588 return false;
7589
7590 c0 = (I == 0) ? cN : c0;
7591 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
7592 return false;
7593 }
7594 C = c0;
7595 return true;
7596}
7597
7598/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
7599/// can replace combinations of VMUL and VCVT (floating-point to integer)
7600/// when the VMUL has a constant operand that is a power of 2.
7601///
7602/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
7603/// vmul.f32 d16, d17, d16
7604/// vcvt.s32.f32 d16, d16
7605/// becomes:
7606/// vcvt.s32.f32 d16, d16, #3
7607static SDValue PerformVCVTCombine(SDNode *N,
7608 TargetLowering::DAGCombinerInfo &DCI,
7609 const ARMSubtarget *Subtarget) {
7610 SelectionDAG &DAG = DCI.DAG;
7611 SDValue Op = N->getOperand(0);
7612
7613 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
7614 Op.getOpcode() != ISD::FMUL)
7615 return SDValue();
7616
7617 uint64_t C;
7618 SDValue N0 = Op->getOperand(0);
7619 SDValue ConstVec = Op->getOperand(1);
7620 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
7621
Eric Christopherfa6f5912011-06-29 21:10:36 +00007622 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00007623 !isConstVecPow2(ConstVec, isSigned, C))
7624 return SDValue();
7625
7626 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
7627 Intrinsic::arm_neon_vcvtfp2fxu;
7628 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7629 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00007630 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00007631 DAG.getConstant(Log2_64(C), MVT::i32));
7632}
7633
7634/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
7635/// can replace combinations of VCVT (integer to floating-point) and VDIV
7636/// when the VDIV has a constant operand that is a power of 2.
7637///
7638/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
7639/// vcvt.f32.s32 d16, d16
7640/// vdiv.f32 d16, d17, d16
7641/// becomes:
7642/// vcvt.f32.s32 d16, d16, #3
7643static SDValue PerformVDIVCombine(SDNode *N,
7644 TargetLowering::DAGCombinerInfo &DCI,
7645 const ARMSubtarget *Subtarget) {
7646 SelectionDAG &DAG = DCI.DAG;
7647 SDValue Op = N->getOperand(0);
7648 unsigned OpOpcode = Op.getNode()->getOpcode();
7649
7650 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
7651 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
7652 return SDValue();
7653
7654 uint64_t C;
7655 SDValue ConstVec = N->getOperand(1);
7656 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
7657
7658 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
7659 !isConstVecPow2(ConstVec, isSigned, C))
7660 return SDValue();
7661
Eric Christopherfa6f5912011-06-29 21:10:36 +00007662 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00007663 Intrinsic::arm_neon_vcvtfxu2fp;
7664 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7665 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00007666 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00007667 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
7668}
7669
7670/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00007671/// operand of a vector shift operation, where all the elements of the
7672/// build_vector must have the same constant integer value.
7673static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
7674 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007675 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00007676 Op = Op.getOperand(0);
7677 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
7678 APInt SplatBits, SplatUndef;
7679 unsigned SplatBitSize;
7680 bool HasAnyUndefs;
7681 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
7682 HasAnyUndefs, ElementBits) ||
7683 SplatBitSize > ElementBits)
7684 return false;
7685 Cnt = SplatBits.getSExtValue();
7686 return true;
7687}
7688
7689/// isVShiftLImm - Check if this is a valid build_vector for the immediate
7690/// operand of a vector shift left operation. That value must be in the range:
7691/// 0 <= Value < ElementBits for a left shift; or
7692/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00007693static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00007694 assert(VT.isVector() && "vector shift count is not a vector type");
7695 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
7696 if (! getVShiftImm(Op, ElementBits, Cnt))
7697 return false;
7698 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
7699}
7700
7701/// isVShiftRImm - Check if this is a valid build_vector for the immediate
7702/// operand of a vector shift right operation. For a shift opcode, the value
7703/// is positive, but for an intrinsic the value count must be negative. The
7704/// absolute value must be in the range:
7705/// 1 <= |Value| <= ElementBits for a right shift; or
7706/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00007707static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00007708 int64_t &Cnt) {
7709 assert(VT.isVector() && "vector shift count is not a vector type");
7710 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
7711 if (! getVShiftImm(Op, ElementBits, Cnt))
7712 return false;
7713 if (isIntrinsic)
7714 Cnt = -Cnt;
7715 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
7716}
7717
7718/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
7719static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
7720 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
7721 switch (IntNo) {
7722 default:
7723 // Don't do anything for most intrinsics.
7724 break;
7725
7726 // Vector shifts: check for immediate versions and lower them.
7727 // Note: This is done during DAG combining instead of DAG legalizing because
7728 // the build_vectors for 64-bit vector element shift counts are generally
7729 // not legal, and it is hard to see their values after they get legalized to
7730 // loads from a constant pool.
7731 case Intrinsic::arm_neon_vshifts:
7732 case Intrinsic::arm_neon_vshiftu:
7733 case Intrinsic::arm_neon_vshiftls:
7734 case Intrinsic::arm_neon_vshiftlu:
7735 case Intrinsic::arm_neon_vshiftn:
7736 case Intrinsic::arm_neon_vrshifts:
7737 case Intrinsic::arm_neon_vrshiftu:
7738 case Intrinsic::arm_neon_vrshiftn:
7739 case Intrinsic::arm_neon_vqshifts:
7740 case Intrinsic::arm_neon_vqshiftu:
7741 case Intrinsic::arm_neon_vqshiftsu:
7742 case Intrinsic::arm_neon_vqshiftns:
7743 case Intrinsic::arm_neon_vqshiftnu:
7744 case Intrinsic::arm_neon_vqshiftnsu:
7745 case Intrinsic::arm_neon_vqrshiftns:
7746 case Intrinsic::arm_neon_vqrshiftnu:
7747 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00007748 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007749 int64_t Cnt;
7750 unsigned VShiftOpc = 0;
7751
7752 switch (IntNo) {
7753 case Intrinsic::arm_neon_vshifts:
7754 case Intrinsic::arm_neon_vshiftu:
7755 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
7756 VShiftOpc = ARMISD::VSHL;
7757 break;
7758 }
7759 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
7760 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
7761 ARMISD::VSHRs : ARMISD::VSHRu);
7762 break;
7763 }
7764 return SDValue();
7765
7766 case Intrinsic::arm_neon_vshiftls:
7767 case Intrinsic::arm_neon_vshiftlu:
7768 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
7769 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007770 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007771
7772 case Intrinsic::arm_neon_vrshifts:
7773 case Intrinsic::arm_neon_vrshiftu:
7774 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
7775 break;
7776 return SDValue();
7777
7778 case Intrinsic::arm_neon_vqshifts:
7779 case Intrinsic::arm_neon_vqshiftu:
7780 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
7781 break;
7782 return SDValue();
7783
7784 case Intrinsic::arm_neon_vqshiftsu:
7785 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
7786 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007787 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007788
7789 case Intrinsic::arm_neon_vshiftn:
7790 case Intrinsic::arm_neon_vrshiftn:
7791 case Intrinsic::arm_neon_vqshiftns:
7792 case Intrinsic::arm_neon_vqshiftnu:
7793 case Intrinsic::arm_neon_vqshiftnsu:
7794 case Intrinsic::arm_neon_vqrshiftns:
7795 case Intrinsic::arm_neon_vqrshiftnu:
7796 case Intrinsic::arm_neon_vqrshiftnsu:
7797 // Narrowing shifts require an immediate right shift.
7798 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
7799 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00007800 llvm_unreachable("invalid shift count for narrowing vector shift "
7801 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007802
7803 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00007804 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00007805 }
7806
7807 switch (IntNo) {
7808 case Intrinsic::arm_neon_vshifts:
7809 case Intrinsic::arm_neon_vshiftu:
7810 // Opcode already set above.
7811 break;
7812 case Intrinsic::arm_neon_vshiftls:
7813 case Intrinsic::arm_neon_vshiftlu:
7814 if (Cnt == VT.getVectorElementType().getSizeInBits())
7815 VShiftOpc = ARMISD::VSHLLi;
7816 else
7817 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
7818 ARMISD::VSHLLs : ARMISD::VSHLLu);
7819 break;
7820 case Intrinsic::arm_neon_vshiftn:
7821 VShiftOpc = ARMISD::VSHRN; break;
7822 case Intrinsic::arm_neon_vrshifts:
7823 VShiftOpc = ARMISD::VRSHRs; break;
7824 case Intrinsic::arm_neon_vrshiftu:
7825 VShiftOpc = ARMISD::VRSHRu; break;
7826 case Intrinsic::arm_neon_vrshiftn:
7827 VShiftOpc = ARMISD::VRSHRN; break;
7828 case Intrinsic::arm_neon_vqshifts:
7829 VShiftOpc = ARMISD::VQSHLs; break;
7830 case Intrinsic::arm_neon_vqshiftu:
7831 VShiftOpc = ARMISD::VQSHLu; break;
7832 case Intrinsic::arm_neon_vqshiftsu:
7833 VShiftOpc = ARMISD::VQSHLsu; break;
7834 case Intrinsic::arm_neon_vqshiftns:
7835 VShiftOpc = ARMISD::VQSHRNs; break;
7836 case Intrinsic::arm_neon_vqshiftnu:
7837 VShiftOpc = ARMISD::VQSHRNu; break;
7838 case Intrinsic::arm_neon_vqshiftnsu:
7839 VShiftOpc = ARMISD::VQSHRNsu; break;
7840 case Intrinsic::arm_neon_vqrshiftns:
7841 VShiftOpc = ARMISD::VQRSHRNs; break;
7842 case Intrinsic::arm_neon_vqrshiftnu:
7843 VShiftOpc = ARMISD::VQRSHRNu; break;
7844 case Intrinsic::arm_neon_vqrshiftnsu:
7845 VShiftOpc = ARMISD::VQRSHRNsu; break;
7846 }
7847
7848 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007849 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007850 }
7851
7852 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00007853 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007854 int64_t Cnt;
7855 unsigned VShiftOpc = 0;
7856
7857 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
7858 VShiftOpc = ARMISD::VSLI;
7859 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
7860 VShiftOpc = ARMISD::VSRI;
7861 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00007862 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007863 }
7864
7865 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
7866 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00007867 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007868 }
7869
7870 case Intrinsic::arm_neon_vqrshifts:
7871 case Intrinsic::arm_neon_vqrshiftu:
7872 // No immediate versions of these to check for.
7873 break;
7874 }
7875
7876 return SDValue();
7877}
7878
7879/// PerformShiftCombine - Checks for immediate versions of vector shifts and
7880/// lowers them. As with the vector shift intrinsics, this is done during DAG
7881/// combining instead of DAG legalizing because the build_vectors for 64-bit
7882/// vector element shift counts are generally not legal, and it is hard to see
7883/// their values after they get legalized to loads from a constant pool.
7884static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
7885 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00007886 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00007887
7888 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00007889 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7890 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00007891 return SDValue();
7892
7893 assert(ST->hasNEON() && "unexpected vector shift");
7894 int64_t Cnt;
7895
7896 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007897 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00007898
7899 case ISD::SHL:
7900 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
7901 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007902 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007903 break;
7904
7905 case ISD::SRA:
7906 case ISD::SRL:
7907 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
7908 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
7909 ARMISD::VSHRs : ARMISD::VSHRu);
7910 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007911 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007912 }
7913 }
7914 return SDValue();
7915}
7916
7917/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
7918/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
7919static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
7920 const ARMSubtarget *ST) {
7921 SDValue N0 = N->getOperand(0);
7922
7923 // Check for sign- and zero-extensions of vector extract operations of 8-
7924 // and 16-bit vector elements. NEON supports these directly. They are
7925 // handled during DAG combining because type legalization will promote them
7926 // to 32-bit types and it is messy to recognize the operations after that.
7927 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
7928 SDValue Vec = N0.getOperand(0);
7929 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00007930 EVT VT = N->getValueType(0);
7931 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007932 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7933
Owen Anderson825b72b2009-08-11 20:47:22 +00007934 if (VT == MVT::i32 &&
7935 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00007936 TLI.isTypeLegal(Vec.getValueType()) &&
7937 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00007938
7939 unsigned Opc = 0;
7940 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007941 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00007942 case ISD::SIGN_EXTEND:
7943 Opc = ARMISD::VGETLANEs;
7944 break;
7945 case ISD::ZERO_EXTEND:
7946 case ISD::ANY_EXTEND:
7947 Opc = ARMISD::VGETLANEu;
7948 break;
7949 }
7950 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
7951 }
7952 }
7953
7954 return SDValue();
7955}
7956
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007957/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
7958/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
7959static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
7960 const ARMSubtarget *ST) {
7961 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00007962 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007963 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
7964 // a NaN; only do the transformation when it matches that behavior.
7965
7966 // For now only do this when using NEON for FP operations; if using VFP, it
7967 // is not obvious that the benefit outweighs the cost of switching to the
7968 // NEON pipeline.
7969 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
7970 N->getValueType(0) != MVT::f32)
7971 return SDValue();
7972
7973 SDValue CondLHS = N->getOperand(0);
7974 SDValue CondRHS = N->getOperand(1);
7975 SDValue LHS = N->getOperand(2);
7976 SDValue RHS = N->getOperand(3);
7977 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
7978
7979 unsigned Opcode = 0;
7980 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00007981 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007982 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00007983 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007984 IsReversed = true ; // x CC y ? y : x
7985 } else {
7986 return SDValue();
7987 }
7988
Bob Wilsone742bb52010-02-24 22:15:53 +00007989 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007990 switch (CC) {
7991 default: break;
7992 case ISD::SETOLT:
7993 case ISD::SETOLE:
7994 case ISD::SETLT:
7995 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007996 case ISD::SETULT:
7997 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00007998 // If LHS is NaN, an ordered comparison will be false and the result will
7999 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
8000 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
8001 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
8002 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8003 break;
8004 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
8005 // will return -0, so vmin can only be used for unsafe math or if one of
8006 // the operands is known to be nonzero.
8007 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
8008 !UnsafeFPMath &&
8009 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8010 break;
8011 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008012 break;
8013
8014 case ISD::SETOGT:
8015 case ISD::SETOGE:
8016 case ISD::SETGT:
8017 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008018 case ISD::SETUGT:
8019 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00008020 // If LHS is NaN, an ordered comparison will be false and the result will
8021 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
8022 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
8023 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
8024 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8025 break;
8026 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
8027 // will return +0, so vmax can only be used for unsafe math or if one of
8028 // the operands is known to be nonzero.
8029 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
8030 !UnsafeFPMath &&
8031 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8032 break;
8033 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008034 break;
8035 }
8036
8037 if (!Opcode)
8038 return SDValue();
8039 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
8040}
8041
Evan Chenge721f5c2011-07-13 00:42:17 +00008042/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
8043SDValue
8044ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
8045 SDValue Cmp = N->getOperand(4);
8046 if (Cmp.getOpcode() != ARMISD::CMPZ)
8047 // Only looking at EQ and NE cases.
8048 return SDValue();
8049
8050 EVT VT = N->getValueType(0);
8051 DebugLoc dl = N->getDebugLoc();
8052 SDValue LHS = Cmp.getOperand(0);
8053 SDValue RHS = Cmp.getOperand(1);
8054 SDValue FalseVal = N->getOperand(0);
8055 SDValue TrueVal = N->getOperand(1);
8056 SDValue ARMcc = N->getOperand(2);
Jim Grosbachb04546f2011-09-13 20:30:37 +00008057 ARMCC::CondCodes CC =
8058 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chenge721f5c2011-07-13 00:42:17 +00008059
8060 // Simplify
8061 // mov r1, r0
8062 // cmp r1, x
8063 // mov r0, y
8064 // moveq r0, x
8065 // to
8066 // cmp r0, x
8067 // movne r0, y
8068 //
8069 // mov r1, r0
8070 // cmp r1, x
8071 // mov r0, x
8072 // movne r0, y
8073 // to
8074 // cmp r0, x
8075 // movne r0, y
8076 /// FIXME: Turn this into a target neutral optimization?
8077 SDValue Res;
Evan Cheng9b88d2d2011-09-28 23:16:31 +00008078 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chenge721f5c2011-07-13 00:42:17 +00008079 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
8080 N->getOperand(3), Cmp);
8081 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
8082 SDValue ARMcc;
8083 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
8084 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
8085 N->getOperand(3), NewCmp);
8086 }
8087
8088 if (Res.getNode()) {
8089 APInt KnownZero, KnownOne;
8090 APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits());
8091 DAG.ComputeMaskedBits(SDValue(N,0), Mask, KnownZero, KnownOne);
8092 // Capture demanded bits information that would be otherwise lost.
8093 if (KnownZero == 0xfffffffe)
8094 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8095 DAG.getValueType(MVT::i1));
8096 else if (KnownZero == 0xffffff00)
8097 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8098 DAG.getValueType(MVT::i8));
8099 else if (KnownZero == 0xffff0000)
8100 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8101 DAG.getValueType(MVT::i16));
8102 }
8103
8104 return Res;
8105}
8106
Dan Gohman475871a2008-07-27 21:46:04 +00008107SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008108 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008109 switch (N->getOpcode()) {
8110 default: break;
Tanya Lattner189531f2011-06-14 23:48:48 +00008111 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008112 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00008113 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008114 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00008115 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00008116 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00008117 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008118 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00008119 case ISD::STORE: return PerformSTORECombine(N, DCI);
8120 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
8121 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00008122 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008123 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00008124 case ISD::FP_TO_SINT:
8125 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
8126 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008127 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00008128 case ISD::SHL:
8129 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008130 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00008131 case ISD::SIGN_EXTEND:
8132 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008133 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
8134 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00008135 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00008136 case ARMISD::VLD2DUP:
8137 case ARMISD::VLD3DUP:
8138 case ARMISD::VLD4DUP:
8139 return CombineBaseUpdate(N, DCI);
8140 case ISD::INTRINSIC_VOID:
8141 case ISD::INTRINSIC_W_CHAIN:
8142 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
8143 case Intrinsic::arm_neon_vld1:
8144 case Intrinsic::arm_neon_vld2:
8145 case Intrinsic::arm_neon_vld3:
8146 case Intrinsic::arm_neon_vld4:
8147 case Intrinsic::arm_neon_vld2lane:
8148 case Intrinsic::arm_neon_vld3lane:
8149 case Intrinsic::arm_neon_vld4lane:
8150 case Intrinsic::arm_neon_vst1:
8151 case Intrinsic::arm_neon_vst2:
8152 case Intrinsic::arm_neon_vst3:
8153 case Intrinsic::arm_neon_vst4:
8154 case Intrinsic::arm_neon_vst2lane:
8155 case Intrinsic::arm_neon_vst3lane:
8156 case Intrinsic::arm_neon_vst4lane:
8157 return CombineBaseUpdate(N, DCI);
8158 default: break;
8159 }
8160 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008161 }
Dan Gohman475871a2008-07-27 21:46:04 +00008162 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008163}
8164
Evan Cheng31959b12011-02-02 01:06:55 +00008165bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
8166 EVT VT) const {
8167 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
8168}
8169
Bill Wendlingaf566342009-08-15 21:21:19 +00008170bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00008171 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00008172 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00008173
8174 switch (VT.getSimpleVT().SimpleTy) {
8175 default:
8176 return false;
8177 case MVT::i8:
8178 case MVT::i16:
8179 case MVT::i32:
8180 return true;
8181 // FIXME: VLD1 etc with standard alignment is legal.
8182 }
8183}
8184
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008185static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
8186 unsigned AlignCheck) {
8187 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
8188 (DstAlign == 0 || DstAlign % AlignCheck == 0));
8189}
8190
8191EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
8192 unsigned DstAlign, unsigned SrcAlign,
Lang Hamesa1e78882011-11-02 23:37:04 +00008193 bool IsZeroVal,
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008194 bool MemcpyStrSrc,
8195 MachineFunction &MF) const {
8196 const Function *F = MF.getFunction();
8197
8198 // See if we can use NEON instructions for this...
Lang Hamesa1e78882011-11-02 23:37:04 +00008199 if (IsZeroVal &&
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008200 !F->hasFnAttr(Attribute::NoImplicitFloat) &&
8201 Subtarget->hasNEON()) {
8202 if (memOpAlign(SrcAlign, DstAlign, 16) && Size >= 16) {
8203 return MVT::v4i32;
8204 } else if (memOpAlign(SrcAlign, DstAlign, 8) && Size >= 8) {
8205 return MVT::v2i32;
8206 }
8207 }
8208
Lang Hames5207bf22011-11-08 18:56:23 +00008209 // Lowering to i32/i16 if the size permits.
8210 if (Size >= 4) {
8211 return MVT::i32;
8212 } else if (Size >= 2) {
8213 return MVT::i16;
8214 }
8215
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008216 // Let the target-independent logic figure it out.
8217 return MVT::Other;
8218}
8219
Evan Chenge6c835f2009-08-14 20:09:37 +00008220static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
8221 if (V < 0)
8222 return false;
8223
8224 unsigned Scale = 1;
8225 switch (VT.getSimpleVT().SimpleTy) {
8226 default: return false;
8227 case MVT::i1:
8228 case MVT::i8:
8229 // Scale == 1;
8230 break;
8231 case MVT::i16:
8232 // Scale == 2;
8233 Scale = 2;
8234 break;
8235 case MVT::i32:
8236 // Scale == 4;
8237 Scale = 4;
8238 break;
8239 }
8240
8241 if ((V & (Scale - 1)) != 0)
8242 return false;
8243 V /= Scale;
8244 return V == (V & ((1LL << 5) - 1));
8245}
8246
8247static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
8248 const ARMSubtarget *Subtarget) {
8249 bool isNeg = false;
8250 if (V < 0) {
8251 isNeg = true;
8252 V = - V;
8253 }
8254
8255 switch (VT.getSimpleVT().SimpleTy) {
8256 default: return false;
8257 case MVT::i1:
8258 case MVT::i8:
8259 case MVT::i16:
8260 case MVT::i32:
8261 // + imm12 or - imm8
8262 if (isNeg)
8263 return V == (V & ((1LL << 8) - 1));
8264 return V == (V & ((1LL << 12) - 1));
8265 case MVT::f32:
8266 case MVT::f64:
8267 // Same as ARM mode. FIXME: NEON?
8268 if (!Subtarget->hasVFP2())
8269 return false;
8270 if ((V & 3) != 0)
8271 return false;
8272 V >>= 2;
8273 return V == (V & ((1LL << 8) - 1));
8274 }
8275}
8276
Evan Chengb01fad62007-03-12 23:30:29 +00008277/// isLegalAddressImmediate - Return true if the integer value can be used
8278/// as the offset of the target addressing mode for load / store of the
8279/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00008280static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00008281 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00008282 if (V == 0)
8283 return true;
8284
Evan Cheng65011532009-03-09 19:15:00 +00008285 if (!VT.isSimple())
8286 return false;
8287
Evan Chenge6c835f2009-08-14 20:09:37 +00008288 if (Subtarget->isThumb1Only())
8289 return isLegalT1AddressImmediate(V, VT);
8290 else if (Subtarget->isThumb2())
8291 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00008292
Evan Chenge6c835f2009-08-14 20:09:37 +00008293 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00008294 if (V < 0)
8295 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00008296 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00008297 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008298 case MVT::i1:
8299 case MVT::i8:
8300 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00008301 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008302 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008303 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00008304 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008305 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008306 case MVT::f32:
8307 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00008308 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00008309 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00008310 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00008311 return false;
8312 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008313 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00008314 }
Evan Chenga8e29892007-01-19 07:51:42 +00008315}
8316
Evan Chenge6c835f2009-08-14 20:09:37 +00008317bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
8318 EVT VT) const {
8319 int Scale = AM.Scale;
8320 if (Scale < 0)
8321 return false;
8322
8323 switch (VT.getSimpleVT().SimpleTy) {
8324 default: return false;
8325 case MVT::i1:
8326 case MVT::i8:
8327 case MVT::i16:
8328 case MVT::i32:
8329 if (Scale == 1)
8330 return true;
8331 // r + r << imm
8332 Scale = Scale & ~1;
8333 return Scale == 2 || Scale == 4 || Scale == 8;
8334 case MVT::i64:
8335 // r + r
8336 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
8337 return true;
8338 return false;
8339 case MVT::isVoid:
8340 // Note, we allow "void" uses (basically, uses that aren't loads or
8341 // stores), because arm allows folding a scale into many arithmetic
8342 // operations. This should be made more precise and revisited later.
8343
8344 // Allow r << imm, but the imm has to be a multiple of two.
8345 if (Scale & 1) return false;
8346 return isPowerOf2_32(Scale);
8347 }
8348}
8349
Chris Lattner37caf8c2007-04-09 23:33:39 +00008350/// isLegalAddressingMode - Return true if the addressing mode represented
8351/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008352bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008353 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008354 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00008355 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00008356 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008357
Chris Lattner37caf8c2007-04-09 23:33:39 +00008358 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008359 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008360 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008361
Chris Lattner37caf8c2007-04-09 23:33:39 +00008362 switch (AM.Scale) {
8363 case 0: // no scale reg, must be "r+i" or "r", or "i".
8364 break;
8365 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00008366 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00008367 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008368 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00008369 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008370 // ARM doesn't support any R+R*scale+imm addr modes.
8371 if (AM.BaseOffs)
8372 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008373
Bob Wilson2c7dab12009-04-08 17:55:28 +00008374 if (!VT.isSimple())
8375 return false;
8376
Evan Chenge6c835f2009-08-14 20:09:37 +00008377 if (Subtarget->isThumb2())
8378 return isLegalT2ScaledAddressingMode(AM, VT);
8379
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008380 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00008381 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00008382 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008383 case MVT::i1:
8384 case MVT::i8:
8385 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008386 if (Scale < 0) Scale = -Scale;
8387 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008388 return true;
8389 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00008390 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008391 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00008392 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00008393 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008394 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008395 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00008396 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008397
Owen Anderson825b72b2009-08-11 20:47:22 +00008398 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00008399 // Note, we allow "void" uses (basically, uses that aren't loads or
8400 // stores), because arm allows folding a scale into many arithmetic
8401 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008402
Chris Lattner37caf8c2007-04-09 23:33:39 +00008403 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00008404 if (Scale & 1) return false;
8405 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00008406 }
8407 break;
Evan Chengb01fad62007-03-12 23:30:29 +00008408 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00008409 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00008410}
8411
Evan Cheng77e47512009-11-11 19:05:52 +00008412/// isLegalICmpImmediate - Return true if the specified immediate is legal
8413/// icmp immediate, that is the target has icmp instructions which can compare
8414/// a register against the immediate without having to materialize the
8415/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00008416bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00008417 if (!Subtarget->isThumb())
8418 return ARM_AM::getSOImmVal(Imm) != -1;
8419 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00008420 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00008421 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00008422}
8423
Dan Gohmancca82142011-05-03 00:46:49 +00008424/// isLegalAddImmediate - Return true if the specified immediate is legal
8425/// add immediate, that is the target has add instructions which can add
8426/// a register with the immediate without having to materialize the
8427/// immediate into a register.
8428bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
8429 return ARM_AM::getSOImmVal(Imm) != -1;
8430}
8431
Owen Andersone50ed302009-08-10 22:56:29 +00008432static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00008433 bool isSEXTLoad, SDValue &Base,
8434 SDValue &Offset, bool &isInc,
8435 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00008436 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
8437 return false;
8438
Owen Anderson825b72b2009-08-11 20:47:22 +00008439 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00008440 // AddressingMode 3
8441 Base = Ptr->getOperand(0);
8442 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008443 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00008444 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008445 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00008446 isInc = false;
8447 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8448 return true;
8449 }
8450 }
8451 isInc = (Ptr->getOpcode() == ISD::ADD);
8452 Offset = Ptr->getOperand(1);
8453 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00008454 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00008455 // AddressingMode 2
8456 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008457 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00008458 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008459 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00008460 isInc = false;
8461 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8462 Base = Ptr->getOperand(0);
8463 return true;
8464 }
8465 }
8466
8467 if (Ptr->getOpcode() == ISD::ADD) {
8468 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00008469 ARM_AM::ShiftOpc ShOpcVal=
8470 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00008471 if (ShOpcVal != ARM_AM::no_shift) {
8472 Base = Ptr->getOperand(1);
8473 Offset = Ptr->getOperand(0);
8474 } else {
8475 Base = Ptr->getOperand(0);
8476 Offset = Ptr->getOperand(1);
8477 }
8478 return true;
8479 }
8480
8481 isInc = (Ptr->getOpcode() == ISD::ADD);
8482 Base = Ptr->getOperand(0);
8483 Offset = Ptr->getOperand(1);
8484 return true;
8485 }
8486
Jim Grosbache5165492009-11-09 00:11:35 +00008487 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00008488 return false;
8489}
8490
Owen Andersone50ed302009-08-10 22:56:29 +00008491static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00008492 bool isSEXTLoad, SDValue &Base,
8493 SDValue &Offset, bool &isInc,
8494 SelectionDAG &DAG) {
8495 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
8496 return false;
8497
8498 Base = Ptr->getOperand(0);
8499 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
8500 int RHSC = (int)RHS->getZExtValue();
8501 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
8502 assert(Ptr->getOpcode() == ISD::ADD);
8503 isInc = false;
8504 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8505 return true;
8506 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
8507 isInc = Ptr->getOpcode() == ISD::ADD;
8508 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
8509 return true;
8510 }
8511 }
8512
8513 return false;
8514}
8515
Evan Chenga8e29892007-01-19 07:51:42 +00008516/// getPreIndexedAddressParts - returns true by value, base pointer and
8517/// offset pointer and addressing mode by reference if the node's address
8518/// can be legally represented as pre-indexed load / store address.
8519bool
Dan Gohman475871a2008-07-27 21:46:04 +00008520ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
8521 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00008522 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00008523 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008524 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00008525 return false;
8526
Owen Andersone50ed302009-08-10 22:56:29 +00008527 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00008528 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00008529 bool isSEXTLoad = false;
8530 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
8531 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008532 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00008533 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
8534 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
8535 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008536 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00008537 } else
8538 return false;
8539
8540 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00008541 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00008542 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00008543 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
8544 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00008545 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00008546 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00008547 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00008548 if (!isLegal)
8549 return false;
8550
8551 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
8552 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00008553}
8554
8555/// getPostIndexedAddressParts - returns true by value, base pointer and
8556/// offset pointer and addressing mode by reference if this node can be
8557/// combined with a load / store to form a post-indexed load / store.
8558bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00008559 SDValue &Base,
8560 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00008561 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00008562 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008563 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00008564 return false;
8565
Owen Andersone50ed302009-08-10 22:56:29 +00008566 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00008567 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00008568 bool isSEXTLoad = false;
8569 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008570 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00008571 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00008572 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
8573 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008574 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00008575 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00008576 } else
8577 return false;
8578
8579 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00008580 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00008581 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00008582 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00008583 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00008584 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00008585 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
8586 isInc, DAG);
8587 if (!isLegal)
8588 return false;
8589
Evan Cheng28dad2a2010-05-18 21:31:17 +00008590 if (Ptr != Base) {
8591 // Swap base ptr and offset to catch more post-index load / store when
8592 // it's legal. In Thumb2 mode, offset must be an immediate.
8593 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
8594 !Subtarget->isThumb2())
8595 std::swap(Base, Offset);
8596
8597 // Post-indexed load / store update the base pointer.
8598 if (Ptr != Base)
8599 return false;
8600 }
8601
Evan Chenge88d5ce2009-07-02 07:28:31 +00008602 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
8603 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00008604}
8605
Dan Gohman475871a2008-07-27 21:46:04 +00008606void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00008607 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008608 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008609 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008610 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00008611 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008612 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00008613 switch (Op.getOpcode()) {
8614 default: break;
8615 case ARMISD::CMOV: {
8616 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00008617 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00008618 if (KnownZero == 0 && KnownOne == 0) return;
8619
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008620 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00008621 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
8622 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00008623 KnownZero &= KnownZeroRHS;
8624 KnownOne &= KnownOneRHS;
8625 return;
8626 }
8627 }
8628}
8629
8630//===----------------------------------------------------------------------===//
8631// ARM Inline Assembly Support
8632//===----------------------------------------------------------------------===//
8633
Evan Cheng55d42002011-01-08 01:24:27 +00008634bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
8635 // Looking for "rev" which is V6+.
8636 if (!Subtarget->hasV6Ops())
8637 return false;
8638
8639 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
8640 std::string AsmStr = IA->getAsmString();
8641 SmallVector<StringRef, 4> AsmPieces;
8642 SplitString(AsmStr, AsmPieces, ";\n");
8643
8644 switch (AsmPieces.size()) {
8645 default: return false;
8646 case 1:
8647 AsmStr = AsmPieces[0];
8648 AsmPieces.clear();
8649 SplitString(AsmStr, AsmPieces, " \t,");
8650
8651 // rev $0, $1
8652 if (AsmPieces.size() == 3 &&
8653 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
8654 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008655 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00008656 if (Ty && Ty->getBitWidth() == 32)
8657 return IntrinsicLowering::LowerToByteSwap(CI);
8658 }
8659 break;
8660 }
8661
8662 return false;
8663}
8664
Evan Chenga8e29892007-01-19 07:51:42 +00008665/// getConstraintType - Given a constraint letter, return the type of
8666/// constraint it is for this target.
8667ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00008668ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
8669 if (Constraint.size() == 1) {
8670 switch (Constraint[0]) {
8671 default: break;
8672 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008673 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00008674 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00008675 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008676 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00008677 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00008678 // An address with a single base register. Due to the way we
8679 // currently handle addresses it is the same as an 'r' memory constraint.
8680 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00008681 }
Eric Christopher1312ca82011-06-21 22:10:57 +00008682 } else if (Constraint.size() == 2) {
8683 switch (Constraint[0]) {
8684 default: break;
8685 // All 'U+' constraints are addresses.
8686 case 'U': return C_Memory;
8687 }
Evan Chenga8e29892007-01-19 07:51:42 +00008688 }
Chris Lattner4234f572007-03-25 02:14:49 +00008689 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00008690}
8691
John Thompson44ab89e2010-10-29 17:29:13 +00008692/// Examine constraint type and operand type and determine a weight value.
8693/// This object must already have been set up with the operand type
8694/// and the current alternative constraint selected.
8695TargetLowering::ConstraintWeight
8696ARMTargetLowering::getSingleConstraintMatchWeight(
8697 AsmOperandInfo &info, const char *constraint) const {
8698 ConstraintWeight weight = CW_Invalid;
8699 Value *CallOperandVal = info.CallOperandVal;
8700 // If we don't have a value, we can't do a match,
8701 // but allow it at the lowest weight.
8702 if (CallOperandVal == NULL)
8703 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008704 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00008705 // Look at the constraint type.
8706 switch (*constraint) {
8707 default:
8708 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
8709 break;
8710 case 'l':
8711 if (type->isIntegerTy()) {
8712 if (Subtarget->isThumb())
8713 weight = CW_SpecificReg;
8714 else
8715 weight = CW_Register;
8716 }
8717 break;
8718 case 'w':
8719 if (type->isFloatingPointTy())
8720 weight = CW_Register;
8721 break;
8722 }
8723 return weight;
8724}
8725
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008726typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
8727RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00008728ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00008729 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00008730 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00008731 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00008732 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00008733 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00008734 if (Subtarget->isThumb())
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008735 return RCPair(0U, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +00008736 else
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008737 return RCPair(0U, ARM::GPRRegisterClass);
Eric Christopher73744df2011-06-30 23:23:01 +00008738 case 'h': // High regs or no regs.
8739 if (Subtarget->isThumb())
Andrew Trick3af7a672011-09-20 03:06:13 +00008740 return RCPair(0U, ARM::hGPRRegisterClass);
Eric Christopher1070f822011-07-01 00:19:27 +00008741 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008742 case 'r':
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008743 return RCPair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008744 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00008745 if (VT == MVT::f32)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008746 return RCPair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00008747 if (VT.getSizeInBits() == 64)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008748 return RCPair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00008749 if (VT.getSizeInBits() == 128)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008750 return RCPair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008751 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00008752 case 'x':
8753 if (VT == MVT::f32)
Andrew Trick3af7a672011-09-20 03:06:13 +00008754 return RCPair(0U, ARM::SPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008755 if (VT.getSizeInBits() == 64)
Andrew Trick3af7a672011-09-20 03:06:13 +00008756 return RCPair(0U, ARM::DPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008757 if (VT.getSizeInBits() == 128)
Andrew Trick3af7a672011-09-20 03:06:13 +00008758 return RCPair(0U, ARM::QPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008759 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008760 case 't':
8761 if (VT == MVT::f32)
Andrew Trick3af7a672011-09-20 03:06:13 +00008762 return RCPair(0U, ARM::SPRRegisterClass);
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008763 break;
Evan Chenga8e29892007-01-19 07:51:42 +00008764 }
8765 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00008766 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00008767 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00008768
Evan Chenga8e29892007-01-19 07:51:42 +00008769 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
8770}
8771
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008772/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
8773/// vector. If it is invalid, don't add anything to Ops.
8774void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00008775 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008776 std::vector<SDValue>&Ops,
8777 SelectionDAG &DAG) const {
8778 SDValue Result(0, 0);
8779
Eric Christopher100c8332011-06-02 23:16:42 +00008780 // Currently only support length 1 constraints.
8781 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00008782
Eric Christopher100c8332011-06-02 23:16:42 +00008783 char ConstraintLetter = Constraint[0];
8784 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008785 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00008786 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008787 case 'I': case 'J': case 'K': case 'L':
8788 case 'M': case 'N': case 'O':
8789 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
8790 if (!C)
8791 return;
8792
8793 int64_t CVal64 = C->getSExtValue();
8794 int CVal = (int) CVal64;
8795 // None of these constraints allow values larger than 32 bits. Check
8796 // that the value fits in an int.
8797 if (CVal != CVal64)
8798 return;
8799
Eric Christopher100c8332011-06-02 23:16:42 +00008800 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00008801 case 'j':
Andrew Trick3af7a672011-09-20 03:06:13 +00008802 // Constant suitable for movw, must be between 0 and
8803 // 65535.
8804 if (Subtarget->hasV6T2Ops())
8805 if (CVal >= 0 && CVal <= 65535)
8806 break;
8807 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008808 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008809 if (Subtarget->isThumb1Only()) {
8810 // This must be a constant between 0 and 255, for ADD
8811 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008812 if (CVal >= 0 && CVal <= 255)
8813 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008814 } else if (Subtarget->isThumb2()) {
8815 // A constant that can be used as an immediate value in a
8816 // data-processing instruction.
8817 if (ARM_AM::getT2SOImmVal(CVal) != -1)
8818 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008819 } else {
8820 // A constant that can be used as an immediate value in a
8821 // data-processing instruction.
8822 if (ARM_AM::getSOImmVal(CVal) != -1)
8823 break;
8824 }
8825 return;
8826
8827 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008828 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008829 // This must be a constant between -255 and -1, for negated ADD
8830 // immediates. This can be used in GCC with an "n" modifier that
8831 // prints the negated value, for use with SUB instructions. It is
8832 // not useful otherwise but is implemented for compatibility.
8833 if (CVal >= -255 && CVal <= -1)
8834 break;
8835 } else {
8836 // This must be a constant between -4095 and 4095. It is not clear
8837 // what this constraint is intended for. Implemented for
8838 // compatibility with GCC.
8839 if (CVal >= -4095 && CVal <= 4095)
8840 break;
8841 }
8842 return;
8843
8844 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008845 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008846 // A 32-bit value where only one byte has a nonzero value. Exclude
8847 // zero to match GCC. This constraint is used by GCC internally for
8848 // constants that can be loaded with a move/shift combination.
8849 // It is not useful otherwise but is implemented for compatibility.
8850 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
8851 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008852 } else if (Subtarget->isThumb2()) {
8853 // A constant whose bitwise inverse can be used as an immediate
8854 // value in a data-processing instruction. This can be used in GCC
8855 // with a "B" modifier that prints the inverted value, for use with
8856 // BIC and MVN instructions. It is not useful otherwise but is
8857 // implemented for compatibility.
8858 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
8859 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008860 } else {
8861 // A constant whose bitwise inverse can be used as an immediate
8862 // value in a data-processing instruction. This can be used in GCC
8863 // with a "B" modifier that prints the inverted value, for use with
8864 // BIC and MVN instructions. It is not useful otherwise but is
8865 // implemented for compatibility.
8866 if (ARM_AM::getSOImmVal(~CVal) != -1)
8867 break;
8868 }
8869 return;
8870
8871 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008872 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008873 // This must be a constant between -7 and 7,
8874 // for 3-operand ADD/SUB immediate instructions.
8875 if (CVal >= -7 && CVal < 7)
8876 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008877 } else if (Subtarget->isThumb2()) {
8878 // A constant whose negation can be used as an immediate value in a
8879 // data-processing instruction. This can be used in GCC with an "n"
8880 // modifier that prints the negated value, for use with SUB
8881 // instructions. It is not useful otherwise but is implemented for
8882 // compatibility.
8883 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
8884 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008885 } else {
8886 // A constant whose negation can be used as an immediate value in a
8887 // data-processing instruction. This can be used in GCC with an "n"
8888 // modifier that prints the negated value, for use with SUB
8889 // instructions. It is not useful otherwise but is implemented for
8890 // compatibility.
8891 if (ARM_AM::getSOImmVal(-CVal) != -1)
8892 break;
8893 }
8894 return;
8895
8896 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008897 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008898 // This must be a multiple of 4 between 0 and 1020, for
8899 // ADD sp + immediate.
8900 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
8901 break;
8902 } else {
8903 // A power of two or a constant between 0 and 32. This is used in
8904 // GCC for the shift amount on shifted register operands, but it is
8905 // useful in general for any shift amounts.
8906 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
8907 break;
8908 }
8909 return;
8910
8911 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008912 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008913 // This must be a constant between 0 and 31, for shift amounts.
8914 if (CVal >= 0 && CVal <= 31)
8915 break;
8916 }
8917 return;
8918
8919 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008920 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008921 // This must be a multiple of 4 between -508 and 508, for
8922 // ADD/SUB sp = sp + immediate.
8923 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
8924 break;
8925 }
8926 return;
8927 }
8928 Result = DAG.getTargetConstant(CVal, Op.getValueType());
8929 break;
8930 }
8931
8932 if (Result.getNode()) {
8933 Ops.push_back(Result);
8934 return;
8935 }
Dale Johannesen1784d162010-06-25 21:55:36 +00008936 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008937}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00008938
8939bool
8940ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
8941 // The ARM target isn't yet aware of offsets.
8942 return false;
8943}
Evan Cheng39382422009-10-28 01:44:26 +00008944
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008945bool ARM::isBitFieldInvertedMask(unsigned v) {
8946 if (v == 0xffffffff)
8947 return 0;
8948 // there can be 1's on either or both "outsides", all the "inside"
8949 // bits must be 0's
8950 unsigned int lsb = 0, msb = 31;
8951 while (v & (1 << msb)) --msb;
8952 while (v & (1 << lsb)) ++lsb;
8953 for (unsigned int i = lsb; i <= msb; ++i) {
8954 if (v & (1 << i))
8955 return 0;
8956 }
8957 return 1;
8958}
8959
Evan Cheng39382422009-10-28 01:44:26 +00008960/// isFPImmLegal - Returns true if the target can instruction select the
8961/// specified FP immediate natively. If false, the legalizer will
8962/// materialize the FP immediate as a load from a constant pool.
8963bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
8964 if (!Subtarget->hasVFP3())
8965 return false;
8966 if (VT == MVT::f32)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00008967 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00008968 if (VT == MVT::f64)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00008969 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00008970 return false;
8971}
Bob Wilson65ffec42010-09-21 17:56:22 +00008972
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008973/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00008974/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
8975/// specified in the intrinsic calls.
8976bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
8977 const CallInst &I,
8978 unsigned Intrinsic) const {
8979 switch (Intrinsic) {
8980 case Intrinsic::arm_neon_vld1:
8981 case Intrinsic::arm_neon_vld2:
8982 case Intrinsic::arm_neon_vld3:
8983 case Intrinsic::arm_neon_vld4:
8984 case Intrinsic::arm_neon_vld2lane:
8985 case Intrinsic::arm_neon_vld3lane:
8986 case Intrinsic::arm_neon_vld4lane: {
8987 Info.opc = ISD::INTRINSIC_W_CHAIN;
8988 // Conservatively set memVT to the entire set of vectors loaded.
8989 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
8990 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
8991 Info.ptrVal = I.getArgOperand(0);
8992 Info.offset = 0;
8993 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
8994 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
8995 Info.vol = false; // volatile loads with NEON intrinsics not supported
8996 Info.readMem = true;
8997 Info.writeMem = false;
8998 return true;
8999 }
9000 case Intrinsic::arm_neon_vst1:
9001 case Intrinsic::arm_neon_vst2:
9002 case Intrinsic::arm_neon_vst3:
9003 case Intrinsic::arm_neon_vst4:
9004 case Intrinsic::arm_neon_vst2lane:
9005 case Intrinsic::arm_neon_vst3lane:
9006 case Intrinsic::arm_neon_vst4lane: {
9007 Info.opc = ISD::INTRINSIC_VOID;
9008 // Conservatively set memVT to the entire set of vectors stored.
9009 unsigned NumElts = 0;
9010 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009011 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +00009012 if (!ArgTy->isVectorTy())
9013 break;
9014 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
9015 }
9016 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
9017 Info.ptrVal = I.getArgOperand(0);
9018 Info.offset = 0;
9019 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
9020 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
9021 Info.vol = false; // volatile stores with NEON intrinsics not supported
9022 Info.readMem = false;
9023 Info.writeMem = true;
9024 return true;
9025 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009026 case Intrinsic::arm_strexd: {
9027 Info.opc = ISD::INTRINSIC_W_CHAIN;
9028 Info.memVT = MVT::i64;
9029 Info.ptrVal = I.getArgOperand(2);
9030 Info.offset = 0;
9031 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009032 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009033 Info.readMem = false;
9034 Info.writeMem = true;
9035 return true;
9036 }
9037 case Intrinsic::arm_ldrexd: {
9038 Info.opc = ISD::INTRINSIC_W_CHAIN;
9039 Info.memVT = MVT::i64;
9040 Info.ptrVal = I.getArgOperand(0);
9041 Info.offset = 0;
9042 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009043 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009044 Info.readMem = true;
9045 Info.writeMem = false;
9046 return true;
9047 }
Bob Wilson65ffec42010-09-21 17:56:22 +00009048 default:
9049 break;
9050 }
9051
9052 return false;
9053}