blob: 8c4c06fa8f0718f808d4d571cca4e9c533e7dc2f [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);
John McCall5f8fd542011-05-29 19:50:32 +0000689 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000690 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000691
Owen Anderson825b72b2009-08-11 20:47:22 +0000692 setOperationAction(ISD::SETCC, MVT::i32, Expand);
693 setOperationAction(ISD::SETCC, MVT::f32, Expand);
694 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000695 setOperationAction(ISD::SELECT, MVT::i32, Custom);
696 setOperationAction(ISD::SELECT, MVT::f32, Custom);
697 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000698 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
699 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
700 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000701
Owen Anderson825b72b2009-08-11 20:47:22 +0000702 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
703 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
704 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
705 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
706 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000707
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000708 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000709 setOperationAction(ISD::FSIN, MVT::f64, Expand);
710 setOperationAction(ISD::FSIN, MVT::f32, Expand);
711 setOperationAction(ISD::FCOS, MVT::f32, Expand);
712 setOperationAction(ISD::FCOS, MVT::f64, Expand);
713 setOperationAction(ISD::FREM, MVT::f64, Expand);
714 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000715 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000716 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
717 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000718 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000719 setOperationAction(ISD::FPOW, MVT::f64, Expand);
720 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000721
Cameron Zwarich33390842011-07-08 21:39:21 +0000722 setOperationAction(ISD::FMA, MVT::f64, Expand);
723 setOperationAction(ISD::FMA, MVT::f32, Expand);
724
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000725 // Various VFP goodness
726 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000727 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
728 if (Subtarget->hasVFP2()) {
729 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
730 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
731 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
732 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
733 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000734 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000735 if (!Subtarget->hasFP16()) {
736 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
737 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000738 }
Evan Cheng110cf482008-04-01 01:50:16 +0000739 }
Evan Chenga8e29892007-01-19 07:51:42 +0000740
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000741 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000742 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000743 setTargetDAGCombine(ISD::ADD);
744 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000745 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000746
Owen Anderson080c0922010-11-05 19:27:46 +0000747 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON())
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000748 setTargetDAGCombine(ISD::OR);
Owen Anderson080c0922010-11-05 19:27:46 +0000749 if (Subtarget->hasNEON())
750 setTargetDAGCombine(ISD::AND);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000751
Evan Chenga8e29892007-01-19 07:51:42 +0000752 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000753
Evan Chengf7d87ee2010-05-21 00:43:17 +0000754 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
755 setSchedulingPreference(Sched::RegPressure);
756 else
757 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000758
Evan Cheng05219282011-01-06 06:52:41 +0000759 //// temporary - rewrite interface to use type
760 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Lang Hames75757f92011-10-26 20:56:52 +0000761 maxStoresPerMemset = 16;
762 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengf6799392010-06-26 01:52:05 +0000763
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000764 // On ARM arguments smaller than 4 bytes are extended, so all arguments
765 // are at least 4 bytes aligned.
766 setMinStackArgumentAlignment(4);
767
Evan Chengfff606d2010-09-24 19:07:23 +0000768 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000769
770 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000771}
772
Andrew Trick32cec0a2011-01-19 02:35:27 +0000773// FIXME: It might make sense to define the representative register class as the
774// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
775// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
776// SPR's representative would be DPR_VFP2. This should work well if register
777// pressure tracking were modified such that a register use would increment the
778// pressure of the register class's representative and all of it's super
779// classes' representatives transitively. We have not implemented this because
780// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000781// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000782// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000783std::pair<const TargetRegisterClass*, uint8_t>
784ARMTargetLowering::findRepresentativeClass(EVT VT) const{
785 const TargetRegisterClass *RRC = 0;
786 uint8_t Cost = 1;
787 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000788 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000789 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000790 // Use DPR as representative register class for all floating point
791 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
792 // the cost is 1 for both f32 and f64.
793 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000794 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000795 RRC = ARM::DPRRegisterClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000796 // When NEON is used for SP, only half of the register file is available
797 // because operations that define both SP and DP results will be constrained
798 // to the VFP2 class (D0-D15). We currently model this constraint prior to
799 // coalescing by double-counting the SP regs. See the FIXME above.
800 if (Subtarget->useNEONForSinglePrecisionFP())
801 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000802 break;
803 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
804 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000805 RRC = ARM::DPRRegisterClass;
806 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000807 break;
808 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000809 RRC = ARM::DPRRegisterClass;
810 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000811 break;
812 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000813 RRC = ARM::DPRRegisterClass;
814 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000815 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000816 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000817 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000818}
819
Evan Chenga8e29892007-01-19 07:51:42 +0000820const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
821 switch (Opcode) {
822 default: return 0;
823 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000824 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000825 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000826 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
827 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000828 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000829 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
830 case ARMISD::tCALL: return "ARMISD::tCALL";
831 case ARMISD::BRCOND: return "ARMISD::BRCOND";
832 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000833 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000834 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
835 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
836 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000837 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000838 case ARMISD::CMPFP: return "ARMISD::CMPFP";
839 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000840 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000841 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
842 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000843
Jim Grosbach3482c802010-01-18 19:58:49 +0000844 case ARMISD::RBIT: return "ARMISD::RBIT";
845
Bob Wilson76a312b2010-03-19 22:51:32 +0000846 case ARMISD::FTOSI: return "ARMISD::FTOSI";
847 case ARMISD::FTOUI: return "ARMISD::FTOUI";
848 case ARMISD::SITOF: return "ARMISD::SITOF";
849 case ARMISD::UITOF: return "ARMISD::UITOF";
850
Evan Chenga8e29892007-01-19 07:51:42 +0000851 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
852 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
853 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000854
Evan Cheng342e3162011-08-30 01:34:54 +0000855 case ARMISD::ADDC: return "ARMISD::ADDC";
856 case ARMISD::ADDE: return "ARMISD::ADDE";
857 case ARMISD::SUBC: return "ARMISD::SUBC";
858 case ARMISD::SUBE: return "ARMISD::SUBE";
859
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000860 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
861 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000862
Evan Chengc5942082009-10-28 06:55:03 +0000863 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
864 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
865
Dale Johannesen51e28e62010-06-03 21:09:53 +0000866 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000867
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000868 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000869
Evan Cheng86198642009-08-07 00:34:42 +0000870 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
871
Jim Grosbach3728e962009-12-10 00:11:09 +0000872 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000873 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000874
Evan Chengdfed19f2010-11-03 06:34:55 +0000875 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
876
Bob Wilson5bafff32009-06-22 23:27:02 +0000877 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000878 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000879 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000880 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
881 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000882 case ARMISD::VCGEU: return "ARMISD::VCGEU";
883 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000884 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
885 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000886 case ARMISD::VCGTU: return "ARMISD::VCGTU";
887 case ARMISD::VTST: return "ARMISD::VTST";
888
889 case ARMISD::VSHL: return "ARMISD::VSHL";
890 case ARMISD::VSHRs: return "ARMISD::VSHRs";
891 case ARMISD::VSHRu: return "ARMISD::VSHRu";
892 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
893 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
894 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
895 case ARMISD::VSHRN: return "ARMISD::VSHRN";
896 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
897 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
898 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
899 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
900 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
901 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
902 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
903 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
904 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
905 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
906 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
907 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
908 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
909 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000910 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000911 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Chengeaa192a2011-11-15 02:12:34 +0000912 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000913 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000914 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000915 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000916 case ARMISD::VREV64: return "ARMISD::VREV64";
917 case ARMISD::VREV32: return "ARMISD::VREV32";
918 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000919 case ARMISD::VZIP: return "ARMISD::VZIP";
920 case ARMISD::VUZP: return "ARMISD::VUZP";
921 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000922 case ARMISD::VTBL1: return "ARMISD::VTBL1";
923 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000924 case ARMISD::VMULLs: return "ARMISD::VMULLs";
925 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000926 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000927 case ARMISD::FMAX: return "ARMISD::FMAX";
928 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000929 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000930 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
931 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000932 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000933 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
934 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
935 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +0000936 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
937 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
938 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
939 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
940 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
941 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
942 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
943 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
944 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
945 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
946 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
947 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
948 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
949 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
950 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
951 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
952 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +0000953 }
954}
955
Duncan Sands28b77e92011-09-06 19:07:46 +0000956EVT ARMTargetLowering::getSetCCResultType(EVT VT) const {
957 if (!VT.isVector()) return getPointerTy();
958 return VT.changeVectorElementTypeToInteger();
959}
960
Evan Cheng06b666c2010-05-15 02:18:07 +0000961/// getRegClassFor - Return the register class that should be used for the
962/// specified value type.
963TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
964 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
965 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
966 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000967 if (Subtarget->hasNEON()) {
968 if (VT == MVT::v4i64)
969 return ARM::QQPRRegisterClass;
970 else if (VT == MVT::v8i64)
971 return ARM::QQQQPRRegisterClass;
972 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000973 return TargetLowering::getRegClassFor(VT);
974}
975
Eric Christopherab695882010-07-21 22:26:11 +0000976// Create a fast isel object.
977FastISel *
978ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
979 return ARM::createFastISel(funcInfo);
980}
981
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000982/// getMaximalGlobalOffset - Returns the maximal possible offset which can
983/// be used for loads / stores from the global.
984unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
985 return (Subtarget->isThumb1Only() ? 127 : 4095);
986}
987
Evan Cheng1cc39842010-05-20 23:26:43 +0000988Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000989 unsigned NumVals = N->getNumValues();
990 if (!NumVals)
991 return Sched::RegPressure;
992
993 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000994 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000995 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +0000996 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +0000997 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman692c1d82011-10-24 17:55:11 +0000998 return Sched::ILP;
Evan Cheng1cc39842010-05-20 23:26:43 +0000999 }
Evan Chengc10f5432010-05-28 23:25:23 +00001000
1001 if (!N->isMachineOpcode())
1002 return Sched::RegPressure;
1003
1004 // Load are scheduled for latency even if there instruction itinerary
1005 // is not available.
1006 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +00001007 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +00001008
Evan Chenge837dea2011-06-28 19:10:37 +00001009 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +00001010 return Sched::RegPressure;
1011 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +00001012 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman692c1d82011-10-24 17:55:11 +00001013 return Sched::ILP;
Evan Chengc10f5432010-05-28 23:25:23 +00001014
Evan Cheng1cc39842010-05-20 23:26:43 +00001015 return Sched::RegPressure;
1016}
1017
Evan Chenga8e29892007-01-19 07:51:42 +00001018//===----------------------------------------------------------------------===//
1019// Lowering Code
1020//===----------------------------------------------------------------------===//
1021
Evan Chenga8e29892007-01-19 07:51:42 +00001022/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1023static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1024 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001025 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +00001026 case ISD::SETNE: return ARMCC::NE;
1027 case ISD::SETEQ: return ARMCC::EQ;
1028 case ISD::SETGT: return ARMCC::GT;
1029 case ISD::SETGE: return ARMCC::GE;
1030 case ISD::SETLT: return ARMCC::LT;
1031 case ISD::SETLE: return ARMCC::LE;
1032 case ISD::SETUGT: return ARMCC::HI;
1033 case ISD::SETUGE: return ARMCC::HS;
1034 case ISD::SETULT: return ARMCC::LO;
1035 case ISD::SETULE: return ARMCC::LS;
1036 }
1037}
1038
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001039/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1040static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001041 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001042 CondCode2 = ARMCC::AL;
1043 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001044 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001045 case ISD::SETEQ:
1046 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1047 case ISD::SETGT:
1048 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1049 case ISD::SETGE:
1050 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1051 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001052 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001053 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1054 case ISD::SETO: CondCode = ARMCC::VC; break;
1055 case ISD::SETUO: CondCode = ARMCC::VS; break;
1056 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1057 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1058 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1059 case ISD::SETLT:
1060 case ISD::SETULT: CondCode = ARMCC::LT; break;
1061 case ISD::SETLE:
1062 case ISD::SETULE: CondCode = ARMCC::LE; break;
1063 case ISD::SETNE:
1064 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1065 }
Evan Chenga8e29892007-01-19 07:51:42 +00001066}
1067
Bob Wilson1f595bb2009-04-17 19:07:39 +00001068//===----------------------------------------------------------------------===//
1069// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001070//===----------------------------------------------------------------------===//
1071
1072#include "ARMGenCallingConv.inc"
1073
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001074/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1075/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001076CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001077 bool Return,
1078 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001079 switch (CC) {
1080 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001081 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001082 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001083 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001084 if (!Subtarget->isAAPCS_ABI())
1085 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1086 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1087 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1088 }
1089 // Fallthrough
1090 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001091 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001092 if (!Subtarget->isAAPCS_ABI())
1093 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1094 else if (Subtarget->hasVFP2() &&
1095 FloatABIType == FloatABI::Hard && !isVarArg)
1096 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1097 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1098 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001099 case CallingConv::ARM_AAPCS_VFP:
Evan Cheng76f920d2010-10-22 18:23:05 +00001100 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001101 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001102 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001103 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001104 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001105 }
1106}
1107
Dan Gohman98ca4f22009-08-05 01:29:28 +00001108/// LowerCallResult - Lower the result values of a call into the
1109/// appropriate copies out of appropriate physical registers.
1110SDValue
1111ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001112 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001113 const SmallVectorImpl<ISD::InputArg> &Ins,
1114 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001115 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001116
Bob Wilson1f595bb2009-04-17 19:07:39 +00001117 // Assign locations to each value returned by this call.
1118 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001119 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1120 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001121 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001122 CCAssignFnForNode(CallConv, /* Return*/ true,
1123 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001124
1125 // Copy all of the result registers out of their specified physreg.
1126 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1127 CCValAssign VA = RVLocs[i];
1128
Bob Wilson80915242009-04-25 00:33:20 +00001129 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001130 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001131 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001132 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001133 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001134 Chain = Lo.getValue(1);
1135 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001136 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001137 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001138 InFlag);
1139 Chain = Hi.getValue(1);
1140 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001141 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001142
Owen Anderson825b72b2009-08-11 20:47:22 +00001143 if (VA.getLocVT() == MVT::v2f64) {
1144 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1145 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1146 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001147
1148 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001149 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001150 Chain = Lo.getValue(1);
1151 InFlag = Lo.getValue(2);
1152 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001153 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001154 Chain = Hi.getValue(1);
1155 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001156 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001157 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1158 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001159 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001160 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001161 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1162 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001163 Chain = Val.getValue(1);
1164 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001165 }
Bob Wilson80915242009-04-25 00:33:20 +00001166
1167 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001168 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001169 case CCValAssign::Full: break;
1170 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001171 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001172 break;
1173 }
1174
Dan Gohman98ca4f22009-08-05 01:29:28 +00001175 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001176 }
1177
Dan Gohman98ca4f22009-08-05 01:29:28 +00001178 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001179}
1180
Bob Wilsondee46d72009-04-17 20:35:10 +00001181/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001182SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001183ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1184 SDValue StackPtr, SDValue Arg,
1185 DebugLoc dl, SelectionDAG &DAG,
1186 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001187 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001188 unsigned LocMemOffset = VA.getLocMemOffset();
1189 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1190 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001191 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001192 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001193 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001194}
1195
Dan Gohman98ca4f22009-08-05 01:29:28 +00001196void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001197 SDValue Chain, SDValue &Arg,
1198 RegsToPassVector &RegsToPass,
1199 CCValAssign &VA, CCValAssign &NextVA,
1200 SDValue &StackPtr,
1201 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001202 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001203
Jim Grosbache5165492009-11-09 00:11:35 +00001204 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001205 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001206 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1207
1208 if (NextVA.isRegLoc())
1209 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1210 else {
1211 assert(NextVA.isMemLoc());
1212 if (StackPtr.getNode() == 0)
1213 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1214
Dan Gohman98ca4f22009-08-05 01:29:28 +00001215 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1216 dl, DAG, NextVA,
1217 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001218 }
1219}
1220
Dan Gohman98ca4f22009-08-05 01:29:28 +00001221/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001222/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1223/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001224SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001225ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001226 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001227 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001228 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001229 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001230 const SmallVectorImpl<ISD::InputArg> &Ins,
1231 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001232 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001233 MachineFunction &MF = DAG.getMachineFunction();
1234 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1235 bool IsSibCall = false;
Bob Wilson6d2f9ce2011-10-07 17:17:49 +00001236 // Disable tail calls if they're not supported.
1237 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Bob Wilson703af3a2010-08-13 22:43:33 +00001238 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001239 if (isTailCall) {
1240 // Check if it's really possible to do a tail call.
1241 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1242 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001243 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001244 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1245 // detected sibcalls.
1246 if (isTailCall) {
1247 ++NumTailCalls;
1248 IsSibCall = true;
1249 }
1250 }
Evan Chenga8e29892007-01-19 07:51:42 +00001251
Bob Wilson1f595bb2009-04-17 19:07:39 +00001252 // Analyze operands of the call, assigning locations to each operand.
1253 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001254 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1255 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001256 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001257 CCAssignFnForNode(CallConv, /* Return*/ false,
1258 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001259
Bob Wilson1f595bb2009-04-17 19:07:39 +00001260 // Get a count of how many bytes are to be pushed on the stack.
1261 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001262
Dale Johannesen51e28e62010-06-03 21:09:53 +00001263 // For tail calls, memory operands are available in our caller's stack.
1264 if (IsSibCall)
1265 NumBytes = 0;
1266
Evan Chenga8e29892007-01-19 07:51:42 +00001267 // Adjust the stack pointer for the new arguments...
1268 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001269 if (!IsSibCall)
1270 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001271
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001272 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001273
Bob Wilson5bafff32009-06-22 23:27:02 +00001274 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001275 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001276
Bob Wilson1f595bb2009-04-17 19:07:39 +00001277 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001278 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001279 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1280 i != e;
1281 ++i, ++realArgIdx) {
1282 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001283 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001284 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001285 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001286
Bob Wilson1f595bb2009-04-17 19:07:39 +00001287 // Promote the value if needed.
1288 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001289 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001290 case CCValAssign::Full: break;
1291 case CCValAssign::SExt:
1292 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1293 break;
1294 case CCValAssign::ZExt:
1295 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1296 break;
1297 case CCValAssign::AExt:
1298 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1299 break;
1300 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001301 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001302 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001303 }
1304
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001305 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001306 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001307 if (VA.getLocVT() == MVT::v2f64) {
1308 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1309 DAG.getConstant(0, MVT::i32));
1310 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1311 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001312
Dan Gohman98ca4f22009-08-05 01:29:28 +00001313 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001314 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1315
1316 VA = ArgLocs[++i]; // skip ahead to next loc
1317 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001318 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001319 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1320 } else {
1321 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001322
Dan Gohman98ca4f22009-08-05 01:29:28 +00001323 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1324 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001325 }
1326 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001327 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001328 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001329 }
1330 } else if (VA.isRegLoc()) {
1331 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001332 } else if (isByVal) {
1333 assert(VA.isMemLoc());
1334 unsigned offset = 0;
1335
1336 // True if this byval aggregate will be split between registers
1337 // and memory.
1338 if (CCInfo.isFirstByValRegValid()) {
1339 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1340 unsigned int i, j;
1341 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1342 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1343 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1344 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1345 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001346 false, false, false, 0);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001347 MemOpChains.push_back(Load.getValue(1));
1348 RegsToPass.push_back(std::make_pair(j, Load));
1349 }
1350 offset = ARM::R4 - CCInfo.getFirstByValReg();
1351 CCInfo.clearFirstByValReg();
1352 }
1353
1354 unsigned LocMemOffset = VA.getLocMemOffset();
1355 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1356 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1357 StkPtrOff);
1358 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1359 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1360 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1361 MVT::i32);
1362 MemOpChains.push_back(DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode,
1363 Flags.getByValAlign(),
1364 /*isVolatile=*/false,
Dan Gohman65fd6562011-11-03 21:49:52 +00001365 /*AlwaysInline=*/false,
Stuart Hastingsc7315872011-04-20 16:47:52 +00001366 MachinePointerInfo(0),
1367 MachinePointerInfo(0)));
1368
1369 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001370 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001371
Dan Gohman98ca4f22009-08-05 01:29:28 +00001372 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1373 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001374 }
Evan Chenga8e29892007-01-19 07:51:42 +00001375 }
1376
1377 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001378 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001379 &MemOpChains[0], MemOpChains.size());
1380
1381 // Build a sequence of copy-to-reg nodes chained together with token chain
1382 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001383 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001384 // Tail call byval lowering might overwrite argument registers so in case of
1385 // tail call optimization the copies to registers are lowered later.
1386 if (!isTailCall)
1387 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1388 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1389 RegsToPass[i].second, InFlag);
1390 InFlag = Chain.getValue(1);
1391 }
Evan Chenga8e29892007-01-19 07:51:42 +00001392
Dale Johannesen51e28e62010-06-03 21:09:53 +00001393 // For tail calls lower the arguments to the 'real' stack slot.
1394 if (isTailCall) {
1395 // Force all the incoming stack arguments to be loaded from the stack
1396 // before any new outgoing arguments are stored to the stack, because the
1397 // outgoing stack slots may alias the incoming argument stack slots, and
1398 // the alias isn't otherwise explicit. This is slightly more conservative
1399 // than necessary, because it means that each store effectively depends
1400 // on every argument instead of just those arguments it would clobber.
1401
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001402 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001403 InFlag = SDValue();
1404 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1405 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1406 RegsToPass[i].second, InFlag);
1407 InFlag = Chain.getValue(1);
1408 }
1409 InFlag =SDValue();
1410 }
1411
Bill Wendling056292f2008-09-16 21:48:12 +00001412 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1413 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1414 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001415 bool isDirect = false;
1416 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001417 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001418 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001419
1420 if (EnableARMLongCalls) {
1421 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1422 && "long-calls with non-static relocation model!");
1423 // Handle a global address or an external symbol. If it's not one of
1424 // those, the target's already in a register, so we don't need to do
1425 // anything extra.
1426 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001427 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001428 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001429 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001430 ARMConstantPoolValue *CPV =
1431 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1432
Jim Grosbache7b52522010-04-14 22:28:31 +00001433 // Get the address of the callee into a register
1434 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1435 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1436 Callee = DAG.getLoad(getPointerTy(), dl,
1437 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001438 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001439 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001440 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1441 const char *Sym = S->getSymbol();
1442
1443 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001444 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001445 ARMConstantPoolValue *CPV =
1446 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1447 ARMPCLabelIndex, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001448 // Get the address of the callee into a register
1449 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1450 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1451 Callee = DAG.getLoad(getPointerTy(), dl,
1452 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001453 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001454 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001455 }
1456 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001457 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001458 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001459 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001460 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001461 getTargetMachine().getRelocationModel() != Reloc::Static;
1462 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001463 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001464 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001465 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001466 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001467 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001468 ARMConstantPoolValue *CPV =
1469 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001470 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001471 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001472 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001473 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001474 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001475 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001476 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001477 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001478 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001479 } else {
1480 // On ELF targets for PIC code, direct calls should go through the PLT
1481 unsigned OpFlags = 0;
1482 if (Subtarget->isTargetELF() &&
1483 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1484 OpFlags = ARMII::MO_PLT;
1485 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1486 }
Bill Wendling056292f2008-09-16 21:48:12 +00001487 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001488 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001489 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001490 getTargetMachine().getRelocationModel() != Reloc::Static;
1491 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001492 // tBX takes a register source operand.
1493 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001494 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001495 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001496 ARMConstantPoolValue *CPV =
1497 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1498 ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001499 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001500 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001501 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001502 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001503 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001504 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001505 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001506 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001507 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001508 } else {
1509 unsigned OpFlags = 0;
1510 // On ELF targets for PIC code, direct calls should go through the PLT
1511 if (Subtarget->isTargetELF() &&
1512 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1513 OpFlags = ARMII::MO_PLT;
1514 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1515 }
Evan Chenga8e29892007-01-19 07:51:42 +00001516 }
1517
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001518 // FIXME: handle tail calls differently.
1519 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001520 if (Subtarget->isThumb()) {
1521 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001522 CallOpc = ARMISD::CALL_NOLINK;
1523 else
1524 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1525 } else {
1526 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001527 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1528 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001529 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001530
Dan Gohman475871a2008-07-27 21:46:04 +00001531 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001532 Ops.push_back(Chain);
1533 Ops.push_back(Callee);
1534
1535 // Add argument registers to the end of the list so that they are known live
1536 // into the call.
1537 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1538 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1539 RegsToPass[i].second.getValueType()));
1540
Gabor Greifba36cb52008-08-28 21:40:38 +00001541 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001542 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001543
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001544 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001545 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001546 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001547
Duncan Sands4bdcb612008-07-02 17:40:58 +00001548 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001549 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001550 InFlag = Chain.getValue(1);
1551
Chris Lattnere563bbc2008-10-11 22:08:30 +00001552 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1553 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001554 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001555 InFlag = Chain.getValue(1);
1556
Bob Wilson1f595bb2009-04-17 19:07:39 +00001557 // Handle result values, copying them out of physregs into vregs that we
1558 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001559 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1560 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001561}
1562
Stuart Hastingsf222e592011-02-28 17:17:53 +00001563/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001564/// on the stack. Remember the next parameter register to allocate,
1565/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001566/// this.
1567void
Stuart Hastingsc7315872011-04-20 16:47:52 +00001568llvm::ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
1569 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1570 assert((State->getCallOrPrologue() == Prologue ||
1571 State->getCallOrPrologue() == Call) &&
1572 "unhandled ParmContext");
1573 if ((!State->isFirstByValRegValid()) &&
1574 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1575 State->setFirstByValReg(reg);
1576 // At a call site, a byval parameter that is split between
1577 // registers and memory needs its size truncated here. In a
1578 // function prologue, such byval parameters are reassembled in
1579 // memory, and are not truncated.
1580 if (State->getCallOrPrologue() == Call) {
1581 unsigned excess = 4 * (ARM::R4 - reg);
1582 assert(size >= excess && "expected larger existing stack allocation");
1583 size -= excess;
1584 }
1585 }
1586 // Confiscate any remaining parameter registers to preclude their
1587 // assignment to subsequent parameters.
1588 while (State->AllocateReg(GPRArgRegs, 4))
1589 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001590}
1591
Dale Johannesen51e28e62010-06-03 21:09:53 +00001592/// MatchingStackOffset - Return true if the given stack call argument is
1593/// already available in the same position (relatively) of the caller's
1594/// incoming argument stack.
1595static
1596bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1597 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1598 const ARMInstrInfo *TII) {
1599 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1600 int FI = INT_MAX;
1601 if (Arg.getOpcode() == ISD::CopyFromReg) {
1602 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001603 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001604 return false;
1605 MachineInstr *Def = MRI->getVRegDef(VR);
1606 if (!Def)
1607 return false;
1608 if (!Flags.isByVal()) {
1609 if (!TII->isLoadFromStackSlot(Def, FI))
1610 return false;
1611 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001612 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001613 }
1614 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1615 if (Flags.isByVal())
1616 // ByVal argument is passed in as a pointer but it's now being
1617 // dereferenced. e.g.
1618 // define @foo(%struct.X* %A) {
1619 // tail call @bar(%struct.X* byval %A)
1620 // }
1621 return false;
1622 SDValue Ptr = Ld->getBasePtr();
1623 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1624 if (!FINode)
1625 return false;
1626 FI = FINode->getIndex();
1627 } else
1628 return false;
1629
1630 assert(FI != INT_MAX);
1631 if (!MFI->isFixedObjectIndex(FI))
1632 return false;
1633 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1634}
1635
1636/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1637/// for tail call optimization. Targets which want to do tail call
1638/// optimization should implement this function.
1639bool
1640ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1641 CallingConv::ID CalleeCC,
1642 bool isVarArg,
1643 bool isCalleeStructRet,
1644 bool isCallerStructRet,
1645 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001646 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001647 const SmallVectorImpl<ISD::InputArg> &Ins,
1648 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001649 const Function *CallerF = DAG.getMachineFunction().getFunction();
1650 CallingConv::ID CallerCC = CallerF->getCallingConv();
1651 bool CCMatch = CallerCC == CalleeCC;
1652
1653 // Look for obvious safe cases to perform tail call optimization that do not
1654 // require ABI changes. This is what gcc calls sibcall.
1655
Jim Grosbach7616b642010-06-16 23:45:49 +00001656 // Do not sibcall optimize vararg calls unless the call site is not passing
1657 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001658 if (isVarArg && !Outs.empty())
1659 return false;
1660
1661 // Also avoid sibcall optimization if either caller or callee uses struct
1662 // return semantics.
1663 if (isCalleeStructRet || isCallerStructRet)
1664 return false;
1665
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001666 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001667 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1668 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1669 // support in the assembler and linker to be used. This would need to be
1670 // fixed to fully support tail calls in Thumb1.
1671 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001672 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1673 // LR. This means if we need to reload LR, it takes an extra instructions,
1674 // which outweighs the value of the tail call; but here we don't know yet
1675 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001676 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001677 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001678
1679 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1680 // but we need to make sure there are enough registers; the only valid
1681 // registers are the 4 used for parameters. We don't currently do this
1682 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001683 if (Subtarget->isThumb1Only())
1684 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001685
Dale Johannesen51e28e62010-06-03 21:09:53 +00001686 // If the calling conventions do not match, then we'd better make sure the
1687 // results are returned in the same way as what the caller expects.
1688 if (!CCMatch) {
1689 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001690 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1691 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001692 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1693
1694 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001695 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1696 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001697 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1698
1699 if (RVLocs1.size() != RVLocs2.size())
1700 return false;
1701 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1702 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1703 return false;
1704 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1705 return false;
1706 if (RVLocs1[i].isRegLoc()) {
1707 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1708 return false;
1709 } else {
1710 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1711 return false;
1712 }
1713 }
1714 }
1715
1716 // If the callee takes no arguments then go on to check the results of the
1717 // call.
1718 if (!Outs.empty()) {
1719 // Check if stack adjustment is needed. For now, do not do this if any
1720 // argument is passed on the stack.
1721 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001722 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1723 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001724 CCInfo.AnalyzeCallOperands(Outs,
1725 CCAssignFnForNode(CalleeCC, false, isVarArg));
1726 if (CCInfo.getNextStackOffset()) {
1727 MachineFunction &MF = DAG.getMachineFunction();
1728
1729 // Check if the arguments are already laid out in the right way as
1730 // the caller's fixed stack objects.
1731 MachineFrameInfo *MFI = MF.getFrameInfo();
1732 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1733 const ARMInstrInfo *TII =
1734 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001735 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1736 i != e;
1737 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001738 CCValAssign &VA = ArgLocs[i];
1739 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001740 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001741 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001742 if (VA.getLocInfo() == CCValAssign::Indirect)
1743 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001744 if (VA.needsCustom()) {
1745 // f64 and vector types are split into multiple registers or
1746 // register/stack-slot combinations. The types will not match
1747 // the registers; give up on memory f64 refs until we figure
1748 // out what to do about this.
1749 if (!VA.isRegLoc())
1750 return false;
1751 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001752 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001753 if (RegVT == MVT::v2f64) {
1754 if (!ArgLocs[++i].isRegLoc())
1755 return false;
1756 if (!ArgLocs[++i].isRegLoc())
1757 return false;
1758 }
1759 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001760 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1761 MFI, MRI, TII))
1762 return false;
1763 }
1764 }
1765 }
1766 }
1767
1768 return true;
1769}
1770
Dan Gohman98ca4f22009-08-05 01:29:28 +00001771SDValue
1772ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001773 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001774 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001775 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001776 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001777
Bob Wilsondee46d72009-04-17 20:35:10 +00001778 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001779 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001780
Bob Wilsondee46d72009-04-17 20:35:10 +00001781 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001782 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1783 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001784
Dan Gohman98ca4f22009-08-05 01:29:28 +00001785 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001786 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1787 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001788
1789 // If this is the first return lowered for this function, add
1790 // the regs to the liveout set for the function.
1791 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1792 for (unsigned i = 0; i != RVLocs.size(); ++i)
1793 if (RVLocs[i].isRegLoc())
1794 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001795 }
1796
Bob Wilson1f595bb2009-04-17 19:07:39 +00001797 SDValue Flag;
1798
1799 // Copy the result values into the output registers.
1800 for (unsigned i = 0, realRVLocIdx = 0;
1801 i != RVLocs.size();
1802 ++i, ++realRVLocIdx) {
1803 CCValAssign &VA = RVLocs[i];
1804 assert(VA.isRegLoc() && "Can only return in registers!");
1805
Dan Gohmanc9403652010-07-07 15:54:55 +00001806 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001807
1808 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001809 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001810 case CCValAssign::Full: break;
1811 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001812 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001813 break;
1814 }
1815
Bob Wilson1f595bb2009-04-17 19:07:39 +00001816 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001817 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001818 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001819 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1820 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001821 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001822 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001823
1824 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1825 Flag = Chain.getValue(1);
1826 VA = RVLocs[++i]; // skip ahead to next loc
1827 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1828 HalfGPRs.getValue(1), Flag);
1829 Flag = Chain.getValue(1);
1830 VA = RVLocs[++i]; // skip ahead to next loc
1831
1832 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001833 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1834 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001835 }
1836 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1837 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001838 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001839 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001840 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001841 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001842 VA = RVLocs[++i]; // skip ahead to next loc
1843 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1844 Flag);
1845 } else
1846 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1847
Bob Wilsondee46d72009-04-17 20:35:10 +00001848 // Guarantee that all emitted copies are
1849 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001850 Flag = Chain.getValue(1);
1851 }
1852
1853 SDValue result;
1854 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001855 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001856 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001857 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001858
1859 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001860}
1861
Evan Cheng3d2125c2010-11-30 23:55:39 +00001862bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1863 if (N->getNumValues() != 1)
1864 return false;
1865 if (!N->hasNUsesOfValue(1, 0))
1866 return false;
1867
1868 unsigned NumCopies = 0;
1869 SDNode* Copies[2];
1870 SDNode *Use = *N->use_begin();
1871 if (Use->getOpcode() == ISD::CopyToReg) {
1872 Copies[NumCopies++] = Use;
1873 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1874 // f64 returned in a pair of GPRs.
1875 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1876 UI != UE; ++UI) {
1877 if (UI->getOpcode() != ISD::CopyToReg)
1878 return false;
1879 Copies[UI.getUse().getResNo()] = *UI;
1880 ++NumCopies;
1881 }
1882 } else if (Use->getOpcode() == ISD::BITCAST) {
1883 // f32 returned in a single GPR.
1884 if (!Use->hasNUsesOfValue(1, 0))
1885 return false;
1886 Use = *Use->use_begin();
1887 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1888 return false;
1889 Copies[NumCopies++] = Use;
1890 } else {
1891 return false;
1892 }
1893
1894 if (NumCopies != 1 && NumCopies != 2)
1895 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001896
1897 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001898 for (unsigned i = 0; i < NumCopies; ++i) {
1899 SDNode *Copy = Copies[i];
1900 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1901 UI != UE; ++UI) {
1902 if (UI->getOpcode() == ISD::CopyToReg) {
1903 SDNode *Use = *UI;
1904 if (Use == Copies[0] || Use == Copies[1])
1905 continue;
1906 return false;
1907 }
1908 if (UI->getOpcode() != ARMISD::RET_FLAG)
1909 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001910 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001911 }
1912 }
1913
Evan Cheng1bf891a2010-12-01 22:59:46 +00001914 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001915}
1916
Evan Cheng485fafc2011-03-21 01:19:09 +00001917bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1918 if (!EnableARMTailCalls)
1919 return false;
1920
1921 if (!CI->isTailCall())
1922 return false;
1923
1924 return !Subtarget->isThumb1Only();
1925}
1926
Bob Wilsonb62d2572009-11-03 00:02:05 +00001927// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1928// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1929// one of the above mentioned nodes. It has to be wrapped because otherwise
1930// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1931// be used to form addressing mode. These wrapped nodes will be selected
1932// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001933static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001934 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001935 // FIXME there is no actual debug info here
1936 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001937 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001938 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001939 if (CP->isMachineConstantPoolEntry())
1940 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1941 CP->getAlignment());
1942 else
1943 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1944 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001945 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001946}
1947
Jim Grosbache1102ca2010-07-19 17:20:38 +00001948unsigned ARMTargetLowering::getJumpTableEncoding() const {
1949 return MachineJumpTableInfo::EK_Inline;
1950}
1951
Dan Gohmand858e902010-04-17 15:26:15 +00001952SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1953 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001954 MachineFunction &MF = DAG.getMachineFunction();
1955 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1956 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001957 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001958 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001959 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001960 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1961 SDValue CPAddr;
1962 if (RelocM == Reloc::Static) {
1963 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1964 } else {
1965 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001966 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001967 ARMConstantPoolValue *CPV =
1968 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
1969 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson907eebd2009-11-02 20:59:23 +00001970 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1971 }
1972 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1973 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001974 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001975 false, false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001976 if (RelocM == Reloc::Static)
1977 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001978 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001979 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001980}
1981
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001982// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001983SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001984ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001985 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001986 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001987 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001988 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001989 MachineFunction &MF = DAG.getMachineFunction();
1990 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001991 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001992 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00001993 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
1994 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001995 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001996 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001997 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001998 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001999 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002000 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002001
Evan Chenge7e0d622009-11-06 22:24:13 +00002002 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002003 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002004
2005 // call __tls_get_addr.
2006 ArgListTy Args;
2007 ArgListEntry Entry;
2008 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002009 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002010 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002011 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00002012 std::pair<SDValue, SDValue> CallResult =
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002013 LowerCallTo(Chain, (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00002014 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002015 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002016 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002017 return CallResult.first;
2018}
2019
2020// Lower ISD::GlobalTLSAddress using the "initial exec" or
2021// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00002022SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002023ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002024 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002025 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002026 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002027 SDValue Offset;
2028 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002029 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002030 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002031 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002032
Chris Lattner4fb63d02009-07-15 04:12:33 +00002033 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002034 MachineFunction &MF = DAG.getMachineFunction();
2035 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002036 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002037 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002038 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2039 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002040 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2041 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2042 true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002043 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002044 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002045 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002046 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002047 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002048 Chain = Offset.getValue(1);
2049
Evan Chenge7e0d622009-11-06 22:24:13 +00002050 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002051 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002052
Evan Cheng9eda6892009-10-31 03:39:36 +00002053 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002054 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002055 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002056 } else {
2057 // local exec model
Bill Wendling5bb77992011-10-01 08:00:54 +00002058 ARMConstantPoolValue *CPV =
2059 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002060 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002061 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002062 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002063 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002064 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002065 }
2066
2067 // The address of the thread local variable is the add of the thread
2068 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002069 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002070}
2071
Dan Gohman475871a2008-07-27 21:46:04 +00002072SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002073ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002074 // TODO: implement the "local dynamic" model
2075 assert(Subtarget->isTargetELF() &&
2076 "TLS not implemented for non-ELF targets");
2077 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2078 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
2079 // otherwise use the "Local Exec" TLS Model
2080 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
2081 return LowerToTLSGeneralDynamicModel(GA, DAG);
2082 else
2083 return LowerToTLSExecModels(GA, DAG);
2084}
2085
Dan Gohman475871a2008-07-27 21:46:04 +00002086SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002087 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002088 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002089 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002090 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002091 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2092 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002093 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002094 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002095 ARMConstantPoolConstant::Create(GV,
2096 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002097 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002098 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002099 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002100 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002101 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002102 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002103 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002104 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002105 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002106 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002107 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002108 MachinePointerInfo::getGOT(),
2109 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002110 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002111 }
2112
2113 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloy015cca62011-10-26 08:53:19 +00002114 // pair. This is always cheaper.
2115 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002116 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002117 // FIXME: Once remat is capable of dealing with instructions with register
2118 // operands, expand this into two nodes.
2119 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2120 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002121 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002122 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2123 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2124 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2125 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002126 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002127 }
2128}
2129
Dan Gohman475871a2008-07-27 21:46:04 +00002130SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002131 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002132 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002133 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002134 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002135 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002136 MachineFunction &MF = DAG.getMachineFunction();
2137 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2138
Evan Cheng4abce0c2011-05-27 20:11:27 +00002139 // FIXME: Enable this for static codegen when tool issues are fixed.
Evan Chengf31151f2011-10-26 01:17:44 +00002140 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002141 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002142 // FIXME: Once remat is capable of dealing with instructions with register
2143 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002144 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002145 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2146 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2147
Evan Cheng53519f02011-01-21 18:55:51 +00002148 unsigned Wrapper = (RelocM == Reloc::PIC_)
2149 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2150 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002151 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002152 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2153 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002154 MachinePointerInfo::getGOT(),
2155 false, false, false, 0);
Evan Chengfc8475b2011-01-19 02:16:49 +00002156 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002157 }
2158
2159 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002160 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002161 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002162 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002163 } else {
2164 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002165 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2166 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002167 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue,
2168 PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002169 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002170 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002171 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002172
Evan Cheng9eda6892009-10-31 03:39:36 +00002173 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002174 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002175 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002176 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002177
2178 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002179 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002180 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002181 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002182
Evan Cheng63476a82009-09-03 07:04:02 +00002183 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002184 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002185 false, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002186
2187 return Result;
2188}
2189
Dan Gohman475871a2008-07-27 21:46:04 +00002190SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002191 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002192 assert(Subtarget->isTargetELF() &&
2193 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002194 MachineFunction &MF = DAG.getMachineFunction();
2195 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002196 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002197 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002198 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002199 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingfe31e672011-10-01 08:58:29 +00002200 ARMConstantPoolValue *CPV =
2201 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2202 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002203 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002204 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002205 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002206 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002207 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002208 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002209 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002210}
2211
Jim Grosbach0e0da732009-05-12 23:59:14 +00002212SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002213ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2214 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002215 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendlingce370cf2011-10-07 21:25:38 +00002216 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2217 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002218 Op.getOperand(1), Val);
2219}
2220
2221SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002222ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2223 DebugLoc dl = Op.getDebugLoc();
2224 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2225 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2226}
2227
2228SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002229ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002230 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002231 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002232 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002233 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002234 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002235 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002236 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002237 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2238 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002239 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002240 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002241 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002242 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002243 EVT PtrVT = getPointerTy();
2244 DebugLoc dl = Op.getDebugLoc();
2245 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2246 SDValue CPAddr;
2247 unsigned PCAdj = (RelocM != Reloc::PIC_)
2248 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002249 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002250 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2251 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002252 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002253 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002254 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002255 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002256 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002257 false, false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002258
2259 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002260 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002261 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2262 }
2263 return Result;
2264 }
Evan Cheng92e39162011-03-29 23:06:19 +00002265 case Intrinsic::arm_neon_vmulls:
2266 case Intrinsic::arm_neon_vmullu: {
2267 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2268 ? ARMISD::VMULLs : ARMISD::VMULLu;
2269 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2270 Op.getOperand(1), Op.getOperand(2));
2271 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002272 }
2273}
2274
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002275static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002276 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002277 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002278 if (!Subtarget->hasDataBarrier()) {
2279 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2280 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2281 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002282 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002283 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002284 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002285 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002286 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002287
2288 SDValue Op5 = Op.getOperand(5);
2289 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2290 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2291 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2292 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2293
2294 ARM_MB::MemBOpt DMBOpt;
2295 if (isDeviceBarrier)
2296 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2297 else
2298 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2299 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2300 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002301}
2302
Eli Friedman26689ac2011-08-03 21:06:02 +00002303
2304static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2305 const ARMSubtarget *Subtarget) {
2306 // FIXME: handle "fence singlethread" more efficiently.
2307 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002308 if (!Subtarget->hasDataBarrier()) {
2309 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2310 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2311 // here.
2312 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2313 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002314 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002315 DAG.getConstant(0, MVT::i32));
2316 }
2317
Eli Friedman26689ac2011-08-03 21:06:02 +00002318 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002319 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002320}
2321
Evan Chengdfed19f2010-11-03 06:34:55 +00002322static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2323 const ARMSubtarget *Subtarget) {
2324 // ARM pre v5TE and Thumb1 does not have preload instructions.
2325 if (!(Subtarget->isThumb2() ||
2326 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2327 // Just preserve the chain.
2328 return Op.getOperand(0);
2329
2330 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002331 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2332 if (!isRead &&
2333 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2334 // ARMv7 with MP extension has PLDW.
2335 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002336
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002337 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2338 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002339 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002340 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002341 isData = ~isData & 1;
2342 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002343
2344 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002345 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2346 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002347}
2348
Dan Gohman1e93df62010-04-17 14:41:14 +00002349static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2350 MachineFunction &MF = DAG.getMachineFunction();
2351 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2352
Evan Chenga8e29892007-01-19 07:51:42 +00002353 // vastart just stores the address of the VarArgsFrameIndex slot into the
2354 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002355 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002356 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002357 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002358 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002359 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2360 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002361}
2362
Dan Gohman475871a2008-07-27 21:46:04 +00002363SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002364ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2365 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002366 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002367 MachineFunction &MF = DAG.getMachineFunction();
2368 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2369
2370 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002371 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002372 RC = ARM::tGPRRegisterClass;
2373 else
2374 RC = ARM::GPRRegisterClass;
2375
2376 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002377 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002378 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002379
2380 SDValue ArgValue2;
2381 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002382 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002383 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002384
2385 // Create load node to retrieve arguments from the stack.
2386 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002387 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002388 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002389 false, false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002390 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002391 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002392 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002393 }
2394
Jim Grosbache5165492009-11-09 00:11:35 +00002395 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002396}
2397
Stuart Hastingsc7315872011-04-20 16:47:52 +00002398void
2399ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2400 unsigned &VARegSize, unsigned &VARegSaveSize)
2401 const {
2402 unsigned NumGPRs;
2403 if (CCInfo.isFirstByValRegValid())
2404 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2405 else {
2406 unsigned int firstUnalloced;
2407 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2408 sizeof(GPRArgRegs) /
2409 sizeof(GPRArgRegs[0]));
2410 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2411 }
2412
2413 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2414 VARegSize = NumGPRs * 4;
2415 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2416}
2417
2418// The remaining GPRs hold either the beginning of variable-argument
2419// data, or the beginning of an aggregate passed by value (usuall
2420// byval). Either way, we allocate stack slots adjacent to the data
2421// provided by our caller, and store the unallocated registers there.
2422// If this is a variadic function, the va_list pointer will begin with
2423// these values; otherwise, this reassembles a (byval) structure that
2424// was split between registers and memory.
2425void
2426ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2427 DebugLoc dl, SDValue &Chain,
2428 unsigned ArgOffset) const {
2429 MachineFunction &MF = DAG.getMachineFunction();
2430 MachineFrameInfo *MFI = MF.getFrameInfo();
2431 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2432 unsigned firstRegToSaveIndex;
2433 if (CCInfo.isFirstByValRegValid())
2434 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2435 else {
2436 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2437 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2438 }
2439
2440 unsigned VARegSize, VARegSaveSize;
2441 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2442 if (VARegSaveSize) {
2443 // If this function is vararg, store any remaining integer argument regs
2444 // to their spots on the stack so that they may be loaded by deferencing
2445 // the result of va_next.
2446 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002447 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2448 ArgOffset + VARegSaveSize
2449 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002450 false));
2451 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2452 getPointerTy());
2453
2454 SmallVector<SDValue, 4> MemOps;
2455 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
2456 TargetRegisterClass *RC;
2457 if (AFI->isThumb1OnlyFunction())
2458 RC = ARM::tGPRRegisterClass;
2459 else
2460 RC = ARM::GPRRegisterClass;
2461
2462 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2463 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2464 SDValue Store =
2465 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002466 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002467 false, false, 0);
2468 MemOps.push_back(Store);
2469 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2470 DAG.getConstant(4, getPointerTy()));
2471 }
2472 if (!MemOps.empty())
2473 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2474 &MemOps[0], MemOps.size());
2475 } else
2476 // This will point to the next argument passed via stack.
2477 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2478}
2479
Bob Wilson5bafff32009-06-22 23:27:02 +00002480SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002481ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002482 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002483 const SmallVectorImpl<ISD::InputArg>
2484 &Ins,
2485 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002486 SmallVectorImpl<SDValue> &InVals)
2487 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002488 MachineFunction &MF = DAG.getMachineFunction();
2489 MachineFrameInfo *MFI = MF.getFrameInfo();
2490
Bob Wilson1f595bb2009-04-17 19:07:39 +00002491 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2492
2493 // Assign locations to all of the incoming arguments.
2494 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002495 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2496 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002497 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002498 CCAssignFnForNode(CallConv, /* Return*/ false,
2499 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002500
2501 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002502 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002503
Stuart Hastingsf222e592011-02-28 17:17:53 +00002504 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002505 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2506 CCValAssign &VA = ArgLocs[i];
2507
Bob Wilsondee46d72009-04-17 20:35:10 +00002508 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002509 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002510 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002511
Bob Wilson1f595bb2009-04-17 19:07:39 +00002512 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002513 // f64 and vector types are split up into multiple registers or
2514 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002515 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002516 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002517 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002518 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002519 SDValue ArgValue2;
2520 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002521 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002522 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2523 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002524 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002525 false, false, false, 0);
Bob Wilson6a234f02010-04-13 22:03:22 +00002526 } else {
2527 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2528 Chain, DAG, dl);
2529 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002530 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2531 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002532 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002533 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002534 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2535 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002536 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002537
Bob Wilson5bafff32009-06-22 23:27:02 +00002538 } else {
2539 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002540
Owen Anderson825b72b2009-08-11 20:47:22 +00002541 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002542 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002543 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002544 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002545 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002546 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002547 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002548 RC = (AFI->isThumb1OnlyFunction() ?
2549 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002550 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002551 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002552
2553 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002554 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002555 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002556 }
2557
2558 // If this is an 8 or 16-bit value, it is really passed promoted
2559 // to 32 bits. Insert an assert[sz]ext to capture this, then
2560 // truncate to the right size.
2561 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002562 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002563 case CCValAssign::Full: break;
2564 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002565 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002566 break;
2567 case CCValAssign::SExt:
2568 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2569 DAG.getValueType(VA.getValVT()));
2570 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2571 break;
2572 case CCValAssign::ZExt:
2573 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2574 DAG.getValueType(VA.getValVT()));
2575 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2576 break;
2577 }
2578
Dan Gohman98ca4f22009-08-05 01:29:28 +00002579 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002580
2581 } else { // VA.isRegLoc()
2582
2583 // sanity check
2584 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002585 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002586
Stuart Hastingsf222e592011-02-28 17:17:53 +00002587 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002588
Stuart Hastingsf222e592011-02-28 17:17:53 +00002589 // Some Ins[] entries become multiple ArgLoc[] entries.
2590 // Process them only once.
2591 if (index != lastInsIndex)
2592 {
2593 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002594 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002595 // This can be changed with more analysis.
2596 // In case of tail call optimization mark all arguments mutable.
2597 // Since they could be overwritten by lowering of arguments in case of
2598 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002599 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002600 unsigned VARegSize, VARegSaveSize;
2601 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2602 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2603 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002604 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002605 int FI = MFI->CreateFixedObject(Bytes,
2606 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002607 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2608 } else {
2609 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2610 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002611
Stuart Hastingsf222e592011-02-28 17:17:53 +00002612 // Create load nodes to retrieve arguments from the stack.
2613 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2614 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2615 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002616 false, false, false, 0));
Stuart Hastingsf222e592011-02-28 17:17:53 +00002617 }
2618 lastInsIndex = index;
2619 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002620 }
2621 }
2622
2623 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002624 if (isVarArg)
2625 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002626
Dan Gohman98ca4f22009-08-05 01:29:28 +00002627 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002628}
2629
2630/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002631static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002632 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002633 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002634 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002635 // Maybe this has already been legalized into the constant pool?
2636 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002637 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002638 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002639 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002640 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002641 }
2642 }
2643 return false;
2644}
2645
Evan Chenga8e29892007-01-19 07:51:42 +00002646/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2647/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002648SDValue
2649ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002650 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002651 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002652 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002653 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002654 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002655 // Constant does not fit, try adjusting it by one?
2656 switch (CC) {
2657 default: break;
2658 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002659 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002660 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002661 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002662 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002663 }
2664 break;
2665 case ISD::SETULT:
2666 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002667 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002668 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002669 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002670 }
2671 break;
2672 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002673 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002674 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002675 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002676 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002677 }
2678 break;
2679 case ISD::SETULE:
2680 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002681 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002682 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002683 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002684 }
2685 break;
2686 }
2687 }
2688 }
2689
2690 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002691 ARMISD::NodeType CompareType;
2692 switch (CondCode) {
2693 default:
2694 CompareType = ARMISD::CMP;
2695 break;
2696 case ARMCC::EQ:
2697 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002698 // Uses only Z Flag
2699 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002700 break;
2701 }
Evan Cheng218977b2010-07-13 19:27:42 +00002702 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002703 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002704}
2705
2706/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002707SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002708ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002709 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002710 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002711 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002712 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002713 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002714 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2715 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002716}
2717
Bob Wilson79f56c92011-03-08 01:17:20 +00002718/// duplicateCmp - Glue values can have only one use, so this function
2719/// duplicates a comparison node.
2720SDValue
2721ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2722 unsigned Opc = Cmp.getOpcode();
2723 DebugLoc DL = Cmp.getDebugLoc();
2724 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2725 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2726
2727 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2728 Cmp = Cmp.getOperand(0);
2729 Opc = Cmp.getOpcode();
2730 if (Opc == ARMISD::CMPFP)
2731 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2732 else {
2733 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2734 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2735 }
2736 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2737}
2738
Bill Wendlingde2b1512010-08-11 08:43:16 +00002739SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2740 SDValue Cond = Op.getOperand(0);
2741 SDValue SelectTrue = Op.getOperand(1);
2742 SDValue SelectFalse = Op.getOperand(2);
2743 DebugLoc dl = Op.getDebugLoc();
2744
2745 // Convert:
2746 //
2747 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2748 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2749 //
2750 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2751 const ConstantSDNode *CMOVTrue =
2752 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2753 const ConstantSDNode *CMOVFalse =
2754 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2755
2756 if (CMOVTrue && CMOVFalse) {
2757 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2758 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2759
2760 SDValue True;
2761 SDValue False;
2762 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2763 True = SelectTrue;
2764 False = SelectFalse;
2765 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2766 True = SelectFalse;
2767 False = SelectTrue;
2768 }
2769
2770 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002771 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002772 SDValue ARMcc = Cond.getOperand(2);
2773 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002774 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002775 assert(True.getValueType() == VT);
2776 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002777 }
2778 }
2779 }
2780
2781 return DAG.getSelectCC(dl, Cond,
2782 DAG.getConstant(0, Cond.getValueType()),
2783 SelectTrue, SelectFalse, ISD::SETNE);
2784}
2785
Dan Gohmand858e902010-04-17 15:26:15 +00002786SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002787 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002788 SDValue LHS = Op.getOperand(0);
2789 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002790 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002791 SDValue TrueVal = Op.getOperand(2);
2792 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002793 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002794
Owen Anderson825b72b2009-08-11 20:47:22 +00002795 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002796 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002797 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002798 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Jim Grosbachb04546f2011-09-13 20:30:37 +00002799 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002800 }
2801
2802 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002803 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002804
Evan Cheng218977b2010-07-13 19:27:42 +00002805 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2806 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002807 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002808 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002809 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002810 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002811 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002812 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002813 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002814 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002815 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002816 }
2817 return Result;
2818}
2819
Evan Cheng218977b2010-07-13 19:27:42 +00002820/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2821/// to morph to an integer compare sequence.
2822static bool canChangeToInt(SDValue Op, bool &SeenZero,
2823 const ARMSubtarget *Subtarget) {
2824 SDNode *N = Op.getNode();
2825 if (!N->hasOneUse())
2826 // Otherwise it requires moving the value from fp to integer registers.
2827 return false;
2828 if (!N->getNumValues())
2829 return false;
2830 EVT VT = Op.getValueType();
2831 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2832 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2833 // vmrs are very slow, e.g. cortex-a8.
2834 return false;
2835
2836 if (isFloatingPointZero(Op)) {
2837 SeenZero = true;
2838 return true;
2839 }
2840 return ISD::isNormalLoad(N);
2841}
2842
2843static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2844 if (isFloatingPointZero(Op))
2845 return DAG.getConstant(0, MVT::i32);
2846
2847 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2848 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002849 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002850 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002851 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002852
2853 llvm_unreachable("Unknown VFP cmp argument!");
2854}
2855
2856static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2857 SDValue &RetVal1, SDValue &RetVal2) {
2858 if (isFloatingPointZero(Op)) {
2859 RetVal1 = DAG.getConstant(0, MVT::i32);
2860 RetVal2 = DAG.getConstant(0, MVT::i32);
2861 return;
2862 }
2863
2864 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2865 SDValue Ptr = Ld->getBasePtr();
2866 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2867 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002868 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002869 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002870 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002871
2872 EVT PtrType = Ptr.getValueType();
2873 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2874 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2875 PtrType, Ptr, DAG.getConstant(4, PtrType));
2876 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2877 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002878 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002879 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002880 Ld->isInvariant(), NewAlign);
Evan Cheng218977b2010-07-13 19:27:42 +00002881 return;
2882 }
2883
2884 llvm_unreachable("Unknown VFP cmp argument!");
2885}
2886
2887/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2888/// f32 and even f64 comparisons to integer ones.
2889SDValue
2890ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2891 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002892 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002893 SDValue LHS = Op.getOperand(2);
2894 SDValue RHS = Op.getOperand(3);
2895 SDValue Dest = Op.getOperand(4);
2896 DebugLoc dl = Op.getDebugLoc();
2897
2898 bool SeenZero = false;
2899 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2900 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002901 // If one of the operand is zero, it's safe to ignore the NaN case since
2902 // we only care about equality comparisons.
2903 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Bob Wilson1b772f92011-03-08 01:17:16 +00002904 // If unsafe fp math optimization is enabled and there are no other uses of
2905 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00002906 // to an integer comparison.
2907 if (CC == ISD::SETOEQ)
2908 CC = ISD::SETEQ;
2909 else if (CC == ISD::SETUNE)
2910 CC = ISD::SETNE;
2911
2912 SDValue ARMcc;
2913 if (LHS.getValueType() == MVT::f32) {
2914 LHS = bitcastf32Toi32(LHS, DAG);
2915 RHS = bitcastf32Toi32(RHS, DAG);
2916 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2917 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2918 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2919 Chain, Dest, ARMcc, CCR, Cmp);
2920 }
2921
2922 SDValue LHS1, LHS2;
2923 SDValue RHS1, RHS2;
2924 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2925 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2926 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2927 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002928 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002929 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2930 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2931 }
2932
2933 return SDValue();
2934}
2935
2936SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2937 SDValue Chain = Op.getOperand(0);
2938 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2939 SDValue LHS = Op.getOperand(2);
2940 SDValue RHS = Op.getOperand(3);
2941 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002942 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002943
Owen Anderson825b72b2009-08-11 20:47:22 +00002944 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002945 SDValue ARMcc;
2946 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002947 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002948 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002949 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002950 }
2951
Owen Anderson825b72b2009-08-11 20:47:22 +00002952 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002953
2954 if (UnsafeFPMath &&
2955 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2956 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2957 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2958 if (Result.getNode())
2959 return Result;
2960 }
2961
Evan Chenga8e29892007-01-19 07:51:42 +00002962 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002963 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002964
Evan Cheng218977b2010-07-13 19:27:42 +00002965 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2966 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002967 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002968 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002969 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002970 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002971 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002972 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2973 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002974 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002975 }
2976 return Res;
2977}
2978
Dan Gohmand858e902010-04-17 15:26:15 +00002979SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002980 SDValue Chain = Op.getOperand(0);
2981 SDValue Table = Op.getOperand(1);
2982 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002983 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002984
Owen Andersone50ed302009-08-10 22:56:29 +00002985 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002986 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2987 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002988 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002989 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002990 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002991 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2992 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002993 if (Subtarget->isThumb2()) {
2994 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2995 // which does another jump to the destination. This also makes it easier
2996 // to translate it to TBB / TBH later.
2997 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002998 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002999 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003000 }
Evan Cheng66ac5312009-07-25 00:33:29 +00003001 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00003002 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003003 MachinePointerInfo::getJumpTable(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003004 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003005 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003006 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00003007 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003008 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00003009 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003010 MachinePointerInfo::getJumpTable(),
3011 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003012 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003013 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003014 }
Evan Chenga8e29892007-01-19 07:51:42 +00003015}
3016
Eli Friedman14e809c2011-11-09 23:36:02 +00003017static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
3018 EVT VT = Op.getValueType();
3019 assert(VT.getVectorElementType() == MVT::i32 && "Unexpected custom lowering");
3020
3021 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3022 return Op;
3023 return DAG.UnrollVectorOp(Op.getNode());
3024}
3025
Bob Wilson76a312b2010-03-19 22:51:32 +00003026static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman14e809c2011-11-09 23:36:02 +00003027 EVT VT = Op.getValueType();
3028 if (VT.isVector())
3029 return LowerVectorFP_TO_INT(Op, DAG);
3030
Bob Wilson76a312b2010-03-19 22:51:32 +00003031 DebugLoc dl = Op.getDebugLoc();
3032 unsigned Opc;
3033
3034 switch (Op.getOpcode()) {
3035 default:
3036 assert(0 && "Invalid opcode!");
3037 case ISD::FP_TO_SINT:
3038 Opc = ARMISD::FTOSI;
3039 break;
3040 case ISD::FP_TO_UINT:
3041 Opc = ARMISD::FTOUI;
3042 break;
3043 }
3044 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003045 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003046}
3047
Cameron Zwarich3007d332011-03-29 21:41:55 +00003048static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3049 EVT VT = Op.getValueType();
3050 DebugLoc dl = Op.getDebugLoc();
3051
Eli Friedman14e809c2011-11-09 23:36:02 +00003052 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3053 if (VT.getVectorElementType() == MVT::f32)
3054 return Op;
3055 return DAG.UnrollVectorOp(Op.getNode());
3056 }
3057
Duncan Sands1f6a3292011-08-12 14:54:45 +00003058 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3059 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003060 if (VT != MVT::v4f32)
3061 return DAG.UnrollVectorOp(Op.getNode());
3062
3063 unsigned CastOpc;
3064 unsigned Opc;
3065 switch (Op.getOpcode()) {
3066 default:
3067 assert(0 && "Invalid opcode!");
3068 case ISD::SINT_TO_FP:
3069 CastOpc = ISD::SIGN_EXTEND;
3070 Opc = ISD::SINT_TO_FP;
3071 break;
3072 case ISD::UINT_TO_FP:
3073 CastOpc = ISD::ZERO_EXTEND;
3074 Opc = ISD::UINT_TO_FP;
3075 break;
3076 }
3077
3078 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3079 return DAG.getNode(Opc, dl, VT, Op);
3080}
3081
Bob Wilson76a312b2010-03-19 22:51:32 +00003082static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3083 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003084 if (VT.isVector())
3085 return LowerVectorINT_TO_FP(Op, DAG);
3086
Bob Wilson76a312b2010-03-19 22:51:32 +00003087 DebugLoc dl = Op.getDebugLoc();
3088 unsigned Opc;
3089
3090 switch (Op.getOpcode()) {
3091 default:
3092 assert(0 && "Invalid opcode!");
3093 case ISD::SINT_TO_FP:
3094 Opc = ARMISD::SITOF;
3095 break;
3096 case ISD::UINT_TO_FP:
3097 Opc = ARMISD::UITOF;
3098 break;
3099 }
3100
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003101 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003102 return DAG.getNode(Opc, dl, VT, Op);
3103}
3104
Evan Cheng515fe3a2010-07-08 02:08:50 +00003105SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003106 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003107 SDValue Tmp0 = Op.getOperand(0);
3108 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003109 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003110 EVT VT = Op.getValueType();
3111 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003112 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3113 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3114 bool UseNEON = !InGPR && Subtarget->hasNEON();
3115
3116 if (UseNEON) {
3117 // Use VBSL to copy the sign bit.
3118 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3119 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3120 DAG.getTargetConstant(EncodedVal, MVT::i32));
3121 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3122 if (VT == MVT::f64)
3123 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3124 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3125 DAG.getConstant(32, MVT::i32));
3126 else /*if (VT == MVT::f32)*/
3127 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3128 if (SrcVT == MVT::f32) {
3129 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3130 if (VT == MVT::f64)
3131 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3132 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3133 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003134 } else if (VT == MVT::f32)
3135 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3136 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3137 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003138 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3139 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3140
3141 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3142 MVT::i32);
3143 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3144 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3145 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003146
Evan Chenge573fb32011-02-23 02:24:55 +00003147 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3148 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3149 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003150 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003151 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3152 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3153 DAG.getConstant(0, MVT::i32));
3154 } else {
3155 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3156 }
3157
3158 return Res;
3159 }
Evan Chengc143dd42011-02-11 02:28:55 +00003160
3161 // Bitcast operand 1 to i32.
3162 if (SrcVT == MVT::f64)
3163 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3164 &Tmp1, 1).getValue(1);
3165 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3166
Evan Chenge573fb32011-02-23 02:24:55 +00003167 // Or in the signbit with integer operations.
3168 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3169 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3170 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3171 if (VT == MVT::f32) {
3172 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3173 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3174 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3175 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003176 }
3177
Evan Chenge573fb32011-02-23 02:24:55 +00003178 // f64: Or the high part with signbit and then combine two parts.
3179 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3180 &Tmp0, 1);
3181 SDValue Lo = Tmp0.getValue(0);
3182 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3183 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3184 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003185}
3186
Evan Cheng2457f2c2010-05-22 01:47:14 +00003187SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3188 MachineFunction &MF = DAG.getMachineFunction();
3189 MachineFrameInfo *MFI = MF.getFrameInfo();
3190 MFI->setReturnAddressIsTaken(true);
3191
3192 EVT VT = Op.getValueType();
3193 DebugLoc dl = Op.getDebugLoc();
3194 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3195 if (Depth) {
3196 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3197 SDValue Offset = DAG.getConstant(4, MVT::i32);
3198 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3199 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003200 MachinePointerInfo(), false, false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003201 }
3202
3203 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003204 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003205 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3206}
3207
Dan Gohmand858e902010-04-17 15:26:15 +00003208SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003209 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3210 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003211
Owen Andersone50ed302009-08-10 22:56:29 +00003212 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003213 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3214 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003215 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003216 ? ARM::R7 : ARM::R11;
3217 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3218 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003219 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3220 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003221 false, false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003222 return FrameAddr;
3223}
3224
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003225/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003226/// expand a bit convert where either the source or destination type is i64 to
3227/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3228/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3229/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003230static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003231 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3232 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003233 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003234
Bob Wilson9f3f0612010-04-17 05:30:19 +00003235 // This function is only supposed to be called for i64 types, either as the
3236 // source or destination of the bit convert.
3237 EVT SrcVT = Op.getValueType();
3238 EVT DstVT = N->getValueType(0);
3239 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003240 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003241
Bob Wilson9f3f0612010-04-17 05:30:19 +00003242 // Turn i64->f64 into VMOVDRR.
3243 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003244 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3245 DAG.getConstant(0, MVT::i32));
3246 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3247 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003248 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003249 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003250 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003251
Jim Grosbache5165492009-11-09 00:11:35 +00003252 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003253 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3254 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3255 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3256 // Merge the pieces into a single i64 value.
3257 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3258 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003259
Bob Wilson9f3f0612010-04-17 05:30:19 +00003260 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003261}
3262
Bob Wilson5bafff32009-06-22 23:27:02 +00003263/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003264/// Zero vectors are used to represent vector negation and in those cases
3265/// will be implemented with the NEON VNEG instruction. However, VNEG does
3266/// not support i64 elements, so sometimes the zero vectors will need to be
3267/// explicitly constructed. Regardless, use a canonical VMOV to create the
3268/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003269static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003270 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003271 // The canonical modified immediate encoding of a zero vector is....0!
3272 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3273 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3274 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003275 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003276}
3277
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003278/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3279/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003280SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3281 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003282 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3283 EVT VT = Op.getValueType();
3284 unsigned VTBits = VT.getSizeInBits();
3285 DebugLoc dl = Op.getDebugLoc();
3286 SDValue ShOpLo = Op.getOperand(0);
3287 SDValue ShOpHi = Op.getOperand(1);
3288 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003289 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003290 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003291
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003292 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3293
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003294 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3295 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3296 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3297 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3298 DAG.getConstant(VTBits, MVT::i32));
3299 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3300 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003301 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003302
3303 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3304 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003305 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003306 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003307 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003308 CCR, Cmp);
3309
3310 SDValue Ops[2] = { Lo, Hi };
3311 return DAG.getMergeValues(Ops, 2, dl);
3312}
3313
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003314/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3315/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003316SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3317 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003318 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3319 EVT VT = Op.getValueType();
3320 unsigned VTBits = VT.getSizeInBits();
3321 DebugLoc dl = Op.getDebugLoc();
3322 SDValue ShOpLo = Op.getOperand(0);
3323 SDValue ShOpHi = Op.getOperand(1);
3324 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003325 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003326
3327 assert(Op.getOpcode() == ISD::SHL_PARTS);
3328 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3329 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3330 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3331 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3332 DAG.getConstant(VTBits, MVT::i32));
3333 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3334 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3335
3336 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3337 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3338 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003339 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003340 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003341 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003342 CCR, Cmp);
3343
3344 SDValue Ops[2] = { Lo, Hi };
3345 return DAG.getMergeValues(Ops, 2, dl);
3346}
3347
Jim Grosbach4725ca72010-09-08 03:54:02 +00003348SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003349 SelectionDAG &DAG) const {
3350 // The rounding mode is in bits 23:22 of the FPSCR.
3351 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3352 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3353 // so that the shift + and get folded into a bitfield extract.
3354 DebugLoc dl = Op.getDebugLoc();
3355 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3356 DAG.getConstant(Intrinsic::arm_get_fpscr,
3357 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003358 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003359 DAG.getConstant(1U << 22, MVT::i32));
3360 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3361 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003362 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003363 DAG.getConstant(3, MVT::i32));
3364}
3365
Jim Grosbach3482c802010-01-18 19:58:49 +00003366static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3367 const ARMSubtarget *ST) {
3368 EVT VT = N->getValueType(0);
3369 DebugLoc dl = N->getDebugLoc();
3370
3371 if (!ST->hasV6T2Ops())
3372 return SDValue();
3373
3374 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3375 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3376}
3377
Bob Wilson5bafff32009-06-22 23:27:02 +00003378static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3379 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003380 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003381 DebugLoc dl = N->getDebugLoc();
3382
Bob Wilsond5448bb2010-11-18 21:16:28 +00003383 if (!VT.isVector())
3384 return SDValue();
3385
Bob Wilson5bafff32009-06-22 23:27:02 +00003386 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003387 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003388
Bob Wilsond5448bb2010-11-18 21:16:28 +00003389 // Left shifts translate directly to the vshiftu intrinsic.
3390 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003391 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003392 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3393 N->getOperand(0), N->getOperand(1));
3394
3395 assert((N->getOpcode() == ISD::SRA ||
3396 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3397
3398 // NEON uses the same intrinsics for both left and right shifts. For
3399 // right shifts, the shift amounts are negative, so negate the vector of
3400 // shift amounts.
3401 EVT ShiftVT = N->getOperand(1).getValueType();
3402 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3403 getZeroVector(ShiftVT, DAG, dl),
3404 N->getOperand(1));
3405 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3406 Intrinsic::arm_neon_vshifts :
3407 Intrinsic::arm_neon_vshiftu);
3408 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3409 DAG.getConstant(vshiftInt, MVT::i32),
3410 N->getOperand(0), NegatedCount);
3411}
3412
3413static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3414 const ARMSubtarget *ST) {
3415 EVT VT = N->getValueType(0);
3416 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003417
Eli Friedmance392eb2009-08-22 03:13:10 +00003418 // We can get here for a node like i32 = ISD::SHL i32, i64
3419 if (VT != MVT::i64)
3420 return SDValue();
3421
3422 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003423 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003424
Chris Lattner27a6c732007-11-24 07:07:01 +00003425 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3426 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003427 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003428 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003429
Chris Lattner27a6c732007-11-24 07:07:01 +00003430 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003431 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003432
Chris Lattner27a6c732007-11-24 07:07:01 +00003433 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003434 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003435 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003436 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003437 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003438
Chris Lattner27a6c732007-11-24 07:07:01 +00003439 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3440 // captures the result into a carry flag.
3441 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003442 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003443
Chris Lattner27a6c732007-11-24 07:07:01 +00003444 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003445 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003446
Chris Lattner27a6c732007-11-24 07:07:01 +00003447 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003448 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003449}
3450
Bob Wilson5bafff32009-06-22 23:27:02 +00003451static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3452 SDValue TmpOp0, TmpOp1;
3453 bool Invert = false;
3454 bool Swap = false;
3455 unsigned Opc = 0;
3456
3457 SDValue Op0 = Op.getOperand(0);
3458 SDValue Op1 = Op.getOperand(1);
3459 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003460 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003461 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3462 DebugLoc dl = Op.getDebugLoc();
3463
3464 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3465 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003466 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003467 case ISD::SETUNE:
3468 case ISD::SETNE: Invert = true; // Fallthrough
3469 case ISD::SETOEQ:
3470 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3471 case ISD::SETOLT:
3472 case ISD::SETLT: Swap = true; // Fallthrough
3473 case ISD::SETOGT:
3474 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3475 case ISD::SETOLE:
3476 case ISD::SETLE: Swap = true; // Fallthrough
3477 case ISD::SETOGE:
3478 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3479 case ISD::SETUGE: Swap = true; // Fallthrough
3480 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3481 case ISD::SETUGT: Swap = true; // Fallthrough
3482 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3483 case ISD::SETUEQ: Invert = true; // Fallthrough
3484 case ISD::SETONE:
3485 // Expand this to (OLT | OGT).
3486 TmpOp0 = Op0;
3487 TmpOp1 = Op1;
3488 Opc = ISD::OR;
3489 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3490 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3491 break;
3492 case ISD::SETUO: Invert = true; // Fallthrough
3493 case ISD::SETO:
3494 // Expand this to (OLT | OGE).
3495 TmpOp0 = Op0;
3496 TmpOp1 = Op1;
3497 Opc = ISD::OR;
3498 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3499 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3500 break;
3501 }
3502 } else {
3503 // Integer comparisons.
3504 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003505 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003506 case ISD::SETNE: Invert = true;
3507 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3508 case ISD::SETLT: Swap = true;
3509 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3510 case ISD::SETLE: Swap = true;
3511 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3512 case ISD::SETULT: Swap = true;
3513 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3514 case ISD::SETULE: Swap = true;
3515 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3516 }
3517
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003518 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003519 if (Opc == ARMISD::VCEQ) {
3520
3521 SDValue AndOp;
3522 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3523 AndOp = Op0;
3524 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3525 AndOp = Op1;
3526
3527 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003528 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003529 AndOp = AndOp.getOperand(0);
3530
3531 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3532 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003533 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3534 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003535 Invert = !Invert;
3536 }
3537 }
3538 }
3539
3540 if (Swap)
3541 std::swap(Op0, Op1);
3542
Owen Andersonc24cb352010-11-08 23:21:22 +00003543 // If one of the operands is a constant vector zero, attempt to fold the
3544 // comparison to a specialized compare-against-zero form.
3545 SDValue SingleOp;
3546 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3547 SingleOp = Op0;
3548 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3549 if (Opc == ARMISD::VCGE)
3550 Opc = ARMISD::VCLEZ;
3551 else if (Opc == ARMISD::VCGT)
3552 Opc = ARMISD::VCLTZ;
3553 SingleOp = Op1;
3554 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003555
Owen Andersonc24cb352010-11-08 23:21:22 +00003556 SDValue Result;
3557 if (SingleOp.getNode()) {
3558 switch (Opc) {
3559 case ARMISD::VCEQ:
3560 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3561 case ARMISD::VCGE:
3562 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3563 case ARMISD::VCLEZ:
3564 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3565 case ARMISD::VCGT:
3566 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3567 case ARMISD::VCLTZ:
3568 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3569 default:
3570 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3571 }
3572 } else {
3573 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3574 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003575
3576 if (Invert)
3577 Result = DAG.getNOT(dl, Result, VT);
3578
3579 return Result;
3580}
3581
Bob Wilsond3c42842010-06-14 22:19:57 +00003582/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3583/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003584/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003585static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3586 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003587 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003588 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003589
Bob Wilson827b2102010-06-15 19:05:35 +00003590 // SplatBitSize is set to the smallest size that splats the vector, so a
3591 // zero vector will always have SplatBitSize == 8. However, NEON modified
3592 // immediate instructions others than VMOV do not support the 8-bit encoding
3593 // of a zero vector, and the default encoding of zero is supposed to be the
3594 // 32-bit version.
3595 if (SplatBits == 0)
3596 SplatBitSize = 32;
3597
Bob Wilson5bafff32009-06-22 23:27:02 +00003598 switch (SplatBitSize) {
3599 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003600 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003601 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003602 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003603 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003604 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003605 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003606 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003607 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003608
3609 case 16:
3610 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003611 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003612 if ((SplatBits & ~0xff) == 0) {
3613 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003614 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003615 Imm = SplatBits;
3616 break;
3617 }
3618 if ((SplatBits & ~0xff00) == 0) {
3619 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003620 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003621 Imm = SplatBits >> 8;
3622 break;
3623 }
3624 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003625
3626 case 32:
3627 // NEON's 32-bit VMOV supports splat values where:
3628 // * only one byte is nonzero, or
3629 // * the least significant byte is 0xff and the second byte is nonzero, or
3630 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003631 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003632 if ((SplatBits & ~0xff) == 0) {
3633 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003634 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003635 Imm = SplatBits;
3636 break;
3637 }
3638 if ((SplatBits & ~0xff00) == 0) {
3639 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003640 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003641 Imm = SplatBits >> 8;
3642 break;
3643 }
3644 if ((SplatBits & ~0xff0000) == 0) {
3645 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003646 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003647 Imm = SplatBits >> 16;
3648 break;
3649 }
3650 if ((SplatBits & ~0xff000000) == 0) {
3651 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003652 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003653 Imm = SplatBits >> 24;
3654 break;
3655 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003656
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003657 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3658 if (type == OtherModImm) return SDValue();
3659
Bob Wilson5bafff32009-06-22 23:27:02 +00003660 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003661 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3662 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003663 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003664 Imm = SplatBits >> 8;
3665 SplatBits |= 0xff;
3666 break;
3667 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003668
3669 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003670 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3671 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003672 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003673 Imm = SplatBits >> 16;
3674 SplatBits |= 0xffff;
3675 break;
3676 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003677
3678 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3679 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3680 // VMOV.I32. A (very) minor optimization would be to replicate the value
3681 // and fall through here to test for a valid 64-bit splat. But, then the
3682 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003683 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003684
3685 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003686 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003687 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003688 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003689 uint64_t BitMask = 0xff;
3690 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003691 unsigned ImmMask = 1;
3692 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003693 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003694 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003695 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003696 Imm |= ImmMask;
3697 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003698 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003699 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003700 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003701 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003702 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003703 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003704 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003705 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003706 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003707 break;
3708 }
3709
Bob Wilson1a913ed2010-06-11 21:34:50 +00003710 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003711 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003712 return SDValue();
3713 }
3714
Bob Wilsoncba270d2010-07-13 21:16:48 +00003715 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3716 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003717}
3718
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003719static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3720 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003721 unsigned NumElts = VT.getVectorNumElements();
3722 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003723
3724 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3725 if (M[0] < 0)
3726 return false;
3727
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003728 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003729
3730 // If this is a VEXT shuffle, the immediate value is the index of the first
3731 // element. The other shuffle indices must be the successive elements after
3732 // the first one.
3733 unsigned ExpectedElt = Imm;
3734 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003735 // Increment the expected index. If it wraps around, it may still be
3736 // a VEXT but the source vectors must be swapped.
3737 ExpectedElt += 1;
3738 if (ExpectedElt == NumElts * 2) {
3739 ExpectedElt = 0;
3740 ReverseVEXT = true;
3741 }
3742
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003743 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003744 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003745 return false;
3746 }
3747
3748 // Adjust the index value if the source operands will be swapped.
3749 if (ReverseVEXT)
3750 Imm -= NumElts;
3751
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003752 return true;
3753}
3754
Bob Wilson8bb9e482009-07-26 00:39:34 +00003755/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3756/// instruction with the specified blocksize. (The order of the elements
3757/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003758static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3759 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003760 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3761 "Only possible block sizes for VREV are: 16, 32, 64");
3762
Bob Wilson8bb9e482009-07-26 00:39:34 +00003763 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003764 if (EltSz == 64)
3765 return false;
3766
3767 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003768 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003769 // If the first shuffle index is UNDEF, be optimistic.
3770 if (M[0] < 0)
3771 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003772
3773 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3774 return false;
3775
3776 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003777 if (M[i] < 0) continue; // ignore UNDEF indices
3778 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003779 return false;
3780 }
3781
3782 return true;
3783}
3784
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003785static bool isVTBLMask(const SmallVectorImpl<int> &M, EVT VT) {
3786 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3787 // range, then 0 is placed into the resulting vector. So pretty much any mask
3788 // of 8 elements can work here.
3789 return VT == MVT::v8i8 && M.size() == 8;
3790}
3791
Bob Wilsonc692cb72009-08-21 20:54:19 +00003792static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3793 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003794 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3795 if (EltSz == 64)
3796 return false;
3797
Bob Wilsonc692cb72009-08-21 20:54:19 +00003798 unsigned NumElts = VT.getVectorNumElements();
3799 WhichResult = (M[0] == 0 ? 0 : 1);
3800 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003801 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3802 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003803 return false;
3804 }
3805 return true;
3806}
3807
Bob Wilson324f4f12009-12-03 06:40:55 +00003808/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3809/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3810/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3811static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3812 unsigned &WhichResult) {
3813 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3814 if (EltSz == 64)
3815 return false;
3816
3817 unsigned NumElts = VT.getVectorNumElements();
3818 WhichResult = (M[0] == 0 ? 0 : 1);
3819 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003820 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3821 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003822 return false;
3823 }
3824 return true;
3825}
3826
Bob Wilsonc692cb72009-08-21 20:54:19 +00003827static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3828 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003829 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3830 if (EltSz == 64)
3831 return false;
3832
Bob Wilsonc692cb72009-08-21 20:54:19 +00003833 unsigned NumElts = VT.getVectorNumElements();
3834 WhichResult = (M[0] == 0 ? 0 : 1);
3835 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003836 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003837 if ((unsigned) M[i] != 2 * i + WhichResult)
3838 return false;
3839 }
3840
3841 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003842 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003843 return false;
3844
3845 return true;
3846}
3847
Bob Wilson324f4f12009-12-03 06:40:55 +00003848/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3849/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3850/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3851static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3852 unsigned &WhichResult) {
3853 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3854 if (EltSz == 64)
3855 return false;
3856
3857 unsigned Half = VT.getVectorNumElements() / 2;
3858 WhichResult = (M[0] == 0 ? 0 : 1);
3859 for (unsigned j = 0; j != 2; ++j) {
3860 unsigned Idx = WhichResult;
3861 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003862 int MIdx = M[i + j * Half];
3863 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003864 return false;
3865 Idx += 2;
3866 }
3867 }
3868
3869 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3870 if (VT.is64BitVector() && EltSz == 32)
3871 return false;
3872
3873 return true;
3874}
3875
Bob Wilsonc692cb72009-08-21 20:54:19 +00003876static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3877 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003878 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3879 if (EltSz == 64)
3880 return false;
3881
Bob Wilsonc692cb72009-08-21 20:54:19 +00003882 unsigned NumElts = VT.getVectorNumElements();
3883 WhichResult = (M[0] == 0 ? 0 : 1);
3884 unsigned Idx = WhichResult * NumElts / 2;
3885 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003886 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3887 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003888 return false;
3889 Idx += 1;
3890 }
3891
3892 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003893 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003894 return false;
3895
3896 return true;
3897}
3898
Bob Wilson324f4f12009-12-03 06:40:55 +00003899/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3900/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3901/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3902static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3903 unsigned &WhichResult) {
3904 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3905 if (EltSz == 64)
3906 return false;
3907
3908 unsigned NumElts = VT.getVectorNumElements();
3909 WhichResult = (M[0] == 0 ? 0 : 1);
3910 unsigned Idx = WhichResult * NumElts / 2;
3911 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003912 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3913 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003914 return false;
3915 Idx += 1;
3916 }
3917
3918 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3919 if (VT.is64BitVector() && EltSz == 32)
3920 return false;
3921
3922 return true;
3923}
3924
Dale Johannesenf630c712010-07-29 20:10:08 +00003925// If N is an integer constant that can be moved into a register in one
3926// instruction, return an SDValue of such a constant (will become a MOV
3927// instruction). Otherwise return null.
3928static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3929 const ARMSubtarget *ST, DebugLoc dl) {
3930 uint64_t Val;
3931 if (!isa<ConstantSDNode>(N))
3932 return SDValue();
3933 Val = cast<ConstantSDNode>(N)->getZExtValue();
3934
3935 if (ST->isThumb1Only()) {
3936 if (Val <= 255 || ~Val <= 255)
3937 return DAG.getConstant(Val, MVT::i32);
3938 } else {
3939 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3940 return DAG.getConstant(Val, MVT::i32);
3941 }
3942 return SDValue();
3943}
3944
Bob Wilson5bafff32009-06-22 23:27:02 +00003945// If this is a case we can't handle, return null and let the default
3946// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003947SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3948 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003949 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003950 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003951 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003952
3953 APInt SplatBits, SplatUndef;
3954 unsigned SplatBitSize;
3955 bool HasAnyUndefs;
3956 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003957 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003958 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003959 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003960 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003961 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003962 DAG, VmovVT, VT.is128BitVector(),
3963 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003964 if (Val.getNode()) {
3965 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003966 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003967 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003968
3969 // Try an immediate VMVN.
Eli Friedman8e4d0422011-10-13 22:40:23 +00003970 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003971 Val = isNEONModifiedImm(NegatedImm,
3972 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003973 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003974 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003975 if (Val.getNode()) {
3976 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003977 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003978 }
Evan Chengeaa192a2011-11-15 02:12:34 +00003979
3980 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
3981 if (VT == MVT::v2f32 || VT == MVT::v4f32) {
3982 ConstantFPSDNode *C = cast<ConstantFPSDNode>(Op.getOperand(0));
3983 int ImmVal = ARM_AM::getFP32Imm(C->getValueAPF());
3984 if (ImmVal != -1) {
3985 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
3986 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
3987 }
3988 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003989 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003990 }
3991
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003992 // Scan through the operands to see if only one value is used.
3993 unsigned NumElts = VT.getVectorNumElements();
3994 bool isOnlyLowElement = true;
3995 bool usesOnlyOneValue = true;
3996 bool isConstant = true;
3997 SDValue Value;
3998 for (unsigned i = 0; i < NumElts; ++i) {
3999 SDValue V = Op.getOperand(i);
4000 if (V.getOpcode() == ISD::UNDEF)
4001 continue;
4002 if (i > 0)
4003 isOnlyLowElement = false;
4004 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4005 isConstant = false;
4006
4007 if (!Value.getNode())
4008 Value = V;
4009 else if (V != Value)
4010 usesOnlyOneValue = false;
4011 }
4012
4013 if (!Value.getNode())
4014 return DAG.getUNDEF(VT);
4015
4016 if (isOnlyLowElement)
4017 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4018
Dale Johannesenf630c712010-07-29 20:10:08 +00004019 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4020
Dale Johannesen575cd142010-10-19 20:00:17 +00004021 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4022 // i32 and try again.
4023 if (usesOnlyOneValue && EltSize <= 32) {
4024 if (!isConstant)
4025 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4026 if (VT.getVectorElementType().isFloatingPoint()) {
4027 SmallVector<SDValue, 8> Ops;
4028 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004029 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00004030 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00004031 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
4032 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00004033 Val = LowerBUILD_VECTOR(Val, DAG, ST);
4034 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004035 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004036 }
Dale Johannesen575cd142010-10-19 20:00:17 +00004037 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
4038 if (Val.getNode())
4039 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004040 }
4041
4042 // If all elements are constants and the case above didn't get hit, fall back
4043 // to the default expansion, which will generate a load from the constant
4044 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004045 if (isConstant)
4046 return SDValue();
4047
Bob Wilson11a1dff2011-01-07 21:37:30 +00004048 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4049 if (NumElts >= 4) {
4050 SDValue shuffle = ReconstructShuffle(Op, DAG);
4051 if (shuffle != SDValue())
4052 return shuffle;
4053 }
4054
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004055 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004056 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4057 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004058 if (EltSize >= 32) {
4059 // Do the expansion with floating-point types, since that is what the VFP
4060 // registers are defined to use, and since i64 is not legal.
4061 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4062 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004063 SmallVector<SDValue, 8> Ops;
4064 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004065 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004066 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004067 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004068 }
4069
4070 return SDValue();
4071}
4072
Bob Wilson11a1dff2011-01-07 21:37:30 +00004073// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004074// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004075SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4076 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004077 DebugLoc dl = Op.getDebugLoc();
4078 EVT VT = Op.getValueType();
4079 unsigned NumElts = VT.getVectorNumElements();
4080
4081 SmallVector<SDValue, 2> SourceVecs;
4082 SmallVector<unsigned, 2> MinElts;
4083 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004084
Bob Wilson11a1dff2011-01-07 21:37:30 +00004085 for (unsigned i = 0; i < NumElts; ++i) {
4086 SDValue V = Op.getOperand(i);
4087 if (V.getOpcode() == ISD::UNDEF)
4088 continue;
4089 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4090 // A shuffle can only come from building a vector from various
4091 // elements of other vectors.
4092 return SDValue();
Eli Friedman46995fa2011-10-14 23:58:49 +00004093 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
4094 VT.getVectorElementType()) {
4095 // This code doesn't know how to handle shuffles where the vector
4096 // element types do not match (this happens because type legalization
4097 // promotes the return type of EXTRACT_VECTOR_ELT).
4098 // FIXME: It might be appropriate to extend this code to handle
4099 // mismatched types.
4100 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004101 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004102
Bob Wilson11a1dff2011-01-07 21:37:30 +00004103 // Record this extraction against the appropriate vector if possible...
4104 SDValue SourceVec = V.getOperand(0);
4105 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4106 bool FoundSource = false;
4107 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4108 if (SourceVecs[j] == SourceVec) {
4109 if (MinElts[j] > EltNo)
4110 MinElts[j] = EltNo;
4111 if (MaxElts[j] < EltNo)
4112 MaxElts[j] = EltNo;
4113 FoundSource = true;
4114 break;
4115 }
4116 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004117
Bob Wilson11a1dff2011-01-07 21:37:30 +00004118 // Or record a new source if not...
4119 if (!FoundSource) {
4120 SourceVecs.push_back(SourceVec);
4121 MinElts.push_back(EltNo);
4122 MaxElts.push_back(EltNo);
4123 }
4124 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004125
Bob Wilson11a1dff2011-01-07 21:37:30 +00004126 // Currently only do something sane when at most two source vectors
4127 // involved.
4128 if (SourceVecs.size() > 2)
4129 return SDValue();
4130
4131 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4132 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004133
Bob Wilson11a1dff2011-01-07 21:37:30 +00004134 // This loop extracts the usage patterns of the source vectors
4135 // and prepares appropriate SDValues for a shuffle if possible.
4136 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4137 if (SourceVecs[i].getValueType() == VT) {
4138 // No VEXT necessary
4139 ShuffleSrcs[i] = SourceVecs[i];
4140 VEXTOffsets[i] = 0;
4141 continue;
4142 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4143 // It probably isn't worth padding out a smaller vector just to
4144 // break it down again in a shuffle.
4145 return SDValue();
4146 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004147
Bob Wilson11a1dff2011-01-07 21:37:30 +00004148 // Since only 64-bit and 128-bit vectors are legal on ARM and
4149 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004150 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4151 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004152
Bob Wilson11a1dff2011-01-07 21:37:30 +00004153 if (MaxElts[i] - MinElts[i] >= NumElts) {
4154 // Span too large for a VEXT to cope
4155 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004156 }
4157
Bob Wilson11a1dff2011-01-07 21:37:30 +00004158 if (MinElts[i] >= NumElts) {
4159 // The extraction can just take the second half
4160 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004161 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4162 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004163 DAG.getIntPtrConstant(NumElts));
4164 } else if (MaxElts[i] < NumElts) {
4165 // The extraction can just take the first half
4166 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004167 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4168 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004169 DAG.getIntPtrConstant(0));
4170 } else {
4171 // An actual VEXT is needed
4172 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004173 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4174 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004175 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004176 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4177 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004178 DAG.getIntPtrConstant(NumElts));
4179 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4180 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4181 }
4182 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004183
Bob Wilson11a1dff2011-01-07 21:37:30 +00004184 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004185
Bob Wilson11a1dff2011-01-07 21:37:30 +00004186 for (unsigned i = 0; i < NumElts; ++i) {
4187 SDValue Entry = Op.getOperand(i);
4188 if (Entry.getOpcode() == ISD::UNDEF) {
4189 Mask.push_back(-1);
4190 continue;
4191 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004192
Bob Wilson11a1dff2011-01-07 21:37:30 +00004193 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004194 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4195 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004196 if (ExtractVec == SourceVecs[0]) {
4197 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4198 } else {
4199 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4200 }
4201 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004202
Bob Wilson11a1dff2011-01-07 21:37:30 +00004203 // Final check before we try to produce nonsense...
4204 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004205 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4206 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004207
Bob Wilson11a1dff2011-01-07 21:37:30 +00004208 return SDValue();
4209}
4210
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004211/// isShuffleMaskLegal - Targets can use this to indicate that they only
4212/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4213/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4214/// are assumed to be legal.
4215bool
4216ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4217 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004218 if (VT.getVectorNumElements() == 4 &&
4219 (VT.is128BitVector() || VT.is64BitVector())) {
4220 unsigned PFIndexes[4];
4221 for (unsigned i = 0; i != 4; ++i) {
4222 if (M[i] < 0)
4223 PFIndexes[i] = 8;
4224 else
4225 PFIndexes[i] = M[i];
4226 }
4227
4228 // Compute the index in the perfect shuffle table.
4229 unsigned PFTableIndex =
4230 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4231 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4232 unsigned Cost = (PFEntry >> 30);
4233
4234 if (Cost <= 4)
4235 return true;
4236 }
4237
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004238 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004239 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004240
Bob Wilson53dd2452010-06-07 23:53:38 +00004241 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4242 return (EltSize >= 32 ||
4243 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004244 isVREVMask(M, VT, 64) ||
4245 isVREVMask(M, VT, 32) ||
4246 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004247 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004248 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004249 isVTRNMask(M, VT, WhichResult) ||
4250 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004251 isVZIPMask(M, VT, WhichResult) ||
4252 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4253 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4254 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004255}
4256
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004257/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4258/// the specified operations to build the shuffle.
4259static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4260 SDValue RHS, SelectionDAG &DAG,
4261 DebugLoc dl) {
4262 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4263 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4264 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4265
4266 enum {
4267 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4268 OP_VREV,
4269 OP_VDUP0,
4270 OP_VDUP1,
4271 OP_VDUP2,
4272 OP_VDUP3,
4273 OP_VEXT1,
4274 OP_VEXT2,
4275 OP_VEXT3,
4276 OP_VUZPL, // VUZP, left result
4277 OP_VUZPR, // VUZP, right result
4278 OP_VZIPL, // VZIP, left result
4279 OP_VZIPR, // VZIP, right result
4280 OP_VTRNL, // VTRN, left result
4281 OP_VTRNR // VTRN, right result
4282 };
4283
4284 if (OpNum == OP_COPY) {
4285 if (LHSID == (1*9+2)*9+3) return LHS;
4286 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4287 return RHS;
4288 }
4289
4290 SDValue OpLHS, OpRHS;
4291 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4292 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4293 EVT VT = OpLHS.getValueType();
4294
4295 switch (OpNum) {
4296 default: llvm_unreachable("Unknown shuffle opcode!");
4297 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004298 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004299 if (VT.getVectorElementType() == MVT::i32 ||
4300 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004301 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4302 // vrev <4 x i16> -> VREV32
4303 if (VT.getVectorElementType() == MVT::i16)
4304 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4305 // vrev <4 x i8> -> VREV16
4306 assert(VT.getVectorElementType() == MVT::i8);
4307 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004308 case OP_VDUP0:
4309 case OP_VDUP1:
4310 case OP_VDUP2:
4311 case OP_VDUP3:
4312 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004313 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004314 case OP_VEXT1:
4315 case OP_VEXT2:
4316 case OP_VEXT3:
4317 return DAG.getNode(ARMISD::VEXT, dl, VT,
4318 OpLHS, OpRHS,
4319 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4320 case OP_VUZPL:
4321 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004322 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004323 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4324 case OP_VZIPL:
4325 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004326 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004327 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4328 case OP_VTRNL:
4329 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004330 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4331 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004332 }
4333}
4334
Bill Wendling69a05a72011-03-14 23:02:38 +00004335static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
4336 SmallVectorImpl<int> &ShuffleMask,
4337 SelectionDAG &DAG) {
4338 // Check to see if we can use the VTBL instruction.
4339 SDValue V1 = Op.getOperand(0);
4340 SDValue V2 = Op.getOperand(1);
4341 DebugLoc DL = Op.getDebugLoc();
4342
4343 SmallVector<SDValue, 8> VTBLMask;
4344 for (SmallVectorImpl<int>::iterator
4345 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4346 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4347
4348 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4349 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4350 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4351 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004352
Owen Anderson76706012011-04-05 21:48:57 +00004353 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004354 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4355 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004356}
4357
Bob Wilson5bafff32009-06-22 23:27:02 +00004358static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004359 SDValue V1 = Op.getOperand(0);
4360 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004361 DebugLoc dl = Op.getDebugLoc();
4362 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004363 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004364 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00004365
Bob Wilson28865062009-08-13 02:13:04 +00004366 // Convert shuffles that are directly supported on NEON to target-specific
4367 // DAG nodes, instead of keeping them as shuffles and matching them again
4368 // during code selection. This is more efficient and avoids the possibility
4369 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004370 // FIXME: floating-point vectors should be canonicalized to integer vectors
4371 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004372 SVN->getMask(ShuffleMask);
4373
Bob Wilson53dd2452010-06-07 23:53:38 +00004374 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4375 if (EltSize <= 32) {
4376 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4377 int Lane = SVN->getSplatIndex();
4378 // If this is undef splat, generate it via "just" vdup, if possible.
4379 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004380
Dan Gohman65fd6562011-11-03 21:49:52 +00004381 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson53dd2452010-06-07 23:53:38 +00004382 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4383 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4384 }
Dan Gohman65fd6562011-11-03 21:49:52 +00004385 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
4386 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
4387 // reaches it).
4388 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
4389 !isa<ConstantSDNode>(V1.getOperand(0))) {
4390 bool IsScalarToVector = true;
4391 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
4392 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
4393 IsScalarToVector = false;
4394 break;
4395 }
4396 if (IsScalarToVector)
4397 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4398 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004399 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4400 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004401 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004402
4403 bool ReverseVEXT;
4404 unsigned Imm;
4405 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4406 if (ReverseVEXT)
4407 std::swap(V1, V2);
4408 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4409 DAG.getConstant(Imm, MVT::i32));
4410 }
4411
4412 if (isVREVMask(ShuffleMask, VT, 64))
4413 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4414 if (isVREVMask(ShuffleMask, VT, 32))
4415 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4416 if (isVREVMask(ShuffleMask, VT, 16))
4417 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4418
4419 // Check for Neon shuffles that modify both input vectors in place.
4420 // If both results are used, i.e., if there are two shuffles with the same
4421 // source operands and with masks corresponding to both results of one of
4422 // these operations, DAG memoization will ensure that a single node is
4423 // used for both shuffles.
4424 unsigned WhichResult;
4425 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4426 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4427 V1, V2).getValue(WhichResult);
4428 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4429 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4430 V1, V2).getValue(WhichResult);
4431 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4432 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4433 V1, V2).getValue(WhichResult);
4434
4435 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4436 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4437 V1, V1).getValue(WhichResult);
4438 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4439 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4440 V1, V1).getValue(WhichResult);
4441 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4442 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4443 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004444 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004445
Bob Wilsonc692cb72009-08-21 20:54:19 +00004446 // If the shuffle is not directly supported and it has 4 elements, use
4447 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004448 unsigned NumElts = VT.getVectorNumElements();
4449 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004450 unsigned PFIndexes[4];
4451 for (unsigned i = 0; i != 4; ++i) {
4452 if (ShuffleMask[i] < 0)
4453 PFIndexes[i] = 8;
4454 else
4455 PFIndexes[i] = ShuffleMask[i];
4456 }
4457
4458 // Compute the index in the perfect shuffle table.
4459 unsigned PFTableIndex =
4460 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004461 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4462 unsigned Cost = (PFEntry >> 30);
4463
4464 if (Cost <= 4)
4465 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4466 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004467
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004468 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004469 if (EltSize >= 32) {
4470 // Do the expansion with floating-point types, since that is what the VFP
4471 // registers are defined to use, and since i64 is not legal.
4472 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4473 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004474 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4475 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004476 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004477 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004478 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004479 Ops.push_back(DAG.getUNDEF(EltVT));
4480 else
4481 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4482 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4483 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4484 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004485 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004486 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004487 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004488 }
4489
Bill Wendling69a05a72011-03-14 23:02:38 +00004490 if (VT == MVT::v8i8) {
4491 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4492 if (NewOp.getNode())
4493 return NewOp;
4494 }
4495
Bob Wilson22cac0d2009-08-14 05:16:33 +00004496 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004497}
4498
Eli Friedman5c89cb82011-10-24 23:08:52 +00004499static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
4500 // INSERT_VECTOR_ELT is legal only for immediate indexes.
4501 SDValue Lane = Op.getOperand(2);
4502 if (!isa<ConstantSDNode>(Lane))
4503 return SDValue();
4504
4505 return Op;
4506}
4507
Bob Wilson5bafff32009-06-22 23:27:02 +00004508static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004509 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004510 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004511 if (!isa<ConstantSDNode>(Lane))
4512 return SDValue();
4513
4514 SDValue Vec = Op.getOperand(0);
4515 if (Op.getValueType() == MVT::i32 &&
4516 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4517 DebugLoc dl = Op.getDebugLoc();
4518 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4519 }
4520
4521 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004522}
4523
Bob Wilsona6d65862009-08-03 20:36:38 +00004524static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4525 // The only time a CONCAT_VECTORS operation can have legal types is when
4526 // two 64-bit vectors are concatenated to a 128-bit vector.
4527 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4528 "unexpected CONCAT_VECTORS");
4529 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004530 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004531 SDValue Op0 = Op.getOperand(0);
4532 SDValue Op1 = Op.getOperand(1);
4533 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004534 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004535 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004536 DAG.getIntPtrConstant(0));
4537 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004538 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004539 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004540 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004541 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004542}
4543
Bob Wilson626613d2010-11-23 19:38:38 +00004544/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4545/// element has been zero/sign-extended, depending on the isSigned parameter,
4546/// from an integer type half its size.
4547static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4548 bool isSigned) {
4549 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4550 EVT VT = N->getValueType(0);
4551 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4552 SDNode *BVN = N->getOperand(0).getNode();
4553 if (BVN->getValueType(0) != MVT::v4i32 ||
4554 BVN->getOpcode() != ISD::BUILD_VECTOR)
4555 return false;
4556 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4557 unsigned HiElt = 1 - LoElt;
4558 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4559 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4560 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4561 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4562 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4563 return false;
4564 if (isSigned) {
4565 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4566 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4567 return true;
4568 } else {
4569 if (Hi0->isNullValue() && Hi1->isNullValue())
4570 return true;
4571 }
4572 return false;
4573 }
4574
4575 if (N->getOpcode() != ISD::BUILD_VECTOR)
4576 return false;
4577
4578 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4579 SDNode *Elt = N->getOperand(i).getNode();
4580 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4581 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4582 unsigned HalfSize = EltSize / 2;
4583 if (isSigned) {
Bob Wilson9d45de22011-10-18 18:46:49 +00004584 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004585 return false;
4586 } else {
Bob Wilson9d45de22011-10-18 18:46:49 +00004587 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004588 return false;
4589 }
4590 continue;
4591 }
4592 return false;
4593 }
4594
4595 return true;
4596}
4597
4598/// isSignExtended - Check if a node is a vector value that is sign-extended
4599/// or a constant BUILD_VECTOR with sign-extended elements.
4600static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4601 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4602 return true;
4603 if (isExtendedBUILD_VECTOR(N, DAG, true))
4604 return true;
4605 return false;
4606}
4607
4608/// isZeroExtended - Check if a node is a vector value that is zero-extended
4609/// or a constant BUILD_VECTOR with zero-extended elements.
4610static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4611 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4612 return true;
4613 if (isExtendedBUILD_VECTOR(N, DAG, false))
4614 return true;
4615 return false;
4616}
4617
4618/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4619/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004620static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4621 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4622 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004623 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4624 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4625 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004626 LD->isNonTemporal(), LD->isInvariant(),
4627 LD->getAlignment());
Bob Wilson626613d2010-11-23 19:38:38 +00004628 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4629 // have been legalized as a BITCAST from v4i32.
4630 if (N->getOpcode() == ISD::BITCAST) {
4631 SDNode *BVN = N->getOperand(0).getNode();
4632 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4633 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4634 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4635 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4636 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4637 }
4638 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4639 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4640 EVT VT = N->getValueType(0);
4641 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4642 unsigned NumElts = VT.getVectorNumElements();
4643 MVT TruncVT = MVT::getIntegerVT(EltSize);
4644 SmallVector<SDValue, 8> Ops;
4645 for (unsigned i = 0; i != NumElts; ++i) {
4646 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4647 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004648 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004649 }
4650 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4651 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004652}
4653
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004654static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4655 unsigned Opcode = N->getOpcode();
4656 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4657 SDNode *N0 = N->getOperand(0).getNode();
4658 SDNode *N1 = N->getOperand(1).getNode();
4659 return N0->hasOneUse() && N1->hasOneUse() &&
4660 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4661 }
4662 return false;
4663}
4664
4665static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4666 unsigned Opcode = N->getOpcode();
4667 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4668 SDNode *N0 = N->getOperand(0).getNode();
4669 SDNode *N1 = N->getOperand(1).getNode();
4670 return N0->hasOneUse() && N1->hasOneUse() &&
4671 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4672 }
4673 return false;
4674}
4675
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004676static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4677 // Multiplications are only custom-lowered for 128-bit vectors so that
4678 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4679 EVT VT = Op.getValueType();
4680 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4681 SDNode *N0 = Op.getOperand(0).getNode();
4682 SDNode *N1 = Op.getOperand(1).getNode();
4683 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004684 bool isMLA = false;
4685 bool isN0SExt = isSignExtended(N0, DAG);
4686 bool isN1SExt = isSignExtended(N1, DAG);
4687 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004688 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004689 else {
4690 bool isN0ZExt = isZeroExtended(N0, DAG);
4691 bool isN1ZExt = isZeroExtended(N1, DAG);
4692 if (isN0ZExt && isN1ZExt)
4693 NewOpc = ARMISD::VMULLu;
4694 else if (isN1SExt || isN1ZExt) {
4695 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4696 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4697 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4698 NewOpc = ARMISD::VMULLs;
4699 isMLA = true;
4700 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4701 NewOpc = ARMISD::VMULLu;
4702 isMLA = true;
4703 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4704 std::swap(N0, N1);
4705 NewOpc = ARMISD::VMULLu;
4706 isMLA = true;
4707 }
4708 }
4709
4710 if (!NewOpc) {
4711 if (VT == MVT::v2i64)
4712 // Fall through to expand this. It is not legal.
4713 return SDValue();
4714 else
4715 // Other vector multiplications are legal.
4716 return Op;
4717 }
4718 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004719
4720 // Legalize to a VMULL instruction.
4721 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004722 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004723 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004724 if (!isMLA) {
4725 Op0 = SkipExtension(N0, DAG);
4726 assert(Op0.getValueType().is64BitVector() &&
4727 Op1.getValueType().is64BitVector() &&
4728 "unexpected types for extended operands to VMULL");
4729 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4730 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004731
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004732 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4733 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4734 // vmull q0, d4, d6
4735 // vmlal q0, d5, d6
4736 // is faster than
4737 // vaddl q0, d4, d5
4738 // vmovl q1, d6
4739 // vmul q0, q0, q1
4740 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4741 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4742 EVT Op1VT = Op1.getValueType();
4743 return DAG.getNode(N0->getOpcode(), DL, VT,
4744 DAG.getNode(NewOpc, DL, VT,
4745 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4746 DAG.getNode(NewOpc, DL, VT,
4747 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004748}
4749
Owen Anderson76706012011-04-05 21:48:57 +00004750static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004751LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4752 // Convert to float
4753 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4754 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4755 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4756 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4757 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4758 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4759 // Get reciprocal estimate.
4760 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004761 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004762 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4763 // Because char has a smaller range than uchar, we can actually get away
4764 // without any newton steps. This requires that we use a weird bias
4765 // of 0xb000, however (again, this has been exhaustively tested).
4766 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4767 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4768 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4769 Y = DAG.getConstant(0xb000, MVT::i32);
4770 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4771 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4772 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4773 // Convert back to short.
4774 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4775 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4776 return X;
4777}
4778
Owen Anderson76706012011-04-05 21:48:57 +00004779static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004780LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4781 SDValue N2;
4782 // Convert to float.
4783 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4784 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4785 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4786 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4787 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4788 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004789
Nate Begeman7973f352011-02-11 20:53:29 +00004790 // Use reciprocal estimate and one refinement step.
4791 // float4 recip = vrecpeq_f32(yf);
4792 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004793 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004794 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004795 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004796 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4797 N1, N2);
4798 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4799 // Because short has a smaller range than ushort, we can actually get away
4800 // with only a single newton step. This requires that we use a weird bias
4801 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004802 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004803 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4804 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004805 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004806 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4807 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4808 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4809 // Convert back to integer and return.
4810 // return vmovn_s32(vcvt_s32_f32(result));
4811 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4812 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4813 return N0;
4814}
4815
4816static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4817 EVT VT = Op.getValueType();
4818 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4819 "unexpected type for custom-lowering ISD::SDIV");
4820
4821 DebugLoc dl = Op.getDebugLoc();
4822 SDValue N0 = Op.getOperand(0);
4823 SDValue N1 = Op.getOperand(1);
4824 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004825
Nate Begeman7973f352011-02-11 20:53:29 +00004826 if (VT == MVT::v8i8) {
4827 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4828 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004829
Nate Begeman7973f352011-02-11 20:53:29 +00004830 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4831 DAG.getIntPtrConstant(4));
4832 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004833 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004834 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4835 DAG.getIntPtrConstant(0));
4836 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4837 DAG.getIntPtrConstant(0));
4838
4839 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4840 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4841
4842 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4843 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004844
Nate Begeman7973f352011-02-11 20:53:29 +00004845 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
4846 return N0;
4847 }
4848 return LowerSDIV_v4i16(N0, N1, dl, DAG);
4849}
4850
4851static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
4852 EVT VT = Op.getValueType();
4853 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4854 "unexpected type for custom-lowering ISD::UDIV");
4855
4856 DebugLoc dl = Op.getDebugLoc();
4857 SDValue N0 = Op.getOperand(0);
4858 SDValue N1 = Op.getOperand(1);
4859 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004860
Nate Begeman7973f352011-02-11 20:53:29 +00004861 if (VT == MVT::v8i8) {
4862 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
4863 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004864
Nate Begeman7973f352011-02-11 20:53:29 +00004865 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4866 DAG.getIntPtrConstant(4));
4867 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004868 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004869 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4870 DAG.getIntPtrConstant(0));
4871 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4872 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00004873
Nate Begeman7973f352011-02-11 20:53:29 +00004874 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
4875 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00004876
Nate Begeman7973f352011-02-11 20:53:29 +00004877 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4878 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004879
4880 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00004881 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
4882 N0);
4883 return N0;
4884 }
Owen Anderson76706012011-04-05 21:48:57 +00004885
Nate Begeman7973f352011-02-11 20:53:29 +00004886 // v4i16 sdiv ... Convert to float.
4887 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
4888 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
4889 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
4890 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
4891 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004892 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00004893
4894 // Use reciprocal estimate and two refinement steps.
4895 // float4 recip = vrecpeq_f32(yf);
4896 // recip *= vrecpsq_f32(yf, recip);
4897 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004898 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004899 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00004900 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004901 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004902 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004903 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00004904 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004905 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004906 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004907 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4908 // Simply multiplying by the reciprocal estimate can leave us a few ulps
4909 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
4910 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004911 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00004912 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4913 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4914 N1 = DAG.getConstant(2, MVT::i32);
4915 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4916 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4917 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4918 // Convert back to integer and return.
4919 // return vmovn_u32(vcvt_s32_f32(result));
4920 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4921 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4922 return N0;
4923}
4924
Evan Cheng342e3162011-08-30 01:34:54 +00004925static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
4926 EVT VT = Op.getNode()->getValueType(0);
4927 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
4928
4929 unsigned Opc;
4930 bool ExtraOp = false;
4931 switch (Op.getOpcode()) {
4932 default: assert(0 && "Invalid code");
4933 case ISD::ADDC: Opc = ARMISD::ADDC; break;
4934 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
4935 case ISD::SUBC: Opc = ARMISD::SUBC; break;
4936 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
4937 }
4938
4939 if (!ExtraOp)
4940 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
4941 Op.getOperand(1));
4942 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
4943 Op.getOperand(1), Op.getOperand(2));
4944}
4945
Eli Friedman74bf18c2011-09-15 22:26:18 +00004946static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedman7cc15662011-09-15 22:18:49 +00004947 // Monotonic load/store is legal for all targets
4948 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
4949 return Op;
4950
4951 // Aquire/Release load/store is not legal for targets without a
4952 // dmb or equivalent available.
4953 return SDValue();
4954}
4955
4956
Eli Friedman2bdffe42011-08-31 00:31:29 +00004957static void
Eli Friedman4d3f3292011-08-31 17:52:22 +00004958ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
4959 SelectionDAG &DAG, unsigned NewOp) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00004960 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +00004961 assert (Node->getValueType(0) == MVT::i64 &&
4962 "Only know how to expand i64 atomics");
Eli Friedman2bdffe42011-08-31 00:31:29 +00004963
Eli Friedman4d3f3292011-08-31 17:52:22 +00004964 SmallVector<SDValue, 6> Ops;
4965 Ops.push_back(Node->getOperand(0)); // Chain
4966 Ops.push_back(Node->getOperand(1)); // Ptr
4967 // Low part of Val1
4968 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4969 Node->getOperand(2), DAG.getIntPtrConstant(0)));
4970 // High part of Val1
4971 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4972 Node->getOperand(2), DAG.getIntPtrConstant(1)));
Andrew Trick3af7a672011-09-20 03:06:13 +00004973 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) {
Eli Friedman4d3f3292011-08-31 17:52:22 +00004974 // High part of Val1
4975 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4976 Node->getOperand(3), DAG.getIntPtrConstant(0)));
4977 // High part of Val2
4978 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4979 Node->getOperand(3), DAG.getIntPtrConstant(1)));
4980 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00004981 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
4982 SDValue Result =
Eli Friedman4d3f3292011-08-31 17:52:22 +00004983 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64,
Eli Friedman2bdffe42011-08-31 00:31:29 +00004984 cast<MemSDNode>(Node)->getMemOperand());
Eli Friedman4d3f3292011-08-31 17:52:22 +00004985 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) };
Eli Friedman2bdffe42011-08-31 00:31:29 +00004986 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
4987 Results.push_back(Result.getValue(2));
4988}
4989
Dan Gohmand858e902010-04-17 15:26:15 +00004990SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004991 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004992 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00004993 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00004994 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004995 case ISD::GlobalAddress:
4996 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
4997 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00004998 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00004999 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00005000 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
5001 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005002 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00005003 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00005004 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00005005 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00005006 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00005007 case ISD::SINT_TO_FP:
5008 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
5009 case ISD::FP_TO_SINT:
5010 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005011 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00005012 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00005013 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005014 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00005015 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00005016 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00005017 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
5018 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00005019 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005020 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00005021 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00005022 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00005023 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00005024 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00005025 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00005026 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Duncan Sands28b77e92011-09-06 19:07:46 +00005027 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00005028 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005029 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedman5c89cb82011-10-24 23:08:52 +00005030 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005031 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00005032 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00005033 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005034 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00005035 case ISD::SDIV: return LowerSDIV(Op, DAG);
5036 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00005037 case ISD::ADDC:
5038 case ISD::ADDE:
5039 case ISD::SUBC:
5040 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Eli Friedman7cc15662011-09-15 22:18:49 +00005041 case ISD::ATOMIC_LOAD:
Eli Friedman74bf18c2011-09-15 22:26:18 +00005042 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005043 }
Dan Gohman475871a2008-07-27 21:46:04 +00005044 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00005045}
5046
Duncan Sands1607f052008-12-01 11:39:25 +00005047/// ReplaceNodeResults - Replace the results of node with an illegal result
5048/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00005049void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
5050 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005051 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00005052 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00005053 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00005054 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005055 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00005056 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005057 case ISD::BITCAST:
5058 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005059 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005060 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00005061 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00005062 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005063 break;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005064 case ISD::ATOMIC_LOAD_ADD:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005065 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005066 return;
5067 case ISD::ATOMIC_LOAD_AND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005068 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005069 return;
5070 case ISD::ATOMIC_LOAD_NAND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005071 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005072 return;
5073 case ISD::ATOMIC_LOAD_OR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005074 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005075 return;
5076 case ISD::ATOMIC_LOAD_SUB:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005077 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005078 return;
5079 case ISD::ATOMIC_LOAD_XOR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005080 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005081 return;
5082 case ISD::ATOMIC_SWAP:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005083 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005084 return;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005085 case ISD::ATOMIC_CMP_SWAP:
5086 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG);
5087 return;
Duncan Sands1607f052008-12-01 11:39:25 +00005088 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00005089 if (Res.getNode())
5090 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00005091}
Chris Lattner27a6c732007-11-24 07:07:01 +00005092
Evan Chenga8e29892007-01-19 07:51:42 +00005093//===----------------------------------------------------------------------===//
5094// ARM Scheduler Hooks
5095//===----------------------------------------------------------------------===//
5096
5097MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005098ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
5099 MachineBasicBlock *BB,
5100 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005101 unsigned dest = MI->getOperand(0).getReg();
5102 unsigned ptr = MI->getOperand(1).getReg();
5103 unsigned oldval = MI->getOperand(2).getReg();
5104 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005105 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5106 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005107 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005108
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005109 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5110 unsigned scratch =
Cameron Zwarich141ec632011-05-18 02:29:50 +00005111 MRI.createVirtualRegister(isThumb2 ? ARM::rGPRRegisterClass
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005112 : ARM::GPRRegisterClass);
5113
5114 if (isThumb2) {
Cameron Zwarich141ec632011-05-18 02:29:50 +00005115 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5116 MRI.constrainRegClass(oldval, ARM::rGPRRegisterClass);
5117 MRI.constrainRegClass(newval, ARM::rGPRRegisterClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005118 }
5119
Jim Grosbach5278eb82009-12-11 01:42:04 +00005120 unsigned ldrOpc, strOpc;
5121 switch (Size) {
5122 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005123 case 1:
5124 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00005125 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005126 break;
5127 case 2:
5128 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5129 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5130 break;
5131 case 4:
5132 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5133 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5134 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00005135 }
5136
5137 MachineFunction *MF = BB->getParent();
5138 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5139 MachineFunction::iterator It = BB;
5140 ++It; // insert the new blocks after the current block
5141
5142 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5143 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5144 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5145 MF->insert(It, loop1MBB);
5146 MF->insert(It, loop2MBB);
5147 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005148
5149 // Transfer the remainder of BB and its successor edges to exitMBB.
5150 exitMBB->splice(exitMBB->begin(), BB,
5151 llvm::next(MachineBasicBlock::iterator(MI)),
5152 BB->end());
5153 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005154
5155 // thisMBB:
5156 // ...
5157 // fallthrough --> loop1MBB
5158 BB->addSuccessor(loop1MBB);
5159
5160 // loop1MBB:
5161 // ldrex dest, [ptr]
5162 // cmp dest, oldval
5163 // bne exitMBB
5164 BB = loop1MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005165 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5166 if (ldrOpc == ARM::t2LDREX)
5167 MIB.addImm(0);
5168 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005169 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005170 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005171 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5172 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005173 BB->addSuccessor(loop2MBB);
5174 BB->addSuccessor(exitMBB);
5175
5176 // loop2MBB:
5177 // strex scratch, newval, [ptr]
5178 // cmp scratch, #0
5179 // bne loop1MBB
5180 BB = loop2MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005181 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr);
5182 if (strOpc == ARM::t2STREX)
5183 MIB.addImm(0);
5184 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005185 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005186 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005187 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5188 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005189 BB->addSuccessor(loop1MBB);
5190 BB->addSuccessor(exitMBB);
5191
5192 // exitMBB:
5193 // ...
5194 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005195
Dan Gohman14152b42010-07-06 20:24:04 +00005196 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005197
Jim Grosbach5278eb82009-12-11 01:42:04 +00005198 return BB;
5199}
5200
5201MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005202ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5203 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005204 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5205 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5206
5207 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005208 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005209 MachineFunction::iterator It = BB;
5210 ++It;
5211
5212 unsigned dest = MI->getOperand(0).getReg();
5213 unsigned ptr = MI->getOperand(1).getReg();
5214 unsigned incr = MI->getOperand(2).getReg();
5215 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005216 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005217
5218 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5219 if (isThumb2) {
5220 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5221 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5222 }
5223
Jim Grosbachc3c23542009-12-14 04:22:04 +00005224 unsigned ldrOpc, strOpc;
5225 switch (Size) {
5226 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005227 case 1:
5228 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005229 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005230 break;
5231 case 2:
5232 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5233 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5234 break;
5235 case 4:
5236 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5237 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5238 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005239 }
5240
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005241 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5242 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5243 MF->insert(It, loopMBB);
5244 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005245
5246 // Transfer the remainder of BB and its successor edges to exitMBB.
5247 exitMBB->splice(exitMBB->begin(), BB,
5248 llvm::next(MachineBasicBlock::iterator(MI)),
5249 BB->end());
5250 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005251
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005252 TargetRegisterClass *TRC =
5253 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5254 unsigned scratch = MRI.createVirtualRegister(TRC);
5255 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005256
5257 // thisMBB:
5258 // ...
5259 // fallthrough --> loopMBB
5260 BB->addSuccessor(loopMBB);
5261
5262 // loopMBB:
5263 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005264 // <binop> scratch2, dest, incr
5265 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005266 // cmp scratch, #0
5267 // bne- loopMBB
5268 // fallthrough --> exitMBB
5269 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005270 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5271 if (ldrOpc == ARM::t2LDREX)
5272 MIB.addImm(0);
5273 AddDefaultPred(MIB);
Jim Grosbachc67b5562009-12-15 00:12:35 +00005274 if (BinOpcode) {
5275 // operand order needs to go the other way for NAND
5276 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5277 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5278 addReg(incr).addReg(dest)).addReg(0);
5279 else
5280 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5281 addReg(dest).addReg(incr)).addReg(0);
5282 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005283
Jim Grosbachb6aed502011-09-09 18:37:27 +00005284 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5285 if (strOpc == ARM::t2STREX)
5286 MIB.addImm(0);
5287 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005288 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005289 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005290 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5291 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005292
5293 BB->addSuccessor(loopMBB);
5294 BB->addSuccessor(exitMBB);
5295
5296 // exitMBB:
5297 // ...
5298 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005299
Dan Gohman14152b42010-07-06 20:24:04 +00005300 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005301
Jim Grosbachc3c23542009-12-14 04:22:04 +00005302 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005303}
5304
Jim Grosbachf7da8822011-04-26 19:44:18 +00005305MachineBasicBlock *
5306ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5307 MachineBasicBlock *BB,
5308 unsigned Size,
5309 bool signExtend,
5310 ARMCC::CondCodes Cond) const {
5311 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5312
5313 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5314 MachineFunction *MF = BB->getParent();
5315 MachineFunction::iterator It = BB;
5316 ++It;
5317
5318 unsigned dest = MI->getOperand(0).getReg();
5319 unsigned ptr = MI->getOperand(1).getReg();
5320 unsigned incr = MI->getOperand(2).getReg();
5321 unsigned oldval = dest;
5322 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005323 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005324
5325 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5326 if (isThumb2) {
5327 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5328 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5329 }
5330
Jim Grosbachf7da8822011-04-26 19:44:18 +00005331 unsigned ldrOpc, strOpc, extendOpc;
5332 switch (Size) {
5333 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5334 case 1:
5335 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5336 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005337 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005338 break;
5339 case 2:
5340 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5341 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005342 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005343 break;
5344 case 4:
5345 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5346 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5347 extendOpc = 0;
5348 break;
5349 }
5350
5351 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5352 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5353 MF->insert(It, loopMBB);
5354 MF->insert(It, exitMBB);
5355
5356 // Transfer the remainder of BB and its successor edges to exitMBB.
5357 exitMBB->splice(exitMBB->begin(), BB,
5358 llvm::next(MachineBasicBlock::iterator(MI)),
5359 BB->end());
5360 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5361
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005362 TargetRegisterClass *TRC =
5363 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5364 unsigned scratch = MRI.createVirtualRegister(TRC);
5365 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005366
5367 // thisMBB:
5368 // ...
5369 // fallthrough --> loopMBB
5370 BB->addSuccessor(loopMBB);
5371
5372 // loopMBB:
5373 // ldrex dest, ptr
5374 // (sign extend dest, if required)
5375 // cmp dest, incr
5376 // cmov.cond scratch2, dest, incr
5377 // strex scratch, scratch2, ptr
5378 // cmp scratch, #0
5379 // bne- loopMBB
5380 // fallthrough --> exitMBB
5381 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005382 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5383 if (ldrOpc == ARM::t2LDREX)
5384 MIB.addImm(0);
5385 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005386
5387 // Sign extend the value, if necessary.
5388 if (signExtend && extendOpc) {
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005389 oldval = MRI.createVirtualRegister(ARM::GPRRegisterClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005390 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5391 .addReg(dest)
5392 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005393 }
5394
5395 // Build compare and cmov instructions.
5396 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5397 .addReg(oldval).addReg(incr));
5398 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5399 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5400
Jim Grosbachb6aed502011-09-09 18:37:27 +00005401 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5402 if (strOpc == ARM::t2STREX)
5403 MIB.addImm(0);
5404 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005405 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5406 .addReg(scratch).addImm(0));
5407 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5408 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5409
5410 BB->addSuccessor(loopMBB);
5411 BB->addSuccessor(exitMBB);
5412
5413 // exitMBB:
5414 // ...
5415 BB = exitMBB;
5416
5417 MI->eraseFromParent(); // The instruction is gone now.
5418
5419 return BB;
5420}
5421
Eli Friedman2bdffe42011-08-31 00:31:29 +00005422MachineBasicBlock *
5423ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
5424 unsigned Op1, unsigned Op2,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005425 bool NeedsCarry, bool IsCmpxchg) const {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005426 // This also handles ATOMIC_SWAP, indicated by Op1==0.
5427 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5428
5429 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5430 MachineFunction *MF = BB->getParent();
5431 MachineFunction::iterator It = BB;
5432 ++It;
5433
5434 unsigned destlo = MI->getOperand(0).getReg();
5435 unsigned desthi = MI->getOperand(1).getReg();
5436 unsigned ptr = MI->getOperand(2).getReg();
5437 unsigned vallo = MI->getOperand(3).getReg();
5438 unsigned valhi = MI->getOperand(4).getReg();
5439 DebugLoc dl = MI->getDebugLoc();
5440 bool isThumb2 = Subtarget->isThumb2();
5441
5442 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5443 if (isThumb2) {
5444 MRI.constrainRegClass(destlo, ARM::rGPRRegisterClass);
5445 MRI.constrainRegClass(desthi, ARM::rGPRRegisterClass);
5446 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5447 }
5448
5449 unsigned ldrOpc = isThumb2 ? ARM::t2LDREXD : ARM::LDREXD;
5450 unsigned strOpc = isThumb2 ? ARM::t2STREXD : ARM::STREXD;
5451
5452 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman7df496d2011-09-01 22:27:41 +00005453 MachineBasicBlock *contBB = 0, *cont2BB = 0;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005454 if (IsCmpxchg) {
5455 contBB = MF->CreateMachineBasicBlock(LLVM_BB);
5456 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB);
5457 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005458 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5459 MF->insert(It, loopMBB);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005460 if (IsCmpxchg) {
5461 MF->insert(It, contBB);
5462 MF->insert(It, cont2BB);
5463 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005464 MF->insert(It, exitMBB);
5465
5466 // Transfer the remainder of BB and its successor edges to exitMBB.
5467 exitMBB->splice(exitMBB->begin(), BB,
5468 llvm::next(MachineBasicBlock::iterator(MI)),
5469 BB->end());
5470 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5471
5472 TargetRegisterClass *TRC =
5473 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5474 unsigned storesuccess = MRI.createVirtualRegister(TRC);
5475
5476 // thisMBB:
5477 // ...
5478 // fallthrough --> loopMBB
5479 BB->addSuccessor(loopMBB);
5480
5481 // loopMBB:
5482 // ldrexd r2, r3, ptr
5483 // <binopa> r0, r2, incr
5484 // <binopb> r1, r3, incr
5485 // strexd storesuccess, r0, r1, ptr
5486 // cmp storesuccess, #0
5487 // bne- loopMBB
5488 // fallthrough --> exitMBB
5489 //
5490 // Note that the registers are explicitly specified because there is not any
5491 // way to force the register allocator to allocate a register pair.
5492 //
Andrew Trick3af7a672011-09-20 03:06:13 +00005493 // FIXME: The hardcoded registers are not necessary for Thumb2, but we
Eli Friedman2bdffe42011-08-31 00:31:29 +00005494 // need to properly enforce the restriction that the two output registers
5495 // for ldrexd must be different.
5496 BB = loopMBB;
5497 // Load
5498 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc))
5499 .addReg(ARM::R2, RegState::Define)
5500 .addReg(ARM::R3, RegState::Define).addReg(ptr));
5501 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
5502 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo).addReg(ARM::R2);
5503 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi).addReg(ARM::R3);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005504
5505 if (IsCmpxchg) {
5506 // Add early exit
5507 for (unsigned i = 0; i < 2; i++) {
5508 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr :
5509 ARM::CMPrr))
5510 .addReg(i == 0 ? destlo : desthi)
5511 .addReg(i == 0 ? vallo : valhi));
5512 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5513 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5514 BB->addSuccessor(exitMBB);
5515 BB->addSuccessor(i == 0 ? contBB : cont2BB);
5516 BB = (i == 0 ? contBB : cont2BB);
5517 }
5518
5519 // Copy to physregs for strexd
5520 unsigned setlo = MI->getOperand(5).getReg();
5521 unsigned sethi = MI->getOperand(6).getReg();
5522 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(setlo);
5523 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(sethi);
5524 } else if (Op1) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005525 // Perform binary operation
5526 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), ARM::R0)
5527 .addReg(destlo).addReg(vallo))
5528 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
5529 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), ARM::R1)
5530 .addReg(desthi).addReg(valhi)).addReg(0);
5531 } else {
5532 // Copy to physregs for strexd
5533 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(vallo);
5534 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(valhi);
5535 }
5536
5537 // Store
5538 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), storesuccess)
5539 .addReg(ARM::R0).addReg(ARM::R1).addReg(ptr));
5540 // Cmp+jump
5541 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5542 .addReg(storesuccess).addImm(0));
5543 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5544 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5545
5546 BB->addSuccessor(loopMBB);
5547 BB->addSuccessor(exitMBB);
5548
5549 // exitMBB:
5550 // ...
5551 BB = exitMBB;
5552
5553 MI->eraseFromParent(); // The instruction is gone now.
5554
5555 return BB;
5556}
5557
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005558/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
5559/// registers the function context.
5560void ARMTargetLowering::
5561SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
5562 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005563 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5564 DebugLoc dl = MI->getDebugLoc();
5565 MachineFunction *MF = MBB->getParent();
5566 MachineRegisterInfo *MRI = &MF->getRegInfo();
5567 MachineConstantPool *MCP = MF->getConstantPool();
5568 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5569 const Function *F = MF->getFunction();
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005570
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005571 bool isThumb = Subtarget->isThumb();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005572 bool isThumb2 = Subtarget->isThumb2();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005573
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005574 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005575 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005576 ARMConstantPoolValue *CPV =
5577 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
5578 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
5579
5580 const TargetRegisterClass *TRC =
5581 isThumb ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5582
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005583 // Grab constant pool and fixed stack memory operands.
5584 MachineMemOperand *CPMMO =
5585 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
5586 MachineMemOperand::MOLoad, 4, 4);
5587
5588 MachineMemOperand *FIMMOSt =
5589 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
5590 MachineMemOperand::MOStore, 4, 4);
5591
5592 // Load the address of the dispatch MBB into the jump buffer.
5593 if (isThumb2) {
5594 // Incoming value: jbuf
5595 // ldr.n r5, LCPI1_1
5596 // orr r5, r5, #1
5597 // add r5, pc
5598 // str r5, [$jbuf, #+4] ; &jbuf[1]
5599 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5600 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
5601 .addConstantPoolIndex(CPI)
5602 .addMemOperand(CPMMO));
5603 // Set the low bit because of thumb mode.
5604 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5605 AddDefaultCC(
5606 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
5607 .addReg(NewVReg1, RegState::Kill)
5608 .addImm(0x01)));
5609 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5610 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
5611 .addReg(NewVReg2, RegState::Kill)
5612 .addImm(PCLabelId);
5613 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
5614 .addReg(NewVReg3, RegState::Kill)
5615 .addFrameIndex(FI)
5616 .addImm(36) // &jbuf[1] :: pc
5617 .addMemOperand(FIMMOSt));
5618 } else if (isThumb) {
5619 // Incoming value: jbuf
5620 // ldr.n r1, LCPI1_4
5621 // add r1, pc
5622 // mov r2, #1
5623 // orrs r1, r2
5624 // add r2, $jbuf, #+4 ; &jbuf[1]
5625 // str r1, [r2]
5626 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5627 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
5628 .addConstantPoolIndex(CPI)
5629 .addMemOperand(CPMMO));
5630 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5631 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
5632 .addReg(NewVReg1, RegState::Kill)
5633 .addImm(PCLabelId);
5634 // Set the low bit because of thumb mode.
5635 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5636 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
5637 .addReg(ARM::CPSR, RegState::Define)
5638 .addImm(1));
5639 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5640 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
5641 .addReg(ARM::CPSR, RegState::Define)
5642 .addReg(NewVReg2, RegState::Kill)
5643 .addReg(NewVReg3, RegState::Kill));
5644 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5645 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
5646 .addFrameIndex(FI)
5647 .addImm(36)); // &jbuf[1] :: pc
5648 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
5649 .addReg(NewVReg4, RegState::Kill)
5650 .addReg(NewVReg5, RegState::Kill)
5651 .addImm(0)
5652 .addMemOperand(FIMMOSt));
5653 } else {
5654 // Incoming value: jbuf
5655 // ldr r1, LCPI1_1
5656 // add r1, pc, r1
5657 // str r1, [$jbuf, #+4] ; &jbuf[1]
5658 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5659 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
5660 .addConstantPoolIndex(CPI)
5661 .addImm(0)
5662 .addMemOperand(CPMMO));
5663 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5664 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
5665 .addReg(NewVReg1, RegState::Kill)
5666 .addImm(PCLabelId));
5667 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
5668 .addReg(NewVReg2, RegState::Kill)
5669 .addFrameIndex(FI)
5670 .addImm(36) // &jbuf[1] :: pc
5671 .addMemOperand(FIMMOSt));
5672 }
5673}
5674
5675MachineBasicBlock *ARMTargetLowering::
5676EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
5677 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5678 DebugLoc dl = MI->getDebugLoc();
5679 MachineFunction *MF = MBB->getParent();
5680 MachineRegisterInfo *MRI = &MF->getRegInfo();
5681 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5682 MachineFrameInfo *MFI = MF->getFrameInfo();
5683 int FI = MFI->getFunctionContextIndex();
5684
5685 const TargetRegisterClass *TRC =
5686 Subtarget->isThumb() ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5687
Bill Wendling04f15b42011-10-06 21:29:56 +00005688 // Get a mapping of the call site numbers to all of the landing pads they're
5689 // associated with.
Bill Wendling2a850152011-10-05 00:02:33 +00005690 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
5691 unsigned MaxCSNum = 0;
5692 MachineModuleInfo &MMI = MF->getMMI();
5693 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E; ++BB) {
5694 if (!BB->isLandingPad()) continue;
5695
5696 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
5697 // pad.
5698 for (MachineBasicBlock::iterator
5699 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
5700 if (!II->isEHLabel()) continue;
5701
5702 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendling5cbef192011-10-05 23:28:57 +00005703 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling2a850152011-10-05 00:02:33 +00005704
Bill Wendling5cbef192011-10-05 23:28:57 +00005705 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
5706 for (SmallVectorImpl<unsigned>::iterator
5707 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
5708 CSI != CSE; ++CSI) {
5709 CallSiteNumToLPad[*CSI].push_back(BB);
5710 MaxCSNum = std::max(MaxCSNum, *CSI);
5711 }
Bill Wendling2a850152011-10-05 00:02:33 +00005712 break;
5713 }
5714 }
5715
5716 // Get an ordered list of the machine basic blocks for the jump table.
5717 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling2acf6382011-10-07 23:18:02 +00005718 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling2a850152011-10-05 00:02:33 +00005719 LPadList.reserve(CallSiteNumToLPad.size());
5720 for (unsigned I = 1; I <= MaxCSNum; ++I) {
5721 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
5722 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00005723 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling2a850152011-10-05 00:02:33 +00005724 LPadList.push_back(*II);
Bill Wendling2acf6382011-10-07 23:18:02 +00005725 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
5726 }
Bill Wendling2a850152011-10-05 00:02:33 +00005727 }
5728
Bill Wendling5cbef192011-10-05 23:28:57 +00005729 assert(!LPadList.empty() &&
5730 "No landing pad destinations for the dispatch jump table!");
5731
Bill Wendling04f15b42011-10-06 21:29:56 +00005732 // Create the jump table and associated information.
Bill Wendling2a850152011-10-05 00:02:33 +00005733 MachineJumpTableInfo *JTI =
5734 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
5735 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
5736 unsigned UId = AFI->createJumpTableUId();
5737
Bill Wendling04f15b42011-10-06 21:29:56 +00005738 // Create the MBBs for the dispatch code.
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005739
5740 // Shove the dispatch's address into the return slot in the function context.
5741 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
5742 DispatchBB->setIsLandingPad();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005743
Bill Wendlingbb734682011-10-05 00:39:32 +00005744 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Bill Wendling083a8eb2011-10-06 23:37:36 +00005745 BuildMI(TrapBB, dl, TII->get(Subtarget->isThumb() ? ARM::tTRAP : ARM::TRAP));
Bill Wendlingbb734682011-10-05 00:39:32 +00005746 DispatchBB->addSuccessor(TrapBB);
5747
5748 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
5749 DispatchBB->addSuccessor(DispContBB);
Bill Wendling2a850152011-10-05 00:02:33 +00005750
Bill Wendlinga48ed4f2011-10-17 21:32:56 +00005751 // Insert and MBBs.
Bill Wendling930193c2011-10-06 00:53:33 +00005752 MF->insert(MF->end(), DispatchBB);
5753 MF->insert(MF->end(), DispContBB);
5754 MF->insert(MF->end(), TrapBB);
Bill Wendling930193c2011-10-06 00:53:33 +00005755
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005756 // Insert code into the entry block that creates and registers the function
5757 // context.
5758 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
5759
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005760 MachineMemOperand *FIMMOLd =
Bill Wendling04f15b42011-10-06 21:29:56 +00005761 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendling083a8eb2011-10-06 23:37:36 +00005762 MachineMemOperand::MOLoad |
5763 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling930193c2011-10-06 00:53:33 +00005764
Bob Wilsoneaab6ef2011-11-16 07:11:57 +00005765 BuildMI(DispatchBB, dl, TII->get(ARM::eh_sjlj_dispatchsetup));
5766
Bill Wendling952cb502011-10-18 22:49:07 +00005767 unsigned NumLPads = LPadList.size();
Bill Wendling95ce2e92011-10-06 22:53:00 +00005768 if (Subtarget->isThumb2()) {
5769 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5770 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
5771 .addFrameIndex(FI)
5772 .addImm(4)
5773 .addMemOperand(FIMMOLd));
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005774
Bill Wendling952cb502011-10-18 22:49:07 +00005775 if (NumLPads < 256) {
5776 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
5777 .addReg(NewVReg1)
5778 .addImm(LPadList.size()));
5779 } else {
5780 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5781 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00005782 .addImm(NumLPads & 0xFFFF));
5783
5784 unsigned VReg2 = VReg1;
5785 if ((NumLPads & 0xFFFF0000) != 0) {
5786 VReg2 = MRI->createVirtualRegister(TRC);
5787 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
5788 .addReg(VReg1)
5789 .addImm(NumLPads >> 16));
5790 }
5791
Bill Wendling952cb502011-10-18 22:49:07 +00005792 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
5793 .addReg(NewVReg1)
5794 .addReg(VReg2));
5795 }
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005796
Bill Wendling95ce2e92011-10-06 22:53:00 +00005797 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
5798 .addMBB(TrapBB)
5799 .addImm(ARMCC::HI)
5800 .addReg(ARM::CPSR);
Bill Wendlingbb734682011-10-05 00:39:32 +00005801
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005802 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5803 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005804 .addJumpTableIndex(MJTI)
5805 .addImm(UId));
Bill Wendling2a850152011-10-05 00:02:33 +00005806
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005807 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005808 AddDefaultCC(
5809 AddDefaultPred(
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005810 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
5811 .addReg(NewVReg3, RegState::Kill)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005812 .addReg(NewVReg1)
5813 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
5814
5815 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005816 .addReg(NewVReg4, RegState::Kill)
Bill Wendling2a850152011-10-05 00:02:33 +00005817 .addReg(NewVReg1)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005818 .addJumpTableIndex(MJTI)
5819 .addImm(UId);
5820 } else if (Subtarget->isThumb()) {
Bill Wendling083a8eb2011-10-06 23:37:36 +00005821 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5822 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
5823 .addFrameIndex(FI)
5824 .addImm(1)
5825 .addMemOperand(FIMMOLd));
Bill Wendlingf1083d42011-10-07 22:08:37 +00005826
Bill Wendlinga5871dc2011-10-18 23:11:05 +00005827 if (NumLPads < 256) {
5828 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
5829 .addReg(NewVReg1)
5830 .addImm(NumLPads));
5831 } else {
5832 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling922ad782011-10-19 09:24:02 +00005833 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
5834 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
5835
5836 // MachineConstantPool wants an explicit alignment.
5837 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
5838 if (Align == 0)
5839 Align = getTargetData()->getTypeAllocSize(C->getType());
5840 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendlinga5871dc2011-10-18 23:11:05 +00005841
5842 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5843 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
5844 .addReg(VReg1, RegState::Define)
5845 .addConstantPoolIndex(Idx));
5846 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
5847 .addReg(NewVReg1)
5848 .addReg(VReg1));
5849 }
5850
Bill Wendling083a8eb2011-10-06 23:37:36 +00005851 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
5852 .addMBB(TrapBB)
5853 .addImm(ARMCC::HI)
5854 .addReg(ARM::CPSR);
5855
5856 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5857 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
5858 .addReg(ARM::CPSR, RegState::Define)
5859 .addReg(NewVReg1)
5860 .addImm(2));
5861
5862 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling217f0e92011-10-06 23:41:14 +00005863 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendling083a8eb2011-10-06 23:37:36 +00005864 .addJumpTableIndex(MJTI)
5865 .addImm(UId));
5866
5867 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5868 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
5869 .addReg(ARM::CPSR, RegState::Define)
5870 .addReg(NewVReg2, RegState::Kill)
5871 .addReg(NewVReg3));
5872
5873 MachineMemOperand *JTMMOLd =
5874 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
5875 MachineMemOperand::MOLoad, 4, 4);
5876
5877 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5878 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
5879 .addReg(NewVReg4, RegState::Kill)
5880 .addImm(0)
5881 .addMemOperand(JTMMOLd));
5882
5883 unsigned NewVReg6 = MRI->createVirtualRegister(TRC);
5884 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
5885 .addReg(ARM::CPSR, RegState::Define)
5886 .addReg(NewVReg5, RegState::Kill)
5887 .addReg(NewVReg3));
5888
5889 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
5890 .addReg(NewVReg6, RegState::Kill)
5891 .addJumpTableIndex(MJTI)
5892 .addImm(UId);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005893 } else {
5894 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5895 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
5896 .addFrameIndex(FI)
5897 .addImm(4)
5898 .addMemOperand(FIMMOLd));
Bill Wendling564392b2011-10-18 22:11:18 +00005899
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005900 if (NumLPads < 256) {
5901 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
5902 .addReg(NewVReg1)
5903 .addImm(NumLPads));
Bill Wendling922ad782011-10-19 09:24:02 +00005904 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005905 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5906 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00005907 .addImm(NumLPads & 0xFFFF));
5908
5909 unsigned VReg2 = VReg1;
5910 if ((NumLPads & 0xFFFF0000) != 0) {
5911 VReg2 = MRI->createVirtualRegister(TRC);
5912 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
5913 .addReg(VReg1)
5914 .addImm(NumLPads >> 16));
5915 }
5916
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005917 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
5918 .addReg(NewVReg1)
5919 .addReg(VReg2));
Bill Wendling922ad782011-10-19 09:24:02 +00005920 } else {
5921 MachineConstantPool *ConstantPool = MF->getConstantPool();
5922 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
5923 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
5924
5925 // MachineConstantPool wants an explicit alignment.
5926 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
5927 if (Align == 0)
5928 Align = getTargetData()->getTypeAllocSize(C->getType());
5929 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
5930
5931 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5932 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
5933 .addReg(VReg1, RegState::Define)
Bill Wendling767f8be2011-10-20 20:37:11 +00005934 .addConstantPoolIndex(Idx)
5935 .addImm(0));
Bill Wendling922ad782011-10-19 09:24:02 +00005936 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
5937 .addReg(NewVReg1)
5938 .addReg(VReg1, RegState::Kill));
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005939 }
5940
Bill Wendling95ce2e92011-10-06 22:53:00 +00005941 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
5942 .addMBB(TrapBB)
5943 .addImm(ARMCC::HI)
5944 .addReg(ARM::CPSR);
Bill Wendling2a850152011-10-05 00:02:33 +00005945
Bill Wendling564392b2011-10-18 22:11:18 +00005946 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005947 AddDefaultCC(
Bill Wendling564392b2011-10-18 22:11:18 +00005948 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005949 .addReg(NewVReg1)
5950 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling564392b2011-10-18 22:11:18 +00005951 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5952 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005953 .addJumpTableIndex(MJTI)
5954 .addImm(UId));
5955
5956 MachineMemOperand *JTMMOLd =
5957 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
5958 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling564392b2011-10-18 22:11:18 +00005959 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005960 AddDefaultPred(
Bill Wendling564392b2011-10-18 22:11:18 +00005961 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
5962 .addReg(NewVReg3, RegState::Kill)
5963 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005964 .addImm(0)
5965 .addMemOperand(JTMMOLd));
5966
5967 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
Bill Wendling564392b2011-10-18 22:11:18 +00005968 .addReg(NewVReg5, RegState::Kill)
5969 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005970 .addJumpTableIndex(MJTI)
5971 .addImm(UId);
5972 }
Bill Wendling2a850152011-10-05 00:02:33 +00005973
Bill Wendlingbb734682011-10-05 00:39:32 +00005974 // Add the jump table entries as successors to the MBB.
Bill Wendling2acf6382011-10-07 23:18:02 +00005975 MachineBasicBlock *PrevMBB = 0;
Bill Wendlingbb734682011-10-05 00:39:32 +00005976 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00005977 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
5978 MachineBasicBlock *CurMBB = *I;
5979 if (PrevMBB != CurMBB)
5980 DispContBB->addSuccessor(CurMBB);
5981 PrevMBB = CurMBB;
5982 }
5983
Bill Wendling24bb9252011-10-17 05:25:09 +00005984 // N.B. the order the invoke BBs are processed in doesn't matter here.
Bill Wendling969c9ef2011-10-14 23:34:37 +00005985 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
5986 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
5987 const unsigned *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendlingf7b02072011-10-18 18:30:49 +00005988 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling2acf6382011-10-07 23:18:02 +00005989 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
5990 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
5991 MachineBasicBlock *BB = *I;
Bill Wendling969c9ef2011-10-14 23:34:37 +00005992
5993 // Remove the landing pad successor from the invoke block and replace it
5994 // with the new dispatch block.
Bill Wendlingde39d862011-10-26 07:16:18 +00005995 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
5996 BB->succ_end());
5997 while (!Successors.empty()) {
5998 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling2acf6382011-10-07 23:18:02 +00005999 if (SMBB->isLandingPad()) {
6000 BB->removeSuccessor(SMBB);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006001 MBBLPads.push_back(SMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006002 }
6003 }
6004
6005 BB->addSuccessor(DispatchBB);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006006
6007 // Find the invoke call and mark all of the callee-saved registers as
6008 // 'implicit defined' so that they're spilled. This prevents code from
6009 // moving instructions to before the EH block, where they will never be
6010 // executed.
6011 for (MachineBasicBlock::reverse_iterator
6012 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
6013 if (!II->getDesc().isCall()) continue;
6014
6015 DenseMap<unsigned, bool> DefRegs;
6016 for (MachineInstr::mop_iterator
6017 OI = II->operands_begin(), OE = II->operands_end();
6018 OI != OE; ++OI) {
6019 if (!OI->isReg()) continue;
6020 DefRegs[OI->getReg()] = true;
6021 }
6022
6023 MachineInstrBuilder MIB(&*II);
6024
Bill Wendling5d798592011-10-14 23:55:44 +00006025 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006026 unsigned Reg = SavedRegs[i];
6027 if (Subtarget->isThumb2() &&
6028 !ARM::tGPRRegisterClass->contains(Reg) &&
6029 !ARM::hGPRRegisterClass->contains(Reg))
6030 continue;
6031 else if (Subtarget->isThumb1Only() &&
6032 !ARM::tGPRRegisterClass->contains(Reg))
6033 continue;
6034 else if (!Subtarget->isThumb() &&
6035 !ARM::GPRRegisterClass->contains(Reg))
6036 continue;
6037 if (!DefRegs[Reg])
6038 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling5d798592011-10-14 23:55:44 +00006039 }
Bill Wendling969c9ef2011-10-14 23:34:37 +00006040
6041 break;
6042 }
Bill Wendling2acf6382011-10-07 23:18:02 +00006043 }
Bill Wendlingbb734682011-10-05 00:39:32 +00006044
Bill Wendlingf7b02072011-10-18 18:30:49 +00006045 // Mark all former landing pads as non-landing pads. The dispatch is the only
6046 // landing pad now.
6047 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6048 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6049 (*I)->setIsLandingPad(false);
6050
Bill Wendlingbb734682011-10-05 00:39:32 +00006051 // The instruction is gone now.
6052 MI->eraseFromParent();
6053
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006054 return MBB;
6055}
6056
Evan Cheng218977b2010-07-13 19:27:42 +00006057static
6058MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6059 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6060 E = MBB->succ_end(); I != E; ++I)
6061 if (*I != Succ)
6062 return *I;
6063 llvm_unreachable("Expecting a BB with two successors!");
6064}
6065
Jim Grosbache801dc42009-12-12 01:40:06 +00006066MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006067ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00006068 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006069 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00006070 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006071 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00006072 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00006073 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00006074 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00006075 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00006076 }
Jim Grosbachee2c2a42011-09-16 21:55:56 +00006077 // The Thumb2 pre-indexed stores have the same MI operands, they just
6078 // define them differently in the .td files from the isel patterns, so
6079 // they need pseudos.
6080 case ARM::t2STR_preidx:
6081 MI->setDesc(TII->get(ARM::t2STR_PRE));
6082 return BB;
6083 case ARM::t2STRB_preidx:
6084 MI->setDesc(TII->get(ARM::t2STRB_PRE));
6085 return BB;
6086 case ARM::t2STRH_preidx:
6087 MI->setDesc(TII->get(ARM::t2STRH_PRE));
6088 return BB;
6089
Jim Grosbach19dec202011-08-05 20:35:44 +00006090 case ARM::STRi_preidx:
6091 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00006092 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00006093 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
6094 // Decode the offset.
6095 unsigned Offset = MI->getOperand(4).getImm();
6096 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
6097 Offset = ARM_AM::getAM2Offset(Offset);
6098 if (isSub)
6099 Offset = -Offset;
6100
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006101 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00006102 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00006103 .addOperand(MI->getOperand(0)) // Rn_wb
6104 .addOperand(MI->getOperand(1)) // Rt
6105 .addOperand(MI->getOperand(2)) // Rn
6106 .addImm(Offset) // offset (skip GPR==zero_reg)
6107 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006108 .addOperand(MI->getOperand(6))
6109 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00006110 MI->eraseFromParent();
6111 return BB;
6112 }
6113 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00006114 case ARM::STRBr_preidx:
6115 case ARM::STRH_preidx: {
6116 unsigned NewOpc;
6117 switch (MI->getOpcode()) {
6118 default: llvm_unreachable("unexpected opcode!");
6119 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
6120 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
6121 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
6122 }
Jim Grosbach19dec202011-08-05 20:35:44 +00006123 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
6124 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
6125 MIB.addOperand(MI->getOperand(i));
6126 MI->eraseFromParent();
6127 return BB;
6128 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006129 case ARM::ATOMIC_LOAD_ADD_I8:
6130 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6131 case ARM::ATOMIC_LOAD_ADD_I16:
6132 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6133 case ARM::ATOMIC_LOAD_ADD_I32:
6134 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006135
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006136 case ARM::ATOMIC_LOAD_AND_I8:
6137 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6138 case ARM::ATOMIC_LOAD_AND_I16:
6139 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6140 case ARM::ATOMIC_LOAD_AND_I32:
6141 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006142
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006143 case ARM::ATOMIC_LOAD_OR_I8:
6144 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6145 case ARM::ATOMIC_LOAD_OR_I16:
6146 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6147 case ARM::ATOMIC_LOAD_OR_I32:
6148 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006149
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006150 case ARM::ATOMIC_LOAD_XOR_I8:
6151 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6152 case ARM::ATOMIC_LOAD_XOR_I16:
6153 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6154 case ARM::ATOMIC_LOAD_XOR_I32:
6155 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006156
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006157 case ARM::ATOMIC_LOAD_NAND_I8:
6158 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6159 case ARM::ATOMIC_LOAD_NAND_I16:
6160 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6161 case ARM::ATOMIC_LOAD_NAND_I32:
6162 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006163
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006164 case ARM::ATOMIC_LOAD_SUB_I8:
6165 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6166 case ARM::ATOMIC_LOAD_SUB_I16:
6167 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6168 case ARM::ATOMIC_LOAD_SUB_I32:
6169 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006170
Jim Grosbachf7da8822011-04-26 19:44:18 +00006171 case ARM::ATOMIC_LOAD_MIN_I8:
6172 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
6173 case ARM::ATOMIC_LOAD_MIN_I16:
6174 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
6175 case ARM::ATOMIC_LOAD_MIN_I32:
6176 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
6177
6178 case ARM::ATOMIC_LOAD_MAX_I8:
6179 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
6180 case ARM::ATOMIC_LOAD_MAX_I16:
6181 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
6182 case ARM::ATOMIC_LOAD_MAX_I32:
6183 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
6184
6185 case ARM::ATOMIC_LOAD_UMIN_I8:
6186 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
6187 case ARM::ATOMIC_LOAD_UMIN_I16:
6188 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
6189 case ARM::ATOMIC_LOAD_UMIN_I32:
6190 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
6191
6192 case ARM::ATOMIC_LOAD_UMAX_I8:
6193 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
6194 case ARM::ATOMIC_LOAD_UMAX_I16:
6195 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
6196 case ARM::ATOMIC_LOAD_UMAX_I32:
6197 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
6198
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006199 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
6200 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
6201 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00006202
6203 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
6204 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
6205 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006206
Eli Friedman2bdffe42011-08-31 00:31:29 +00006207
6208 case ARM::ATOMADD6432:
6209 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006210 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr,
6211 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006212 case ARM::ATOMSUB6432:
6213 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006214 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6215 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006216 case ARM::ATOMOR6432:
6217 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006218 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006219 case ARM::ATOMXOR6432:
6220 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006221 isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006222 case ARM::ATOMAND6432:
6223 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006224 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006225 case ARM::ATOMSWAP6432:
6226 return EmitAtomicBinary64(MI, BB, 0, 0, false);
Eli Friedman4d3f3292011-08-31 17:52:22 +00006227 case ARM::ATOMCMPXCHG6432:
6228 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
6229 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6230 /*NeedsCarry*/ false, /*IsCmpxchg*/true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006231
Evan Cheng007ea272009-08-12 05:17:19 +00006232 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00006233 // To "insert" a SELECT_CC instruction, we actually have to insert the
6234 // diamond control-flow pattern. The incoming instruction knows the
6235 // destination vreg to set, the condition code register to branch on, the
6236 // true/false values to select between, and a branch opcode to use.
6237 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006238 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00006239 ++It;
6240
6241 // thisMBB:
6242 // ...
6243 // TrueVal = ...
6244 // cmpTY ccX, r1, r2
6245 // bCC copy1MBB
6246 // fallthrough --> copy0MBB
6247 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006248 MachineFunction *F = BB->getParent();
6249 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6250 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00006251 F->insert(It, copy0MBB);
6252 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006253
6254 // Transfer the remainder of BB and its successor edges to sinkMBB.
6255 sinkMBB->splice(sinkMBB->begin(), BB,
6256 llvm::next(MachineBasicBlock::iterator(MI)),
6257 BB->end());
6258 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6259
Dan Gohman258c58c2010-07-06 15:49:48 +00006260 BB->addSuccessor(copy0MBB);
6261 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00006262
Dan Gohman14152b42010-07-06 20:24:04 +00006263 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
6264 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
6265
Evan Chenga8e29892007-01-19 07:51:42 +00006266 // copy0MBB:
6267 // %FalseValue = ...
6268 // # fallthrough to sinkMBB
6269 BB = copy0MBB;
6270
6271 // Update machine-CFG edges
6272 BB->addSuccessor(sinkMBB);
6273
6274 // sinkMBB:
6275 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6276 // ...
6277 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00006278 BuildMI(*BB, BB->begin(), dl,
6279 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00006280 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6281 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6282
Dan Gohman14152b42010-07-06 20:24:04 +00006283 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00006284 return BB;
6285 }
Evan Cheng86198642009-08-07 00:34:42 +00006286
Evan Cheng218977b2010-07-13 19:27:42 +00006287 case ARM::BCCi64:
6288 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00006289 // If there is an unconditional branch to the other successor, remove it.
6290 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00006291
Evan Cheng218977b2010-07-13 19:27:42 +00006292 // Compare both parts that make up the double comparison separately for
6293 // equality.
6294 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
6295
6296 unsigned LHS1 = MI->getOperand(1).getReg();
6297 unsigned LHS2 = MI->getOperand(2).getReg();
6298 if (RHSisZero) {
6299 AddDefaultPred(BuildMI(BB, dl,
6300 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6301 .addReg(LHS1).addImm(0));
6302 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6303 .addReg(LHS2).addImm(0)
6304 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6305 } else {
6306 unsigned RHS1 = MI->getOperand(3).getReg();
6307 unsigned RHS2 = MI->getOperand(4).getReg();
6308 AddDefaultPred(BuildMI(BB, dl,
6309 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6310 .addReg(LHS1).addReg(RHS1));
6311 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6312 .addReg(LHS2).addReg(RHS2)
6313 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6314 }
6315
6316 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
6317 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
6318 if (MI->getOperand(0).getImm() == ARMCC::NE)
6319 std::swap(destMBB, exitMBB);
6320
6321 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6322 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson51f6a7a2011-09-09 21:48:23 +00006323 if (isThumb2)
6324 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
6325 else
6326 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng218977b2010-07-13 19:27:42 +00006327
6328 MI->eraseFromParent(); // The pseudo instruction is gone now.
6329 return BB;
6330 }
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006331
Bill Wendling5bc85282011-10-17 20:37:20 +00006332 case ARM::Int_eh_sjlj_setjmp:
6333 case ARM::Int_eh_sjlj_setjmp_nofp:
6334 case ARM::tInt_eh_sjlj_setjmp:
6335 case ARM::t2Int_eh_sjlj_setjmp:
6336 case ARM::t2Int_eh_sjlj_setjmp_nofp:
6337 EmitSjLjDispatchBlock(MI, BB);
6338 return BB;
6339
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006340 case ARM::ABS:
6341 case ARM::t2ABS: {
6342 // To insert an ABS instruction, we have to insert the
6343 // diamond control-flow pattern. The incoming instruction knows the
6344 // source vreg to test against 0, the destination vreg to set,
6345 // the condition code register to branch on, the
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006346 // true/false values to select between, and a branch opcode to use.
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006347 // It transforms
6348 // V1 = ABS V0
6349 // into
6350 // V2 = MOVS V0
6351 // BCC (branch to SinkBB if V0 >= 0)
6352 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006353 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006354 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6355 MachineFunction::iterator BBI = BB;
6356 ++BBI;
6357 MachineFunction *Fn = BB->getParent();
6358 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6359 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6360 Fn->insert(BBI, RSBBB);
6361 Fn->insert(BBI, SinkBB);
6362
6363 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
6364 unsigned int ABSDstReg = MI->getOperand(0).getReg();
6365 bool isThumb2 = Subtarget->isThumb2();
6366 MachineRegisterInfo &MRI = Fn->getRegInfo();
6367 // In Thumb mode S must not be specified if source register is the SP or
6368 // PC and if destination register is the SP, so restrict register class
6369 unsigned NewMovDstReg = MRI.createVirtualRegister(
6370 isThumb2 ? ARM::rGPRRegisterClass : ARM::GPRRegisterClass);
6371 unsigned NewRsbDstReg = MRI.createVirtualRegister(
6372 isThumb2 ? ARM::rGPRRegisterClass : ARM::GPRRegisterClass);
6373
6374 // Transfer the remainder of BB and its successor edges to sinkMBB.
6375 SinkBB->splice(SinkBB->begin(), BB,
6376 llvm::next(MachineBasicBlock::iterator(MI)),
6377 BB->end());
6378 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
6379
6380 BB->addSuccessor(RSBBB);
6381 BB->addSuccessor(SinkBB);
6382
6383 // fall through to SinkMBB
6384 RSBBB->addSuccessor(SinkBB);
6385
6386 // insert a movs at the end of BB
6387 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVr : ARM::MOVr),
6388 NewMovDstReg)
6389 .addReg(ABSSrcReg, RegState::Kill)
6390 .addImm((unsigned)ARMCC::AL).addReg(0)
6391 .addReg(ARM::CPSR, RegState::Define);
6392
6393 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006394 BuildMI(BB, dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006395 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
6396 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
6397
6398 // insert rsbri in RSBBB
6399 // Note: BCC and rsbri will be converted into predicated rsbmi
6400 // by if-conversion pass
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006401 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006402 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
6403 .addReg(NewMovDstReg, RegState::Kill)
6404 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
6405
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006406 // insert PHI in SinkBB,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006407 // reuse ABSDstReg to not change uses of ABS instruction
6408 BuildMI(*SinkBB, SinkBB->begin(), dl,
6409 TII->get(ARM::PHI), ABSDstReg)
6410 .addReg(NewRsbDstReg).addMBB(RSBBB)
6411 .addReg(NewMovDstReg).addMBB(BB);
6412
6413 // remove ABS instruction
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006414 MI->eraseFromParent();
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006415
6416 // return last added BB
6417 return SinkBB;
6418 }
Evan Chenga8e29892007-01-19 07:51:42 +00006419 }
6420}
6421
Evan Cheng37fefc22011-08-30 19:09:48 +00006422void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
6423 SDNode *Node) const {
Andrew Trick90b7b122011-10-18 19:18:52 +00006424 const MCInstrDesc *MCID = &MI->getDesc();
6425 if (!MCID->hasPostISelHook()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006426 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
6427 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
6428 return;
6429 }
6430
Andrew Trick4815d562011-09-20 03:17:40 +00006431 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
6432 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
6433 // operand is still set to noreg. If needed, set the optional operand's
6434 // register to CPSR, and remove the redundant implicit def.
Andrew Trick3be654f2011-09-21 02:20:46 +00006435 //
Andrew Trick90b7b122011-10-18 19:18:52 +00006436 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick4815d562011-09-20 03:17:40 +00006437
Andrew Trick3be654f2011-09-21 02:20:46 +00006438 // Rename pseudo opcodes.
6439 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
6440 if (NewOpc) {
6441 const ARMBaseInstrInfo *TII =
6442 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick90b7b122011-10-18 19:18:52 +00006443 MCID = &TII->get(NewOpc);
6444
6445 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
6446 "converted opcode should be the same except for cc_out");
6447
6448 MI->setDesc(*MCID);
6449
6450 // Add the optional cc_out operand
6451 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick3be654f2011-09-21 02:20:46 +00006452 }
Andrew Trick90b7b122011-10-18 19:18:52 +00006453 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick4815d562011-09-20 03:17:40 +00006454
6455 // Any ARM instruction that sets the 's' bit should specify an optional
6456 // "cc_out" operand in the last operand position.
Andrew Trick90b7b122011-10-18 19:18:52 +00006457 if (!MCID->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006458 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006459 return;
6460 }
Andrew Trick3be654f2011-09-21 02:20:46 +00006461 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
6462 // since we already have an optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006463 bool definesCPSR = false;
6464 bool deadCPSR = false;
Andrew Trick90b7b122011-10-18 19:18:52 +00006465 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick4815d562011-09-20 03:17:40 +00006466 i != e; ++i) {
6467 const MachineOperand &MO = MI->getOperand(i);
6468 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
6469 definesCPSR = true;
6470 if (MO.isDead())
6471 deadCPSR = true;
6472 MI->RemoveOperand(i);
6473 break;
Evan Cheng37fefc22011-08-30 19:09:48 +00006474 }
6475 }
Andrew Trick4815d562011-09-20 03:17:40 +00006476 if (!definesCPSR) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006477 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006478 return;
6479 }
6480 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick3be654f2011-09-21 02:20:46 +00006481 if (deadCPSR) {
6482 assert(!MI->getOperand(ccOutIdx).getReg() &&
6483 "expect uninitialized optional cc_out operand");
Andrew Trick4815d562011-09-20 03:17:40 +00006484 return;
Andrew Trick3be654f2011-09-21 02:20:46 +00006485 }
Andrew Trick4815d562011-09-20 03:17:40 +00006486
Andrew Trick3be654f2011-09-21 02:20:46 +00006487 // If this instruction was defined with an optional CPSR def and its dag node
6488 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006489 MachineOperand &MO = MI->getOperand(ccOutIdx);
6490 MO.setReg(ARM::CPSR);
6491 MO.setIsDef(true);
Evan Cheng37fefc22011-08-30 19:09:48 +00006492}
6493
Evan Chenga8e29892007-01-19 07:51:42 +00006494//===----------------------------------------------------------------------===//
6495// ARM Optimization Hooks
6496//===----------------------------------------------------------------------===//
6497
Chris Lattnerd1980a52009-03-12 06:52:53 +00006498static
6499SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
6500 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00006501 SelectionDAG &DAG = DCI.DAG;
6502 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00006503 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00006504 unsigned Opc = N->getOpcode();
6505 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
6506 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
6507 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
6508 ISD::CondCode CC = ISD::SETCC_INVALID;
6509
6510 if (isSlctCC) {
6511 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
6512 } else {
6513 SDValue CCOp = Slct.getOperand(0);
6514 if (CCOp.getOpcode() == ISD::SETCC)
6515 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
6516 }
6517
6518 bool DoXform = false;
6519 bool InvCC = false;
6520 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
6521 "Bad input!");
6522
6523 if (LHS.getOpcode() == ISD::Constant &&
6524 cast<ConstantSDNode>(LHS)->isNullValue()) {
6525 DoXform = true;
6526 } else if (CC != ISD::SETCC_INVALID &&
6527 RHS.getOpcode() == ISD::Constant &&
6528 cast<ConstantSDNode>(RHS)->isNullValue()) {
6529 std::swap(LHS, RHS);
6530 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006531 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00006532 Op0.getOperand(0).getValueType();
6533 bool isInt = OpVT.isInteger();
6534 CC = ISD::getSetCCInverse(CC, isInt);
6535
6536 if (!TLI.isCondCodeLegal(CC, OpVT))
6537 return SDValue(); // Inverse operator isn't legal.
6538
6539 DoXform = true;
6540 InvCC = true;
6541 }
6542
6543 if (DoXform) {
6544 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
6545 if (isSlctCC)
6546 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
6547 Slct.getOperand(0), Slct.getOperand(1), CC);
6548 SDValue CCOp = Slct.getOperand(0);
6549 if (InvCC)
6550 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
6551 CCOp.getOperand(0), CCOp.getOperand(1), CC);
6552 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
6553 CCOp, OtherOp, Result);
6554 }
6555 return SDValue();
6556}
6557
Eric Christopherfa6f5912011-06-29 21:10:36 +00006558// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00006559// (only after legalization).
6560static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
6561 TargetLowering::DAGCombinerInfo &DCI,
6562 const ARMSubtarget *Subtarget) {
6563
6564 // Only perform optimization if after legalize, and if NEON is available. We
6565 // also expected both operands to be BUILD_VECTORs.
6566 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
6567 || N0.getOpcode() != ISD::BUILD_VECTOR
6568 || N1.getOpcode() != ISD::BUILD_VECTOR)
6569 return SDValue();
6570
6571 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
6572 EVT VT = N->getValueType(0);
6573 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
6574 return SDValue();
6575
6576 // Check that the vector operands are of the right form.
6577 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
6578 // operands, where N is the size of the formed vector.
6579 // Each EXTRACT_VECTOR should have the same input vector and odd or even
6580 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00006581
6582 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00006583 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00006584 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00006585 SDValue Vec = N0->getOperand(0)->getOperand(0);
6586 SDNode *V = Vec.getNode();
6587 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00006588
Eric Christopherfa6f5912011-06-29 21:10:36 +00006589 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00006590 // check to see if each of their operands are an EXTRACT_VECTOR with
6591 // the same vector and appropriate index.
6592 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
6593 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
6594 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00006595
Tanya Lattner189531f2011-06-14 23:48:48 +00006596 SDValue ExtVec0 = N0->getOperand(i);
6597 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00006598
Tanya Lattner189531f2011-06-14 23:48:48 +00006599 // First operand is the vector, verify its the same.
6600 if (V != ExtVec0->getOperand(0).getNode() ||
6601 V != ExtVec1->getOperand(0).getNode())
6602 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00006603
Tanya Lattner189531f2011-06-14 23:48:48 +00006604 // Second is the constant, verify its correct.
6605 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
6606 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00006607
Tanya Lattner189531f2011-06-14 23:48:48 +00006608 // For the constant, we want to see all the even or all the odd.
6609 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
6610 || C1->getZExtValue() != nextIndex+1)
6611 return SDValue();
6612
6613 // Increment index.
6614 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00006615 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00006616 return SDValue();
6617 }
6618
6619 // Create VPADDL node.
6620 SelectionDAG &DAG = DCI.DAG;
6621 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00006622
6623 // Build operand list.
6624 SmallVector<SDValue, 8> Ops;
6625 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
6626 TLI.getPointerTy()));
6627
6628 // Input is the vector.
6629 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00006630
Tanya Lattner189531f2011-06-14 23:48:48 +00006631 // Get widened type and narrowed type.
6632 MVT widenType;
6633 unsigned numElem = VT.getVectorNumElements();
6634 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
6635 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
6636 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
6637 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
6638 default:
6639 assert(0 && "Invalid vector element type for padd optimization.");
6640 }
6641
6642 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
6643 widenType, &Ops[0], Ops.size());
6644 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
6645}
6646
Bob Wilson3d5792a2010-07-29 20:34:14 +00006647/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
6648/// operands N0 and N1. This is a helper for PerformADDCombine that is
6649/// called with the default operands, and if that fails, with commuted
6650/// operands.
6651static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00006652 TargetLowering::DAGCombinerInfo &DCI,
6653 const ARMSubtarget *Subtarget){
6654
6655 // Attempt to create vpaddl for this add.
6656 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
6657 if (Result.getNode())
6658 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00006659
Chris Lattnerd1980a52009-03-12 06:52:53 +00006660 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
6661 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
6662 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
6663 if (Result.getNode()) return Result;
6664 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00006665 return SDValue();
6666}
6667
Bob Wilson3d5792a2010-07-29 20:34:14 +00006668/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
6669///
6670static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00006671 TargetLowering::DAGCombinerInfo &DCI,
6672 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00006673 SDValue N0 = N->getOperand(0);
6674 SDValue N1 = N->getOperand(1);
6675
6676 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00006677 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00006678 if (Result.getNode())
6679 return Result;
6680
6681 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00006682 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00006683}
6684
Chris Lattnerd1980a52009-03-12 06:52:53 +00006685/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00006686///
Chris Lattnerd1980a52009-03-12 06:52:53 +00006687static SDValue PerformSUBCombine(SDNode *N,
6688 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00006689 SDValue N0 = N->getOperand(0);
6690 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00006691
Chris Lattnerd1980a52009-03-12 06:52:53 +00006692 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
6693 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
6694 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
6695 if (Result.getNode()) return Result;
6696 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00006697
Chris Lattnerd1980a52009-03-12 06:52:53 +00006698 return SDValue();
6699}
6700
Evan Cheng463d3582011-03-31 19:38:48 +00006701/// PerformVMULCombine
6702/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
6703/// special multiplier accumulator forwarding.
6704/// vmul d3, d0, d2
6705/// vmla d3, d1, d2
6706/// is faster than
6707/// vadd d3, d0, d1
6708/// vmul d3, d3, d2
6709static SDValue PerformVMULCombine(SDNode *N,
6710 TargetLowering::DAGCombinerInfo &DCI,
6711 const ARMSubtarget *Subtarget) {
6712 if (!Subtarget->hasVMLxForwarding())
6713 return SDValue();
6714
6715 SelectionDAG &DAG = DCI.DAG;
6716 SDValue N0 = N->getOperand(0);
6717 SDValue N1 = N->getOperand(1);
6718 unsigned Opcode = N0.getOpcode();
6719 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
6720 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00006721 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00006722 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
6723 Opcode != ISD::FADD && Opcode != ISD::FSUB)
6724 return SDValue();
6725 std::swap(N0, N1);
6726 }
6727
6728 EVT VT = N->getValueType(0);
6729 DebugLoc DL = N->getDebugLoc();
6730 SDValue N00 = N0->getOperand(0);
6731 SDValue N01 = N0->getOperand(1);
6732 return DAG.getNode(Opcode, DL, VT,
6733 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
6734 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
6735}
6736
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006737static SDValue PerformMULCombine(SDNode *N,
6738 TargetLowering::DAGCombinerInfo &DCI,
6739 const ARMSubtarget *Subtarget) {
6740 SelectionDAG &DAG = DCI.DAG;
6741
6742 if (Subtarget->isThumb1Only())
6743 return SDValue();
6744
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006745 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
6746 return SDValue();
6747
6748 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00006749 if (VT.is64BitVector() || VT.is128BitVector())
6750 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006751 if (VT != MVT::i32)
6752 return SDValue();
6753
6754 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
6755 if (!C)
6756 return SDValue();
6757
6758 uint64_t MulAmt = C->getZExtValue();
6759 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
6760 ShiftAmt = ShiftAmt & (32 - 1);
6761 SDValue V = N->getOperand(0);
6762 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006763
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006764 SDValue Res;
6765 MulAmt >>= ShiftAmt;
6766 if (isPowerOf2_32(MulAmt - 1)) {
6767 // (mul x, 2^N + 1) => (add (shl x, N), x)
6768 Res = DAG.getNode(ISD::ADD, DL, VT,
6769 V, DAG.getNode(ISD::SHL, DL, VT,
6770 V, DAG.getConstant(Log2_32(MulAmt-1),
6771 MVT::i32)));
6772 } else if (isPowerOf2_32(MulAmt + 1)) {
6773 // (mul x, 2^N - 1) => (sub (shl x, N), x)
6774 Res = DAG.getNode(ISD::SUB, DL, VT,
6775 DAG.getNode(ISD::SHL, DL, VT,
6776 V, DAG.getConstant(Log2_32(MulAmt+1),
6777 MVT::i32)),
6778 V);
6779 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006780 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006781
6782 if (ShiftAmt != 0)
6783 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
6784 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006785
6786 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006787 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006788 return SDValue();
6789}
6790
Owen Anderson080c0922010-11-05 19:27:46 +00006791static SDValue PerformANDCombine(SDNode *N,
6792 TargetLowering::DAGCombinerInfo &DCI) {
Owen Anderson76706012011-04-05 21:48:57 +00006793
Owen Anderson080c0922010-11-05 19:27:46 +00006794 // Attempt to use immediate-form VBIC
6795 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
6796 DebugLoc dl = N->getDebugLoc();
6797 EVT VT = N->getValueType(0);
6798 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006799
Tanya Lattner0433b212011-04-07 15:24:20 +00006800 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
6801 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00006802
Owen Anderson080c0922010-11-05 19:27:46 +00006803 APInt SplatBits, SplatUndef;
6804 unsigned SplatBitSize;
6805 bool HasAnyUndefs;
6806 if (BVN &&
6807 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
6808 if (SplatBitSize <= 64) {
6809 EVT VbicVT;
6810 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
6811 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006812 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00006813 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00006814 if (Val.getNode()) {
6815 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006816 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00006817 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006818 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00006819 }
6820 }
6821 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006822
Owen Anderson080c0922010-11-05 19:27:46 +00006823 return SDValue();
6824}
6825
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006826/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
6827static SDValue PerformORCombine(SDNode *N,
6828 TargetLowering::DAGCombinerInfo &DCI,
6829 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00006830 // Attempt to use immediate-form VORR
6831 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
6832 DebugLoc dl = N->getDebugLoc();
6833 EVT VT = N->getValueType(0);
6834 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006835
Tanya Lattner0433b212011-04-07 15:24:20 +00006836 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
6837 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00006838
Owen Anderson60f48702010-11-03 23:15:26 +00006839 APInt SplatBits, SplatUndef;
6840 unsigned SplatBitSize;
6841 bool HasAnyUndefs;
6842 if (BVN && Subtarget->hasNEON() &&
6843 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
6844 if (SplatBitSize <= 64) {
6845 EVT VorrVT;
6846 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
6847 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00006848 DAG, VorrVT, VT.is128BitVector(),
6849 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00006850 if (Val.getNode()) {
6851 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006852 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00006853 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006854 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00006855 }
6856 }
6857 }
6858
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00006859 SDValue N0 = N->getOperand(0);
6860 if (N0.getOpcode() != ISD::AND)
6861 return SDValue();
6862 SDValue N1 = N->getOperand(1);
6863
6864 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
6865 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
6866 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
6867 APInt SplatUndef;
6868 unsigned SplatBitSize;
6869 bool HasAnyUndefs;
6870
6871 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
6872 APInt SplatBits0;
6873 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
6874 HasAnyUndefs) && !HasAnyUndefs) {
6875 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
6876 APInt SplatBits1;
6877 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
6878 HasAnyUndefs) && !HasAnyUndefs &&
6879 SplatBits0 == ~SplatBits1) {
6880 // Canonicalize the vector type to make instruction selection simpler.
6881 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
6882 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
6883 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00006884 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00006885 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
6886 }
6887 }
6888 }
6889
Jim Grosbach54238562010-07-17 03:30:54 +00006890 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
6891 // reasonable.
6892
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006893 // BFI is only available on V6T2+
6894 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
6895 return SDValue();
6896
Jim Grosbach54238562010-07-17 03:30:54 +00006897 DebugLoc DL = N->getDebugLoc();
6898 // 1) or (and A, mask), val => ARMbfi A, val, mask
6899 // iff (val & mask) == val
6900 //
6901 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
6902 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00006903 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00006904 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00006905 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00006906 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006907
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006908 if (VT != MVT::i32)
6909 return SDValue();
6910
Evan Cheng30fb13f2010-12-13 20:32:54 +00006911 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00006912
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006913 // The value and the mask need to be constants so we can verify this is
6914 // actually a bitfield set. If the mask is 0xffff, we can do better
6915 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00006916 SDValue MaskOp = N0.getOperand(1);
6917 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
6918 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006919 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00006920 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006921 if (Mask == 0xffff)
6922 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006923 SDValue Res;
6924 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00006925 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
6926 if (N1C) {
6927 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00006928 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00006929 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006930
Evan Chenga9688c42010-12-11 04:11:38 +00006931 if (ARM::isBitFieldInvertedMask(Mask)) {
6932 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006933
Evan Cheng30fb13f2010-12-13 20:32:54 +00006934 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00006935 DAG.getConstant(Val, MVT::i32),
6936 DAG.getConstant(Mask, MVT::i32));
6937
6938 // Do not add new nodes to DAG combiner worklist.
6939 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00006940 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00006941 }
Jim Grosbach54238562010-07-17 03:30:54 +00006942 } else if (N1.getOpcode() == ISD::AND) {
6943 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00006944 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
6945 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00006946 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00006947 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006948
Eric Christopher29aeed12011-03-26 01:21:03 +00006949 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
6950 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00006951 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00006952 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00006953 // The pack halfword instruction works better for masks that fit it,
6954 // so use that when it's available.
6955 if (Subtarget->hasT2ExtractPack() &&
6956 (Mask == 0xffff || Mask == 0xffff0000))
6957 return SDValue();
6958 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00006959 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00006960 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00006961 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00006962 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00006963 DAG.getConstant(Mask, MVT::i32));
6964 // Do not add new nodes to DAG combiner worklist.
6965 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00006966 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006967 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00006968 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00006969 // The pack halfword instruction works better for masks that fit it,
6970 // so use that when it's available.
6971 if (Subtarget->hasT2ExtractPack() &&
6972 (Mask2 == 0xffff || Mask2 == 0xffff0000))
6973 return SDValue();
6974 // 2b
6975 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00006976 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00006977 DAG.getConstant(lsb, MVT::i32));
6978 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00006979 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00006980 // Do not add new nodes to DAG combiner worklist.
6981 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00006982 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006983 }
6984 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006985
Evan Cheng30fb13f2010-12-13 20:32:54 +00006986 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
6987 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
6988 ARM::isBitFieldInvertedMask(~Mask)) {
6989 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
6990 // where lsb(mask) == #shamt and masked bits of B are known zero.
6991 SDValue ShAmt = N00.getOperand(1);
6992 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
6993 unsigned LSB = CountTrailingZeros_32(Mask);
6994 if (ShAmtC != LSB)
6995 return SDValue();
6996
6997 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
6998 DAG.getConstant(~Mask, MVT::i32));
6999
7000 // Do not add new nodes to DAG combiner worklist.
7001 DCI.CombineTo(N, Res, false);
7002 }
7003
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007004 return SDValue();
7005}
7006
Evan Chengbf188ae2011-06-15 01:12:31 +00007007/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
7008/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00007009static SDValue PerformBFICombine(SDNode *N,
7010 TargetLowering::DAGCombinerInfo &DCI) {
7011 SDValue N1 = N->getOperand(1);
7012 if (N1.getOpcode() == ISD::AND) {
7013 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7014 if (!N11C)
7015 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007016 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
7017 unsigned LSB = CountTrailingZeros_32(~InvMask);
7018 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
7019 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00007020 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007021 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00007022 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
7023 N->getOperand(0), N1.getOperand(0),
7024 N->getOperand(2));
7025 }
7026 return SDValue();
7027}
7028
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007029/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
7030/// ARMISD::VMOVRRD.
7031static SDValue PerformVMOVRRDCombine(SDNode *N,
7032 TargetLowering::DAGCombinerInfo &DCI) {
7033 // vmovrrd(vmovdrr x, y) -> x,y
7034 SDValue InDouble = N->getOperand(0);
7035 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
7036 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00007037
7038 // vmovrrd(load f64) -> (load i32), (load i32)
7039 SDNode *InNode = InDouble.getNode();
7040 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
7041 InNode->getValueType(0) == MVT::f64 &&
7042 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
7043 !cast<LoadSDNode>(InNode)->isVolatile()) {
7044 // TODO: Should this be done for non-FrameIndex operands?
7045 LoadSDNode *LD = cast<LoadSDNode>(InNode);
7046
7047 SelectionDAG &DAG = DCI.DAG;
7048 DebugLoc DL = LD->getDebugLoc();
7049 SDValue BasePtr = LD->getBasePtr();
7050 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
7051 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007052 LD->isNonTemporal(), LD->isInvariant(),
7053 LD->getAlignment());
Cameron Zwarich4071a712011-04-02 02:40:43 +00007054
7055 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7056 DAG.getConstant(4, MVT::i32));
7057 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
7058 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007059 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich4071a712011-04-02 02:40:43 +00007060 std::min(4U, LD->getAlignment() / 2));
7061
7062 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
7063 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
7064 DCI.RemoveFromWorklist(LD);
7065 DAG.DeleteNode(LD);
7066 return Result;
7067 }
7068
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007069 return SDValue();
7070}
7071
7072/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
7073/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
7074static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
7075 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
7076 SDValue Op0 = N->getOperand(0);
7077 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007078 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007079 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007080 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007081 Op1 = Op1.getOperand(0);
7082 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
7083 Op0.getNode() == Op1.getNode() &&
7084 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007085 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007086 N->getValueType(0), Op0.getOperand(0));
7087 return SDValue();
7088}
7089
Bob Wilson31600902010-12-21 06:43:19 +00007090/// PerformSTORECombine - Target-specific dag combine xforms for
7091/// ISD::STORE.
7092static SDValue PerformSTORECombine(SDNode *N,
7093 TargetLowering::DAGCombinerInfo &DCI) {
7094 // Bitcast an i64 store extracted from a vector to f64.
7095 // Otherwise, the i64 value will be legalized to a pair of i32 values.
7096 StoreSDNode *St = cast<StoreSDNode>(N);
7097 SDValue StVal = St->getValue();
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007098 if (!ISD::isNormalStore(St) || St->isVolatile())
7099 return SDValue();
7100
7101 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
7102 StVal.getNode()->hasOneUse() && !St->isVolatile()) {
7103 SelectionDAG &DAG = DCI.DAG;
7104 DebugLoc DL = St->getDebugLoc();
7105 SDValue BasePtr = St->getBasePtr();
7106 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
7107 StVal.getNode()->getOperand(0), BasePtr,
7108 St->getPointerInfo(), St->isVolatile(),
7109 St->isNonTemporal(), St->getAlignment());
7110
7111 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7112 DAG.getConstant(4, MVT::i32));
7113 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
7114 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
7115 St->isNonTemporal(),
7116 std::min(4U, St->getAlignment() / 2));
7117 }
7118
7119 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00007120 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
7121 return SDValue();
7122
7123 SelectionDAG &DAG = DCI.DAG;
7124 DebugLoc dl = StVal.getDebugLoc();
7125 SDValue IntVec = StVal.getOperand(0);
7126 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7127 IntVec.getValueType().getVectorNumElements());
7128 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
7129 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
7130 Vec, StVal.getOperand(1));
7131 dl = N->getDebugLoc();
7132 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
7133 // Make the DAGCombiner fold the bitcasts.
7134 DCI.AddToWorklist(Vec.getNode());
7135 DCI.AddToWorklist(ExtElt.getNode());
7136 DCI.AddToWorklist(V.getNode());
7137 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
7138 St->getPointerInfo(), St->isVolatile(),
7139 St->isNonTemporal(), St->getAlignment(),
7140 St->getTBAAInfo());
7141}
7142
7143/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
7144/// are normal, non-volatile loads. If so, it is profitable to bitcast an
7145/// i64 vector to have f64 elements, since the value can then be loaded
7146/// directly into a VFP register.
7147static bool hasNormalLoadOperand(SDNode *N) {
7148 unsigned NumElts = N->getValueType(0).getVectorNumElements();
7149 for (unsigned i = 0; i < NumElts; ++i) {
7150 SDNode *Elt = N->getOperand(i).getNode();
7151 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
7152 return true;
7153 }
7154 return false;
7155}
7156
Bob Wilson75f02882010-09-17 22:59:05 +00007157/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
7158/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00007159static SDValue PerformBUILD_VECTORCombine(SDNode *N,
7160 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00007161 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
7162 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
7163 // into a pair of GPRs, which is fine when the value is used as a scalar,
7164 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00007165 SelectionDAG &DAG = DCI.DAG;
7166 if (N->getNumOperands() == 2) {
7167 SDValue RV = PerformVMOVDRRCombine(N, DAG);
7168 if (RV.getNode())
7169 return RV;
7170 }
Bob Wilson75f02882010-09-17 22:59:05 +00007171
Bob Wilson31600902010-12-21 06:43:19 +00007172 // Load i64 elements as f64 values so that type legalization does not split
7173 // them up into i32 values.
7174 EVT VT = N->getValueType(0);
7175 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
7176 return SDValue();
7177 DebugLoc dl = N->getDebugLoc();
7178 SmallVector<SDValue, 8> Ops;
7179 unsigned NumElts = VT.getVectorNumElements();
7180 for (unsigned i = 0; i < NumElts; ++i) {
7181 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
7182 Ops.push_back(V);
7183 // Make the DAGCombiner fold the bitcast.
7184 DCI.AddToWorklist(V.getNode());
7185 }
7186 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
7187 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
7188 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
7189}
7190
7191/// PerformInsertEltCombine - Target-specific dag combine xforms for
7192/// ISD::INSERT_VECTOR_ELT.
7193static SDValue PerformInsertEltCombine(SDNode *N,
7194 TargetLowering::DAGCombinerInfo &DCI) {
7195 // Bitcast an i64 load inserted into a vector to f64.
7196 // Otherwise, the i64 value will be legalized to a pair of i32 values.
7197 EVT VT = N->getValueType(0);
7198 SDNode *Elt = N->getOperand(1).getNode();
7199 if (VT.getVectorElementType() != MVT::i64 ||
7200 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
7201 return SDValue();
7202
7203 SelectionDAG &DAG = DCI.DAG;
7204 DebugLoc dl = N->getDebugLoc();
7205 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7206 VT.getVectorNumElements());
7207 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
7208 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
7209 // Make the DAGCombiner fold the bitcasts.
7210 DCI.AddToWorklist(Vec.getNode());
7211 DCI.AddToWorklist(V.getNode());
7212 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
7213 Vec, V, N->getOperand(2));
7214 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00007215}
7216
Bob Wilsonf20700c2010-10-27 20:38:28 +00007217/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
7218/// ISD::VECTOR_SHUFFLE.
7219static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
7220 // The LLVM shufflevector instruction does not require the shuffle mask
7221 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
7222 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
7223 // operands do not match the mask length, they are extended by concatenating
7224 // them with undef vectors. That is probably the right thing for other
7225 // targets, but for NEON it is better to concatenate two double-register
7226 // size vector operands into a single quad-register size vector. Do that
7227 // transformation here:
7228 // shuffle(concat(v1, undef), concat(v2, undef)) ->
7229 // shuffle(concat(v1, v2), undef)
7230 SDValue Op0 = N->getOperand(0);
7231 SDValue Op1 = N->getOperand(1);
7232 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
7233 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
7234 Op0.getNumOperands() != 2 ||
7235 Op1.getNumOperands() != 2)
7236 return SDValue();
7237 SDValue Concat0Op1 = Op0.getOperand(1);
7238 SDValue Concat1Op1 = Op1.getOperand(1);
7239 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
7240 Concat1Op1.getOpcode() != ISD::UNDEF)
7241 return SDValue();
7242 // Skip the transformation if any of the types are illegal.
7243 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7244 EVT VT = N->getValueType(0);
7245 if (!TLI.isTypeLegal(VT) ||
7246 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
7247 !TLI.isTypeLegal(Concat1Op1.getValueType()))
7248 return SDValue();
7249
7250 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
7251 Op0.getOperand(0), Op1.getOperand(0));
7252 // Translate the shuffle mask.
7253 SmallVector<int, 16> NewMask;
7254 unsigned NumElts = VT.getVectorNumElements();
7255 unsigned HalfElts = NumElts/2;
7256 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
7257 for (unsigned n = 0; n < NumElts; ++n) {
7258 int MaskElt = SVN->getMaskElt(n);
7259 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007260 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00007261 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007262 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00007263 NewElt = HalfElts + MaskElt - NumElts;
7264 NewMask.push_back(NewElt);
7265 }
7266 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
7267 DAG.getUNDEF(VT), NewMask.data());
7268}
7269
Bob Wilson1c3ef902011-02-07 17:43:21 +00007270/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
7271/// NEON load/store intrinsics to merge base address updates.
7272static SDValue CombineBaseUpdate(SDNode *N,
7273 TargetLowering::DAGCombinerInfo &DCI) {
7274 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7275 return SDValue();
7276
7277 SelectionDAG &DAG = DCI.DAG;
7278 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
7279 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
7280 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
7281 SDValue Addr = N->getOperand(AddrOpIdx);
7282
7283 // Search for a use of the address operand that is an increment.
7284 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
7285 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
7286 SDNode *User = *UI;
7287 if (User->getOpcode() != ISD::ADD ||
7288 UI.getUse().getResNo() != Addr.getResNo())
7289 continue;
7290
7291 // Check that the add is independent of the load/store. Otherwise, folding
7292 // it would create a cycle.
7293 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
7294 continue;
7295
7296 // Find the new opcode for the updating load/store.
7297 bool isLoad = true;
7298 bool isLaneOp = false;
7299 unsigned NewOpc = 0;
7300 unsigned NumVecs = 0;
7301 if (isIntrinsic) {
7302 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
7303 switch (IntNo) {
7304 default: assert(0 && "unexpected intrinsic for Neon base update");
7305 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
7306 NumVecs = 1; break;
7307 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
7308 NumVecs = 2; break;
7309 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
7310 NumVecs = 3; break;
7311 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
7312 NumVecs = 4; break;
7313 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
7314 NumVecs = 2; isLaneOp = true; break;
7315 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
7316 NumVecs = 3; isLaneOp = true; break;
7317 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
7318 NumVecs = 4; isLaneOp = true; break;
7319 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
7320 NumVecs = 1; isLoad = false; break;
7321 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
7322 NumVecs = 2; isLoad = false; break;
7323 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
7324 NumVecs = 3; isLoad = false; break;
7325 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
7326 NumVecs = 4; isLoad = false; break;
7327 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
7328 NumVecs = 2; isLoad = false; isLaneOp = true; break;
7329 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
7330 NumVecs = 3; isLoad = false; isLaneOp = true; break;
7331 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
7332 NumVecs = 4; isLoad = false; isLaneOp = true; break;
7333 }
7334 } else {
7335 isLaneOp = true;
7336 switch (N->getOpcode()) {
7337 default: assert(0 && "unexpected opcode for Neon base update");
7338 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
7339 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
7340 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
7341 }
7342 }
7343
7344 // Find the size of memory referenced by the load/store.
7345 EVT VecTy;
7346 if (isLoad)
7347 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00007348 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00007349 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
7350 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
7351 if (isLaneOp)
7352 NumBytes /= VecTy.getVectorNumElements();
7353
7354 // If the increment is a constant, it must match the memory ref size.
7355 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
7356 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
7357 uint64_t IncVal = CInc->getZExtValue();
7358 if (IncVal != NumBytes)
7359 continue;
7360 } else if (NumBytes >= 3 * 16) {
7361 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
7362 // separate instructions that make it harder to use a non-constant update.
7363 continue;
7364 }
7365
7366 // Create the new updating load/store node.
7367 EVT Tys[6];
7368 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
7369 unsigned n;
7370 for (n = 0; n < NumResultVecs; ++n)
7371 Tys[n] = VecTy;
7372 Tys[n++] = MVT::i32;
7373 Tys[n] = MVT::Other;
7374 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
7375 SmallVector<SDValue, 8> Ops;
7376 Ops.push_back(N->getOperand(0)); // incoming chain
7377 Ops.push_back(N->getOperand(AddrOpIdx));
7378 Ops.push_back(Inc);
7379 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
7380 Ops.push_back(N->getOperand(i));
7381 }
7382 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
7383 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
7384 Ops.data(), Ops.size(),
7385 MemInt->getMemoryVT(),
7386 MemInt->getMemOperand());
7387
7388 // Update the uses.
7389 std::vector<SDValue> NewResults;
7390 for (unsigned i = 0; i < NumResultVecs; ++i) {
7391 NewResults.push_back(SDValue(UpdN.getNode(), i));
7392 }
7393 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
7394 DCI.CombineTo(N, NewResults);
7395 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
7396
7397 break;
Owen Anderson76706012011-04-05 21:48:57 +00007398 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00007399 return SDValue();
7400}
7401
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007402/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
7403/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
7404/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
7405/// return true.
7406static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
7407 SelectionDAG &DAG = DCI.DAG;
7408 EVT VT = N->getValueType(0);
7409 // vldN-dup instructions only support 64-bit vectors for N > 1.
7410 if (!VT.is64BitVector())
7411 return false;
7412
7413 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
7414 SDNode *VLD = N->getOperand(0).getNode();
7415 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
7416 return false;
7417 unsigned NumVecs = 0;
7418 unsigned NewOpc = 0;
7419 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
7420 if (IntNo == Intrinsic::arm_neon_vld2lane) {
7421 NumVecs = 2;
7422 NewOpc = ARMISD::VLD2DUP;
7423 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
7424 NumVecs = 3;
7425 NewOpc = ARMISD::VLD3DUP;
7426 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
7427 NumVecs = 4;
7428 NewOpc = ARMISD::VLD4DUP;
7429 } else {
7430 return false;
7431 }
7432
7433 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
7434 // numbers match the load.
7435 unsigned VLDLaneNo =
7436 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
7437 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
7438 UI != UE; ++UI) {
7439 // Ignore uses of the chain result.
7440 if (UI.getUse().getResNo() == NumVecs)
7441 continue;
7442 SDNode *User = *UI;
7443 if (User->getOpcode() != ARMISD::VDUPLANE ||
7444 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
7445 return false;
7446 }
7447
7448 // Create the vldN-dup node.
7449 EVT Tys[5];
7450 unsigned n;
7451 for (n = 0; n < NumVecs; ++n)
7452 Tys[n] = VT;
7453 Tys[n] = MVT::Other;
7454 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
7455 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
7456 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
7457 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
7458 Ops, 2, VLDMemInt->getMemoryVT(),
7459 VLDMemInt->getMemOperand());
7460
7461 // Update the uses.
7462 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
7463 UI != UE; ++UI) {
7464 unsigned ResNo = UI.getUse().getResNo();
7465 // Ignore uses of the chain result.
7466 if (ResNo == NumVecs)
7467 continue;
7468 SDNode *User = *UI;
7469 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
7470 }
7471
7472 // Now the vldN-lane intrinsic is dead except for its chain result.
7473 // Update uses of the chain.
7474 std::vector<SDValue> VLDDupResults;
7475 for (unsigned n = 0; n < NumVecs; ++n)
7476 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
7477 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
7478 DCI.CombineTo(VLD, VLDDupResults);
7479
7480 return true;
7481}
7482
Bob Wilson9e82bf12010-07-14 01:22:12 +00007483/// PerformVDUPLANECombine - Target-specific dag combine xforms for
7484/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007485static SDValue PerformVDUPLANECombine(SDNode *N,
7486 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00007487 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007488
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007489 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
7490 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
7491 if (CombineVLDDUP(N, DCI))
7492 return SDValue(N, 0);
7493
7494 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
7495 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007496 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00007497 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00007498 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00007499 return SDValue();
7500
7501 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
7502 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
7503 // The canonical VMOV for a zero vector uses a 32-bit element size.
7504 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7505 unsigned EltBits;
7506 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
7507 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007508 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007509 if (EltSize > VT.getVectorElementType().getSizeInBits())
7510 return SDValue();
7511
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007512 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007513}
7514
Eric Christopherfa6f5912011-06-29 21:10:36 +00007515// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00007516// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
7517static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
7518{
Chad Rosier118c9a02011-06-28 17:26:57 +00007519 integerPart cN;
7520 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00007521 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
7522 I != E; I++) {
7523 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
7524 if (!C)
7525 return false;
7526
Eric Christopherfa6f5912011-06-29 21:10:36 +00007527 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00007528 APFloat APF = C->getValueAPF();
7529 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
7530 != APFloat::opOK || !isExact)
7531 return false;
7532
7533 c0 = (I == 0) ? cN : c0;
7534 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
7535 return false;
7536 }
7537 C = c0;
7538 return true;
7539}
7540
7541/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
7542/// can replace combinations of VMUL and VCVT (floating-point to integer)
7543/// when the VMUL has a constant operand that is a power of 2.
7544///
7545/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
7546/// vmul.f32 d16, d17, d16
7547/// vcvt.s32.f32 d16, d16
7548/// becomes:
7549/// vcvt.s32.f32 d16, d16, #3
7550static SDValue PerformVCVTCombine(SDNode *N,
7551 TargetLowering::DAGCombinerInfo &DCI,
7552 const ARMSubtarget *Subtarget) {
7553 SelectionDAG &DAG = DCI.DAG;
7554 SDValue Op = N->getOperand(0);
7555
7556 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
7557 Op.getOpcode() != ISD::FMUL)
7558 return SDValue();
7559
7560 uint64_t C;
7561 SDValue N0 = Op->getOperand(0);
7562 SDValue ConstVec = Op->getOperand(1);
7563 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
7564
Eric Christopherfa6f5912011-06-29 21:10:36 +00007565 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00007566 !isConstVecPow2(ConstVec, isSigned, C))
7567 return SDValue();
7568
7569 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
7570 Intrinsic::arm_neon_vcvtfp2fxu;
7571 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7572 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00007573 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00007574 DAG.getConstant(Log2_64(C), MVT::i32));
7575}
7576
7577/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
7578/// can replace combinations of VCVT (integer to floating-point) and VDIV
7579/// when the VDIV has a constant operand that is a power of 2.
7580///
7581/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
7582/// vcvt.f32.s32 d16, d16
7583/// vdiv.f32 d16, d17, d16
7584/// becomes:
7585/// vcvt.f32.s32 d16, d16, #3
7586static SDValue PerformVDIVCombine(SDNode *N,
7587 TargetLowering::DAGCombinerInfo &DCI,
7588 const ARMSubtarget *Subtarget) {
7589 SelectionDAG &DAG = DCI.DAG;
7590 SDValue Op = N->getOperand(0);
7591 unsigned OpOpcode = Op.getNode()->getOpcode();
7592
7593 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
7594 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
7595 return SDValue();
7596
7597 uint64_t C;
7598 SDValue ConstVec = N->getOperand(1);
7599 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
7600
7601 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
7602 !isConstVecPow2(ConstVec, isSigned, C))
7603 return SDValue();
7604
Eric Christopherfa6f5912011-06-29 21:10:36 +00007605 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00007606 Intrinsic::arm_neon_vcvtfxu2fp;
7607 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7608 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00007609 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00007610 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
7611}
7612
7613/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00007614/// operand of a vector shift operation, where all the elements of the
7615/// build_vector must have the same constant integer value.
7616static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
7617 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007618 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00007619 Op = Op.getOperand(0);
7620 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
7621 APInt SplatBits, SplatUndef;
7622 unsigned SplatBitSize;
7623 bool HasAnyUndefs;
7624 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
7625 HasAnyUndefs, ElementBits) ||
7626 SplatBitSize > ElementBits)
7627 return false;
7628 Cnt = SplatBits.getSExtValue();
7629 return true;
7630}
7631
7632/// isVShiftLImm - Check if this is a valid build_vector for the immediate
7633/// operand of a vector shift left operation. That value must be in the range:
7634/// 0 <= Value < ElementBits for a left shift; or
7635/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00007636static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00007637 assert(VT.isVector() && "vector shift count is not a vector type");
7638 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
7639 if (! getVShiftImm(Op, ElementBits, Cnt))
7640 return false;
7641 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
7642}
7643
7644/// isVShiftRImm - Check if this is a valid build_vector for the immediate
7645/// operand of a vector shift right operation. For a shift opcode, the value
7646/// is positive, but for an intrinsic the value count must be negative. The
7647/// absolute value must be in the range:
7648/// 1 <= |Value| <= ElementBits for a right shift; or
7649/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00007650static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00007651 int64_t &Cnt) {
7652 assert(VT.isVector() && "vector shift count is not a vector type");
7653 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
7654 if (! getVShiftImm(Op, ElementBits, Cnt))
7655 return false;
7656 if (isIntrinsic)
7657 Cnt = -Cnt;
7658 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
7659}
7660
7661/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
7662static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
7663 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
7664 switch (IntNo) {
7665 default:
7666 // Don't do anything for most intrinsics.
7667 break;
7668
7669 // Vector shifts: check for immediate versions and lower them.
7670 // Note: This is done during DAG combining instead of DAG legalizing because
7671 // the build_vectors for 64-bit vector element shift counts are generally
7672 // not legal, and it is hard to see their values after they get legalized to
7673 // loads from a constant pool.
7674 case Intrinsic::arm_neon_vshifts:
7675 case Intrinsic::arm_neon_vshiftu:
7676 case Intrinsic::arm_neon_vshiftls:
7677 case Intrinsic::arm_neon_vshiftlu:
7678 case Intrinsic::arm_neon_vshiftn:
7679 case Intrinsic::arm_neon_vrshifts:
7680 case Intrinsic::arm_neon_vrshiftu:
7681 case Intrinsic::arm_neon_vrshiftn:
7682 case Intrinsic::arm_neon_vqshifts:
7683 case Intrinsic::arm_neon_vqshiftu:
7684 case Intrinsic::arm_neon_vqshiftsu:
7685 case Intrinsic::arm_neon_vqshiftns:
7686 case Intrinsic::arm_neon_vqshiftnu:
7687 case Intrinsic::arm_neon_vqshiftnsu:
7688 case Intrinsic::arm_neon_vqrshiftns:
7689 case Intrinsic::arm_neon_vqrshiftnu:
7690 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00007691 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007692 int64_t Cnt;
7693 unsigned VShiftOpc = 0;
7694
7695 switch (IntNo) {
7696 case Intrinsic::arm_neon_vshifts:
7697 case Intrinsic::arm_neon_vshiftu:
7698 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
7699 VShiftOpc = ARMISD::VSHL;
7700 break;
7701 }
7702 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
7703 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
7704 ARMISD::VSHRs : ARMISD::VSHRu);
7705 break;
7706 }
7707 return SDValue();
7708
7709 case Intrinsic::arm_neon_vshiftls:
7710 case Intrinsic::arm_neon_vshiftlu:
7711 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
7712 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007713 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007714
7715 case Intrinsic::arm_neon_vrshifts:
7716 case Intrinsic::arm_neon_vrshiftu:
7717 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
7718 break;
7719 return SDValue();
7720
7721 case Intrinsic::arm_neon_vqshifts:
7722 case Intrinsic::arm_neon_vqshiftu:
7723 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
7724 break;
7725 return SDValue();
7726
7727 case Intrinsic::arm_neon_vqshiftsu:
7728 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
7729 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007730 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007731
7732 case Intrinsic::arm_neon_vshiftn:
7733 case Intrinsic::arm_neon_vrshiftn:
7734 case Intrinsic::arm_neon_vqshiftns:
7735 case Intrinsic::arm_neon_vqshiftnu:
7736 case Intrinsic::arm_neon_vqshiftnsu:
7737 case Intrinsic::arm_neon_vqrshiftns:
7738 case Intrinsic::arm_neon_vqrshiftnu:
7739 case Intrinsic::arm_neon_vqrshiftnsu:
7740 // Narrowing shifts require an immediate right shift.
7741 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
7742 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00007743 llvm_unreachable("invalid shift count for narrowing vector shift "
7744 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007745
7746 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00007747 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00007748 }
7749
7750 switch (IntNo) {
7751 case Intrinsic::arm_neon_vshifts:
7752 case Intrinsic::arm_neon_vshiftu:
7753 // Opcode already set above.
7754 break;
7755 case Intrinsic::arm_neon_vshiftls:
7756 case Intrinsic::arm_neon_vshiftlu:
7757 if (Cnt == VT.getVectorElementType().getSizeInBits())
7758 VShiftOpc = ARMISD::VSHLLi;
7759 else
7760 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
7761 ARMISD::VSHLLs : ARMISD::VSHLLu);
7762 break;
7763 case Intrinsic::arm_neon_vshiftn:
7764 VShiftOpc = ARMISD::VSHRN; break;
7765 case Intrinsic::arm_neon_vrshifts:
7766 VShiftOpc = ARMISD::VRSHRs; break;
7767 case Intrinsic::arm_neon_vrshiftu:
7768 VShiftOpc = ARMISD::VRSHRu; break;
7769 case Intrinsic::arm_neon_vrshiftn:
7770 VShiftOpc = ARMISD::VRSHRN; break;
7771 case Intrinsic::arm_neon_vqshifts:
7772 VShiftOpc = ARMISD::VQSHLs; break;
7773 case Intrinsic::arm_neon_vqshiftu:
7774 VShiftOpc = ARMISD::VQSHLu; break;
7775 case Intrinsic::arm_neon_vqshiftsu:
7776 VShiftOpc = ARMISD::VQSHLsu; break;
7777 case Intrinsic::arm_neon_vqshiftns:
7778 VShiftOpc = ARMISD::VQSHRNs; break;
7779 case Intrinsic::arm_neon_vqshiftnu:
7780 VShiftOpc = ARMISD::VQSHRNu; break;
7781 case Intrinsic::arm_neon_vqshiftnsu:
7782 VShiftOpc = ARMISD::VQSHRNsu; break;
7783 case Intrinsic::arm_neon_vqrshiftns:
7784 VShiftOpc = ARMISD::VQRSHRNs; break;
7785 case Intrinsic::arm_neon_vqrshiftnu:
7786 VShiftOpc = ARMISD::VQRSHRNu; break;
7787 case Intrinsic::arm_neon_vqrshiftnsu:
7788 VShiftOpc = ARMISD::VQRSHRNsu; break;
7789 }
7790
7791 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007792 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007793 }
7794
7795 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00007796 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007797 int64_t Cnt;
7798 unsigned VShiftOpc = 0;
7799
7800 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
7801 VShiftOpc = ARMISD::VSLI;
7802 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
7803 VShiftOpc = ARMISD::VSRI;
7804 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00007805 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007806 }
7807
7808 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
7809 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00007810 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007811 }
7812
7813 case Intrinsic::arm_neon_vqrshifts:
7814 case Intrinsic::arm_neon_vqrshiftu:
7815 // No immediate versions of these to check for.
7816 break;
7817 }
7818
7819 return SDValue();
7820}
7821
7822/// PerformShiftCombine - Checks for immediate versions of vector shifts and
7823/// lowers them. As with the vector shift intrinsics, this is done during DAG
7824/// combining instead of DAG legalizing because the build_vectors for 64-bit
7825/// vector element shift counts are generally not legal, and it is hard to see
7826/// their values after they get legalized to loads from a constant pool.
7827static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
7828 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00007829 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00007830
7831 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00007832 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7833 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00007834 return SDValue();
7835
7836 assert(ST->hasNEON() && "unexpected vector shift");
7837 int64_t Cnt;
7838
7839 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007840 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00007841
7842 case ISD::SHL:
7843 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
7844 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007845 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007846 break;
7847
7848 case ISD::SRA:
7849 case ISD::SRL:
7850 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
7851 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
7852 ARMISD::VSHRs : ARMISD::VSHRu);
7853 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007854 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007855 }
7856 }
7857 return SDValue();
7858}
7859
7860/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
7861/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
7862static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
7863 const ARMSubtarget *ST) {
7864 SDValue N0 = N->getOperand(0);
7865
7866 // Check for sign- and zero-extensions of vector extract operations of 8-
7867 // and 16-bit vector elements. NEON supports these directly. They are
7868 // handled during DAG combining because type legalization will promote them
7869 // to 32-bit types and it is messy to recognize the operations after that.
7870 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
7871 SDValue Vec = N0.getOperand(0);
7872 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00007873 EVT VT = N->getValueType(0);
7874 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007875 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7876
Owen Anderson825b72b2009-08-11 20:47:22 +00007877 if (VT == MVT::i32 &&
7878 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00007879 TLI.isTypeLegal(Vec.getValueType()) &&
7880 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00007881
7882 unsigned Opc = 0;
7883 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007884 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00007885 case ISD::SIGN_EXTEND:
7886 Opc = ARMISD::VGETLANEs;
7887 break;
7888 case ISD::ZERO_EXTEND:
7889 case ISD::ANY_EXTEND:
7890 Opc = ARMISD::VGETLANEu;
7891 break;
7892 }
7893 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
7894 }
7895 }
7896
7897 return SDValue();
7898}
7899
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007900/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
7901/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
7902static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
7903 const ARMSubtarget *ST) {
7904 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00007905 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007906 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
7907 // a NaN; only do the transformation when it matches that behavior.
7908
7909 // For now only do this when using NEON for FP operations; if using VFP, it
7910 // is not obvious that the benefit outweighs the cost of switching to the
7911 // NEON pipeline.
7912 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
7913 N->getValueType(0) != MVT::f32)
7914 return SDValue();
7915
7916 SDValue CondLHS = N->getOperand(0);
7917 SDValue CondRHS = N->getOperand(1);
7918 SDValue LHS = N->getOperand(2);
7919 SDValue RHS = N->getOperand(3);
7920 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
7921
7922 unsigned Opcode = 0;
7923 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00007924 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007925 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00007926 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007927 IsReversed = true ; // x CC y ? y : x
7928 } else {
7929 return SDValue();
7930 }
7931
Bob Wilsone742bb52010-02-24 22:15:53 +00007932 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007933 switch (CC) {
7934 default: break;
7935 case ISD::SETOLT:
7936 case ISD::SETOLE:
7937 case ISD::SETLT:
7938 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007939 case ISD::SETULT:
7940 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00007941 // If LHS is NaN, an ordered comparison will be false and the result will
7942 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
7943 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
7944 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
7945 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
7946 break;
7947 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
7948 // will return -0, so vmin can only be used for unsafe math or if one of
7949 // the operands is known to be nonzero.
7950 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
7951 !UnsafeFPMath &&
7952 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
7953 break;
7954 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007955 break;
7956
7957 case ISD::SETOGT:
7958 case ISD::SETOGE:
7959 case ISD::SETGT:
7960 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007961 case ISD::SETUGT:
7962 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00007963 // If LHS is NaN, an ordered comparison will be false and the result will
7964 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
7965 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
7966 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
7967 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
7968 break;
7969 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
7970 // will return +0, so vmax can only be used for unsafe math or if one of
7971 // the operands is known to be nonzero.
7972 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
7973 !UnsafeFPMath &&
7974 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
7975 break;
7976 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007977 break;
7978 }
7979
7980 if (!Opcode)
7981 return SDValue();
7982 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
7983}
7984
Evan Chenge721f5c2011-07-13 00:42:17 +00007985/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
7986SDValue
7987ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
7988 SDValue Cmp = N->getOperand(4);
7989 if (Cmp.getOpcode() != ARMISD::CMPZ)
7990 // Only looking at EQ and NE cases.
7991 return SDValue();
7992
7993 EVT VT = N->getValueType(0);
7994 DebugLoc dl = N->getDebugLoc();
7995 SDValue LHS = Cmp.getOperand(0);
7996 SDValue RHS = Cmp.getOperand(1);
7997 SDValue FalseVal = N->getOperand(0);
7998 SDValue TrueVal = N->getOperand(1);
7999 SDValue ARMcc = N->getOperand(2);
Jim Grosbachb04546f2011-09-13 20:30:37 +00008000 ARMCC::CondCodes CC =
8001 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chenge721f5c2011-07-13 00:42:17 +00008002
8003 // Simplify
8004 // mov r1, r0
8005 // cmp r1, x
8006 // mov r0, y
8007 // moveq r0, x
8008 // to
8009 // cmp r0, x
8010 // movne r0, y
8011 //
8012 // mov r1, r0
8013 // cmp r1, x
8014 // mov r0, x
8015 // movne r0, y
8016 // to
8017 // cmp r0, x
8018 // movne r0, y
8019 /// FIXME: Turn this into a target neutral optimization?
8020 SDValue Res;
Evan Cheng9b88d2d2011-09-28 23:16:31 +00008021 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chenge721f5c2011-07-13 00:42:17 +00008022 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
8023 N->getOperand(3), Cmp);
8024 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
8025 SDValue ARMcc;
8026 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
8027 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
8028 N->getOperand(3), NewCmp);
8029 }
8030
8031 if (Res.getNode()) {
8032 APInt KnownZero, KnownOne;
8033 APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits());
8034 DAG.ComputeMaskedBits(SDValue(N,0), Mask, KnownZero, KnownOne);
8035 // Capture demanded bits information that would be otherwise lost.
8036 if (KnownZero == 0xfffffffe)
8037 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8038 DAG.getValueType(MVT::i1));
8039 else if (KnownZero == 0xffffff00)
8040 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8041 DAG.getValueType(MVT::i8));
8042 else if (KnownZero == 0xffff0000)
8043 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8044 DAG.getValueType(MVT::i16));
8045 }
8046
8047 return Res;
8048}
8049
Dan Gohman475871a2008-07-27 21:46:04 +00008050SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008051 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008052 switch (N->getOpcode()) {
8053 default: break;
Tanya Lattner189531f2011-06-14 23:48:48 +00008054 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008055 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00008056 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008057 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00008058 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00008059 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00008060 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008061 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00008062 case ISD::STORE: return PerformSTORECombine(N, DCI);
8063 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
8064 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00008065 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008066 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00008067 case ISD::FP_TO_SINT:
8068 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
8069 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008070 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00008071 case ISD::SHL:
8072 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008073 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00008074 case ISD::SIGN_EXTEND:
8075 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008076 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
8077 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00008078 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00008079 case ARMISD::VLD2DUP:
8080 case ARMISD::VLD3DUP:
8081 case ARMISD::VLD4DUP:
8082 return CombineBaseUpdate(N, DCI);
8083 case ISD::INTRINSIC_VOID:
8084 case ISD::INTRINSIC_W_CHAIN:
8085 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
8086 case Intrinsic::arm_neon_vld1:
8087 case Intrinsic::arm_neon_vld2:
8088 case Intrinsic::arm_neon_vld3:
8089 case Intrinsic::arm_neon_vld4:
8090 case Intrinsic::arm_neon_vld2lane:
8091 case Intrinsic::arm_neon_vld3lane:
8092 case Intrinsic::arm_neon_vld4lane:
8093 case Intrinsic::arm_neon_vst1:
8094 case Intrinsic::arm_neon_vst2:
8095 case Intrinsic::arm_neon_vst3:
8096 case Intrinsic::arm_neon_vst4:
8097 case Intrinsic::arm_neon_vst2lane:
8098 case Intrinsic::arm_neon_vst3lane:
8099 case Intrinsic::arm_neon_vst4lane:
8100 return CombineBaseUpdate(N, DCI);
8101 default: break;
8102 }
8103 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008104 }
Dan Gohman475871a2008-07-27 21:46:04 +00008105 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008106}
8107
Evan Cheng31959b12011-02-02 01:06:55 +00008108bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
8109 EVT VT) const {
8110 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
8111}
8112
Bill Wendlingaf566342009-08-15 21:21:19 +00008113bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00008114 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00008115 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00008116
8117 switch (VT.getSimpleVT().SimpleTy) {
8118 default:
8119 return false;
8120 case MVT::i8:
8121 case MVT::i16:
8122 case MVT::i32:
8123 return true;
8124 // FIXME: VLD1 etc with standard alignment is legal.
8125 }
8126}
8127
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008128static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
8129 unsigned AlignCheck) {
8130 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
8131 (DstAlign == 0 || DstAlign % AlignCheck == 0));
8132}
8133
8134EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
8135 unsigned DstAlign, unsigned SrcAlign,
Lang Hamesa1e78882011-11-02 23:37:04 +00008136 bool IsZeroVal,
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008137 bool MemcpyStrSrc,
8138 MachineFunction &MF) const {
8139 const Function *F = MF.getFunction();
8140
8141 // See if we can use NEON instructions for this...
Lang Hamesa1e78882011-11-02 23:37:04 +00008142 if (IsZeroVal &&
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008143 !F->hasFnAttr(Attribute::NoImplicitFloat) &&
8144 Subtarget->hasNEON()) {
8145 if (memOpAlign(SrcAlign, DstAlign, 16) && Size >= 16) {
8146 return MVT::v4i32;
8147 } else if (memOpAlign(SrcAlign, DstAlign, 8) && Size >= 8) {
8148 return MVT::v2i32;
8149 }
8150 }
8151
Lang Hames5207bf22011-11-08 18:56:23 +00008152 // Lowering to i32/i16 if the size permits.
8153 if (Size >= 4) {
8154 return MVT::i32;
8155 } else if (Size >= 2) {
8156 return MVT::i16;
8157 }
8158
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008159 // Let the target-independent logic figure it out.
8160 return MVT::Other;
8161}
8162
Evan Chenge6c835f2009-08-14 20:09:37 +00008163static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
8164 if (V < 0)
8165 return false;
8166
8167 unsigned Scale = 1;
8168 switch (VT.getSimpleVT().SimpleTy) {
8169 default: return false;
8170 case MVT::i1:
8171 case MVT::i8:
8172 // Scale == 1;
8173 break;
8174 case MVT::i16:
8175 // Scale == 2;
8176 Scale = 2;
8177 break;
8178 case MVT::i32:
8179 // Scale == 4;
8180 Scale = 4;
8181 break;
8182 }
8183
8184 if ((V & (Scale - 1)) != 0)
8185 return false;
8186 V /= Scale;
8187 return V == (V & ((1LL << 5) - 1));
8188}
8189
8190static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
8191 const ARMSubtarget *Subtarget) {
8192 bool isNeg = false;
8193 if (V < 0) {
8194 isNeg = true;
8195 V = - V;
8196 }
8197
8198 switch (VT.getSimpleVT().SimpleTy) {
8199 default: return false;
8200 case MVT::i1:
8201 case MVT::i8:
8202 case MVT::i16:
8203 case MVT::i32:
8204 // + imm12 or - imm8
8205 if (isNeg)
8206 return V == (V & ((1LL << 8) - 1));
8207 return V == (V & ((1LL << 12) - 1));
8208 case MVT::f32:
8209 case MVT::f64:
8210 // Same as ARM mode. FIXME: NEON?
8211 if (!Subtarget->hasVFP2())
8212 return false;
8213 if ((V & 3) != 0)
8214 return false;
8215 V >>= 2;
8216 return V == (V & ((1LL << 8) - 1));
8217 }
8218}
8219
Evan Chengb01fad62007-03-12 23:30:29 +00008220/// isLegalAddressImmediate - Return true if the integer value can be used
8221/// as the offset of the target addressing mode for load / store of the
8222/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00008223static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00008224 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00008225 if (V == 0)
8226 return true;
8227
Evan Cheng65011532009-03-09 19:15:00 +00008228 if (!VT.isSimple())
8229 return false;
8230
Evan Chenge6c835f2009-08-14 20:09:37 +00008231 if (Subtarget->isThumb1Only())
8232 return isLegalT1AddressImmediate(V, VT);
8233 else if (Subtarget->isThumb2())
8234 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00008235
Evan Chenge6c835f2009-08-14 20:09:37 +00008236 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00008237 if (V < 0)
8238 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00008239 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00008240 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008241 case MVT::i1:
8242 case MVT::i8:
8243 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00008244 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008245 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008246 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00008247 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008248 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008249 case MVT::f32:
8250 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00008251 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00008252 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00008253 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00008254 return false;
8255 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008256 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00008257 }
Evan Chenga8e29892007-01-19 07:51:42 +00008258}
8259
Evan Chenge6c835f2009-08-14 20:09:37 +00008260bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
8261 EVT VT) const {
8262 int Scale = AM.Scale;
8263 if (Scale < 0)
8264 return false;
8265
8266 switch (VT.getSimpleVT().SimpleTy) {
8267 default: return false;
8268 case MVT::i1:
8269 case MVT::i8:
8270 case MVT::i16:
8271 case MVT::i32:
8272 if (Scale == 1)
8273 return true;
8274 // r + r << imm
8275 Scale = Scale & ~1;
8276 return Scale == 2 || Scale == 4 || Scale == 8;
8277 case MVT::i64:
8278 // r + r
8279 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
8280 return true;
8281 return false;
8282 case MVT::isVoid:
8283 // Note, we allow "void" uses (basically, uses that aren't loads or
8284 // stores), because arm allows folding a scale into many arithmetic
8285 // operations. This should be made more precise and revisited later.
8286
8287 // Allow r << imm, but the imm has to be a multiple of two.
8288 if (Scale & 1) return false;
8289 return isPowerOf2_32(Scale);
8290 }
8291}
8292
Chris Lattner37caf8c2007-04-09 23:33:39 +00008293/// isLegalAddressingMode - Return true if the addressing mode represented
8294/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008295bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008296 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008297 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00008298 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00008299 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008300
Chris Lattner37caf8c2007-04-09 23:33:39 +00008301 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008302 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008303 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008304
Chris Lattner37caf8c2007-04-09 23:33:39 +00008305 switch (AM.Scale) {
8306 case 0: // no scale reg, must be "r+i" or "r", or "i".
8307 break;
8308 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00008309 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00008310 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008311 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00008312 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008313 // ARM doesn't support any R+R*scale+imm addr modes.
8314 if (AM.BaseOffs)
8315 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008316
Bob Wilson2c7dab12009-04-08 17:55:28 +00008317 if (!VT.isSimple())
8318 return false;
8319
Evan Chenge6c835f2009-08-14 20:09:37 +00008320 if (Subtarget->isThumb2())
8321 return isLegalT2ScaledAddressingMode(AM, VT);
8322
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008323 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00008324 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00008325 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008326 case MVT::i1:
8327 case MVT::i8:
8328 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008329 if (Scale < 0) Scale = -Scale;
8330 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008331 return true;
8332 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00008333 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008334 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00008335 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00008336 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008337 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008338 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00008339 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008340
Owen Anderson825b72b2009-08-11 20:47:22 +00008341 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00008342 // Note, we allow "void" uses (basically, uses that aren't loads or
8343 // stores), because arm allows folding a scale into many arithmetic
8344 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008345
Chris Lattner37caf8c2007-04-09 23:33:39 +00008346 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00008347 if (Scale & 1) return false;
8348 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00008349 }
8350 break;
Evan Chengb01fad62007-03-12 23:30:29 +00008351 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00008352 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00008353}
8354
Evan Cheng77e47512009-11-11 19:05:52 +00008355/// isLegalICmpImmediate - Return true if the specified immediate is legal
8356/// icmp immediate, that is the target has icmp instructions which can compare
8357/// a register against the immediate without having to materialize the
8358/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00008359bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00008360 if (!Subtarget->isThumb())
8361 return ARM_AM::getSOImmVal(Imm) != -1;
8362 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00008363 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00008364 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00008365}
8366
Dan Gohmancca82142011-05-03 00:46:49 +00008367/// isLegalAddImmediate - Return true if the specified immediate is legal
8368/// add immediate, that is the target has add instructions which can add
8369/// a register with the immediate without having to materialize the
8370/// immediate into a register.
8371bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
8372 return ARM_AM::getSOImmVal(Imm) != -1;
8373}
8374
Owen Andersone50ed302009-08-10 22:56:29 +00008375static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00008376 bool isSEXTLoad, SDValue &Base,
8377 SDValue &Offset, bool &isInc,
8378 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00008379 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
8380 return false;
8381
Owen Anderson825b72b2009-08-11 20:47:22 +00008382 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00008383 // AddressingMode 3
8384 Base = Ptr->getOperand(0);
8385 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008386 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00008387 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008388 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00008389 isInc = false;
8390 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8391 return true;
8392 }
8393 }
8394 isInc = (Ptr->getOpcode() == ISD::ADD);
8395 Offset = Ptr->getOperand(1);
8396 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00008397 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00008398 // AddressingMode 2
8399 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008400 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00008401 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008402 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00008403 isInc = false;
8404 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8405 Base = Ptr->getOperand(0);
8406 return true;
8407 }
8408 }
8409
8410 if (Ptr->getOpcode() == ISD::ADD) {
8411 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00008412 ARM_AM::ShiftOpc ShOpcVal=
8413 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00008414 if (ShOpcVal != ARM_AM::no_shift) {
8415 Base = Ptr->getOperand(1);
8416 Offset = Ptr->getOperand(0);
8417 } else {
8418 Base = Ptr->getOperand(0);
8419 Offset = Ptr->getOperand(1);
8420 }
8421 return true;
8422 }
8423
8424 isInc = (Ptr->getOpcode() == ISD::ADD);
8425 Base = Ptr->getOperand(0);
8426 Offset = Ptr->getOperand(1);
8427 return true;
8428 }
8429
Jim Grosbache5165492009-11-09 00:11:35 +00008430 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00008431 return false;
8432}
8433
Owen Andersone50ed302009-08-10 22:56:29 +00008434static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00008435 bool isSEXTLoad, SDValue &Base,
8436 SDValue &Offset, bool &isInc,
8437 SelectionDAG &DAG) {
8438 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
8439 return false;
8440
8441 Base = Ptr->getOperand(0);
8442 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
8443 int RHSC = (int)RHS->getZExtValue();
8444 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
8445 assert(Ptr->getOpcode() == ISD::ADD);
8446 isInc = false;
8447 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8448 return true;
8449 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
8450 isInc = Ptr->getOpcode() == ISD::ADD;
8451 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
8452 return true;
8453 }
8454 }
8455
8456 return false;
8457}
8458
Evan Chenga8e29892007-01-19 07:51:42 +00008459/// getPreIndexedAddressParts - returns true by value, base pointer and
8460/// offset pointer and addressing mode by reference if the node's address
8461/// can be legally represented as pre-indexed load / store address.
8462bool
Dan Gohman475871a2008-07-27 21:46:04 +00008463ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
8464 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00008465 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00008466 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008467 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00008468 return false;
8469
Owen Andersone50ed302009-08-10 22:56:29 +00008470 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00008471 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00008472 bool isSEXTLoad = false;
8473 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
8474 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008475 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00008476 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
8477 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
8478 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008479 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00008480 } else
8481 return false;
8482
8483 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00008484 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00008485 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00008486 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
8487 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00008488 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00008489 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00008490 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00008491 if (!isLegal)
8492 return false;
8493
8494 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
8495 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00008496}
8497
8498/// getPostIndexedAddressParts - returns true by value, base pointer and
8499/// offset pointer and addressing mode by reference if this node can be
8500/// combined with a load / store to form a post-indexed load / store.
8501bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00008502 SDValue &Base,
8503 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00008504 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00008505 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008506 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00008507 return false;
8508
Owen Andersone50ed302009-08-10 22:56:29 +00008509 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00008510 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00008511 bool isSEXTLoad = false;
8512 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008513 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00008514 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00008515 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
8516 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008517 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00008518 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00008519 } else
8520 return false;
8521
8522 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00008523 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00008524 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00008525 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00008526 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00008527 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00008528 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
8529 isInc, DAG);
8530 if (!isLegal)
8531 return false;
8532
Evan Cheng28dad2a2010-05-18 21:31:17 +00008533 if (Ptr != Base) {
8534 // Swap base ptr and offset to catch more post-index load / store when
8535 // it's legal. In Thumb2 mode, offset must be an immediate.
8536 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
8537 !Subtarget->isThumb2())
8538 std::swap(Base, Offset);
8539
8540 // Post-indexed load / store update the base pointer.
8541 if (Ptr != Base)
8542 return false;
8543 }
8544
Evan Chenge88d5ce2009-07-02 07:28:31 +00008545 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
8546 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00008547}
8548
Dan Gohman475871a2008-07-27 21:46:04 +00008549void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00008550 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008551 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008552 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008553 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00008554 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008555 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00008556 switch (Op.getOpcode()) {
8557 default: break;
8558 case ARMISD::CMOV: {
8559 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00008560 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00008561 if (KnownZero == 0 && KnownOne == 0) return;
8562
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008563 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00008564 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
8565 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00008566 KnownZero &= KnownZeroRHS;
8567 KnownOne &= KnownOneRHS;
8568 return;
8569 }
8570 }
8571}
8572
8573//===----------------------------------------------------------------------===//
8574// ARM Inline Assembly Support
8575//===----------------------------------------------------------------------===//
8576
Evan Cheng55d42002011-01-08 01:24:27 +00008577bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
8578 // Looking for "rev" which is V6+.
8579 if (!Subtarget->hasV6Ops())
8580 return false;
8581
8582 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
8583 std::string AsmStr = IA->getAsmString();
8584 SmallVector<StringRef, 4> AsmPieces;
8585 SplitString(AsmStr, AsmPieces, ";\n");
8586
8587 switch (AsmPieces.size()) {
8588 default: return false;
8589 case 1:
8590 AsmStr = AsmPieces[0];
8591 AsmPieces.clear();
8592 SplitString(AsmStr, AsmPieces, " \t,");
8593
8594 // rev $0, $1
8595 if (AsmPieces.size() == 3 &&
8596 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
8597 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008598 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00008599 if (Ty && Ty->getBitWidth() == 32)
8600 return IntrinsicLowering::LowerToByteSwap(CI);
8601 }
8602 break;
8603 }
8604
8605 return false;
8606}
8607
Evan Chenga8e29892007-01-19 07:51:42 +00008608/// getConstraintType - Given a constraint letter, return the type of
8609/// constraint it is for this target.
8610ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00008611ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
8612 if (Constraint.size() == 1) {
8613 switch (Constraint[0]) {
8614 default: break;
8615 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008616 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00008617 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00008618 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008619 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00008620 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00008621 // An address with a single base register. Due to the way we
8622 // currently handle addresses it is the same as an 'r' memory constraint.
8623 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00008624 }
Eric Christopher1312ca82011-06-21 22:10:57 +00008625 } else if (Constraint.size() == 2) {
8626 switch (Constraint[0]) {
8627 default: break;
8628 // All 'U+' constraints are addresses.
8629 case 'U': return C_Memory;
8630 }
Evan Chenga8e29892007-01-19 07:51:42 +00008631 }
Chris Lattner4234f572007-03-25 02:14:49 +00008632 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00008633}
8634
John Thompson44ab89e2010-10-29 17:29:13 +00008635/// Examine constraint type and operand type and determine a weight value.
8636/// This object must already have been set up with the operand type
8637/// and the current alternative constraint selected.
8638TargetLowering::ConstraintWeight
8639ARMTargetLowering::getSingleConstraintMatchWeight(
8640 AsmOperandInfo &info, const char *constraint) const {
8641 ConstraintWeight weight = CW_Invalid;
8642 Value *CallOperandVal = info.CallOperandVal;
8643 // If we don't have a value, we can't do a match,
8644 // but allow it at the lowest weight.
8645 if (CallOperandVal == NULL)
8646 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008647 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00008648 // Look at the constraint type.
8649 switch (*constraint) {
8650 default:
8651 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
8652 break;
8653 case 'l':
8654 if (type->isIntegerTy()) {
8655 if (Subtarget->isThumb())
8656 weight = CW_SpecificReg;
8657 else
8658 weight = CW_Register;
8659 }
8660 break;
8661 case 'w':
8662 if (type->isFloatingPointTy())
8663 weight = CW_Register;
8664 break;
8665 }
8666 return weight;
8667}
8668
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008669typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
8670RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00008671ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00008672 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00008673 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00008674 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00008675 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00008676 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00008677 if (Subtarget->isThumb())
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008678 return RCPair(0U, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +00008679 else
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008680 return RCPair(0U, ARM::GPRRegisterClass);
Eric Christopher73744df2011-06-30 23:23:01 +00008681 case 'h': // High regs or no regs.
8682 if (Subtarget->isThumb())
Andrew Trick3af7a672011-09-20 03:06:13 +00008683 return RCPair(0U, ARM::hGPRRegisterClass);
Eric Christopher1070f822011-07-01 00:19:27 +00008684 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008685 case 'r':
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008686 return RCPair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008687 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00008688 if (VT == MVT::f32)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008689 return RCPair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00008690 if (VT.getSizeInBits() == 64)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008691 return RCPair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00008692 if (VT.getSizeInBits() == 128)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008693 return RCPair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008694 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00008695 case 'x':
8696 if (VT == MVT::f32)
Andrew Trick3af7a672011-09-20 03:06:13 +00008697 return RCPair(0U, ARM::SPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008698 if (VT.getSizeInBits() == 64)
Andrew Trick3af7a672011-09-20 03:06:13 +00008699 return RCPair(0U, ARM::DPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008700 if (VT.getSizeInBits() == 128)
Andrew Trick3af7a672011-09-20 03:06:13 +00008701 return RCPair(0U, ARM::QPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008702 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008703 case 't':
8704 if (VT == MVT::f32)
Andrew Trick3af7a672011-09-20 03:06:13 +00008705 return RCPair(0U, ARM::SPRRegisterClass);
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008706 break;
Evan Chenga8e29892007-01-19 07:51:42 +00008707 }
8708 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00008709 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00008710 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00008711
Evan Chenga8e29892007-01-19 07:51:42 +00008712 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
8713}
8714
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008715/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
8716/// vector. If it is invalid, don't add anything to Ops.
8717void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00008718 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008719 std::vector<SDValue>&Ops,
8720 SelectionDAG &DAG) const {
8721 SDValue Result(0, 0);
8722
Eric Christopher100c8332011-06-02 23:16:42 +00008723 // Currently only support length 1 constraints.
8724 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00008725
Eric Christopher100c8332011-06-02 23:16:42 +00008726 char ConstraintLetter = Constraint[0];
8727 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008728 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00008729 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008730 case 'I': case 'J': case 'K': case 'L':
8731 case 'M': case 'N': case 'O':
8732 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
8733 if (!C)
8734 return;
8735
8736 int64_t CVal64 = C->getSExtValue();
8737 int CVal = (int) CVal64;
8738 // None of these constraints allow values larger than 32 bits. Check
8739 // that the value fits in an int.
8740 if (CVal != CVal64)
8741 return;
8742
Eric Christopher100c8332011-06-02 23:16:42 +00008743 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00008744 case 'j':
Andrew Trick3af7a672011-09-20 03:06:13 +00008745 // Constant suitable for movw, must be between 0 and
8746 // 65535.
8747 if (Subtarget->hasV6T2Ops())
8748 if (CVal >= 0 && CVal <= 65535)
8749 break;
8750 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008751 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008752 if (Subtarget->isThumb1Only()) {
8753 // This must be a constant between 0 and 255, for ADD
8754 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008755 if (CVal >= 0 && CVal <= 255)
8756 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008757 } else if (Subtarget->isThumb2()) {
8758 // A constant that can be used as an immediate value in a
8759 // data-processing instruction.
8760 if (ARM_AM::getT2SOImmVal(CVal) != -1)
8761 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008762 } else {
8763 // A constant that can be used as an immediate value in a
8764 // data-processing instruction.
8765 if (ARM_AM::getSOImmVal(CVal) != -1)
8766 break;
8767 }
8768 return;
8769
8770 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008771 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008772 // This must be a constant between -255 and -1, for negated ADD
8773 // immediates. This can be used in GCC with an "n" modifier that
8774 // prints the negated value, for use with SUB instructions. It is
8775 // not useful otherwise but is implemented for compatibility.
8776 if (CVal >= -255 && CVal <= -1)
8777 break;
8778 } else {
8779 // This must be a constant between -4095 and 4095. It is not clear
8780 // what this constraint is intended for. Implemented for
8781 // compatibility with GCC.
8782 if (CVal >= -4095 && CVal <= 4095)
8783 break;
8784 }
8785 return;
8786
8787 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008788 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008789 // A 32-bit value where only one byte has a nonzero value. Exclude
8790 // zero to match GCC. This constraint is used by GCC internally for
8791 // constants that can be loaded with a move/shift combination.
8792 // It is not useful otherwise but is implemented for compatibility.
8793 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
8794 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008795 } else if (Subtarget->isThumb2()) {
8796 // A constant whose bitwise inverse can be used as an immediate
8797 // value in a data-processing instruction. This can be used in GCC
8798 // with a "B" modifier that prints the inverted value, for use with
8799 // BIC and MVN instructions. It is not useful otherwise but is
8800 // implemented for compatibility.
8801 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
8802 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008803 } else {
8804 // A constant whose bitwise inverse can be used as an immediate
8805 // value in a data-processing instruction. This can be used in GCC
8806 // with a "B" modifier that prints the inverted value, for use with
8807 // BIC and MVN instructions. It is not useful otherwise but is
8808 // implemented for compatibility.
8809 if (ARM_AM::getSOImmVal(~CVal) != -1)
8810 break;
8811 }
8812 return;
8813
8814 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008815 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008816 // This must be a constant between -7 and 7,
8817 // for 3-operand ADD/SUB immediate instructions.
8818 if (CVal >= -7 && CVal < 7)
8819 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008820 } else if (Subtarget->isThumb2()) {
8821 // A constant whose negation can be used as an immediate value in a
8822 // data-processing instruction. This can be used in GCC with an "n"
8823 // modifier that prints the negated value, for use with SUB
8824 // instructions. It is not useful otherwise but is implemented for
8825 // compatibility.
8826 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
8827 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008828 } else {
8829 // A constant whose negation can be used as an immediate value in a
8830 // data-processing instruction. This can be used in GCC with an "n"
8831 // modifier that prints the negated value, for use with SUB
8832 // instructions. It is not useful otherwise but is implemented for
8833 // compatibility.
8834 if (ARM_AM::getSOImmVal(-CVal) != -1)
8835 break;
8836 }
8837 return;
8838
8839 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008840 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008841 // This must be a multiple of 4 between 0 and 1020, for
8842 // ADD sp + immediate.
8843 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
8844 break;
8845 } else {
8846 // A power of two or a constant between 0 and 32. This is used in
8847 // GCC for the shift amount on shifted register operands, but it is
8848 // useful in general for any shift amounts.
8849 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
8850 break;
8851 }
8852 return;
8853
8854 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008855 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008856 // This must be a constant between 0 and 31, for shift amounts.
8857 if (CVal >= 0 && CVal <= 31)
8858 break;
8859 }
8860 return;
8861
8862 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008863 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008864 // This must be a multiple of 4 between -508 and 508, for
8865 // ADD/SUB sp = sp + immediate.
8866 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
8867 break;
8868 }
8869 return;
8870 }
8871 Result = DAG.getTargetConstant(CVal, Op.getValueType());
8872 break;
8873 }
8874
8875 if (Result.getNode()) {
8876 Ops.push_back(Result);
8877 return;
8878 }
Dale Johannesen1784d162010-06-25 21:55:36 +00008879 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008880}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00008881
8882bool
8883ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
8884 // The ARM target isn't yet aware of offsets.
8885 return false;
8886}
Evan Cheng39382422009-10-28 01:44:26 +00008887
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008888bool ARM::isBitFieldInvertedMask(unsigned v) {
8889 if (v == 0xffffffff)
8890 return 0;
8891 // there can be 1's on either or both "outsides", all the "inside"
8892 // bits must be 0's
8893 unsigned int lsb = 0, msb = 31;
8894 while (v & (1 << msb)) --msb;
8895 while (v & (1 << lsb)) ++lsb;
8896 for (unsigned int i = lsb; i <= msb; ++i) {
8897 if (v & (1 << i))
8898 return 0;
8899 }
8900 return 1;
8901}
8902
Evan Cheng39382422009-10-28 01:44:26 +00008903/// isFPImmLegal - Returns true if the target can instruction select the
8904/// specified FP immediate natively. If false, the legalizer will
8905/// materialize the FP immediate as a load from a constant pool.
8906bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
8907 if (!Subtarget->hasVFP3())
8908 return false;
8909 if (VT == MVT::f32)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00008910 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00008911 if (VT == MVT::f64)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00008912 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00008913 return false;
8914}
Bob Wilson65ffec42010-09-21 17:56:22 +00008915
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008916/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00008917/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
8918/// specified in the intrinsic calls.
8919bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
8920 const CallInst &I,
8921 unsigned Intrinsic) const {
8922 switch (Intrinsic) {
8923 case Intrinsic::arm_neon_vld1:
8924 case Intrinsic::arm_neon_vld2:
8925 case Intrinsic::arm_neon_vld3:
8926 case Intrinsic::arm_neon_vld4:
8927 case Intrinsic::arm_neon_vld2lane:
8928 case Intrinsic::arm_neon_vld3lane:
8929 case Intrinsic::arm_neon_vld4lane: {
8930 Info.opc = ISD::INTRINSIC_W_CHAIN;
8931 // Conservatively set memVT to the entire set of vectors loaded.
8932 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
8933 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
8934 Info.ptrVal = I.getArgOperand(0);
8935 Info.offset = 0;
8936 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
8937 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
8938 Info.vol = false; // volatile loads with NEON intrinsics not supported
8939 Info.readMem = true;
8940 Info.writeMem = false;
8941 return true;
8942 }
8943 case Intrinsic::arm_neon_vst1:
8944 case Intrinsic::arm_neon_vst2:
8945 case Intrinsic::arm_neon_vst3:
8946 case Intrinsic::arm_neon_vst4:
8947 case Intrinsic::arm_neon_vst2lane:
8948 case Intrinsic::arm_neon_vst3lane:
8949 case Intrinsic::arm_neon_vst4lane: {
8950 Info.opc = ISD::INTRINSIC_VOID;
8951 // Conservatively set memVT to the entire set of vectors stored.
8952 unsigned NumElts = 0;
8953 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008954 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +00008955 if (!ArgTy->isVectorTy())
8956 break;
8957 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
8958 }
8959 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
8960 Info.ptrVal = I.getArgOperand(0);
8961 Info.offset = 0;
8962 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
8963 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
8964 Info.vol = false; // volatile stores with NEON intrinsics not supported
8965 Info.readMem = false;
8966 Info.writeMem = true;
8967 return true;
8968 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00008969 case Intrinsic::arm_strexd: {
8970 Info.opc = ISD::INTRINSIC_W_CHAIN;
8971 Info.memVT = MVT::i64;
8972 Info.ptrVal = I.getArgOperand(2);
8973 Info.offset = 0;
8974 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00008975 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00008976 Info.readMem = false;
8977 Info.writeMem = true;
8978 return true;
8979 }
8980 case Intrinsic::arm_ldrexd: {
8981 Info.opc = ISD::INTRINSIC_W_CHAIN;
8982 Info.memVT = MVT::i64;
8983 Info.ptrVal = I.getArgOperand(0);
8984 Info.offset = 0;
8985 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00008986 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00008987 Info.readMem = true;
8988 Info.writeMem = false;
8989 return true;
8990 }
Bob Wilson65ffec42010-09-21 17:56:22 +00008991 default:
8992 break;
8993 }
8994
8995 return false;
8996}