blob: 2045073fb6cdd5f87ade943d8cd19c9fe9990f41 [file] [log] [blame]
Evan Chenga8e29892007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chenga8e29892007-01-19 07:51:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that ARM uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Dale Johannesen51e28e62010-06-03 21:09:53 +000015#define DEBUG_TYPE "arm-isel"
Evan Chenga8e29892007-01-19 07:51:42 +000016#include "ARM.h"
Eric Christopher6f2ccef2010-09-10 22:42:06 +000017#include "ARMCallingConv.h"
Evan Chenga8e29892007-01-19 07:51:42 +000018#include "ARMConstantPoolValue.h"
19#include "ARMISelLowering.h"
20#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +000021#include "ARMPerfectShuffle.h"
Evan Chenga8e29892007-01-19 07:51:42 +000022#include "ARMRegisterInfo.h"
23#include "ARMSubtarget.h"
24#include "ARMTargetMachine.h"
Chris Lattner80ec2792009-08-02 00:34:36 +000025#include "ARMTargetObjectFile.h"
Evan Chengee04a6d2011-07-20 23:34:39 +000026#include "MCTargetDesc/ARMAddressingModes.h"
Evan Chenga8e29892007-01-19 07:51:42 +000027#include "llvm/CallingConv.h"
28#include "llvm/Constants.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000029#include "llvm/Function.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000030#include "llvm/GlobalValue.h"
Evan Cheng27707472007-03-16 08:43:56 +000031#include "llvm/Instruction.h"
Bob Wilson65ffec42010-09-21 17:56:22 +000032#include "llvm/Instructions.h"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000033#include "llvm/Intrinsics.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000034#include "llvm/Type.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000035#include "llvm/CodeGen/CallingConvLower.h"
Evan Cheng55d42002011-01-08 01:24:27 +000036#include "llvm/CodeGen/IntrinsicLowering.h"
Evan Chenga8e29892007-01-19 07:51:42 +000037#include "llvm/CodeGen/MachineBasicBlock.h"
38#include "llvm/CodeGen/MachineFrameInfo.h"
39#include "llvm/CodeGen/MachineFunction.h"
40#include "llvm/CodeGen/MachineInstrBuilder.h"
Bill Wendling2a850152011-10-05 00:02:33 +000041#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000042#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chenga8e29892007-01-19 07:51:42 +000043#include "llvm/CodeGen/SelectionDAG.h"
Bill Wendling94a1c632010-03-09 02:46:12 +000044#include "llvm/MC/MCSectionMachO.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000045#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000046#include "llvm/ADT/VectorExtras.h"
Evan Cheng55d42002011-01-08 01:24:27 +000047#include "llvm/ADT/StringExtras.h"
Dale Johannesen51e28e62010-06-03 21:09:53 +000048#include "llvm/ADT/Statistic.h"
Jim Grosbache7b52522010-04-14 22:28:31 +000049#include "llvm/Support/CommandLine.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000050#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000051#include "llvm/Support/MathExtras.h"
Jim Grosbache801dc42009-12-12 01:40:06 +000052#include "llvm/Support/raw_ostream.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000053#include <sstream>
Evan Chenga8e29892007-01-19 07:51:42 +000054using namespace llvm;
55
Dale Johannesen51e28e62010-06-03 21:09:53 +000056STATISTIC(NumTailCalls, "Number of tail calls");
Evan Chengfc8475b2011-01-19 02:16:49 +000057STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Dale Johannesen51e28e62010-06-03 21:09:53 +000058
Bob Wilson703af3a2010-08-13 22:43:33 +000059// This option should go away when tail calls fully work.
60static cl::opt<bool>
61EnableARMTailCalls("arm-tail-calls", cl::Hidden,
62 cl::desc("Generate tail calls (TEMPORARY OPTION)."),
63 cl::init(false));
64
Eric Christopher836c6242010-12-15 23:47:29 +000065cl::opt<bool>
Jim Grosbache7b52522010-04-14 22:28:31 +000066EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng515fe3a2010-07-08 02:08:50 +000067 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbache7b52522010-04-14 22:28:31 +000068 cl::init(false));
69
Evan Cheng46df4eb2010-06-16 07:35:02 +000070static cl::opt<bool>
71ARMInterworking("arm-interworking", cl::Hidden,
72 cl::desc("Enable / disable ARM interworking (for debugging only)"),
73 cl::init(true));
74
Benjamin Kramer0861f572011-11-26 23:01:57 +000075namespace {
Cameron Zwaricha86686e2011-06-10 20:59:24 +000076 class ARMCCState : public CCState {
77 public:
78 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
79 const TargetMachine &TM, SmallVector<CCValAssign, 16> &locs,
80 LLVMContext &C, ParmContext PC)
81 : CCState(CC, isVarArg, MF, TM, locs, C) {
82 assert(((PC == Call) || (PC == Prologue)) &&
83 "ARMCCState users must specify whether their context is call"
84 "or prologue generation.");
85 CallOrPrologue = PC;
86 }
87 };
88}
89
Stuart Hastingsc7315872011-04-20 16:47:52 +000090// The APCS parameter registers.
91static const unsigned GPRArgRegs[] = {
92 ARM::R0, ARM::R1, ARM::R2, ARM::R3
93};
94
Owen Andersone50ed302009-08-10 22:56:29 +000095void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
96 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000097 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000098 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000099 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
100 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000101
Owen Anderson70671842009-08-10 20:18:46 +0000102 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000103 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000104 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000105 }
106
Owen Andersone50ed302009-08-10 22:56:29 +0000107 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000108 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Duncan Sands28b77e92011-09-06 19:07:46 +0000109 setOperationAction(ISD::SETCC, VT.getSimpleVT(), Custom);
Eli Friedman5c89cb82011-10-24 23:08:52 +0000110 setOperationAction(ISD::INSERT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson3468c2e2010-11-03 16:24:50 +0000111 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Eli Friedman14e809c2011-11-09 23:36:02 +0000112 if (ElemTy == MVT::i32) {
113 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Custom);
114 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Custom);
115 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Custom);
116 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Custom);
117 } else {
Bob Wilson0696fdf2009-09-16 20:20:44 +0000118 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
119 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
120 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
121 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
122 }
Owen Anderson70671842009-08-10 20:18:46 +0000123 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
124 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Bob Wilson07f6e802010-06-16 21:34:01 +0000125 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
Bob Wilson5e8b8332011-01-07 04:59:04 +0000126 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Legal);
Bob Wilsond0910c42010-04-06 22:02:24 +0000127 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
128 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Eli Friedman15f58c52011-11-11 03:16:38 +0000129 setOperationAction(ISD::SIGN_EXTEND_INREG, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000130 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000131 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
132 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
133 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +0000134 }
135
136 // Promote all bit-wise operations.
137 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000138 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000139 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
140 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000141 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000142 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000143 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000144 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000145 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000146 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000147 }
Bob Wilson16330762009-09-16 00:17:28 +0000148
149 // Neon does not support vector divide/remainder operations.
150 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
151 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
152 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
153 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
154 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
155 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000156}
157
Owen Andersone50ed302009-08-10 22:56:29 +0000158void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000159 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000160 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000161}
162
Owen Andersone50ed302009-08-10 22:56:29 +0000163void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000164 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000165 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000166}
167
Chris Lattnerf0144122009-07-28 03:13:23 +0000168static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
169 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000170 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000171
Chris Lattner80ec2792009-08-02 00:34:36 +0000172 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000173}
174
Evan Chenga8e29892007-01-19 07:51:42 +0000175ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000176 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000177 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng31446872010-07-23 22:39:59 +0000178 RegInfo = TM.getRegisterInfo();
Evan Cheng3ef1c872010-09-10 01:29:16 +0000179 Itins = TM.getInstrItineraryData();
Evan Chenga8e29892007-01-19 07:51:42 +0000180
Duncan Sands28b77e92011-09-06 19:07:46 +0000181 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
182
Evan Chengb1df8f22007-04-27 08:15:43 +0000183 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000184 // Uses VFP for Thumb libfuncs if available.
185 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
186 // Single-precision floating-point arithmetic.
187 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
188 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
189 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
190 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000191
Evan Chengb1df8f22007-04-27 08:15:43 +0000192 // Double-precision floating-point arithmetic.
193 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
194 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
195 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
196 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000197
Evan Chengb1df8f22007-04-27 08:15:43 +0000198 // Single-precision comparisons.
199 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
200 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
201 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
202 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
203 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
204 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
205 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
206 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000207
Evan Chengb1df8f22007-04-27 08:15:43 +0000208 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
209 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
210 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
211 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
212 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
213 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
214 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
215 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000216
Evan Chengb1df8f22007-04-27 08:15:43 +0000217 // Double-precision comparisons.
218 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
219 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
220 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
221 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
222 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
223 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
224 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
225 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000226
Evan Chengb1df8f22007-04-27 08:15:43 +0000227 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
228 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
229 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
230 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
231 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
232 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
233 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
234 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000235
Evan Chengb1df8f22007-04-27 08:15:43 +0000236 // Floating-point to integer conversions.
237 // i64 conversions are done via library routines even when generating VFP
238 // instructions, so use the same ones.
239 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
240 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
241 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
242 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000243
Evan Chengb1df8f22007-04-27 08:15:43 +0000244 // Conversions between floating types.
245 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
246 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
247
248 // Integer to floating-point conversions.
249 // i64 conversions are done via library routines even when generating VFP
250 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000251 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
252 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000253 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
254 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
255 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
256 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
257 }
Evan Chenga8e29892007-01-19 07:51:42 +0000258 }
259
Bob Wilson2f954612009-05-22 17:38:41 +0000260 // These libcalls are not available in 32-bit.
261 setLibcallName(RTLIB::SHL_I128, 0);
262 setLibcallName(RTLIB::SRL_I128, 0);
263 setLibcallName(RTLIB::SRA_I128, 0);
264
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000265 if (Subtarget->isAAPCS_ABI()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000266 // Double-precision floating-point arithmetic helper functions
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000267 // RTABI chapter 4.1.2, Table 2
268 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd");
269 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv");
270 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul");
271 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub");
272 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS);
273 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS);
274 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS);
275 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS);
276
277 // Double-precision floating-point comparison helper functions
278 // RTABI chapter 4.1.2, Table 3
279 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq");
280 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
281 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq");
282 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ);
283 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt");
284 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
285 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple");
286 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
287 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge");
288 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
289 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt");
290 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
291 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun");
292 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
293 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun");
294 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
295 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS);
296 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS);
297 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS);
298 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS);
299 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS);
300 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS);
301 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS);
302 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS);
303
304 // Single-precision floating-point arithmetic helper functions
305 // RTABI chapter 4.1.2, Table 4
306 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd");
307 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv");
308 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul");
309 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub");
310 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS);
311 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS);
312 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS);
313 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS);
314
315 // Single-precision floating-point comparison helper functions
316 // RTABI chapter 4.1.2, Table 5
317 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq");
318 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
319 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq");
320 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ);
321 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt");
322 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
323 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple");
324 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
325 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge");
326 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
327 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt");
328 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
329 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun");
330 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
331 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun");
332 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
333 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS);
334 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS);
335 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS);
336 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS);
337 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS);
338 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS);
339 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS);
340 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS);
341
342 // Floating-point to integer conversions.
343 // RTABI chapter 4.1.2, Table 6
344 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz");
345 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz");
346 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz");
347 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz");
348 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz");
349 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz");
350 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz");
351 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz");
352 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS);
353 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS);
354 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS);
355 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS);
356 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS);
357 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS);
358 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS);
359 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS);
360
361 // Conversions between floating types.
362 // RTABI chapter 4.1.2, Table 7
363 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f");
364 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d");
365 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000366 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000367
368 // Integer to floating-point conversions.
369 // RTABI chapter 4.1.2, Table 8
370 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d");
371 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d");
372 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d");
373 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d");
374 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f");
375 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f");
376 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f");
377 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f");
378 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
379 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
380 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
381 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
382 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
383 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
384 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
385 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
386
387 // Long long helper functions
388 // RTABI chapter 4.2, Table 9
389 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul");
390 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
391 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
392 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
393 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
394 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
395 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
396 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
397 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
398 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
399 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
400 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
401
402 // Integer division functions
403 // RTABI chapter 4.3.1
404 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
405 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
406 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
407 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
408 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
409 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
410 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
411 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
412 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
413 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS);
414 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000415 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS);
Renato Golin1ec11fb2011-05-22 21:41:23 +0000416
417 // Memory operations
418 // RTABI chapter 4.3.4
419 setLibcallName(RTLIB::MEMCPY, "__aeabi_memcpy");
420 setLibcallName(RTLIB::MEMMOVE, "__aeabi_memmove");
421 setLibcallName(RTLIB::MEMSET, "__aeabi_memset");
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000422 }
423
Bob Wilson2fef4572011-10-07 16:59:21 +0000424 // Use divmod compiler-rt calls for iOS 5.0 and later.
425 if (Subtarget->getTargetTriple().getOS() == Triple::IOS &&
426 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
427 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
428 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
429 }
430
David Goodwinf1daf7d2009-07-08 23:10:31 +0000431 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000432 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000433 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000434 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000435 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
436 !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000437 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000438 if (!Subtarget->isFPOnlySP())
439 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000440
Owen Anderson825b72b2009-08-11 20:47:22 +0000441 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000442 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000443
Eli Friedman9f1f26a2011-11-08 01:43:53 +0000444 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
445 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
446 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
447 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
448 setTruncStoreAction((MVT::SimpleValueType)VT,
449 (MVT::SimpleValueType)InnerVT, Expand);
450 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
451 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
452 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
453 }
454
Bob Wilson5bafff32009-06-22 23:27:02 +0000455 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000456 addDRTypeForNEON(MVT::v2f32);
457 addDRTypeForNEON(MVT::v8i8);
458 addDRTypeForNEON(MVT::v4i16);
459 addDRTypeForNEON(MVT::v2i32);
460 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000461
Owen Anderson825b72b2009-08-11 20:47:22 +0000462 addQRTypeForNEON(MVT::v4f32);
463 addQRTypeForNEON(MVT::v2f64);
464 addQRTypeForNEON(MVT::v16i8);
465 addQRTypeForNEON(MVT::v8i16);
466 addQRTypeForNEON(MVT::v4i32);
467 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000468
Bob Wilson74dc72e2009-09-15 23:55:57 +0000469 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
470 // neither Neon nor VFP support any arithmetic operations on it.
471 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
472 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
473 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
474 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
475 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
476 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000477 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Bob Wilson74dc72e2009-09-15 23:55:57 +0000478 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
479 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
480 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
481 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
482 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
483 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
484 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
485 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
486 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
487 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
488 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
489 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
490 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
491 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
492 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
493 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
494 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
495
Bob Wilson642b3292009-09-16 00:32:15 +0000496 // Neon does not support some operations on v1i64 and v2i64 types.
497 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000498 // Custom handling for some quad-vector types to detect VMULL.
499 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
500 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
501 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begeman7973f352011-02-11 20:53:29 +0000502 // Custom handling for some vector types to avoid expensive expansions
503 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
504 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
505 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
506 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000507 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
508 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000509 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
510 // a destination type that is wider than the source.
511 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
512 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000513
Bob Wilson1c3ef902011-02-07 17:43:21 +0000514 setTargetDAGCombine(ISD::INTRINSIC_VOID);
515 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000516 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
517 setTargetDAGCombine(ISD::SHL);
518 setTargetDAGCombine(ISD::SRL);
519 setTargetDAGCombine(ISD::SRA);
520 setTargetDAGCombine(ISD::SIGN_EXTEND);
521 setTargetDAGCombine(ISD::ZERO_EXTEND);
522 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000523 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000524 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000525 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000526 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
527 setTargetDAGCombine(ISD::STORE);
Chad Rosieref01edf2011-06-24 19:23:04 +0000528 setTargetDAGCombine(ISD::FP_TO_SINT);
529 setTargetDAGCombine(ISD::FP_TO_UINT);
530 setTargetDAGCombine(ISD::FDIV);
Nadav Rotem004a24b2011-10-15 20:03:12 +0000531
532 setLoadExtAction(ISD::EXTLOAD, MVT::v4i8, Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000533 }
534
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000535 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000536
537 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000538 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000539
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000540 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000542
Evan Chenga8e29892007-01-19 07:51:42 +0000543 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000544 if (!Subtarget->isThumb1Only()) {
545 for (unsigned im = (unsigned)ISD::PRE_INC;
546 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000547 setIndexedLoadAction(im, MVT::i1, Legal);
548 setIndexedLoadAction(im, MVT::i8, Legal);
549 setIndexedLoadAction(im, MVT::i16, Legal);
550 setIndexedLoadAction(im, MVT::i32, Legal);
551 setIndexedStoreAction(im, MVT::i1, Legal);
552 setIndexedStoreAction(im, MVT::i8, Legal);
553 setIndexedStoreAction(im, MVT::i16, Legal);
554 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000555 }
Evan Chenga8e29892007-01-19 07:51:42 +0000556 }
557
558 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000559 setOperationAction(ISD::MUL, MVT::i64, Expand);
560 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000561 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000562 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
563 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000564 }
Jim Grosbacha7603982011-07-01 21:12:19 +0000565 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
566 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopher2cc40132011-04-19 18:49:19 +0000567 setOperationAction(ISD::MULHS, MVT::i32, Expand);
568
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000569 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000570 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000571 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000572 setOperationAction(ISD::SRL, MVT::i64, Custom);
573 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000574
Evan Cheng342e3162011-08-30 01:34:54 +0000575 if (!Subtarget->isThumb1Only()) {
576 // FIXME: We should do this for Thumb1 as well.
577 setOperationAction(ISD::ADDC, MVT::i32, Custom);
578 setOperationAction(ISD::ADDE, MVT::i32, Custom);
579 setOperationAction(ISD::SUBC, MVT::i32, Custom);
580 setOperationAction(ISD::SUBE, MVT::i32, Custom);
581 }
582
Evan Chenga8e29892007-01-19 07:51:42 +0000583 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000584 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000585 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000586 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000587 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000588 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000589
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000590 // Only ARMv6 has BSWAP.
591 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000592 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000593
Evan Chenga8e29892007-01-19 07:51:42 +0000594 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000595 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000596 // v7M has a hardware divider
597 setOperationAction(ISD::SDIV, MVT::i32, Expand);
598 setOperationAction(ISD::UDIV, MVT::i32, Expand);
599 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000600 setOperationAction(ISD::SREM, MVT::i32, Expand);
601 setOperationAction(ISD::UREM, MVT::i32, Expand);
602 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
603 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000604
Owen Anderson825b72b2009-08-11 20:47:22 +0000605 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
606 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
607 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
608 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000609 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000610
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000611 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000612
Evan Chenga8e29892007-01-19 07:51:42 +0000613 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000614 setOperationAction(ISD::VASTART, MVT::Other, Custom);
615 setOperationAction(ISD::VAARG, MVT::Other, Expand);
616 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
617 setOperationAction(ISD::VAEND, MVT::Other, Expand);
618 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
619 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000620 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000621 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
622 setExceptionPointerRegister(ARM::R0);
623 setExceptionSelectorRegister(ARM::R1);
624
Evan Cheng3a1588a2010-04-15 22:20:34 +0000625 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000626 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
627 // the default expansion.
Eli Friedman4db5aca2011-08-29 18:23:02 +0000628 // FIXME: This should be checking for v6k, not just v6.
Evan Cheng11db0682010-08-11 06:22:01 +0000629 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000630 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000631 // membarrier needs custom lowering; the rest are legal and handled
632 // normally.
633 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000634 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Eli Friedman2bdffe42011-08-31 00:31:29 +0000635 // Custom lowering for 64-bit ops
636 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
637 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
638 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
639 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
640 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
641 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Eli Friedman4d3f3292011-08-31 17:52:22 +0000642 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Eli Friedman26689ac2011-08-03 21:06:02 +0000643 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
644 setInsertFencesForAtomic(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000645 } else {
646 // Set them all for expansion, which will force libcalls.
647 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000648 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000649 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000650 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000651 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000652 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000653 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000654 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000655 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000656 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000657 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000658 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000659 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000660 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedman7cc15662011-09-15 22:18:49 +0000661 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
662 // Unordered/Monotonic case.
663 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
664 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000665 // Since the libcalls include locking, fold in the fences
666 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000667 }
Evan Chenga8e29892007-01-19 07:51:42 +0000668
Evan Cheng416941d2010-11-04 05:19:35 +0000669 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000670
Eli Friedmana2c6f452010-06-26 04:36:50 +0000671 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
672 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000673 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
674 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000675 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000676 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000677
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000678 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
679 !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000680 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
681 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000682 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000683 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
684 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000685
686 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000687 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000688 if (Subtarget->isTargetDarwin()) {
689 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
690 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000691 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000692 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000693
Owen Anderson825b72b2009-08-11 20:47:22 +0000694 setOperationAction(ISD::SETCC, MVT::i32, Expand);
695 setOperationAction(ISD::SETCC, MVT::f32, Expand);
696 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000697 setOperationAction(ISD::SELECT, MVT::i32, Custom);
698 setOperationAction(ISD::SELECT, MVT::f32, Custom);
699 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000700 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
701 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
702 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000703
Owen Anderson825b72b2009-08-11 20:47:22 +0000704 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
705 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
706 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
707 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
708 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000709
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000710 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000711 setOperationAction(ISD::FSIN, MVT::f64, Expand);
712 setOperationAction(ISD::FSIN, MVT::f32, Expand);
713 setOperationAction(ISD::FCOS, MVT::f32, Expand);
714 setOperationAction(ISD::FCOS, MVT::f64, Expand);
715 setOperationAction(ISD::FREM, MVT::f64, Expand);
716 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000717 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
718 !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000719 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
720 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000721 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000722 setOperationAction(ISD::FPOW, MVT::f64, Expand);
723 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000724
Cameron Zwarich33390842011-07-08 21:39:21 +0000725 setOperationAction(ISD::FMA, MVT::f64, Expand);
726 setOperationAction(ISD::FMA, MVT::f32, Expand);
727
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000728 // Various VFP goodness
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000729 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000730 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
731 if (Subtarget->hasVFP2()) {
732 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
733 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
734 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
735 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
736 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000737 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000738 if (!Subtarget->hasFP16()) {
739 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
740 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000741 }
Evan Cheng110cf482008-04-01 01:50:16 +0000742 }
Evan Chenga8e29892007-01-19 07:51:42 +0000743
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000744 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000745 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000746 setTargetDAGCombine(ISD::ADD);
747 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000748 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000749
Owen Anderson080c0922010-11-05 19:27:46 +0000750 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON())
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000751 setTargetDAGCombine(ISD::OR);
Owen Anderson080c0922010-11-05 19:27:46 +0000752 if (Subtarget->hasNEON())
753 setTargetDAGCombine(ISD::AND);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000754
Evan Chenga8e29892007-01-19 07:51:42 +0000755 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000756
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000757 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
758 !Subtarget->hasVFP2())
Evan Chengf7d87ee2010-05-21 00:43:17 +0000759 setSchedulingPreference(Sched::RegPressure);
760 else
761 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000762
Evan Cheng05219282011-01-06 06:52:41 +0000763 //// temporary - rewrite interface to use type
764 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Lang Hames75757f92011-10-26 20:56:52 +0000765 maxStoresPerMemset = 16;
766 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengf6799392010-06-26 01:52:05 +0000767
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000768 // On ARM arguments smaller than 4 bytes are extended, so all arguments
769 // are at least 4 bytes aligned.
770 setMinStackArgumentAlignment(4);
771
Evan Chengfff606d2010-09-24 19:07:23 +0000772 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000773
774 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000775}
776
Andrew Trick32cec0a2011-01-19 02:35:27 +0000777// FIXME: It might make sense to define the representative register class as the
778// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
779// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
780// SPR's representative would be DPR_VFP2. This should work well if register
781// pressure tracking were modified such that a register use would increment the
782// pressure of the register class's representative and all of it's super
783// classes' representatives transitively. We have not implemented this because
784// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000785// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000786// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000787std::pair<const TargetRegisterClass*, uint8_t>
788ARMTargetLowering::findRepresentativeClass(EVT VT) const{
789 const TargetRegisterClass *RRC = 0;
790 uint8_t Cost = 1;
791 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000792 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000793 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000794 // Use DPR as representative register class for all floating point
795 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
796 // the cost is 1 for both f32 and f64.
797 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000798 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000799 RRC = ARM::DPRRegisterClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000800 // When NEON is used for SP, only half of the register file is available
801 // because operations that define both SP and DP results will be constrained
802 // to the VFP2 class (D0-D15). We currently model this constraint prior to
803 // coalescing by double-counting the SP regs. See the FIXME above.
804 if (Subtarget->useNEONForSinglePrecisionFP())
805 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000806 break;
807 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
808 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000809 RRC = ARM::DPRRegisterClass;
810 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000811 break;
812 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000813 RRC = ARM::DPRRegisterClass;
814 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000815 break;
816 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000817 RRC = ARM::DPRRegisterClass;
818 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000819 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000820 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000821 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000822}
823
Evan Chenga8e29892007-01-19 07:51:42 +0000824const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
825 switch (Opcode) {
826 default: return 0;
827 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000828 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000829 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000830 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
831 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000832 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000833 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
834 case ARMISD::tCALL: return "ARMISD::tCALL";
835 case ARMISD::BRCOND: return "ARMISD::BRCOND";
836 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000837 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000838 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
839 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
840 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000841 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000842 case ARMISD::CMPFP: return "ARMISD::CMPFP";
843 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000844 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000845 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
846 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000847
Jim Grosbach3482c802010-01-18 19:58:49 +0000848 case ARMISD::RBIT: return "ARMISD::RBIT";
849
Bob Wilson76a312b2010-03-19 22:51:32 +0000850 case ARMISD::FTOSI: return "ARMISD::FTOSI";
851 case ARMISD::FTOUI: return "ARMISD::FTOUI";
852 case ARMISD::SITOF: return "ARMISD::SITOF";
853 case ARMISD::UITOF: return "ARMISD::UITOF";
854
Evan Chenga8e29892007-01-19 07:51:42 +0000855 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
856 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
857 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000858
Evan Cheng342e3162011-08-30 01:34:54 +0000859 case ARMISD::ADDC: return "ARMISD::ADDC";
860 case ARMISD::ADDE: return "ARMISD::ADDE";
861 case ARMISD::SUBC: return "ARMISD::SUBC";
862 case ARMISD::SUBE: return "ARMISD::SUBE";
863
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000864 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
865 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000866
Evan Chengc5942082009-10-28 06:55:03 +0000867 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
868 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
869
Dale Johannesen51e28e62010-06-03 21:09:53 +0000870 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000871
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000872 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000873
Evan Cheng86198642009-08-07 00:34:42 +0000874 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
875
Jim Grosbach3728e962009-12-10 00:11:09 +0000876 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000877 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000878
Evan Chengdfed19f2010-11-03 06:34:55 +0000879 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
880
Bob Wilson5bafff32009-06-22 23:27:02 +0000881 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000882 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000883 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000884 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
885 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000886 case ARMISD::VCGEU: return "ARMISD::VCGEU";
887 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000888 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
889 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000890 case ARMISD::VCGTU: return "ARMISD::VCGTU";
891 case ARMISD::VTST: return "ARMISD::VTST";
892
893 case ARMISD::VSHL: return "ARMISD::VSHL";
894 case ARMISD::VSHRs: return "ARMISD::VSHRs";
895 case ARMISD::VSHRu: return "ARMISD::VSHRu";
896 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
897 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
898 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
899 case ARMISD::VSHRN: return "ARMISD::VSHRN";
900 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
901 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
902 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
903 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
904 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
905 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
906 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
907 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
908 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
909 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
910 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
911 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
912 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
913 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000914 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000915 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Chengeaa192a2011-11-15 02:12:34 +0000916 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000917 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000918 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000919 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000920 case ARMISD::VREV64: return "ARMISD::VREV64";
921 case ARMISD::VREV32: return "ARMISD::VREV32";
922 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000923 case ARMISD::VZIP: return "ARMISD::VZIP";
924 case ARMISD::VUZP: return "ARMISD::VUZP";
925 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000926 case ARMISD::VTBL1: return "ARMISD::VTBL1";
927 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000928 case ARMISD::VMULLs: return "ARMISD::VMULLs";
929 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000930 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000931 case ARMISD::FMAX: return "ARMISD::FMAX";
932 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000933 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000934 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
935 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000936 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000937 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
938 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
939 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +0000940 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
941 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
942 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
943 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
944 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
945 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
946 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
947 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
948 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
949 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
950 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
951 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
952 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
953 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
954 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
955 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
956 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +0000957 }
958}
959
Duncan Sands28b77e92011-09-06 19:07:46 +0000960EVT ARMTargetLowering::getSetCCResultType(EVT VT) const {
961 if (!VT.isVector()) return getPointerTy();
962 return VT.changeVectorElementTypeToInteger();
963}
964
Evan Cheng06b666c2010-05-15 02:18:07 +0000965/// getRegClassFor - Return the register class that should be used for the
966/// specified value type.
967TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
968 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
969 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
970 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000971 if (Subtarget->hasNEON()) {
972 if (VT == MVT::v4i64)
973 return ARM::QQPRRegisterClass;
974 else if (VT == MVT::v8i64)
975 return ARM::QQQQPRRegisterClass;
976 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000977 return TargetLowering::getRegClassFor(VT);
978}
979
Eric Christopherab695882010-07-21 22:26:11 +0000980// Create a fast isel object.
981FastISel *
982ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
983 return ARM::createFastISel(funcInfo);
984}
985
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000986/// getMaximalGlobalOffset - Returns the maximal possible offset which can
987/// be used for loads / stores from the global.
988unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
989 return (Subtarget->isThumb1Only() ? 127 : 4095);
990}
991
Evan Cheng1cc39842010-05-20 23:26:43 +0000992Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000993 unsigned NumVals = N->getNumValues();
994 if (!NumVals)
995 return Sched::RegPressure;
996
997 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000998 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000999 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +00001000 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +00001001 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman692c1d82011-10-24 17:55:11 +00001002 return Sched::ILP;
Evan Cheng1cc39842010-05-20 23:26:43 +00001003 }
Evan Chengc10f5432010-05-28 23:25:23 +00001004
1005 if (!N->isMachineOpcode())
1006 return Sched::RegPressure;
1007
1008 // Load are scheduled for latency even if there instruction itinerary
1009 // is not available.
1010 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +00001011 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +00001012
Evan Chenge837dea2011-06-28 19:10:37 +00001013 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +00001014 return Sched::RegPressure;
1015 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +00001016 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman692c1d82011-10-24 17:55:11 +00001017 return Sched::ILP;
Evan Chengc10f5432010-05-28 23:25:23 +00001018
Evan Cheng1cc39842010-05-20 23:26:43 +00001019 return Sched::RegPressure;
1020}
1021
Evan Chenga8e29892007-01-19 07:51:42 +00001022//===----------------------------------------------------------------------===//
1023// Lowering Code
1024//===----------------------------------------------------------------------===//
1025
Evan Chenga8e29892007-01-19 07:51:42 +00001026/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1027static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1028 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001029 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +00001030 case ISD::SETNE: return ARMCC::NE;
1031 case ISD::SETEQ: return ARMCC::EQ;
1032 case ISD::SETGT: return ARMCC::GT;
1033 case ISD::SETGE: return ARMCC::GE;
1034 case ISD::SETLT: return ARMCC::LT;
1035 case ISD::SETLE: return ARMCC::LE;
1036 case ISD::SETUGT: return ARMCC::HI;
1037 case ISD::SETUGE: return ARMCC::HS;
1038 case ISD::SETULT: return ARMCC::LO;
1039 case ISD::SETULE: return ARMCC::LS;
1040 }
1041}
1042
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001043/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1044static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001045 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001046 CondCode2 = ARMCC::AL;
1047 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001048 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001049 case ISD::SETEQ:
1050 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1051 case ISD::SETGT:
1052 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1053 case ISD::SETGE:
1054 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1055 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001056 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001057 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1058 case ISD::SETO: CondCode = ARMCC::VC; break;
1059 case ISD::SETUO: CondCode = ARMCC::VS; break;
1060 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1061 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1062 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1063 case ISD::SETLT:
1064 case ISD::SETULT: CondCode = ARMCC::LT; break;
1065 case ISD::SETLE:
1066 case ISD::SETULE: CondCode = ARMCC::LE; break;
1067 case ISD::SETNE:
1068 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1069 }
Evan Chenga8e29892007-01-19 07:51:42 +00001070}
1071
Bob Wilson1f595bb2009-04-17 19:07:39 +00001072//===----------------------------------------------------------------------===//
1073// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001074//===----------------------------------------------------------------------===//
1075
1076#include "ARMGenCallingConv.inc"
1077
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001078/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1079/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001080CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001081 bool Return,
1082 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001083 switch (CC) {
1084 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001085 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001086 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001087 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001088 if (!Subtarget->isAAPCS_ABI())
1089 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1090 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1091 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1092 }
1093 // Fallthrough
1094 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001095 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001096 if (!Subtarget->isAAPCS_ABI())
1097 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1098 else if (Subtarget->hasVFP2() &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001099 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1100 !isVarArg)
Evan Cheng76f920d2010-10-22 18:23:05 +00001101 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1102 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1103 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001104 case CallingConv::ARM_AAPCS_VFP:
Evan Cheng76f920d2010-10-22 18:23:05 +00001105 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001106 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001107 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001108 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001109 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001110 }
1111}
1112
Dan Gohman98ca4f22009-08-05 01:29:28 +00001113/// LowerCallResult - Lower the result values of a call into the
1114/// appropriate copies out of appropriate physical registers.
1115SDValue
1116ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001117 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001118 const SmallVectorImpl<ISD::InputArg> &Ins,
1119 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001120 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001121
Bob Wilson1f595bb2009-04-17 19:07:39 +00001122 // Assign locations to each value returned by this call.
1123 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001124 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1125 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001126 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001127 CCAssignFnForNode(CallConv, /* Return*/ true,
1128 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001129
1130 // Copy all of the result registers out of their specified physreg.
1131 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1132 CCValAssign VA = RVLocs[i];
1133
Bob Wilson80915242009-04-25 00:33:20 +00001134 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001135 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001136 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001137 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001138 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001139 Chain = Lo.getValue(1);
1140 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001141 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001142 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001143 InFlag);
1144 Chain = Hi.getValue(1);
1145 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001146 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001147
Owen Anderson825b72b2009-08-11 20:47:22 +00001148 if (VA.getLocVT() == MVT::v2f64) {
1149 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1150 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1151 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001152
1153 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001154 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001155 Chain = Lo.getValue(1);
1156 InFlag = Lo.getValue(2);
1157 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001158 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001159 Chain = Hi.getValue(1);
1160 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001161 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001162 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1163 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001164 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001165 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001166 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1167 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001168 Chain = Val.getValue(1);
1169 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001170 }
Bob Wilson80915242009-04-25 00:33:20 +00001171
1172 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001173 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001174 case CCValAssign::Full: break;
1175 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001176 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001177 break;
1178 }
1179
Dan Gohman98ca4f22009-08-05 01:29:28 +00001180 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001181 }
1182
Dan Gohman98ca4f22009-08-05 01:29:28 +00001183 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001184}
1185
Bob Wilsondee46d72009-04-17 20:35:10 +00001186/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001187SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001188ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1189 SDValue StackPtr, SDValue Arg,
1190 DebugLoc dl, SelectionDAG &DAG,
1191 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001192 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001193 unsigned LocMemOffset = VA.getLocMemOffset();
1194 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1195 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001196 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001197 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001198 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001199}
1200
Dan Gohman98ca4f22009-08-05 01:29:28 +00001201void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001202 SDValue Chain, SDValue &Arg,
1203 RegsToPassVector &RegsToPass,
1204 CCValAssign &VA, CCValAssign &NextVA,
1205 SDValue &StackPtr,
1206 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001207 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001208
Jim Grosbache5165492009-11-09 00:11:35 +00001209 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001210 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001211 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1212
1213 if (NextVA.isRegLoc())
1214 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1215 else {
1216 assert(NextVA.isMemLoc());
1217 if (StackPtr.getNode() == 0)
1218 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1219
Dan Gohman98ca4f22009-08-05 01:29:28 +00001220 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1221 dl, DAG, NextVA,
1222 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001223 }
1224}
1225
Dan Gohman98ca4f22009-08-05 01:29:28 +00001226/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001227/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1228/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001229SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001230ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001231 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001232 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001233 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001234 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001235 const SmallVectorImpl<ISD::InputArg> &Ins,
1236 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001237 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001238 MachineFunction &MF = DAG.getMachineFunction();
1239 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1240 bool IsSibCall = false;
Bob Wilson6d2f9ce2011-10-07 17:17:49 +00001241 // Disable tail calls if they're not supported.
1242 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Bob Wilson703af3a2010-08-13 22:43:33 +00001243 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001244 if (isTailCall) {
1245 // Check if it's really possible to do a tail call.
1246 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1247 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001248 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001249 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1250 // detected sibcalls.
1251 if (isTailCall) {
1252 ++NumTailCalls;
1253 IsSibCall = true;
1254 }
1255 }
Evan Chenga8e29892007-01-19 07:51:42 +00001256
Bob Wilson1f595bb2009-04-17 19:07:39 +00001257 // Analyze operands of the call, assigning locations to each operand.
1258 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001259 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1260 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001261 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001262 CCAssignFnForNode(CallConv, /* Return*/ false,
1263 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001264
Bob Wilson1f595bb2009-04-17 19:07:39 +00001265 // Get a count of how many bytes are to be pushed on the stack.
1266 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001267
Dale Johannesen51e28e62010-06-03 21:09:53 +00001268 // For tail calls, memory operands are available in our caller's stack.
1269 if (IsSibCall)
1270 NumBytes = 0;
1271
Evan Chenga8e29892007-01-19 07:51:42 +00001272 // Adjust the stack pointer for the new arguments...
1273 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001274 if (!IsSibCall)
1275 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001276
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001277 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001278
Bob Wilson5bafff32009-06-22 23:27:02 +00001279 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001280 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001281
Bob Wilson1f595bb2009-04-17 19:07:39 +00001282 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001283 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001284 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1285 i != e;
1286 ++i, ++realArgIdx) {
1287 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001288 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001289 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001290 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001291
Bob Wilson1f595bb2009-04-17 19:07:39 +00001292 // Promote the value if needed.
1293 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001294 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001295 case CCValAssign::Full: break;
1296 case CCValAssign::SExt:
1297 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1298 break;
1299 case CCValAssign::ZExt:
1300 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1301 break;
1302 case CCValAssign::AExt:
1303 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1304 break;
1305 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001306 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001307 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001308 }
1309
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001310 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001311 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001312 if (VA.getLocVT() == MVT::v2f64) {
1313 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1314 DAG.getConstant(0, MVT::i32));
1315 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1316 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001317
Dan Gohman98ca4f22009-08-05 01:29:28 +00001318 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001319 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1320
1321 VA = ArgLocs[++i]; // skip ahead to next loc
1322 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001323 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001324 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1325 } else {
1326 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001327
Dan Gohman98ca4f22009-08-05 01:29:28 +00001328 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1329 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001330 }
1331 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001332 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001333 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001334 }
1335 } else if (VA.isRegLoc()) {
1336 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001337 } else if (isByVal) {
1338 assert(VA.isMemLoc());
1339 unsigned offset = 0;
1340
1341 // True if this byval aggregate will be split between registers
1342 // and memory.
1343 if (CCInfo.isFirstByValRegValid()) {
1344 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1345 unsigned int i, j;
1346 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1347 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1348 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1349 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1350 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001351 false, false, false, 0);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001352 MemOpChains.push_back(Load.getValue(1));
1353 RegsToPass.push_back(std::make_pair(j, Load));
1354 }
1355 offset = ARM::R4 - CCInfo.getFirstByValReg();
1356 CCInfo.clearFirstByValReg();
1357 }
1358
1359 unsigned LocMemOffset = VA.getLocMemOffset();
1360 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1361 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1362 StkPtrOff);
1363 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1364 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1365 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1366 MVT::i32);
1367 MemOpChains.push_back(DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode,
1368 Flags.getByValAlign(),
1369 /*isVolatile=*/false,
Dan Gohman65fd6562011-11-03 21:49:52 +00001370 /*AlwaysInline=*/false,
Stuart Hastingsc7315872011-04-20 16:47:52 +00001371 MachinePointerInfo(0),
1372 MachinePointerInfo(0)));
1373
1374 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001375 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001376
Dan Gohman98ca4f22009-08-05 01:29:28 +00001377 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1378 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001379 }
Evan Chenga8e29892007-01-19 07:51:42 +00001380 }
1381
1382 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001383 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001384 &MemOpChains[0], MemOpChains.size());
1385
1386 // Build a sequence of copy-to-reg nodes chained together with token chain
1387 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001388 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001389 // Tail call byval lowering might overwrite argument registers so in case of
1390 // tail call optimization the copies to registers are lowered later.
1391 if (!isTailCall)
1392 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1393 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1394 RegsToPass[i].second, InFlag);
1395 InFlag = Chain.getValue(1);
1396 }
Evan Chenga8e29892007-01-19 07:51:42 +00001397
Dale Johannesen51e28e62010-06-03 21:09:53 +00001398 // For tail calls lower the arguments to the 'real' stack slot.
1399 if (isTailCall) {
1400 // Force all the incoming stack arguments to be loaded from the stack
1401 // before any new outgoing arguments are stored to the stack, because the
1402 // outgoing stack slots may alias the incoming argument stack slots, and
1403 // the alias isn't otherwise explicit. This is slightly more conservative
1404 // than necessary, because it means that each store effectively depends
1405 // on every argument instead of just those arguments it would clobber.
1406
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001407 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001408 InFlag = SDValue();
1409 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1410 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1411 RegsToPass[i].second, InFlag);
1412 InFlag = Chain.getValue(1);
1413 }
1414 InFlag =SDValue();
1415 }
1416
Bill Wendling056292f2008-09-16 21:48:12 +00001417 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1418 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1419 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001420 bool isDirect = false;
1421 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001422 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001423 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001424
1425 if (EnableARMLongCalls) {
1426 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1427 && "long-calls with non-static relocation model!");
1428 // Handle a global address or an external symbol. If it's not one of
1429 // those, the target's already in a register, so we don't need to do
1430 // anything extra.
1431 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001432 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001433 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001434 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001435 ARMConstantPoolValue *CPV =
1436 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1437
Jim Grosbache7b52522010-04-14 22:28:31 +00001438 // Get the address of the callee into a register
1439 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1440 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1441 Callee = DAG.getLoad(getPointerTy(), dl,
1442 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001443 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001444 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001445 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1446 const char *Sym = S->getSymbol();
1447
1448 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001449 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001450 ARMConstantPoolValue *CPV =
1451 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1452 ARMPCLabelIndex, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001453 // Get the address of the callee into a register
1454 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1455 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1456 Callee = DAG.getLoad(getPointerTy(), dl,
1457 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001458 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001459 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001460 }
1461 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001462 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001463 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001464 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001465 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001466 getTargetMachine().getRelocationModel() != Reloc::Static;
1467 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001468 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001469 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001470 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001471 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001472 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001473 ARMConstantPoolValue *CPV =
1474 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001475 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001476 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001477 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001478 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001479 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001480 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001481 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001482 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001483 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001484 } else {
1485 // On ELF targets for PIC code, direct calls should go through the PLT
1486 unsigned OpFlags = 0;
1487 if (Subtarget->isTargetELF() &&
1488 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1489 OpFlags = ARMII::MO_PLT;
1490 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1491 }
Bill Wendling056292f2008-09-16 21:48:12 +00001492 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001493 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001494 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001495 getTargetMachine().getRelocationModel() != Reloc::Static;
1496 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001497 // tBX takes a register source operand.
1498 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001499 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001500 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001501 ARMConstantPoolValue *CPV =
1502 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1503 ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001504 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001505 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001506 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001507 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001508 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001509 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001510 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001511 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001512 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001513 } else {
1514 unsigned OpFlags = 0;
1515 // On ELF targets for PIC code, direct calls should go through the PLT
1516 if (Subtarget->isTargetELF() &&
1517 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1518 OpFlags = ARMII::MO_PLT;
1519 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1520 }
Evan Chenga8e29892007-01-19 07:51:42 +00001521 }
1522
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001523 // FIXME: handle tail calls differently.
1524 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001525 if (Subtarget->isThumb()) {
1526 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001527 CallOpc = ARMISD::CALL_NOLINK;
1528 else
1529 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1530 } else {
1531 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001532 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1533 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001534 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001535
Dan Gohman475871a2008-07-27 21:46:04 +00001536 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001537 Ops.push_back(Chain);
1538 Ops.push_back(Callee);
1539
1540 // Add argument registers to the end of the list so that they are known live
1541 // into the call.
1542 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1543 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1544 RegsToPass[i].second.getValueType()));
1545
Gabor Greifba36cb52008-08-28 21:40:38 +00001546 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001547 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001548
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001549 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001550 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001551 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001552
Duncan Sands4bdcb612008-07-02 17:40:58 +00001553 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001554 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001555 InFlag = Chain.getValue(1);
1556
Chris Lattnere563bbc2008-10-11 22:08:30 +00001557 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1558 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001559 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001560 InFlag = Chain.getValue(1);
1561
Bob Wilson1f595bb2009-04-17 19:07:39 +00001562 // Handle result values, copying them out of physregs into vregs that we
1563 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001564 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1565 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001566}
1567
Stuart Hastingsf222e592011-02-28 17:17:53 +00001568/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001569/// on the stack. Remember the next parameter register to allocate,
1570/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001571/// this.
1572void
Stuart Hastingsc7315872011-04-20 16:47:52 +00001573llvm::ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
1574 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1575 assert((State->getCallOrPrologue() == Prologue ||
1576 State->getCallOrPrologue() == Call) &&
1577 "unhandled ParmContext");
1578 if ((!State->isFirstByValRegValid()) &&
1579 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1580 State->setFirstByValReg(reg);
1581 // At a call site, a byval parameter that is split between
1582 // registers and memory needs its size truncated here. In a
1583 // function prologue, such byval parameters are reassembled in
1584 // memory, and are not truncated.
1585 if (State->getCallOrPrologue() == Call) {
1586 unsigned excess = 4 * (ARM::R4 - reg);
1587 assert(size >= excess && "expected larger existing stack allocation");
1588 size -= excess;
1589 }
1590 }
1591 // Confiscate any remaining parameter registers to preclude their
1592 // assignment to subsequent parameters.
1593 while (State->AllocateReg(GPRArgRegs, 4))
1594 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001595}
1596
Dale Johannesen51e28e62010-06-03 21:09:53 +00001597/// MatchingStackOffset - Return true if the given stack call argument is
1598/// already available in the same position (relatively) of the caller's
1599/// incoming argument stack.
1600static
1601bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1602 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1603 const ARMInstrInfo *TII) {
1604 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1605 int FI = INT_MAX;
1606 if (Arg.getOpcode() == ISD::CopyFromReg) {
1607 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001608 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001609 return false;
1610 MachineInstr *Def = MRI->getVRegDef(VR);
1611 if (!Def)
1612 return false;
1613 if (!Flags.isByVal()) {
1614 if (!TII->isLoadFromStackSlot(Def, FI))
1615 return false;
1616 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001617 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001618 }
1619 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1620 if (Flags.isByVal())
1621 // ByVal argument is passed in as a pointer but it's now being
1622 // dereferenced. e.g.
1623 // define @foo(%struct.X* %A) {
1624 // tail call @bar(%struct.X* byval %A)
1625 // }
1626 return false;
1627 SDValue Ptr = Ld->getBasePtr();
1628 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1629 if (!FINode)
1630 return false;
1631 FI = FINode->getIndex();
1632 } else
1633 return false;
1634
1635 assert(FI != INT_MAX);
1636 if (!MFI->isFixedObjectIndex(FI))
1637 return false;
1638 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1639}
1640
1641/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1642/// for tail call optimization. Targets which want to do tail call
1643/// optimization should implement this function.
1644bool
1645ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1646 CallingConv::ID CalleeCC,
1647 bool isVarArg,
1648 bool isCalleeStructRet,
1649 bool isCallerStructRet,
1650 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001651 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001652 const SmallVectorImpl<ISD::InputArg> &Ins,
1653 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001654 const Function *CallerF = DAG.getMachineFunction().getFunction();
1655 CallingConv::ID CallerCC = CallerF->getCallingConv();
1656 bool CCMatch = CallerCC == CalleeCC;
1657
1658 // Look for obvious safe cases to perform tail call optimization that do not
1659 // require ABI changes. This is what gcc calls sibcall.
1660
Jim Grosbach7616b642010-06-16 23:45:49 +00001661 // Do not sibcall optimize vararg calls unless the call site is not passing
1662 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001663 if (isVarArg && !Outs.empty())
1664 return false;
1665
1666 // Also avoid sibcall optimization if either caller or callee uses struct
1667 // return semantics.
1668 if (isCalleeStructRet || isCallerStructRet)
1669 return false;
1670
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001671 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001672 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1673 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1674 // support in the assembler and linker to be used. This would need to be
1675 // fixed to fully support tail calls in Thumb1.
1676 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001677 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1678 // LR. This means if we need to reload LR, it takes an extra instructions,
1679 // which outweighs the value of the tail call; but here we don't know yet
1680 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001681 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001682 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001683
1684 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1685 // but we need to make sure there are enough registers; the only valid
1686 // registers are the 4 used for parameters. We don't currently do this
1687 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001688 if (Subtarget->isThumb1Only())
1689 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001690
Dale Johannesen51e28e62010-06-03 21:09:53 +00001691 // If the calling conventions do not match, then we'd better make sure the
1692 // results are returned in the same way as what the caller expects.
1693 if (!CCMatch) {
1694 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001695 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1696 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001697 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1698
1699 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001700 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1701 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001702 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1703
1704 if (RVLocs1.size() != RVLocs2.size())
1705 return false;
1706 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1707 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1708 return false;
1709 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1710 return false;
1711 if (RVLocs1[i].isRegLoc()) {
1712 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1713 return false;
1714 } else {
1715 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1716 return false;
1717 }
1718 }
1719 }
1720
1721 // If the callee takes no arguments then go on to check the results of the
1722 // call.
1723 if (!Outs.empty()) {
1724 // Check if stack adjustment is needed. For now, do not do this if any
1725 // argument is passed on the stack.
1726 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001727 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1728 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001729 CCInfo.AnalyzeCallOperands(Outs,
1730 CCAssignFnForNode(CalleeCC, false, isVarArg));
1731 if (CCInfo.getNextStackOffset()) {
1732 MachineFunction &MF = DAG.getMachineFunction();
1733
1734 // Check if the arguments are already laid out in the right way as
1735 // the caller's fixed stack objects.
1736 MachineFrameInfo *MFI = MF.getFrameInfo();
1737 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1738 const ARMInstrInfo *TII =
1739 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001740 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1741 i != e;
1742 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001743 CCValAssign &VA = ArgLocs[i];
1744 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001745 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001746 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001747 if (VA.getLocInfo() == CCValAssign::Indirect)
1748 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001749 if (VA.needsCustom()) {
1750 // f64 and vector types are split into multiple registers or
1751 // register/stack-slot combinations. The types will not match
1752 // the registers; give up on memory f64 refs until we figure
1753 // out what to do about this.
1754 if (!VA.isRegLoc())
1755 return false;
1756 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001757 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001758 if (RegVT == MVT::v2f64) {
1759 if (!ArgLocs[++i].isRegLoc())
1760 return false;
1761 if (!ArgLocs[++i].isRegLoc())
1762 return false;
1763 }
1764 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001765 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1766 MFI, MRI, TII))
1767 return false;
1768 }
1769 }
1770 }
1771 }
1772
1773 return true;
1774}
1775
Dan Gohman98ca4f22009-08-05 01:29:28 +00001776SDValue
1777ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001778 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001779 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001780 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001781 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001782
Bob Wilsondee46d72009-04-17 20:35:10 +00001783 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001784 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001785
Bob Wilsondee46d72009-04-17 20:35:10 +00001786 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001787 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1788 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001789
Dan Gohman98ca4f22009-08-05 01:29:28 +00001790 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001791 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1792 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001793
1794 // If this is the first return lowered for this function, add
1795 // the regs to the liveout set for the function.
1796 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1797 for (unsigned i = 0; i != RVLocs.size(); ++i)
1798 if (RVLocs[i].isRegLoc())
1799 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001800 }
1801
Bob Wilson1f595bb2009-04-17 19:07:39 +00001802 SDValue Flag;
1803
1804 // Copy the result values into the output registers.
1805 for (unsigned i = 0, realRVLocIdx = 0;
1806 i != RVLocs.size();
1807 ++i, ++realRVLocIdx) {
1808 CCValAssign &VA = RVLocs[i];
1809 assert(VA.isRegLoc() && "Can only return in registers!");
1810
Dan Gohmanc9403652010-07-07 15:54:55 +00001811 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001812
1813 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001814 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001815 case CCValAssign::Full: break;
1816 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001817 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001818 break;
1819 }
1820
Bob Wilson1f595bb2009-04-17 19:07:39 +00001821 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001822 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001823 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001824 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1825 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001826 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001827 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001828
1829 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1830 Flag = Chain.getValue(1);
1831 VA = RVLocs[++i]; // skip ahead to next loc
1832 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1833 HalfGPRs.getValue(1), Flag);
1834 Flag = Chain.getValue(1);
1835 VA = RVLocs[++i]; // skip ahead to next loc
1836
1837 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001838 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1839 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001840 }
1841 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1842 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001843 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001844 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001845 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001846 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001847 VA = RVLocs[++i]; // skip ahead to next loc
1848 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1849 Flag);
1850 } else
1851 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1852
Bob Wilsondee46d72009-04-17 20:35:10 +00001853 // Guarantee that all emitted copies are
1854 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001855 Flag = Chain.getValue(1);
1856 }
1857
1858 SDValue result;
1859 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001860 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001861 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001862 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001863
1864 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001865}
1866
Evan Cheng3d2125c2010-11-30 23:55:39 +00001867bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1868 if (N->getNumValues() != 1)
1869 return false;
1870 if (!N->hasNUsesOfValue(1, 0))
1871 return false;
1872
1873 unsigned NumCopies = 0;
1874 SDNode* Copies[2];
1875 SDNode *Use = *N->use_begin();
1876 if (Use->getOpcode() == ISD::CopyToReg) {
1877 Copies[NumCopies++] = Use;
1878 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1879 // f64 returned in a pair of GPRs.
1880 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1881 UI != UE; ++UI) {
1882 if (UI->getOpcode() != ISD::CopyToReg)
1883 return false;
1884 Copies[UI.getUse().getResNo()] = *UI;
1885 ++NumCopies;
1886 }
1887 } else if (Use->getOpcode() == ISD::BITCAST) {
1888 // f32 returned in a single GPR.
1889 if (!Use->hasNUsesOfValue(1, 0))
1890 return false;
1891 Use = *Use->use_begin();
1892 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1893 return false;
1894 Copies[NumCopies++] = Use;
1895 } else {
1896 return false;
1897 }
1898
1899 if (NumCopies != 1 && NumCopies != 2)
1900 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001901
1902 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001903 for (unsigned i = 0; i < NumCopies; ++i) {
1904 SDNode *Copy = Copies[i];
1905 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1906 UI != UE; ++UI) {
1907 if (UI->getOpcode() == ISD::CopyToReg) {
1908 SDNode *Use = *UI;
1909 if (Use == Copies[0] || Use == Copies[1])
1910 continue;
1911 return false;
1912 }
1913 if (UI->getOpcode() != ARMISD::RET_FLAG)
1914 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001915 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001916 }
1917 }
1918
Evan Cheng1bf891a2010-12-01 22:59:46 +00001919 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001920}
1921
Evan Cheng485fafc2011-03-21 01:19:09 +00001922bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1923 if (!EnableARMTailCalls)
1924 return false;
1925
1926 if (!CI->isTailCall())
1927 return false;
1928
1929 return !Subtarget->isThumb1Only();
1930}
1931
Bob Wilsonb62d2572009-11-03 00:02:05 +00001932// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1933// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1934// one of the above mentioned nodes. It has to be wrapped because otherwise
1935// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1936// be used to form addressing mode. These wrapped nodes will be selected
1937// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001938static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001939 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001940 // FIXME there is no actual debug info here
1941 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001942 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001943 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001944 if (CP->isMachineConstantPoolEntry())
1945 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1946 CP->getAlignment());
1947 else
1948 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1949 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001950 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001951}
1952
Jim Grosbache1102ca2010-07-19 17:20:38 +00001953unsigned ARMTargetLowering::getJumpTableEncoding() const {
1954 return MachineJumpTableInfo::EK_Inline;
1955}
1956
Dan Gohmand858e902010-04-17 15:26:15 +00001957SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1958 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001959 MachineFunction &MF = DAG.getMachineFunction();
1960 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1961 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001962 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001963 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001964 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001965 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1966 SDValue CPAddr;
1967 if (RelocM == Reloc::Static) {
1968 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1969 } else {
1970 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001971 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001972 ARMConstantPoolValue *CPV =
1973 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
1974 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson907eebd2009-11-02 20:59:23 +00001975 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1976 }
1977 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1978 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001979 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001980 false, false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001981 if (RelocM == Reloc::Static)
1982 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001983 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001984 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001985}
1986
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001987// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001988SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001989ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001990 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001991 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001992 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001993 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001994 MachineFunction &MF = DAG.getMachineFunction();
1995 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001996 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001997 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00001998 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
1999 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002000 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002001 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00002002 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002003 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002004 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002005 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002006
Evan Chenge7e0d622009-11-06 22:24:13 +00002007 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002008 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002009
2010 // call __tls_get_addr.
2011 ArgListTy Args;
2012 ArgListEntry Entry;
2013 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002014 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002015 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002016 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00002017 std::pair<SDValue, SDValue> CallResult =
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002018 LowerCallTo(Chain, (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00002019 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002020 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002021 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002022 return CallResult.first;
2023}
2024
2025// Lower ISD::GlobalTLSAddress using the "initial exec" or
2026// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00002027SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002028ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002029 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002030 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002031 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002032 SDValue Offset;
2033 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002034 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002035 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002036 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002037
Chris Lattner4fb63d02009-07-15 04:12:33 +00002038 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002039 MachineFunction &MF = DAG.getMachineFunction();
2040 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002041 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002042 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002043 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2044 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002045 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2046 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2047 true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002048 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002049 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002050 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002051 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002052 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002053 Chain = Offset.getValue(1);
2054
Evan Chenge7e0d622009-11-06 22:24:13 +00002055 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002056 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002057
Evan Cheng9eda6892009-10-31 03:39:36 +00002058 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002059 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002060 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002061 } else {
2062 // local exec model
Bill Wendling5bb77992011-10-01 08:00:54 +00002063 ARMConstantPoolValue *CPV =
2064 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002065 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002066 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002067 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002068 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002069 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002070 }
2071
2072 // The address of the thread local variable is the add of the thread
2073 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002074 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002075}
2076
Dan Gohman475871a2008-07-27 21:46:04 +00002077SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002078ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002079 // TODO: implement the "local dynamic" model
2080 assert(Subtarget->isTargetELF() &&
2081 "TLS not implemented for non-ELF targets");
2082 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2083 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
2084 // otherwise use the "Local Exec" TLS Model
2085 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
2086 return LowerToTLSGeneralDynamicModel(GA, DAG);
2087 else
2088 return LowerToTLSExecModels(GA, DAG);
2089}
2090
Dan Gohman475871a2008-07-27 21:46:04 +00002091SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002092 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002093 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002094 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002095 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002096 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2097 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002098 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002099 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002100 ARMConstantPoolConstant::Create(GV,
2101 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002102 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002103 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002104 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002105 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002106 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002107 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002108 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002109 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002110 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002111 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002112 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002113 MachinePointerInfo::getGOT(),
2114 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002115 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002116 }
2117
2118 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloy015cca62011-10-26 08:53:19 +00002119 // pair. This is always cheaper.
2120 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002121 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002122 // FIXME: Once remat is capable of dealing with instructions with register
2123 // operands, expand this into two nodes.
2124 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2125 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002126 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002127 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2128 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2129 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2130 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002131 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002132 }
2133}
2134
Dan Gohman475871a2008-07-27 21:46:04 +00002135SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002136 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002137 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002138 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002139 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002140 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002141 MachineFunction &MF = DAG.getMachineFunction();
2142 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2143
Evan Cheng4abce0c2011-05-27 20:11:27 +00002144 // FIXME: Enable this for static codegen when tool issues are fixed.
Evan Chengf31151f2011-10-26 01:17:44 +00002145 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002146 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002147 // FIXME: Once remat is capable of dealing with instructions with register
2148 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002149 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002150 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2151 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2152
Evan Cheng53519f02011-01-21 18:55:51 +00002153 unsigned Wrapper = (RelocM == Reloc::PIC_)
2154 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2155 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002156 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002157 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2158 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002159 MachinePointerInfo::getGOT(),
2160 false, false, false, 0);
Evan Chengfc8475b2011-01-19 02:16:49 +00002161 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002162 }
2163
2164 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002165 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002166 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002167 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002168 } else {
2169 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002170 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2171 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002172 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue,
2173 PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002174 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002175 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002176 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002177
Evan Cheng9eda6892009-10-31 03:39:36 +00002178 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002179 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002180 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002181 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002182
2183 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002184 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002185 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002186 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002187
Evan Cheng63476a82009-09-03 07:04:02 +00002188 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002189 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002190 false, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002191
2192 return Result;
2193}
2194
Dan Gohman475871a2008-07-27 21:46:04 +00002195SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002196 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002197 assert(Subtarget->isTargetELF() &&
2198 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002199 MachineFunction &MF = DAG.getMachineFunction();
2200 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002201 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002202 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002203 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002204 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingfe31e672011-10-01 08:58:29 +00002205 ARMConstantPoolValue *CPV =
2206 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2207 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002208 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002209 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002210 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002211 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002212 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002213 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002214 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002215}
2216
Jim Grosbach0e0da732009-05-12 23:59:14 +00002217SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002218ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2219 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002220 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendlingce370cf2011-10-07 21:25:38 +00002221 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2222 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002223 Op.getOperand(1), Val);
2224}
2225
2226SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002227ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2228 DebugLoc dl = Op.getDebugLoc();
2229 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2230 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2231}
2232
2233SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002234ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002235 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002236 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002237 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002238 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002239 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002240 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002241 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002242 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2243 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002244 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002245 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002246 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002247 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002248 EVT PtrVT = getPointerTy();
2249 DebugLoc dl = Op.getDebugLoc();
2250 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2251 SDValue CPAddr;
2252 unsigned PCAdj = (RelocM != Reloc::PIC_)
2253 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002254 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002255 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2256 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002257 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002258 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002259 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002260 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002261 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002262 false, false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002263
2264 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002265 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002266 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2267 }
2268 return Result;
2269 }
Evan Cheng92e39162011-03-29 23:06:19 +00002270 case Intrinsic::arm_neon_vmulls:
2271 case Intrinsic::arm_neon_vmullu: {
2272 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2273 ? ARMISD::VMULLs : ARMISD::VMULLu;
2274 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2275 Op.getOperand(1), Op.getOperand(2));
2276 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002277 }
2278}
2279
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002280static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002281 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002282 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002283 if (!Subtarget->hasDataBarrier()) {
2284 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2285 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2286 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002287 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002288 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002289 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002290 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002291 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002292
2293 SDValue Op5 = Op.getOperand(5);
2294 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2295 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2296 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2297 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2298
2299 ARM_MB::MemBOpt DMBOpt;
2300 if (isDeviceBarrier)
2301 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2302 else
2303 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2304 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2305 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002306}
2307
Eli Friedman26689ac2011-08-03 21:06:02 +00002308
2309static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2310 const ARMSubtarget *Subtarget) {
2311 // FIXME: handle "fence singlethread" more efficiently.
2312 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002313 if (!Subtarget->hasDataBarrier()) {
2314 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2315 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2316 // here.
2317 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2318 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002319 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002320 DAG.getConstant(0, MVT::i32));
2321 }
2322
Eli Friedman26689ac2011-08-03 21:06:02 +00002323 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002324 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002325}
2326
Evan Chengdfed19f2010-11-03 06:34:55 +00002327static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2328 const ARMSubtarget *Subtarget) {
2329 // ARM pre v5TE and Thumb1 does not have preload instructions.
2330 if (!(Subtarget->isThumb2() ||
2331 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2332 // Just preserve the chain.
2333 return Op.getOperand(0);
2334
2335 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002336 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2337 if (!isRead &&
2338 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2339 // ARMv7 with MP extension has PLDW.
2340 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002341
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002342 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2343 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002344 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002345 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002346 isData = ~isData & 1;
2347 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002348
2349 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002350 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2351 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002352}
2353
Dan Gohman1e93df62010-04-17 14:41:14 +00002354static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2355 MachineFunction &MF = DAG.getMachineFunction();
2356 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2357
Evan Chenga8e29892007-01-19 07:51:42 +00002358 // vastart just stores the address of the VarArgsFrameIndex slot into the
2359 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002360 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002361 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002362 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002363 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002364 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2365 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002366}
2367
Dan Gohman475871a2008-07-27 21:46:04 +00002368SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002369ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2370 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002371 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002372 MachineFunction &MF = DAG.getMachineFunction();
2373 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2374
2375 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002376 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002377 RC = ARM::tGPRRegisterClass;
2378 else
2379 RC = ARM::GPRRegisterClass;
2380
2381 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002382 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002383 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002384
2385 SDValue ArgValue2;
2386 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002387 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002388 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002389
2390 // Create load node to retrieve arguments from the stack.
2391 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002392 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002393 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002394 false, false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002395 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002396 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002397 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002398 }
2399
Jim Grosbache5165492009-11-09 00:11:35 +00002400 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002401}
2402
Stuart Hastingsc7315872011-04-20 16:47:52 +00002403void
2404ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2405 unsigned &VARegSize, unsigned &VARegSaveSize)
2406 const {
2407 unsigned NumGPRs;
2408 if (CCInfo.isFirstByValRegValid())
2409 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2410 else {
2411 unsigned int firstUnalloced;
2412 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2413 sizeof(GPRArgRegs) /
2414 sizeof(GPRArgRegs[0]));
2415 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2416 }
2417
2418 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2419 VARegSize = NumGPRs * 4;
2420 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2421}
2422
2423// The remaining GPRs hold either the beginning of variable-argument
2424// data, or the beginning of an aggregate passed by value (usuall
2425// byval). Either way, we allocate stack slots adjacent to the data
2426// provided by our caller, and store the unallocated registers there.
2427// If this is a variadic function, the va_list pointer will begin with
2428// these values; otherwise, this reassembles a (byval) structure that
2429// was split between registers and memory.
2430void
2431ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2432 DebugLoc dl, SDValue &Chain,
2433 unsigned ArgOffset) const {
2434 MachineFunction &MF = DAG.getMachineFunction();
2435 MachineFrameInfo *MFI = MF.getFrameInfo();
2436 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2437 unsigned firstRegToSaveIndex;
2438 if (CCInfo.isFirstByValRegValid())
2439 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2440 else {
2441 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2442 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2443 }
2444
2445 unsigned VARegSize, VARegSaveSize;
2446 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2447 if (VARegSaveSize) {
2448 // If this function is vararg, store any remaining integer argument regs
2449 // to their spots on the stack so that they may be loaded by deferencing
2450 // the result of va_next.
2451 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002452 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2453 ArgOffset + VARegSaveSize
2454 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002455 false));
2456 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2457 getPointerTy());
2458
2459 SmallVector<SDValue, 4> MemOps;
2460 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
2461 TargetRegisterClass *RC;
2462 if (AFI->isThumb1OnlyFunction())
2463 RC = ARM::tGPRRegisterClass;
2464 else
2465 RC = ARM::GPRRegisterClass;
2466
2467 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2468 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2469 SDValue Store =
2470 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002471 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002472 false, false, 0);
2473 MemOps.push_back(Store);
2474 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2475 DAG.getConstant(4, getPointerTy()));
2476 }
2477 if (!MemOps.empty())
2478 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2479 &MemOps[0], MemOps.size());
2480 } else
2481 // This will point to the next argument passed via stack.
2482 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2483}
2484
Bob Wilson5bafff32009-06-22 23:27:02 +00002485SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002486ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002487 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002488 const SmallVectorImpl<ISD::InputArg>
2489 &Ins,
2490 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002491 SmallVectorImpl<SDValue> &InVals)
2492 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002493 MachineFunction &MF = DAG.getMachineFunction();
2494 MachineFrameInfo *MFI = MF.getFrameInfo();
2495
Bob Wilson1f595bb2009-04-17 19:07:39 +00002496 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2497
2498 // Assign locations to all of the incoming arguments.
2499 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002500 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2501 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002502 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002503 CCAssignFnForNode(CallConv, /* Return*/ false,
2504 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002505
2506 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002507 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002508
Stuart Hastingsf222e592011-02-28 17:17:53 +00002509 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002510 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2511 CCValAssign &VA = ArgLocs[i];
2512
Bob Wilsondee46d72009-04-17 20:35:10 +00002513 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002514 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002515 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002516
Bob Wilson1f595bb2009-04-17 19:07:39 +00002517 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002518 // f64 and vector types are split up into multiple registers or
2519 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002520 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002521 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002522 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002523 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002524 SDValue ArgValue2;
2525 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002526 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002527 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2528 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002529 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002530 false, false, false, 0);
Bob Wilson6a234f02010-04-13 22:03:22 +00002531 } else {
2532 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2533 Chain, DAG, dl);
2534 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002535 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2536 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002537 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002538 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002539 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2540 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002541 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002542
Bob Wilson5bafff32009-06-22 23:27:02 +00002543 } else {
2544 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002545
Owen Anderson825b72b2009-08-11 20:47:22 +00002546 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002547 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002548 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002549 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002550 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002551 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002552 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002553 RC = (AFI->isThumb1OnlyFunction() ?
2554 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002555 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002556 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002557
2558 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002559 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002560 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002561 }
2562
2563 // If this is an 8 or 16-bit value, it is really passed promoted
2564 // to 32 bits. Insert an assert[sz]ext to capture this, then
2565 // truncate to the right size.
2566 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002567 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002568 case CCValAssign::Full: break;
2569 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002570 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002571 break;
2572 case CCValAssign::SExt:
2573 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2574 DAG.getValueType(VA.getValVT()));
2575 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2576 break;
2577 case CCValAssign::ZExt:
2578 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2579 DAG.getValueType(VA.getValVT()));
2580 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2581 break;
2582 }
2583
Dan Gohman98ca4f22009-08-05 01:29:28 +00002584 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002585
2586 } else { // VA.isRegLoc()
2587
2588 // sanity check
2589 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002590 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002591
Stuart Hastingsf222e592011-02-28 17:17:53 +00002592 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002593
Stuart Hastingsf222e592011-02-28 17:17:53 +00002594 // Some Ins[] entries become multiple ArgLoc[] entries.
2595 // Process them only once.
2596 if (index != lastInsIndex)
2597 {
2598 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002599 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002600 // This can be changed with more analysis.
2601 // In case of tail call optimization mark all arguments mutable.
2602 // Since they could be overwritten by lowering of arguments in case of
2603 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002604 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002605 unsigned VARegSize, VARegSaveSize;
2606 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2607 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2608 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002609 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002610 int FI = MFI->CreateFixedObject(Bytes,
2611 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002612 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2613 } else {
2614 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2615 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002616
Stuart Hastingsf222e592011-02-28 17:17:53 +00002617 // Create load nodes to retrieve arguments from the stack.
2618 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2619 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2620 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002621 false, false, false, 0));
Stuart Hastingsf222e592011-02-28 17:17:53 +00002622 }
2623 lastInsIndex = index;
2624 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002625 }
2626 }
2627
2628 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002629 if (isVarArg)
2630 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002631
Dan Gohman98ca4f22009-08-05 01:29:28 +00002632 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002633}
2634
2635/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002636static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002637 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002638 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002639 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002640 // Maybe this has already been legalized into the constant pool?
2641 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002642 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002643 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002644 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002645 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002646 }
2647 }
2648 return false;
2649}
2650
Evan Chenga8e29892007-01-19 07:51:42 +00002651/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2652/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002653SDValue
2654ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002655 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002656 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002657 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002658 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002659 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002660 // Constant does not fit, try adjusting it by one?
2661 switch (CC) {
2662 default: break;
2663 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002664 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002665 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002666 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002667 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002668 }
2669 break;
2670 case ISD::SETULT:
2671 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002672 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002673 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002674 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002675 }
2676 break;
2677 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002678 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002679 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002680 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002681 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002682 }
2683 break;
2684 case ISD::SETULE:
2685 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002686 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002687 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002688 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002689 }
2690 break;
2691 }
2692 }
2693 }
2694
2695 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002696 ARMISD::NodeType CompareType;
2697 switch (CondCode) {
2698 default:
2699 CompareType = ARMISD::CMP;
2700 break;
2701 case ARMCC::EQ:
2702 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002703 // Uses only Z Flag
2704 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002705 break;
2706 }
Evan Cheng218977b2010-07-13 19:27:42 +00002707 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002708 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002709}
2710
2711/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002712SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002713ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002714 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002715 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002716 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002717 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002718 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002719 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2720 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002721}
2722
Bob Wilson79f56c92011-03-08 01:17:20 +00002723/// duplicateCmp - Glue values can have only one use, so this function
2724/// duplicates a comparison node.
2725SDValue
2726ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2727 unsigned Opc = Cmp.getOpcode();
2728 DebugLoc DL = Cmp.getDebugLoc();
2729 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2730 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2731
2732 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2733 Cmp = Cmp.getOperand(0);
2734 Opc = Cmp.getOpcode();
2735 if (Opc == ARMISD::CMPFP)
2736 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2737 else {
2738 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2739 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2740 }
2741 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2742}
2743
Bill Wendlingde2b1512010-08-11 08:43:16 +00002744SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2745 SDValue Cond = Op.getOperand(0);
2746 SDValue SelectTrue = Op.getOperand(1);
2747 SDValue SelectFalse = Op.getOperand(2);
2748 DebugLoc dl = Op.getDebugLoc();
2749
2750 // Convert:
2751 //
2752 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2753 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2754 //
2755 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2756 const ConstantSDNode *CMOVTrue =
2757 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2758 const ConstantSDNode *CMOVFalse =
2759 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2760
2761 if (CMOVTrue && CMOVFalse) {
2762 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2763 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2764
2765 SDValue True;
2766 SDValue False;
2767 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2768 True = SelectTrue;
2769 False = SelectFalse;
2770 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2771 True = SelectFalse;
2772 False = SelectTrue;
2773 }
2774
2775 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002776 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002777 SDValue ARMcc = Cond.getOperand(2);
2778 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002779 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002780 assert(True.getValueType() == VT);
2781 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002782 }
2783 }
2784 }
2785
2786 return DAG.getSelectCC(dl, Cond,
2787 DAG.getConstant(0, Cond.getValueType()),
2788 SelectTrue, SelectFalse, ISD::SETNE);
2789}
2790
Dan Gohmand858e902010-04-17 15:26:15 +00002791SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002792 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002793 SDValue LHS = Op.getOperand(0);
2794 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002795 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002796 SDValue TrueVal = Op.getOperand(2);
2797 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002798 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002799
Owen Anderson825b72b2009-08-11 20:47:22 +00002800 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002801 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002802 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002803 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Jim Grosbachb04546f2011-09-13 20:30:37 +00002804 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002805 }
2806
2807 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002808 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002809
Evan Cheng218977b2010-07-13 19:27:42 +00002810 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2811 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002812 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002813 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002814 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002815 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002816 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002817 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002818 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002819 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002820 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002821 }
2822 return Result;
2823}
2824
Evan Cheng218977b2010-07-13 19:27:42 +00002825/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2826/// to morph to an integer compare sequence.
2827static bool canChangeToInt(SDValue Op, bool &SeenZero,
2828 const ARMSubtarget *Subtarget) {
2829 SDNode *N = Op.getNode();
2830 if (!N->hasOneUse())
2831 // Otherwise it requires moving the value from fp to integer registers.
2832 return false;
2833 if (!N->getNumValues())
2834 return false;
2835 EVT VT = Op.getValueType();
2836 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2837 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2838 // vmrs are very slow, e.g. cortex-a8.
2839 return false;
2840
2841 if (isFloatingPointZero(Op)) {
2842 SeenZero = true;
2843 return true;
2844 }
2845 return ISD::isNormalLoad(N);
2846}
2847
2848static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2849 if (isFloatingPointZero(Op))
2850 return DAG.getConstant(0, MVT::i32);
2851
2852 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2853 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002854 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002855 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002856 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002857
2858 llvm_unreachable("Unknown VFP cmp argument!");
2859}
2860
2861static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2862 SDValue &RetVal1, SDValue &RetVal2) {
2863 if (isFloatingPointZero(Op)) {
2864 RetVal1 = DAG.getConstant(0, MVT::i32);
2865 RetVal2 = DAG.getConstant(0, MVT::i32);
2866 return;
2867 }
2868
2869 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2870 SDValue Ptr = Ld->getBasePtr();
2871 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2872 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002873 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002874 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002875 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002876
2877 EVT PtrType = Ptr.getValueType();
2878 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2879 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2880 PtrType, Ptr, DAG.getConstant(4, PtrType));
2881 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2882 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002883 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002884 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002885 Ld->isInvariant(), NewAlign);
Evan Cheng218977b2010-07-13 19:27:42 +00002886 return;
2887 }
2888
2889 llvm_unreachable("Unknown VFP cmp argument!");
2890}
2891
2892/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2893/// f32 and even f64 comparisons to integer ones.
2894SDValue
2895ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2896 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002897 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002898 SDValue LHS = Op.getOperand(2);
2899 SDValue RHS = Op.getOperand(3);
2900 SDValue Dest = Op.getOperand(4);
2901 DebugLoc dl = Op.getDebugLoc();
2902
2903 bool SeenZero = false;
2904 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2905 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002906 // If one of the operand is zero, it's safe to ignore the NaN case since
2907 // we only care about equality comparisons.
2908 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Bob Wilson1b772f92011-03-08 01:17:16 +00002909 // If unsafe fp math optimization is enabled and there are no other uses of
2910 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00002911 // to an integer comparison.
2912 if (CC == ISD::SETOEQ)
2913 CC = ISD::SETEQ;
2914 else if (CC == ISD::SETUNE)
2915 CC = ISD::SETNE;
2916
2917 SDValue ARMcc;
2918 if (LHS.getValueType() == MVT::f32) {
2919 LHS = bitcastf32Toi32(LHS, DAG);
2920 RHS = bitcastf32Toi32(RHS, DAG);
2921 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2922 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2923 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2924 Chain, Dest, ARMcc, CCR, Cmp);
2925 }
2926
2927 SDValue LHS1, LHS2;
2928 SDValue RHS1, RHS2;
2929 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2930 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2931 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2932 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002933 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002934 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2935 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2936 }
2937
2938 return SDValue();
2939}
2940
2941SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2942 SDValue Chain = Op.getOperand(0);
2943 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2944 SDValue LHS = Op.getOperand(2);
2945 SDValue RHS = Op.getOperand(3);
2946 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002947 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002948
Owen Anderson825b72b2009-08-11 20:47:22 +00002949 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002950 SDValue ARMcc;
2951 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002952 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002953 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002954 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002955 }
2956
Owen Anderson825b72b2009-08-11 20:47:22 +00002957 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002958
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002959 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng218977b2010-07-13 19:27:42 +00002960 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2961 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2962 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2963 if (Result.getNode())
2964 return Result;
2965 }
2966
Evan Chenga8e29892007-01-19 07:51:42 +00002967 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002968 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002969
Evan Cheng218977b2010-07-13 19:27:42 +00002970 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2971 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002972 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002973 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002974 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002975 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002976 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002977 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2978 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002979 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002980 }
2981 return Res;
2982}
2983
Dan Gohmand858e902010-04-17 15:26:15 +00002984SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002985 SDValue Chain = Op.getOperand(0);
2986 SDValue Table = Op.getOperand(1);
2987 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002988 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002989
Owen Andersone50ed302009-08-10 22:56:29 +00002990 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002991 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2992 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002993 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002994 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002995 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002996 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2997 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002998 if (Subtarget->isThumb2()) {
2999 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3000 // which does another jump to the destination. This also makes it easier
3001 // to translate it to TBB / TBH later.
3002 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00003003 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00003004 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003005 }
Evan Cheng66ac5312009-07-25 00:33:29 +00003006 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00003007 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003008 MachinePointerInfo::getJumpTable(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003009 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003010 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003011 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00003012 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003013 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00003014 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003015 MachinePointerInfo::getJumpTable(),
3016 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003017 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003018 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003019 }
Evan Chenga8e29892007-01-19 07:51:42 +00003020}
3021
Eli Friedman14e809c2011-11-09 23:36:02 +00003022static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
3023 EVT VT = Op.getValueType();
3024 assert(VT.getVectorElementType() == MVT::i32 && "Unexpected custom lowering");
3025
3026 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3027 return Op;
3028 return DAG.UnrollVectorOp(Op.getNode());
3029}
3030
Bob Wilson76a312b2010-03-19 22:51:32 +00003031static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman14e809c2011-11-09 23:36:02 +00003032 EVT VT = Op.getValueType();
3033 if (VT.isVector())
3034 return LowerVectorFP_TO_INT(Op, DAG);
3035
Bob Wilson76a312b2010-03-19 22:51:32 +00003036 DebugLoc dl = Op.getDebugLoc();
3037 unsigned Opc;
3038
3039 switch (Op.getOpcode()) {
3040 default:
3041 assert(0 && "Invalid opcode!");
3042 case ISD::FP_TO_SINT:
3043 Opc = ARMISD::FTOSI;
3044 break;
3045 case ISD::FP_TO_UINT:
3046 Opc = ARMISD::FTOUI;
3047 break;
3048 }
3049 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003050 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003051}
3052
Cameron Zwarich3007d332011-03-29 21:41:55 +00003053static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3054 EVT VT = Op.getValueType();
3055 DebugLoc dl = Op.getDebugLoc();
3056
Eli Friedman14e809c2011-11-09 23:36:02 +00003057 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3058 if (VT.getVectorElementType() == MVT::f32)
3059 return Op;
3060 return DAG.UnrollVectorOp(Op.getNode());
3061 }
3062
Duncan Sands1f6a3292011-08-12 14:54:45 +00003063 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3064 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003065 if (VT != MVT::v4f32)
3066 return DAG.UnrollVectorOp(Op.getNode());
3067
3068 unsigned CastOpc;
3069 unsigned Opc;
3070 switch (Op.getOpcode()) {
3071 default:
3072 assert(0 && "Invalid opcode!");
3073 case ISD::SINT_TO_FP:
3074 CastOpc = ISD::SIGN_EXTEND;
3075 Opc = ISD::SINT_TO_FP;
3076 break;
3077 case ISD::UINT_TO_FP:
3078 CastOpc = ISD::ZERO_EXTEND;
3079 Opc = ISD::UINT_TO_FP;
3080 break;
3081 }
3082
3083 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3084 return DAG.getNode(Opc, dl, VT, Op);
3085}
3086
Bob Wilson76a312b2010-03-19 22:51:32 +00003087static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3088 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003089 if (VT.isVector())
3090 return LowerVectorINT_TO_FP(Op, DAG);
3091
Bob Wilson76a312b2010-03-19 22:51:32 +00003092 DebugLoc dl = Op.getDebugLoc();
3093 unsigned Opc;
3094
3095 switch (Op.getOpcode()) {
3096 default:
3097 assert(0 && "Invalid opcode!");
3098 case ISD::SINT_TO_FP:
3099 Opc = ARMISD::SITOF;
3100 break;
3101 case ISD::UINT_TO_FP:
3102 Opc = ARMISD::UITOF;
3103 break;
3104 }
3105
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003106 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003107 return DAG.getNode(Opc, dl, VT, Op);
3108}
3109
Evan Cheng515fe3a2010-07-08 02:08:50 +00003110SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003111 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003112 SDValue Tmp0 = Op.getOperand(0);
3113 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003114 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003115 EVT VT = Op.getValueType();
3116 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003117 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3118 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3119 bool UseNEON = !InGPR && Subtarget->hasNEON();
3120
3121 if (UseNEON) {
3122 // Use VBSL to copy the sign bit.
3123 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3124 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3125 DAG.getTargetConstant(EncodedVal, MVT::i32));
3126 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3127 if (VT == MVT::f64)
3128 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3129 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3130 DAG.getConstant(32, MVT::i32));
3131 else /*if (VT == MVT::f32)*/
3132 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3133 if (SrcVT == MVT::f32) {
3134 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3135 if (VT == MVT::f64)
3136 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3137 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3138 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003139 } else if (VT == MVT::f32)
3140 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3141 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3142 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003143 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3144 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3145
3146 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3147 MVT::i32);
3148 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3149 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3150 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003151
Evan Chenge573fb32011-02-23 02:24:55 +00003152 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3153 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3154 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003155 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003156 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3157 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3158 DAG.getConstant(0, MVT::i32));
3159 } else {
3160 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3161 }
3162
3163 return Res;
3164 }
Evan Chengc143dd42011-02-11 02:28:55 +00003165
3166 // Bitcast operand 1 to i32.
3167 if (SrcVT == MVT::f64)
3168 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3169 &Tmp1, 1).getValue(1);
3170 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3171
Evan Chenge573fb32011-02-23 02:24:55 +00003172 // Or in the signbit with integer operations.
3173 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3174 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3175 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3176 if (VT == MVT::f32) {
3177 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3178 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3179 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3180 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003181 }
3182
Evan Chenge573fb32011-02-23 02:24:55 +00003183 // f64: Or the high part with signbit and then combine two parts.
3184 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3185 &Tmp0, 1);
3186 SDValue Lo = Tmp0.getValue(0);
3187 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3188 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3189 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003190}
3191
Evan Cheng2457f2c2010-05-22 01:47:14 +00003192SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3193 MachineFunction &MF = DAG.getMachineFunction();
3194 MachineFrameInfo *MFI = MF.getFrameInfo();
3195 MFI->setReturnAddressIsTaken(true);
3196
3197 EVT VT = Op.getValueType();
3198 DebugLoc dl = Op.getDebugLoc();
3199 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3200 if (Depth) {
3201 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3202 SDValue Offset = DAG.getConstant(4, MVT::i32);
3203 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3204 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003205 MachinePointerInfo(), false, false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003206 }
3207
3208 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003209 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003210 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3211}
3212
Dan Gohmand858e902010-04-17 15:26:15 +00003213SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003214 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3215 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003216
Owen Andersone50ed302009-08-10 22:56:29 +00003217 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003218 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3219 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003220 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003221 ? ARM::R7 : ARM::R11;
3222 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3223 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003224 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3225 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003226 false, false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003227 return FrameAddr;
3228}
3229
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003230/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003231/// expand a bit convert where either the source or destination type is i64 to
3232/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3233/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3234/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003235static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003236 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3237 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003238 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003239
Bob Wilson9f3f0612010-04-17 05:30:19 +00003240 // This function is only supposed to be called for i64 types, either as the
3241 // source or destination of the bit convert.
3242 EVT SrcVT = Op.getValueType();
3243 EVT DstVT = N->getValueType(0);
3244 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003245 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003246
Bob Wilson9f3f0612010-04-17 05:30:19 +00003247 // Turn i64->f64 into VMOVDRR.
3248 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003249 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3250 DAG.getConstant(0, MVT::i32));
3251 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3252 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003253 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003254 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003255 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003256
Jim Grosbache5165492009-11-09 00:11:35 +00003257 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003258 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3259 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3260 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3261 // Merge the pieces into a single i64 value.
3262 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3263 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003264
Bob Wilson9f3f0612010-04-17 05:30:19 +00003265 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003266}
3267
Bob Wilson5bafff32009-06-22 23:27:02 +00003268/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003269/// Zero vectors are used to represent vector negation and in those cases
3270/// will be implemented with the NEON VNEG instruction. However, VNEG does
3271/// not support i64 elements, so sometimes the zero vectors will need to be
3272/// explicitly constructed. Regardless, use a canonical VMOV to create the
3273/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003274static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003275 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003276 // The canonical modified immediate encoding of a zero vector is....0!
3277 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3278 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3279 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003280 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003281}
3282
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003283/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3284/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003285SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3286 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003287 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3288 EVT VT = Op.getValueType();
3289 unsigned VTBits = VT.getSizeInBits();
3290 DebugLoc dl = Op.getDebugLoc();
3291 SDValue ShOpLo = Op.getOperand(0);
3292 SDValue ShOpHi = Op.getOperand(1);
3293 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003294 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003295 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003296
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003297 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3298
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003299 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3300 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3301 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3302 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3303 DAG.getConstant(VTBits, MVT::i32));
3304 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3305 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003306 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003307
3308 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3309 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003310 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003311 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003312 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003313 CCR, Cmp);
3314
3315 SDValue Ops[2] = { Lo, Hi };
3316 return DAG.getMergeValues(Ops, 2, dl);
3317}
3318
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003319/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3320/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003321SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3322 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003323 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3324 EVT VT = Op.getValueType();
3325 unsigned VTBits = VT.getSizeInBits();
3326 DebugLoc dl = Op.getDebugLoc();
3327 SDValue ShOpLo = Op.getOperand(0);
3328 SDValue ShOpHi = Op.getOperand(1);
3329 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003330 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003331
3332 assert(Op.getOpcode() == ISD::SHL_PARTS);
3333 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3334 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3335 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3336 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3337 DAG.getConstant(VTBits, MVT::i32));
3338 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3339 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3340
3341 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3342 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3343 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003344 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003345 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003346 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003347 CCR, Cmp);
3348
3349 SDValue Ops[2] = { Lo, Hi };
3350 return DAG.getMergeValues(Ops, 2, dl);
3351}
3352
Jim Grosbach4725ca72010-09-08 03:54:02 +00003353SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003354 SelectionDAG &DAG) const {
3355 // The rounding mode is in bits 23:22 of the FPSCR.
3356 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3357 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3358 // so that the shift + and get folded into a bitfield extract.
3359 DebugLoc dl = Op.getDebugLoc();
3360 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3361 DAG.getConstant(Intrinsic::arm_get_fpscr,
3362 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003363 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003364 DAG.getConstant(1U << 22, MVT::i32));
3365 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3366 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003367 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003368 DAG.getConstant(3, MVT::i32));
3369}
3370
Jim Grosbach3482c802010-01-18 19:58:49 +00003371static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3372 const ARMSubtarget *ST) {
3373 EVT VT = N->getValueType(0);
3374 DebugLoc dl = N->getDebugLoc();
3375
3376 if (!ST->hasV6T2Ops())
3377 return SDValue();
3378
3379 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3380 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3381}
3382
Bob Wilson5bafff32009-06-22 23:27:02 +00003383static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3384 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003385 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003386 DebugLoc dl = N->getDebugLoc();
3387
Bob Wilsond5448bb2010-11-18 21:16:28 +00003388 if (!VT.isVector())
3389 return SDValue();
3390
Bob Wilson5bafff32009-06-22 23:27:02 +00003391 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003392 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003393
Bob Wilsond5448bb2010-11-18 21:16:28 +00003394 // Left shifts translate directly to the vshiftu intrinsic.
3395 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003396 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003397 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3398 N->getOperand(0), N->getOperand(1));
3399
3400 assert((N->getOpcode() == ISD::SRA ||
3401 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3402
3403 // NEON uses the same intrinsics for both left and right shifts. For
3404 // right shifts, the shift amounts are negative, so negate the vector of
3405 // shift amounts.
3406 EVT ShiftVT = N->getOperand(1).getValueType();
3407 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3408 getZeroVector(ShiftVT, DAG, dl),
3409 N->getOperand(1));
3410 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3411 Intrinsic::arm_neon_vshifts :
3412 Intrinsic::arm_neon_vshiftu);
3413 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3414 DAG.getConstant(vshiftInt, MVT::i32),
3415 N->getOperand(0), NegatedCount);
3416}
3417
3418static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3419 const ARMSubtarget *ST) {
3420 EVT VT = N->getValueType(0);
3421 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003422
Eli Friedmance392eb2009-08-22 03:13:10 +00003423 // We can get here for a node like i32 = ISD::SHL i32, i64
3424 if (VT != MVT::i64)
3425 return SDValue();
3426
3427 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003428 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003429
Chris Lattner27a6c732007-11-24 07:07:01 +00003430 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3431 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003432 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003433 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003434
Chris Lattner27a6c732007-11-24 07:07:01 +00003435 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003436 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003437
Chris Lattner27a6c732007-11-24 07:07:01 +00003438 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003439 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003440 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003441 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003442 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003443
Chris Lattner27a6c732007-11-24 07:07:01 +00003444 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3445 // captures the result into a carry flag.
3446 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003447 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003448
Chris Lattner27a6c732007-11-24 07:07:01 +00003449 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003450 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003451
Chris Lattner27a6c732007-11-24 07:07:01 +00003452 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003453 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003454}
3455
Bob Wilson5bafff32009-06-22 23:27:02 +00003456static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3457 SDValue TmpOp0, TmpOp1;
3458 bool Invert = false;
3459 bool Swap = false;
3460 unsigned Opc = 0;
3461
3462 SDValue Op0 = Op.getOperand(0);
3463 SDValue Op1 = Op.getOperand(1);
3464 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003465 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003466 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3467 DebugLoc dl = Op.getDebugLoc();
3468
3469 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3470 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003471 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003472 case ISD::SETUNE:
3473 case ISD::SETNE: Invert = true; // Fallthrough
3474 case ISD::SETOEQ:
3475 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3476 case ISD::SETOLT:
3477 case ISD::SETLT: Swap = true; // Fallthrough
3478 case ISD::SETOGT:
3479 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3480 case ISD::SETOLE:
3481 case ISD::SETLE: Swap = true; // Fallthrough
3482 case ISD::SETOGE:
3483 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3484 case ISD::SETUGE: Swap = true; // Fallthrough
3485 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3486 case ISD::SETUGT: Swap = true; // Fallthrough
3487 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3488 case ISD::SETUEQ: Invert = true; // Fallthrough
3489 case ISD::SETONE:
3490 // Expand this to (OLT | OGT).
3491 TmpOp0 = Op0;
3492 TmpOp1 = Op1;
3493 Opc = ISD::OR;
3494 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3495 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3496 break;
3497 case ISD::SETUO: Invert = true; // Fallthrough
3498 case ISD::SETO:
3499 // Expand this to (OLT | OGE).
3500 TmpOp0 = Op0;
3501 TmpOp1 = Op1;
3502 Opc = ISD::OR;
3503 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3504 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3505 break;
3506 }
3507 } else {
3508 // Integer comparisons.
3509 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003510 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003511 case ISD::SETNE: Invert = true;
3512 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3513 case ISD::SETLT: Swap = true;
3514 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3515 case ISD::SETLE: Swap = true;
3516 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3517 case ISD::SETULT: Swap = true;
3518 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3519 case ISD::SETULE: Swap = true;
3520 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3521 }
3522
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003523 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003524 if (Opc == ARMISD::VCEQ) {
3525
3526 SDValue AndOp;
3527 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3528 AndOp = Op0;
3529 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3530 AndOp = Op1;
3531
3532 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003533 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003534 AndOp = AndOp.getOperand(0);
3535
3536 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3537 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003538 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3539 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003540 Invert = !Invert;
3541 }
3542 }
3543 }
3544
3545 if (Swap)
3546 std::swap(Op0, Op1);
3547
Owen Andersonc24cb352010-11-08 23:21:22 +00003548 // If one of the operands is a constant vector zero, attempt to fold the
3549 // comparison to a specialized compare-against-zero form.
3550 SDValue SingleOp;
3551 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3552 SingleOp = Op0;
3553 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3554 if (Opc == ARMISD::VCGE)
3555 Opc = ARMISD::VCLEZ;
3556 else if (Opc == ARMISD::VCGT)
3557 Opc = ARMISD::VCLTZ;
3558 SingleOp = Op1;
3559 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003560
Owen Andersonc24cb352010-11-08 23:21:22 +00003561 SDValue Result;
3562 if (SingleOp.getNode()) {
3563 switch (Opc) {
3564 case ARMISD::VCEQ:
3565 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3566 case ARMISD::VCGE:
3567 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3568 case ARMISD::VCLEZ:
3569 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3570 case ARMISD::VCGT:
3571 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3572 case ARMISD::VCLTZ:
3573 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3574 default:
3575 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3576 }
3577 } else {
3578 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3579 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003580
3581 if (Invert)
3582 Result = DAG.getNOT(dl, Result, VT);
3583
3584 return Result;
3585}
3586
Bob Wilsond3c42842010-06-14 22:19:57 +00003587/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3588/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003589/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003590static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3591 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003592 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003593 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003594
Bob Wilson827b2102010-06-15 19:05:35 +00003595 // SplatBitSize is set to the smallest size that splats the vector, so a
3596 // zero vector will always have SplatBitSize == 8. However, NEON modified
3597 // immediate instructions others than VMOV do not support the 8-bit encoding
3598 // of a zero vector, and the default encoding of zero is supposed to be the
3599 // 32-bit version.
3600 if (SplatBits == 0)
3601 SplatBitSize = 32;
3602
Bob Wilson5bafff32009-06-22 23:27:02 +00003603 switch (SplatBitSize) {
3604 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003605 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003606 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003607 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003608 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003609 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003610 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003611 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003612 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003613
3614 case 16:
3615 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003616 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003617 if ((SplatBits & ~0xff) == 0) {
3618 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003619 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003620 Imm = SplatBits;
3621 break;
3622 }
3623 if ((SplatBits & ~0xff00) == 0) {
3624 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003625 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003626 Imm = SplatBits >> 8;
3627 break;
3628 }
3629 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003630
3631 case 32:
3632 // NEON's 32-bit VMOV supports splat values where:
3633 // * only one byte is nonzero, or
3634 // * the least significant byte is 0xff and the second byte is nonzero, or
3635 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003636 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003637 if ((SplatBits & ~0xff) == 0) {
3638 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003639 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003640 Imm = SplatBits;
3641 break;
3642 }
3643 if ((SplatBits & ~0xff00) == 0) {
3644 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003645 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003646 Imm = SplatBits >> 8;
3647 break;
3648 }
3649 if ((SplatBits & ~0xff0000) == 0) {
3650 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003651 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003652 Imm = SplatBits >> 16;
3653 break;
3654 }
3655 if ((SplatBits & ~0xff000000) == 0) {
3656 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003657 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003658 Imm = SplatBits >> 24;
3659 break;
3660 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003661
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003662 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3663 if (type == OtherModImm) return SDValue();
3664
Bob Wilson5bafff32009-06-22 23:27:02 +00003665 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003666 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3667 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003668 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003669 Imm = SplatBits >> 8;
3670 SplatBits |= 0xff;
3671 break;
3672 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003673
3674 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003675 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3676 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003677 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003678 Imm = SplatBits >> 16;
3679 SplatBits |= 0xffff;
3680 break;
3681 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003682
3683 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3684 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3685 // VMOV.I32. A (very) minor optimization would be to replicate the value
3686 // and fall through here to test for a valid 64-bit splat. But, then the
3687 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003688 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003689
3690 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003691 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003692 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003693 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003694 uint64_t BitMask = 0xff;
3695 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003696 unsigned ImmMask = 1;
3697 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003698 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003699 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003700 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003701 Imm |= ImmMask;
3702 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003703 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003704 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003705 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003706 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003707 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003708 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003709 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003710 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003711 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003712 break;
3713 }
3714
Bob Wilson1a913ed2010-06-11 21:34:50 +00003715 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003716 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003717 return SDValue();
3718 }
3719
Bob Wilsoncba270d2010-07-13 21:16:48 +00003720 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3721 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003722}
3723
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003724static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3725 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003726 unsigned NumElts = VT.getVectorNumElements();
3727 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003728
3729 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3730 if (M[0] < 0)
3731 return false;
3732
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003733 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003734
3735 // If this is a VEXT shuffle, the immediate value is the index of the first
3736 // element. The other shuffle indices must be the successive elements after
3737 // the first one.
3738 unsigned ExpectedElt = Imm;
3739 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003740 // Increment the expected index. If it wraps around, it may still be
3741 // a VEXT but the source vectors must be swapped.
3742 ExpectedElt += 1;
3743 if (ExpectedElt == NumElts * 2) {
3744 ExpectedElt = 0;
3745 ReverseVEXT = true;
3746 }
3747
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003748 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003749 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003750 return false;
3751 }
3752
3753 // Adjust the index value if the source operands will be swapped.
3754 if (ReverseVEXT)
3755 Imm -= NumElts;
3756
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003757 return true;
3758}
3759
Bob Wilson8bb9e482009-07-26 00:39:34 +00003760/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3761/// instruction with the specified blocksize. (The order of the elements
3762/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003763static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3764 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003765 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3766 "Only possible block sizes for VREV are: 16, 32, 64");
3767
Bob Wilson8bb9e482009-07-26 00:39:34 +00003768 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003769 if (EltSz == 64)
3770 return false;
3771
3772 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003773 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003774 // If the first shuffle index is UNDEF, be optimistic.
3775 if (M[0] < 0)
3776 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003777
3778 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3779 return false;
3780
3781 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003782 if (M[i] < 0) continue; // ignore UNDEF indices
3783 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003784 return false;
3785 }
3786
3787 return true;
3788}
3789
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003790static bool isVTBLMask(const SmallVectorImpl<int> &M, EVT VT) {
3791 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3792 // range, then 0 is placed into the resulting vector. So pretty much any mask
3793 // of 8 elements can work here.
3794 return VT == MVT::v8i8 && M.size() == 8;
3795}
3796
Bob Wilsonc692cb72009-08-21 20:54:19 +00003797static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3798 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003799 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3800 if (EltSz == 64)
3801 return false;
3802
Bob Wilsonc692cb72009-08-21 20:54:19 +00003803 unsigned NumElts = VT.getVectorNumElements();
3804 WhichResult = (M[0] == 0 ? 0 : 1);
3805 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003806 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3807 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003808 return false;
3809 }
3810 return true;
3811}
3812
Bob Wilson324f4f12009-12-03 06:40:55 +00003813/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3814/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3815/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3816static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3817 unsigned &WhichResult) {
3818 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3819 if (EltSz == 64)
3820 return false;
3821
3822 unsigned NumElts = VT.getVectorNumElements();
3823 WhichResult = (M[0] == 0 ? 0 : 1);
3824 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003825 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3826 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003827 return false;
3828 }
3829 return true;
3830}
3831
Bob Wilsonc692cb72009-08-21 20:54:19 +00003832static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3833 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003834 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3835 if (EltSz == 64)
3836 return false;
3837
Bob Wilsonc692cb72009-08-21 20:54:19 +00003838 unsigned NumElts = VT.getVectorNumElements();
3839 WhichResult = (M[0] == 0 ? 0 : 1);
3840 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003841 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003842 if ((unsigned) M[i] != 2 * i + WhichResult)
3843 return false;
3844 }
3845
3846 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003847 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003848 return false;
3849
3850 return true;
3851}
3852
Bob Wilson324f4f12009-12-03 06:40:55 +00003853/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3854/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3855/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3856static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3857 unsigned &WhichResult) {
3858 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3859 if (EltSz == 64)
3860 return false;
3861
3862 unsigned Half = VT.getVectorNumElements() / 2;
3863 WhichResult = (M[0] == 0 ? 0 : 1);
3864 for (unsigned j = 0; j != 2; ++j) {
3865 unsigned Idx = WhichResult;
3866 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003867 int MIdx = M[i + j * Half];
3868 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003869 return false;
3870 Idx += 2;
3871 }
3872 }
3873
3874 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3875 if (VT.is64BitVector() && EltSz == 32)
3876 return false;
3877
3878 return true;
3879}
3880
Bob Wilsonc692cb72009-08-21 20:54:19 +00003881static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3882 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003883 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3884 if (EltSz == 64)
3885 return false;
3886
Bob Wilsonc692cb72009-08-21 20:54:19 +00003887 unsigned NumElts = VT.getVectorNumElements();
3888 WhichResult = (M[0] == 0 ? 0 : 1);
3889 unsigned Idx = WhichResult * NumElts / 2;
3890 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003891 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3892 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003893 return false;
3894 Idx += 1;
3895 }
3896
3897 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003898 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003899 return false;
3900
3901 return true;
3902}
3903
Bob Wilson324f4f12009-12-03 06:40:55 +00003904/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3905/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3906/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3907static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3908 unsigned &WhichResult) {
3909 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3910 if (EltSz == 64)
3911 return false;
3912
3913 unsigned NumElts = VT.getVectorNumElements();
3914 WhichResult = (M[0] == 0 ? 0 : 1);
3915 unsigned Idx = WhichResult * NumElts / 2;
3916 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003917 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3918 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003919 return false;
3920 Idx += 1;
3921 }
3922
3923 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3924 if (VT.is64BitVector() && EltSz == 32)
3925 return false;
3926
3927 return true;
3928}
3929
Dale Johannesenf630c712010-07-29 20:10:08 +00003930// If N is an integer constant that can be moved into a register in one
3931// instruction, return an SDValue of such a constant (will become a MOV
3932// instruction). Otherwise return null.
3933static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3934 const ARMSubtarget *ST, DebugLoc dl) {
3935 uint64_t Val;
3936 if (!isa<ConstantSDNode>(N))
3937 return SDValue();
3938 Val = cast<ConstantSDNode>(N)->getZExtValue();
3939
3940 if (ST->isThumb1Only()) {
3941 if (Val <= 255 || ~Val <= 255)
3942 return DAG.getConstant(Val, MVT::i32);
3943 } else {
3944 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3945 return DAG.getConstant(Val, MVT::i32);
3946 }
3947 return SDValue();
3948}
3949
Bob Wilson5bafff32009-06-22 23:27:02 +00003950// If this is a case we can't handle, return null and let the default
3951// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003952SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3953 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003954 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003955 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003956 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003957
3958 APInt SplatBits, SplatUndef;
3959 unsigned SplatBitSize;
3960 bool HasAnyUndefs;
3961 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003962 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003963 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003964 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003965 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003966 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003967 DAG, VmovVT, VT.is128BitVector(),
3968 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003969 if (Val.getNode()) {
3970 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003971 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003972 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003973
3974 // Try an immediate VMVN.
Eli Friedman8e4d0422011-10-13 22:40:23 +00003975 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003976 Val = isNEONModifiedImm(NegatedImm,
3977 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003978 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003979 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003980 if (Val.getNode()) {
3981 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003982 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003983 }
Evan Chengeaa192a2011-11-15 02:12:34 +00003984
3985 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
3986 if (VT == MVT::v2f32 || VT == MVT::v4f32) {
Eli Friedmaneffab8f2011-12-09 23:54:42 +00003987 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Chengeaa192a2011-11-15 02:12:34 +00003988 if (ImmVal != -1) {
3989 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
3990 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
3991 }
3992 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003993 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003994 }
3995
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003996 // Scan through the operands to see if only one value is used.
3997 unsigned NumElts = VT.getVectorNumElements();
3998 bool isOnlyLowElement = true;
3999 bool usesOnlyOneValue = true;
4000 bool isConstant = true;
4001 SDValue Value;
4002 for (unsigned i = 0; i < NumElts; ++i) {
4003 SDValue V = Op.getOperand(i);
4004 if (V.getOpcode() == ISD::UNDEF)
4005 continue;
4006 if (i > 0)
4007 isOnlyLowElement = false;
4008 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4009 isConstant = false;
4010
4011 if (!Value.getNode())
4012 Value = V;
4013 else if (V != Value)
4014 usesOnlyOneValue = false;
4015 }
4016
4017 if (!Value.getNode())
4018 return DAG.getUNDEF(VT);
4019
4020 if (isOnlyLowElement)
4021 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4022
Dale Johannesenf630c712010-07-29 20:10:08 +00004023 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4024
Dale Johannesen575cd142010-10-19 20:00:17 +00004025 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4026 // i32 and try again.
4027 if (usesOnlyOneValue && EltSize <= 32) {
4028 if (!isConstant)
4029 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4030 if (VT.getVectorElementType().isFloatingPoint()) {
4031 SmallVector<SDValue, 8> Ops;
4032 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004033 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00004034 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00004035 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
4036 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00004037 Val = LowerBUILD_VECTOR(Val, DAG, ST);
4038 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004039 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004040 }
Dale Johannesen575cd142010-10-19 20:00:17 +00004041 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
4042 if (Val.getNode())
4043 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004044 }
4045
4046 // If all elements are constants and the case above didn't get hit, fall back
4047 // to the default expansion, which will generate a load from the constant
4048 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004049 if (isConstant)
4050 return SDValue();
4051
Bob Wilson11a1dff2011-01-07 21:37:30 +00004052 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4053 if (NumElts >= 4) {
4054 SDValue shuffle = ReconstructShuffle(Op, DAG);
4055 if (shuffle != SDValue())
4056 return shuffle;
4057 }
4058
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004059 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004060 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4061 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004062 if (EltSize >= 32) {
4063 // Do the expansion with floating-point types, since that is what the VFP
4064 // registers are defined to use, and since i64 is not legal.
4065 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4066 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004067 SmallVector<SDValue, 8> Ops;
4068 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004069 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004070 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004071 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004072 }
4073
4074 return SDValue();
4075}
4076
Bob Wilson11a1dff2011-01-07 21:37:30 +00004077// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004078// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004079SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4080 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004081 DebugLoc dl = Op.getDebugLoc();
4082 EVT VT = Op.getValueType();
4083 unsigned NumElts = VT.getVectorNumElements();
4084
4085 SmallVector<SDValue, 2> SourceVecs;
4086 SmallVector<unsigned, 2> MinElts;
4087 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004088
Bob Wilson11a1dff2011-01-07 21:37:30 +00004089 for (unsigned i = 0; i < NumElts; ++i) {
4090 SDValue V = Op.getOperand(i);
4091 if (V.getOpcode() == ISD::UNDEF)
4092 continue;
4093 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4094 // A shuffle can only come from building a vector from various
4095 // elements of other vectors.
4096 return SDValue();
Eli Friedman46995fa2011-10-14 23:58:49 +00004097 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
4098 VT.getVectorElementType()) {
4099 // This code doesn't know how to handle shuffles where the vector
4100 // element types do not match (this happens because type legalization
4101 // promotes the return type of EXTRACT_VECTOR_ELT).
4102 // FIXME: It might be appropriate to extend this code to handle
4103 // mismatched types.
4104 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004105 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004106
Bob Wilson11a1dff2011-01-07 21:37:30 +00004107 // Record this extraction against the appropriate vector if possible...
4108 SDValue SourceVec = V.getOperand(0);
4109 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4110 bool FoundSource = false;
4111 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4112 if (SourceVecs[j] == SourceVec) {
4113 if (MinElts[j] > EltNo)
4114 MinElts[j] = EltNo;
4115 if (MaxElts[j] < EltNo)
4116 MaxElts[j] = EltNo;
4117 FoundSource = true;
4118 break;
4119 }
4120 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004121
Bob Wilson11a1dff2011-01-07 21:37:30 +00004122 // Or record a new source if not...
4123 if (!FoundSource) {
4124 SourceVecs.push_back(SourceVec);
4125 MinElts.push_back(EltNo);
4126 MaxElts.push_back(EltNo);
4127 }
4128 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004129
Bob Wilson11a1dff2011-01-07 21:37:30 +00004130 // Currently only do something sane when at most two source vectors
4131 // involved.
4132 if (SourceVecs.size() > 2)
4133 return SDValue();
4134
4135 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4136 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004137
Bob Wilson11a1dff2011-01-07 21:37:30 +00004138 // This loop extracts the usage patterns of the source vectors
4139 // and prepares appropriate SDValues for a shuffle if possible.
4140 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4141 if (SourceVecs[i].getValueType() == VT) {
4142 // No VEXT necessary
4143 ShuffleSrcs[i] = SourceVecs[i];
4144 VEXTOffsets[i] = 0;
4145 continue;
4146 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4147 // It probably isn't worth padding out a smaller vector just to
4148 // break it down again in a shuffle.
4149 return SDValue();
4150 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004151
Bob Wilson11a1dff2011-01-07 21:37:30 +00004152 // Since only 64-bit and 128-bit vectors are legal on ARM and
4153 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004154 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4155 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004156
Bob Wilson11a1dff2011-01-07 21:37:30 +00004157 if (MaxElts[i] - MinElts[i] >= NumElts) {
4158 // Span too large for a VEXT to cope
4159 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004160 }
4161
Bob Wilson11a1dff2011-01-07 21:37:30 +00004162 if (MinElts[i] >= NumElts) {
4163 // The extraction can just take the second half
4164 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004165 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4166 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004167 DAG.getIntPtrConstant(NumElts));
4168 } else if (MaxElts[i] < NumElts) {
4169 // The extraction can just take the first half
4170 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004171 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4172 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004173 DAG.getIntPtrConstant(0));
4174 } else {
4175 // An actual VEXT is needed
4176 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004177 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4178 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004179 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004180 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4181 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004182 DAG.getIntPtrConstant(NumElts));
4183 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4184 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4185 }
4186 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004187
Bob Wilson11a1dff2011-01-07 21:37:30 +00004188 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004189
Bob Wilson11a1dff2011-01-07 21:37:30 +00004190 for (unsigned i = 0; i < NumElts; ++i) {
4191 SDValue Entry = Op.getOperand(i);
4192 if (Entry.getOpcode() == ISD::UNDEF) {
4193 Mask.push_back(-1);
4194 continue;
4195 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004196
Bob Wilson11a1dff2011-01-07 21:37:30 +00004197 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004198 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4199 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004200 if (ExtractVec == SourceVecs[0]) {
4201 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4202 } else {
4203 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4204 }
4205 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004206
Bob Wilson11a1dff2011-01-07 21:37:30 +00004207 // Final check before we try to produce nonsense...
4208 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004209 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4210 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004211
Bob Wilson11a1dff2011-01-07 21:37:30 +00004212 return SDValue();
4213}
4214
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004215/// isShuffleMaskLegal - Targets can use this to indicate that they only
4216/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4217/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4218/// are assumed to be legal.
4219bool
4220ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4221 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004222 if (VT.getVectorNumElements() == 4 &&
4223 (VT.is128BitVector() || VT.is64BitVector())) {
4224 unsigned PFIndexes[4];
4225 for (unsigned i = 0; i != 4; ++i) {
4226 if (M[i] < 0)
4227 PFIndexes[i] = 8;
4228 else
4229 PFIndexes[i] = M[i];
4230 }
4231
4232 // Compute the index in the perfect shuffle table.
4233 unsigned PFTableIndex =
4234 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4235 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4236 unsigned Cost = (PFEntry >> 30);
4237
4238 if (Cost <= 4)
4239 return true;
4240 }
4241
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004242 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004243 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004244
Bob Wilson53dd2452010-06-07 23:53:38 +00004245 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4246 return (EltSize >= 32 ||
4247 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004248 isVREVMask(M, VT, 64) ||
4249 isVREVMask(M, VT, 32) ||
4250 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004251 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004252 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004253 isVTRNMask(M, VT, WhichResult) ||
4254 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004255 isVZIPMask(M, VT, WhichResult) ||
4256 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4257 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4258 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004259}
4260
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004261/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4262/// the specified operations to build the shuffle.
4263static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4264 SDValue RHS, SelectionDAG &DAG,
4265 DebugLoc dl) {
4266 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4267 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4268 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4269
4270 enum {
4271 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4272 OP_VREV,
4273 OP_VDUP0,
4274 OP_VDUP1,
4275 OP_VDUP2,
4276 OP_VDUP3,
4277 OP_VEXT1,
4278 OP_VEXT2,
4279 OP_VEXT3,
4280 OP_VUZPL, // VUZP, left result
4281 OP_VUZPR, // VUZP, right result
4282 OP_VZIPL, // VZIP, left result
4283 OP_VZIPR, // VZIP, right result
4284 OP_VTRNL, // VTRN, left result
4285 OP_VTRNR // VTRN, right result
4286 };
4287
4288 if (OpNum == OP_COPY) {
4289 if (LHSID == (1*9+2)*9+3) return LHS;
4290 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4291 return RHS;
4292 }
4293
4294 SDValue OpLHS, OpRHS;
4295 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4296 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4297 EVT VT = OpLHS.getValueType();
4298
4299 switch (OpNum) {
4300 default: llvm_unreachable("Unknown shuffle opcode!");
4301 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004302 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004303 if (VT.getVectorElementType() == MVT::i32 ||
4304 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004305 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4306 // vrev <4 x i16> -> VREV32
4307 if (VT.getVectorElementType() == MVT::i16)
4308 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4309 // vrev <4 x i8> -> VREV16
4310 assert(VT.getVectorElementType() == MVT::i8);
4311 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004312 case OP_VDUP0:
4313 case OP_VDUP1:
4314 case OP_VDUP2:
4315 case OP_VDUP3:
4316 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004317 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004318 case OP_VEXT1:
4319 case OP_VEXT2:
4320 case OP_VEXT3:
4321 return DAG.getNode(ARMISD::VEXT, dl, VT,
4322 OpLHS, OpRHS,
4323 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4324 case OP_VUZPL:
4325 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004326 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004327 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4328 case OP_VZIPL:
4329 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004330 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004331 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4332 case OP_VTRNL:
4333 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004334 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4335 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004336 }
4337}
4338
Bill Wendling69a05a72011-03-14 23:02:38 +00004339static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
4340 SmallVectorImpl<int> &ShuffleMask,
4341 SelectionDAG &DAG) {
4342 // Check to see if we can use the VTBL instruction.
4343 SDValue V1 = Op.getOperand(0);
4344 SDValue V2 = Op.getOperand(1);
4345 DebugLoc DL = Op.getDebugLoc();
4346
4347 SmallVector<SDValue, 8> VTBLMask;
4348 for (SmallVectorImpl<int>::iterator
4349 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4350 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4351
4352 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4353 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4354 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4355 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004356
Owen Anderson76706012011-04-05 21:48:57 +00004357 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004358 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4359 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004360}
4361
Bob Wilson5bafff32009-06-22 23:27:02 +00004362static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004363 SDValue V1 = Op.getOperand(0);
4364 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004365 DebugLoc dl = Op.getDebugLoc();
4366 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004367 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004368 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00004369
Bob Wilson28865062009-08-13 02:13:04 +00004370 // Convert shuffles that are directly supported on NEON to target-specific
4371 // DAG nodes, instead of keeping them as shuffles and matching them again
4372 // during code selection. This is more efficient and avoids the possibility
4373 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004374 // FIXME: floating-point vectors should be canonicalized to integer vectors
4375 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004376 SVN->getMask(ShuffleMask);
4377
Bob Wilson53dd2452010-06-07 23:53:38 +00004378 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4379 if (EltSize <= 32) {
4380 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4381 int Lane = SVN->getSplatIndex();
4382 // If this is undef splat, generate it via "just" vdup, if possible.
4383 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004384
Dan Gohman65fd6562011-11-03 21:49:52 +00004385 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson53dd2452010-06-07 23:53:38 +00004386 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4387 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4388 }
Dan Gohman65fd6562011-11-03 21:49:52 +00004389 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
4390 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
4391 // reaches it).
4392 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
4393 !isa<ConstantSDNode>(V1.getOperand(0))) {
4394 bool IsScalarToVector = true;
4395 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
4396 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
4397 IsScalarToVector = false;
4398 break;
4399 }
4400 if (IsScalarToVector)
4401 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4402 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004403 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4404 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004405 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004406
4407 bool ReverseVEXT;
4408 unsigned Imm;
4409 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4410 if (ReverseVEXT)
4411 std::swap(V1, V2);
4412 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4413 DAG.getConstant(Imm, MVT::i32));
4414 }
4415
4416 if (isVREVMask(ShuffleMask, VT, 64))
4417 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4418 if (isVREVMask(ShuffleMask, VT, 32))
4419 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4420 if (isVREVMask(ShuffleMask, VT, 16))
4421 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4422
4423 // Check for Neon shuffles that modify both input vectors in place.
4424 // If both results are used, i.e., if there are two shuffles with the same
4425 // source operands and with masks corresponding to both results of one of
4426 // these operations, DAG memoization will ensure that a single node is
4427 // used for both shuffles.
4428 unsigned WhichResult;
4429 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4430 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4431 V1, V2).getValue(WhichResult);
4432 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4433 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4434 V1, V2).getValue(WhichResult);
4435 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4436 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4437 V1, V2).getValue(WhichResult);
4438
4439 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4440 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4441 V1, V1).getValue(WhichResult);
4442 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4443 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4444 V1, V1).getValue(WhichResult);
4445 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4446 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4447 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004448 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004449
Bob Wilsonc692cb72009-08-21 20:54:19 +00004450 // If the shuffle is not directly supported and it has 4 elements, use
4451 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004452 unsigned NumElts = VT.getVectorNumElements();
4453 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004454 unsigned PFIndexes[4];
4455 for (unsigned i = 0; i != 4; ++i) {
4456 if (ShuffleMask[i] < 0)
4457 PFIndexes[i] = 8;
4458 else
4459 PFIndexes[i] = ShuffleMask[i];
4460 }
4461
4462 // Compute the index in the perfect shuffle table.
4463 unsigned PFTableIndex =
4464 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004465 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4466 unsigned Cost = (PFEntry >> 30);
4467
4468 if (Cost <= 4)
4469 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4470 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004471
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004472 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004473 if (EltSize >= 32) {
4474 // Do the expansion with floating-point types, since that is what the VFP
4475 // registers are defined to use, and since i64 is not legal.
4476 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4477 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004478 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4479 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004480 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004481 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004482 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004483 Ops.push_back(DAG.getUNDEF(EltVT));
4484 else
4485 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4486 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4487 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4488 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004489 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004490 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004491 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004492 }
4493
Bill Wendling69a05a72011-03-14 23:02:38 +00004494 if (VT == MVT::v8i8) {
4495 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4496 if (NewOp.getNode())
4497 return NewOp;
4498 }
4499
Bob Wilson22cac0d2009-08-14 05:16:33 +00004500 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004501}
4502
Eli Friedman5c89cb82011-10-24 23:08:52 +00004503static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
4504 // INSERT_VECTOR_ELT is legal only for immediate indexes.
4505 SDValue Lane = Op.getOperand(2);
4506 if (!isa<ConstantSDNode>(Lane))
4507 return SDValue();
4508
4509 return Op;
4510}
4511
Bob Wilson5bafff32009-06-22 23:27:02 +00004512static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004513 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004514 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004515 if (!isa<ConstantSDNode>(Lane))
4516 return SDValue();
4517
4518 SDValue Vec = Op.getOperand(0);
4519 if (Op.getValueType() == MVT::i32 &&
4520 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4521 DebugLoc dl = Op.getDebugLoc();
4522 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4523 }
4524
4525 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004526}
4527
Bob Wilsona6d65862009-08-03 20:36:38 +00004528static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4529 // The only time a CONCAT_VECTORS operation can have legal types is when
4530 // two 64-bit vectors are concatenated to a 128-bit vector.
4531 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4532 "unexpected CONCAT_VECTORS");
4533 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004534 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004535 SDValue Op0 = Op.getOperand(0);
4536 SDValue Op1 = Op.getOperand(1);
4537 if (Op0.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, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004540 DAG.getIntPtrConstant(0));
4541 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004542 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004543 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004544 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004545 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004546}
4547
Bob Wilson626613d2010-11-23 19:38:38 +00004548/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4549/// element has been zero/sign-extended, depending on the isSigned parameter,
4550/// from an integer type half its size.
4551static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4552 bool isSigned) {
4553 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4554 EVT VT = N->getValueType(0);
4555 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4556 SDNode *BVN = N->getOperand(0).getNode();
4557 if (BVN->getValueType(0) != MVT::v4i32 ||
4558 BVN->getOpcode() != ISD::BUILD_VECTOR)
4559 return false;
4560 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4561 unsigned HiElt = 1 - LoElt;
4562 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4563 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4564 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4565 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4566 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4567 return false;
4568 if (isSigned) {
4569 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4570 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4571 return true;
4572 } else {
4573 if (Hi0->isNullValue() && Hi1->isNullValue())
4574 return true;
4575 }
4576 return false;
4577 }
4578
4579 if (N->getOpcode() != ISD::BUILD_VECTOR)
4580 return false;
4581
4582 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4583 SDNode *Elt = N->getOperand(i).getNode();
4584 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4585 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4586 unsigned HalfSize = EltSize / 2;
4587 if (isSigned) {
Bob Wilson9d45de22011-10-18 18:46:49 +00004588 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004589 return false;
4590 } else {
Bob Wilson9d45de22011-10-18 18:46:49 +00004591 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004592 return false;
4593 }
4594 continue;
4595 }
4596 return false;
4597 }
4598
4599 return true;
4600}
4601
4602/// isSignExtended - Check if a node is a vector value that is sign-extended
4603/// or a constant BUILD_VECTOR with sign-extended elements.
4604static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4605 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4606 return true;
4607 if (isExtendedBUILD_VECTOR(N, DAG, true))
4608 return true;
4609 return false;
4610}
4611
4612/// isZeroExtended - Check if a node is a vector value that is zero-extended
4613/// or a constant BUILD_VECTOR with zero-extended elements.
4614static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4615 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4616 return true;
4617 if (isExtendedBUILD_VECTOR(N, DAG, false))
4618 return true;
4619 return false;
4620}
4621
4622/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4623/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004624static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4625 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4626 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004627 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4628 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4629 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004630 LD->isNonTemporal(), LD->isInvariant(),
4631 LD->getAlignment());
Bob Wilson626613d2010-11-23 19:38:38 +00004632 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4633 // have been legalized as a BITCAST from v4i32.
4634 if (N->getOpcode() == ISD::BITCAST) {
4635 SDNode *BVN = N->getOperand(0).getNode();
4636 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4637 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4638 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4639 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4640 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4641 }
4642 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4643 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4644 EVT VT = N->getValueType(0);
4645 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4646 unsigned NumElts = VT.getVectorNumElements();
4647 MVT TruncVT = MVT::getIntegerVT(EltSize);
4648 SmallVector<SDValue, 8> Ops;
4649 for (unsigned i = 0; i != NumElts; ++i) {
4650 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4651 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004652 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004653 }
4654 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4655 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004656}
4657
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004658static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4659 unsigned Opcode = N->getOpcode();
4660 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4661 SDNode *N0 = N->getOperand(0).getNode();
4662 SDNode *N1 = N->getOperand(1).getNode();
4663 return N0->hasOneUse() && N1->hasOneUse() &&
4664 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4665 }
4666 return false;
4667}
4668
4669static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4670 unsigned Opcode = N->getOpcode();
4671 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4672 SDNode *N0 = N->getOperand(0).getNode();
4673 SDNode *N1 = N->getOperand(1).getNode();
4674 return N0->hasOneUse() && N1->hasOneUse() &&
4675 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4676 }
4677 return false;
4678}
4679
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004680static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4681 // Multiplications are only custom-lowered for 128-bit vectors so that
4682 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4683 EVT VT = Op.getValueType();
4684 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4685 SDNode *N0 = Op.getOperand(0).getNode();
4686 SDNode *N1 = Op.getOperand(1).getNode();
4687 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004688 bool isMLA = false;
4689 bool isN0SExt = isSignExtended(N0, DAG);
4690 bool isN1SExt = isSignExtended(N1, DAG);
4691 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004692 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004693 else {
4694 bool isN0ZExt = isZeroExtended(N0, DAG);
4695 bool isN1ZExt = isZeroExtended(N1, DAG);
4696 if (isN0ZExt && isN1ZExt)
4697 NewOpc = ARMISD::VMULLu;
4698 else if (isN1SExt || isN1ZExt) {
4699 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4700 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4701 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4702 NewOpc = ARMISD::VMULLs;
4703 isMLA = true;
4704 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4705 NewOpc = ARMISD::VMULLu;
4706 isMLA = true;
4707 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4708 std::swap(N0, N1);
4709 NewOpc = ARMISD::VMULLu;
4710 isMLA = true;
4711 }
4712 }
4713
4714 if (!NewOpc) {
4715 if (VT == MVT::v2i64)
4716 // Fall through to expand this. It is not legal.
4717 return SDValue();
4718 else
4719 // Other vector multiplications are legal.
4720 return Op;
4721 }
4722 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004723
4724 // Legalize to a VMULL instruction.
4725 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004726 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004727 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004728 if (!isMLA) {
4729 Op0 = SkipExtension(N0, DAG);
4730 assert(Op0.getValueType().is64BitVector() &&
4731 Op1.getValueType().is64BitVector() &&
4732 "unexpected types for extended operands to VMULL");
4733 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4734 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004735
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004736 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4737 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4738 // vmull q0, d4, d6
4739 // vmlal q0, d5, d6
4740 // is faster than
4741 // vaddl q0, d4, d5
4742 // vmovl q1, d6
4743 // vmul q0, q0, q1
4744 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4745 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4746 EVT Op1VT = Op1.getValueType();
4747 return DAG.getNode(N0->getOpcode(), DL, VT,
4748 DAG.getNode(NewOpc, DL, VT,
4749 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4750 DAG.getNode(NewOpc, DL, VT,
4751 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004752}
4753
Owen Anderson76706012011-04-05 21:48:57 +00004754static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004755LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4756 // Convert to float
4757 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4758 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4759 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4760 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4761 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4762 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4763 // Get reciprocal estimate.
4764 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004765 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004766 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4767 // Because char has a smaller range than uchar, we can actually get away
4768 // without any newton steps. This requires that we use a weird bias
4769 // of 0xb000, however (again, this has been exhaustively tested).
4770 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4771 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4772 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4773 Y = DAG.getConstant(0xb000, MVT::i32);
4774 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4775 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4776 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4777 // Convert back to short.
4778 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4779 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4780 return X;
4781}
4782
Owen Anderson76706012011-04-05 21:48:57 +00004783static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004784LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4785 SDValue N2;
4786 // Convert to float.
4787 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4788 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4789 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4790 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4791 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4792 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004793
Nate Begeman7973f352011-02-11 20:53:29 +00004794 // Use reciprocal estimate and one refinement step.
4795 // float4 recip = vrecpeq_f32(yf);
4796 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004797 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004798 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004799 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004800 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4801 N1, N2);
4802 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4803 // Because short has a smaller range than ushort, we can actually get away
4804 // with only a single newton step. This requires that we use a weird bias
4805 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004806 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004807 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4808 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004809 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004810 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4811 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4812 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4813 // Convert back to integer and return.
4814 // return vmovn_s32(vcvt_s32_f32(result));
4815 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4816 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4817 return N0;
4818}
4819
4820static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4821 EVT VT = Op.getValueType();
4822 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4823 "unexpected type for custom-lowering ISD::SDIV");
4824
4825 DebugLoc dl = Op.getDebugLoc();
4826 SDValue N0 = Op.getOperand(0);
4827 SDValue N1 = Op.getOperand(1);
4828 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004829
Nate Begeman7973f352011-02-11 20:53:29 +00004830 if (VT == MVT::v8i8) {
4831 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4832 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004833
Nate Begeman7973f352011-02-11 20:53:29 +00004834 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4835 DAG.getIntPtrConstant(4));
4836 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004837 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004838 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4839 DAG.getIntPtrConstant(0));
4840 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4841 DAG.getIntPtrConstant(0));
4842
4843 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4844 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4845
4846 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4847 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004848
Nate Begeman7973f352011-02-11 20:53:29 +00004849 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
4850 return N0;
4851 }
4852 return LowerSDIV_v4i16(N0, N1, dl, DAG);
4853}
4854
4855static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
4856 EVT VT = Op.getValueType();
4857 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4858 "unexpected type for custom-lowering ISD::UDIV");
4859
4860 DebugLoc dl = Op.getDebugLoc();
4861 SDValue N0 = Op.getOperand(0);
4862 SDValue N1 = Op.getOperand(1);
4863 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004864
Nate Begeman7973f352011-02-11 20:53:29 +00004865 if (VT == MVT::v8i8) {
4866 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
4867 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004868
Nate Begeman7973f352011-02-11 20:53:29 +00004869 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4870 DAG.getIntPtrConstant(4));
4871 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004872 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004873 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4874 DAG.getIntPtrConstant(0));
4875 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4876 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00004877
Nate Begeman7973f352011-02-11 20:53:29 +00004878 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
4879 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00004880
Nate Begeman7973f352011-02-11 20:53:29 +00004881 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4882 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004883
4884 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00004885 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
4886 N0);
4887 return N0;
4888 }
Owen Anderson76706012011-04-05 21:48:57 +00004889
Nate Begeman7973f352011-02-11 20:53:29 +00004890 // v4i16 sdiv ... Convert to float.
4891 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
4892 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
4893 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
4894 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
4895 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004896 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00004897
4898 // Use reciprocal estimate and two refinement steps.
4899 // float4 recip = vrecpeq_f32(yf);
4900 // recip *= vrecpsq_f32(yf, recip);
4901 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004902 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004903 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
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);
Owen Anderson76706012011-04-05 21:48:57 +00004908 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004909 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004910 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004911 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4912 // Simply multiplying by the reciprocal estimate can leave us a few ulps
4913 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
4914 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004915 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00004916 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4917 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4918 N1 = DAG.getConstant(2, MVT::i32);
4919 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4920 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4921 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4922 // Convert back to integer and return.
4923 // return vmovn_u32(vcvt_s32_f32(result));
4924 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4925 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4926 return N0;
4927}
4928
Evan Cheng342e3162011-08-30 01:34:54 +00004929static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
4930 EVT VT = Op.getNode()->getValueType(0);
4931 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
4932
4933 unsigned Opc;
4934 bool ExtraOp = false;
4935 switch (Op.getOpcode()) {
4936 default: assert(0 && "Invalid code");
4937 case ISD::ADDC: Opc = ARMISD::ADDC; break;
4938 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
4939 case ISD::SUBC: Opc = ARMISD::SUBC; break;
4940 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
4941 }
4942
4943 if (!ExtraOp)
4944 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
4945 Op.getOperand(1));
4946 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
4947 Op.getOperand(1), Op.getOperand(2));
4948}
4949
Eli Friedman74bf18c2011-09-15 22:26:18 +00004950static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedman7cc15662011-09-15 22:18:49 +00004951 // Monotonic load/store is legal for all targets
4952 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
4953 return Op;
4954
4955 // Aquire/Release load/store is not legal for targets without a
4956 // dmb or equivalent available.
4957 return SDValue();
4958}
4959
4960
Eli Friedman2bdffe42011-08-31 00:31:29 +00004961static void
Eli Friedman4d3f3292011-08-31 17:52:22 +00004962ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
4963 SelectionDAG &DAG, unsigned NewOp) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00004964 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +00004965 assert (Node->getValueType(0) == MVT::i64 &&
4966 "Only know how to expand i64 atomics");
Eli Friedman2bdffe42011-08-31 00:31:29 +00004967
Eli Friedman4d3f3292011-08-31 17:52:22 +00004968 SmallVector<SDValue, 6> Ops;
4969 Ops.push_back(Node->getOperand(0)); // Chain
4970 Ops.push_back(Node->getOperand(1)); // Ptr
4971 // Low part of Val1
4972 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4973 Node->getOperand(2), DAG.getIntPtrConstant(0)));
4974 // High part of Val1
4975 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4976 Node->getOperand(2), DAG.getIntPtrConstant(1)));
Andrew Trick3af7a672011-09-20 03:06:13 +00004977 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) {
Eli Friedman4d3f3292011-08-31 17:52:22 +00004978 // High part of Val1
4979 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4980 Node->getOperand(3), DAG.getIntPtrConstant(0)));
4981 // High part of Val2
4982 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4983 Node->getOperand(3), DAG.getIntPtrConstant(1)));
4984 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00004985 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
4986 SDValue Result =
Eli Friedman4d3f3292011-08-31 17:52:22 +00004987 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64,
Eli Friedman2bdffe42011-08-31 00:31:29 +00004988 cast<MemSDNode>(Node)->getMemOperand());
Eli Friedman4d3f3292011-08-31 17:52:22 +00004989 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) };
Eli Friedman2bdffe42011-08-31 00:31:29 +00004990 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
4991 Results.push_back(Result.getValue(2));
4992}
4993
Dan Gohmand858e902010-04-17 15:26:15 +00004994SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004995 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004996 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00004997 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00004998 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004999 case ISD::GlobalAddress:
5000 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
5001 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00005002 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00005003 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00005004 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
5005 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005006 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00005007 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00005008 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00005009 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00005010 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00005011 case ISD::SINT_TO_FP:
5012 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
5013 case ISD::FP_TO_SINT:
5014 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005015 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00005016 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00005017 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005018 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00005019 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00005020 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00005021 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
5022 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00005023 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005024 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00005025 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00005026 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00005027 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00005028 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00005029 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00005030 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Duncan Sands28b77e92011-09-06 19:07:46 +00005031 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00005032 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005033 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedman5c89cb82011-10-24 23:08:52 +00005034 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005035 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00005036 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00005037 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005038 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00005039 case ISD::SDIV: return LowerSDIV(Op, DAG);
5040 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00005041 case ISD::ADDC:
5042 case ISD::ADDE:
5043 case ISD::SUBC:
5044 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Eli Friedman7cc15662011-09-15 22:18:49 +00005045 case ISD::ATOMIC_LOAD:
Eli Friedman74bf18c2011-09-15 22:26:18 +00005046 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005047 }
Dan Gohman475871a2008-07-27 21:46:04 +00005048 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00005049}
5050
Duncan Sands1607f052008-12-01 11:39:25 +00005051/// ReplaceNodeResults - Replace the results of node with an illegal result
5052/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00005053void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
5054 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005055 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00005056 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00005057 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00005058 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005059 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00005060 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005061 case ISD::BITCAST:
5062 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005063 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005064 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00005065 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00005066 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005067 break;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005068 case ISD::ATOMIC_LOAD_ADD:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005069 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005070 return;
5071 case ISD::ATOMIC_LOAD_AND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005072 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005073 return;
5074 case ISD::ATOMIC_LOAD_NAND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005075 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005076 return;
5077 case ISD::ATOMIC_LOAD_OR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005078 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005079 return;
5080 case ISD::ATOMIC_LOAD_SUB:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005081 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005082 return;
5083 case ISD::ATOMIC_LOAD_XOR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005084 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005085 return;
5086 case ISD::ATOMIC_SWAP:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005087 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005088 return;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005089 case ISD::ATOMIC_CMP_SWAP:
5090 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG);
5091 return;
Duncan Sands1607f052008-12-01 11:39:25 +00005092 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00005093 if (Res.getNode())
5094 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00005095}
Chris Lattner27a6c732007-11-24 07:07:01 +00005096
Evan Chenga8e29892007-01-19 07:51:42 +00005097//===----------------------------------------------------------------------===//
5098// ARM Scheduler Hooks
5099//===----------------------------------------------------------------------===//
5100
5101MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005102ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
5103 MachineBasicBlock *BB,
5104 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005105 unsigned dest = MI->getOperand(0).getReg();
5106 unsigned ptr = MI->getOperand(1).getReg();
5107 unsigned oldval = MI->getOperand(2).getReg();
5108 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005109 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5110 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005111 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005112
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005113 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5114 unsigned scratch =
Cameron Zwarich141ec632011-05-18 02:29:50 +00005115 MRI.createVirtualRegister(isThumb2 ? ARM::rGPRRegisterClass
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005116 : ARM::GPRRegisterClass);
5117
5118 if (isThumb2) {
Cameron Zwarich141ec632011-05-18 02:29:50 +00005119 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5120 MRI.constrainRegClass(oldval, ARM::rGPRRegisterClass);
5121 MRI.constrainRegClass(newval, ARM::rGPRRegisterClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005122 }
5123
Jim Grosbach5278eb82009-12-11 01:42:04 +00005124 unsigned ldrOpc, strOpc;
5125 switch (Size) {
5126 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005127 case 1:
5128 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00005129 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005130 break;
5131 case 2:
5132 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5133 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5134 break;
5135 case 4:
5136 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5137 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5138 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00005139 }
5140
5141 MachineFunction *MF = BB->getParent();
5142 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5143 MachineFunction::iterator It = BB;
5144 ++It; // insert the new blocks after the current block
5145
5146 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5147 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5148 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5149 MF->insert(It, loop1MBB);
5150 MF->insert(It, loop2MBB);
5151 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005152
5153 // Transfer the remainder of BB and its successor edges to exitMBB.
5154 exitMBB->splice(exitMBB->begin(), BB,
5155 llvm::next(MachineBasicBlock::iterator(MI)),
5156 BB->end());
5157 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005158
5159 // thisMBB:
5160 // ...
5161 // fallthrough --> loop1MBB
5162 BB->addSuccessor(loop1MBB);
5163
5164 // loop1MBB:
5165 // ldrex dest, [ptr]
5166 // cmp dest, oldval
5167 // bne exitMBB
5168 BB = loop1MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005169 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5170 if (ldrOpc == ARM::t2LDREX)
5171 MIB.addImm(0);
5172 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005173 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005174 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005175 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5176 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005177 BB->addSuccessor(loop2MBB);
5178 BB->addSuccessor(exitMBB);
5179
5180 // loop2MBB:
5181 // strex scratch, newval, [ptr]
5182 // cmp scratch, #0
5183 // bne loop1MBB
5184 BB = loop2MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005185 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr);
5186 if (strOpc == ARM::t2STREX)
5187 MIB.addImm(0);
5188 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005189 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005190 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005191 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5192 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005193 BB->addSuccessor(loop1MBB);
5194 BB->addSuccessor(exitMBB);
5195
5196 // exitMBB:
5197 // ...
5198 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005199
Dan Gohman14152b42010-07-06 20:24:04 +00005200 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005201
Jim Grosbach5278eb82009-12-11 01:42:04 +00005202 return BB;
5203}
5204
5205MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005206ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5207 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005208 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5209 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5210
5211 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005212 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005213 MachineFunction::iterator It = BB;
5214 ++It;
5215
5216 unsigned dest = MI->getOperand(0).getReg();
5217 unsigned ptr = MI->getOperand(1).getReg();
5218 unsigned incr = MI->getOperand(2).getReg();
5219 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005220 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005221
5222 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5223 if (isThumb2) {
5224 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5225 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5226 }
5227
Jim Grosbachc3c23542009-12-14 04:22:04 +00005228 unsigned ldrOpc, strOpc;
5229 switch (Size) {
5230 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005231 case 1:
5232 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005233 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005234 break;
5235 case 2:
5236 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5237 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5238 break;
5239 case 4:
5240 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5241 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5242 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005243 }
5244
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005245 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5246 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5247 MF->insert(It, loopMBB);
5248 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005249
5250 // Transfer the remainder of BB and its successor edges to exitMBB.
5251 exitMBB->splice(exitMBB->begin(), BB,
5252 llvm::next(MachineBasicBlock::iterator(MI)),
5253 BB->end());
5254 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005255
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005256 TargetRegisterClass *TRC =
5257 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5258 unsigned scratch = MRI.createVirtualRegister(TRC);
5259 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005260
5261 // thisMBB:
5262 // ...
5263 // fallthrough --> loopMBB
5264 BB->addSuccessor(loopMBB);
5265
5266 // loopMBB:
5267 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005268 // <binop> scratch2, dest, incr
5269 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005270 // cmp scratch, #0
5271 // bne- loopMBB
5272 // fallthrough --> exitMBB
5273 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005274 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5275 if (ldrOpc == ARM::t2LDREX)
5276 MIB.addImm(0);
5277 AddDefaultPred(MIB);
Jim Grosbachc67b5562009-12-15 00:12:35 +00005278 if (BinOpcode) {
5279 // operand order needs to go the other way for NAND
5280 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5281 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5282 addReg(incr).addReg(dest)).addReg(0);
5283 else
5284 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5285 addReg(dest).addReg(incr)).addReg(0);
5286 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005287
Jim Grosbachb6aed502011-09-09 18:37:27 +00005288 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5289 if (strOpc == ARM::t2STREX)
5290 MIB.addImm(0);
5291 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005292 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005293 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005294 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5295 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005296
5297 BB->addSuccessor(loopMBB);
5298 BB->addSuccessor(exitMBB);
5299
5300 // exitMBB:
5301 // ...
5302 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005303
Dan Gohman14152b42010-07-06 20:24:04 +00005304 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005305
Jim Grosbachc3c23542009-12-14 04:22:04 +00005306 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005307}
5308
Jim Grosbachf7da8822011-04-26 19:44:18 +00005309MachineBasicBlock *
5310ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5311 MachineBasicBlock *BB,
5312 unsigned Size,
5313 bool signExtend,
5314 ARMCC::CondCodes Cond) const {
5315 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5316
5317 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5318 MachineFunction *MF = BB->getParent();
5319 MachineFunction::iterator It = BB;
5320 ++It;
5321
5322 unsigned dest = MI->getOperand(0).getReg();
5323 unsigned ptr = MI->getOperand(1).getReg();
5324 unsigned incr = MI->getOperand(2).getReg();
5325 unsigned oldval = dest;
5326 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005327 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005328
5329 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5330 if (isThumb2) {
5331 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5332 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5333 }
5334
Jim Grosbachf7da8822011-04-26 19:44:18 +00005335 unsigned ldrOpc, strOpc, extendOpc;
5336 switch (Size) {
5337 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5338 case 1:
5339 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5340 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005341 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005342 break;
5343 case 2:
5344 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5345 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005346 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005347 break;
5348 case 4:
5349 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5350 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5351 extendOpc = 0;
5352 break;
5353 }
5354
5355 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5356 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5357 MF->insert(It, loopMBB);
5358 MF->insert(It, exitMBB);
5359
5360 // Transfer the remainder of BB and its successor edges to exitMBB.
5361 exitMBB->splice(exitMBB->begin(), BB,
5362 llvm::next(MachineBasicBlock::iterator(MI)),
5363 BB->end());
5364 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5365
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005366 TargetRegisterClass *TRC =
5367 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5368 unsigned scratch = MRI.createVirtualRegister(TRC);
5369 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005370
5371 // thisMBB:
5372 // ...
5373 // fallthrough --> loopMBB
5374 BB->addSuccessor(loopMBB);
5375
5376 // loopMBB:
5377 // ldrex dest, ptr
5378 // (sign extend dest, if required)
5379 // cmp dest, incr
5380 // cmov.cond scratch2, dest, incr
5381 // strex scratch, scratch2, ptr
5382 // cmp scratch, #0
5383 // bne- loopMBB
5384 // fallthrough --> exitMBB
5385 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005386 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5387 if (ldrOpc == ARM::t2LDREX)
5388 MIB.addImm(0);
5389 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005390
5391 // Sign extend the value, if necessary.
5392 if (signExtend && extendOpc) {
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005393 oldval = MRI.createVirtualRegister(ARM::GPRRegisterClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005394 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5395 .addReg(dest)
5396 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005397 }
5398
5399 // Build compare and cmov instructions.
5400 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5401 .addReg(oldval).addReg(incr));
5402 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5403 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5404
Jim Grosbachb6aed502011-09-09 18:37:27 +00005405 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5406 if (strOpc == ARM::t2STREX)
5407 MIB.addImm(0);
5408 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005409 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5410 .addReg(scratch).addImm(0));
5411 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5412 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5413
5414 BB->addSuccessor(loopMBB);
5415 BB->addSuccessor(exitMBB);
5416
5417 // exitMBB:
5418 // ...
5419 BB = exitMBB;
5420
5421 MI->eraseFromParent(); // The instruction is gone now.
5422
5423 return BB;
5424}
5425
Eli Friedman2bdffe42011-08-31 00:31:29 +00005426MachineBasicBlock *
5427ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
5428 unsigned Op1, unsigned Op2,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005429 bool NeedsCarry, bool IsCmpxchg) const {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005430 // This also handles ATOMIC_SWAP, indicated by Op1==0.
5431 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5432
5433 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5434 MachineFunction *MF = BB->getParent();
5435 MachineFunction::iterator It = BB;
5436 ++It;
5437
5438 unsigned destlo = MI->getOperand(0).getReg();
5439 unsigned desthi = MI->getOperand(1).getReg();
5440 unsigned ptr = MI->getOperand(2).getReg();
5441 unsigned vallo = MI->getOperand(3).getReg();
5442 unsigned valhi = MI->getOperand(4).getReg();
5443 DebugLoc dl = MI->getDebugLoc();
5444 bool isThumb2 = Subtarget->isThumb2();
5445
5446 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5447 if (isThumb2) {
5448 MRI.constrainRegClass(destlo, ARM::rGPRRegisterClass);
5449 MRI.constrainRegClass(desthi, ARM::rGPRRegisterClass);
5450 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5451 }
5452
5453 unsigned ldrOpc = isThumb2 ? ARM::t2LDREXD : ARM::LDREXD;
5454 unsigned strOpc = isThumb2 ? ARM::t2STREXD : ARM::STREXD;
5455
5456 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman7df496d2011-09-01 22:27:41 +00005457 MachineBasicBlock *contBB = 0, *cont2BB = 0;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005458 if (IsCmpxchg) {
5459 contBB = MF->CreateMachineBasicBlock(LLVM_BB);
5460 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB);
5461 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005462 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5463 MF->insert(It, loopMBB);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005464 if (IsCmpxchg) {
5465 MF->insert(It, contBB);
5466 MF->insert(It, cont2BB);
5467 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005468 MF->insert(It, exitMBB);
5469
5470 // Transfer the remainder of BB and its successor edges to exitMBB.
5471 exitMBB->splice(exitMBB->begin(), BB,
5472 llvm::next(MachineBasicBlock::iterator(MI)),
5473 BB->end());
5474 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5475
5476 TargetRegisterClass *TRC =
5477 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5478 unsigned storesuccess = MRI.createVirtualRegister(TRC);
5479
5480 // thisMBB:
5481 // ...
5482 // fallthrough --> loopMBB
5483 BB->addSuccessor(loopMBB);
5484
5485 // loopMBB:
5486 // ldrexd r2, r3, ptr
5487 // <binopa> r0, r2, incr
5488 // <binopb> r1, r3, incr
5489 // strexd storesuccess, r0, r1, ptr
5490 // cmp storesuccess, #0
5491 // bne- loopMBB
5492 // fallthrough --> exitMBB
5493 //
5494 // Note that the registers are explicitly specified because there is not any
5495 // way to force the register allocator to allocate a register pair.
5496 //
Andrew Trick3af7a672011-09-20 03:06:13 +00005497 // FIXME: The hardcoded registers are not necessary for Thumb2, but we
Eli Friedman2bdffe42011-08-31 00:31:29 +00005498 // need to properly enforce the restriction that the two output registers
5499 // for ldrexd must be different.
5500 BB = loopMBB;
5501 // Load
5502 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc))
5503 .addReg(ARM::R2, RegState::Define)
5504 .addReg(ARM::R3, RegState::Define).addReg(ptr));
5505 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
5506 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo).addReg(ARM::R2);
5507 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi).addReg(ARM::R3);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005508
5509 if (IsCmpxchg) {
5510 // Add early exit
5511 for (unsigned i = 0; i < 2; i++) {
5512 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr :
5513 ARM::CMPrr))
5514 .addReg(i == 0 ? destlo : desthi)
5515 .addReg(i == 0 ? vallo : valhi));
5516 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5517 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5518 BB->addSuccessor(exitMBB);
5519 BB->addSuccessor(i == 0 ? contBB : cont2BB);
5520 BB = (i == 0 ? contBB : cont2BB);
5521 }
5522
5523 // Copy to physregs for strexd
5524 unsigned setlo = MI->getOperand(5).getReg();
5525 unsigned sethi = MI->getOperand(6).getReg();
5526 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(setlo);
5527 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(sethi);
5528 } else if (Op1) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005529 // Perform binary operation
5530 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), ARM::R0)
5531 .addReg(destlo).addReg(vallo))
5532 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
5533 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), ARM::R1)
5534 .addReg(desthi).addReg(valhi)).addReg(0);
5535 } else {
5536 // Copy to physregs for strexd
5537 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(vallo);
5538 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(valhi);
5539 }
5540
5541 // Store
5542 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), storesuccess)
5543 .addReg(ARM::R0).addReg(ARM::R1).addReg(ptr));
5544 // Cmp+jump
5545 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5546 .addReg(storesuccess).addImm(0));
5547 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5548 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5549
5550 BB->addSuccessor(loopMBB);
5551 BB->addSuccessor(exitMBB);
5552
5553 // exitMBB:
5554 // ...
5555 BB = exitMBB;
5556
5557 MI->eraseFromParent(); // The instruction is gone now.
5558
5559 return BB;
5560}
5561
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005562/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
5563/// registers the function context.
5564void ARMTargetLowering::
5565SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
5566 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005567 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5568 DebugLoc dl = MI->getDebugLoc();
5569 MachineFunction *MF = MBB->getParent();
5570 MachineRegisterInfo *MRI = &MF->getRegInfo();
5571 MachineConstantPool *MCP = MF->getConstantPool();
5572 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5573 const Function *F = MF->getFunction();
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005574
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005575 bool isThumb = Subtarget->isThumb();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005576 bool isThumb2 = Subtarget->isThumb2();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005577
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005578 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005579 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005580 ARMConstantPoolValue *CPV =
5581 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
5582 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
5583
5584 const TargetRegisterClass *TRC =
5585 isThumb ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5586
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005587 // Grab constant pool and fixed stack memory operands.
5588 MachineMemOperand *CPMMO =
5589 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
5590 MachineMemOperand::MOLoad, 4, 4);
5591
5592 MachineMemOperand *FIMMOSt =
5593 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
5594 MachineMemOperand::MOStore, 4, 4);
5595
5596 // Load the address of the dispatch MBB into the jump buffer.
5597 if (isThumb2) {
5598 // Incoming value: jbuf
5599 // ldr.n r5, LCPI1_1
5600 // orr r5, r5, #1
5601 // add r5, pc
5602 // str r5, [$jbuf, #+4] ; &jbuf[1]
5603 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5604 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
5605 .addConstantPoolIndex(CPI)
5606 .addMemOperand(CPMMO));
5607 // Set the low bit because of thumb mode.
5608 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5609 AddDefaultCC(
5610 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
5611 .addReg(NewVReg1, RegState::Kill)
5612 .addImm(0x01)));
5613 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5614 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
5615 .addReg(NewVReg2, RegState::Kill)
5616 .addImm(PCLabelId);
5617 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
5618 .addReg(NewVReg3, RegState::Kill)
5619 .addFrameIndex(FI)
5620 .addImm(36) // &jbuf[1] :: pc
5621 .addMemOperand(FIMMOSt));
5622 } else if (isThumb) {
5623 // Incoming value: jbuf
5624 // ldr.n r1, LCPI1_4
5625 // add r1, pc
5626 // mov r2, #1
5627 // orrs r1, r2
5628 // add r2, $jbuf, #+4 ; &jbuf[1]
5629 // str r1, [r2]
5630 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5631 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
5632 .addConstantPoolIndex(CPI)
5633 .addMemOperand(CPMMO));
5634 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5635 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
5636 .addReg(NewVReg1, RegState::Kill)
5637 .addImm(PCLabelId);
5638 // Set the low bit because of thumb mode.
5639 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5640 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
5641 .addReg(ARM::CPSR, RegState::Define)
5642 .addImm(1));
5643 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5644 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
5645 .addReg(ARM::CPSR, RegState::Define)
5646 .addReg(NewVReg2, RegState::Kill)
5647 .addReg(NewVReg3, RegState::Kill));
5648 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5649 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
5650 .addFrameIndex(FI)
5651 .addImm(36)); // &jbuf[1] :: pc
5652 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
5653 .addReg(NewVReg4, RegState::Kill)
5654 .addReg(NewVReg5, RegState::Kill)
5655 .addImm(0)
5656 .addMemOperand(FIMMOSt));
5657 } else {
5658 // Incoming value: jbuf
5659 // ldr r1, LCPI1_1
5660 // add r1, pc, r1
5661 // str r1, [$jbuf, #+4] ; &jbuf[1]
5662 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5663 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
5664 .addConstantPoolIndex(CPI)
5665 .addImm(0)
5666 .addMemOperand(CPMMO));
5667 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5668 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
5669 .addReg(NewVReg1, RegState::Kill)
5670 .addImm(PCLabelId));
5671 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
5672 .addReg(NewVReg2, RegState::Kill)
5673 .addFrameIndex(FI)
5674 .addImm(36) // &jbuf[1] :: pc
5675 .addMemOperand(FIMMOSt));
5676 }
5677}
5678
5679MachineBasicBlock *ARMTargetLowering::
5680EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
5681 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5682 DebugLoc dl = MI->getDebugLoc();
5683 MachineFunction *MF = MBB->getParent();
5684 MachineRegisterInfo *MRI = &MF->getRegInfo();
5685 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5686 MachineFrameInfo *MFI = MF->getFrameInfo();
5687 int FI = MFI->getFunctionContextIndex();
5688
5689 const TargetRegisterClass *TRC =
5690 Subtarget->isThumb() ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5691
Bill Wendling04f15b42011-10-06 21:29:56 +00005692 // Get a mapping of the call site numbers to all of the landing pads they're
5693 // associated with.
Bill Wendling2a850152011-10-05 00:02:33 +00005694 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
5695 unsigned MaxCSNum = 0;
5696 MachineModuleInfo &MMI = MF->getMMI();
5697 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E; ++BB) {
5698 if (!BB->isLandingPad()) continue;
5699
5700 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
5701 // pad.
5702 for (MachineBasicBlock::iterator
5703 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
5704 if (!II->isEHLabel()) continue;
5705
5706 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendling5cbef192011-10-05 23:28:57 +00005707 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling2a850152011-10-05 00:02:33 +00005708
Bill Wendling5cbef192011-10-05 23:28:57 +00005709 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
5710 for (SmallVectorImpl<unsigned>::iterator
5711 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
5712 CSI != CSE; ++CSI) {
5713 CallSiteNumToLPad[*CSI].push_back(BB);
5714 MaxCSNum = std::max(MaxCSNum, *CSI);
5715 }
Bill Wendling2a850152011-10-05 00:02:33 +00005716 break;
5717 }
5718 }
5719
5720 // Get an ordered list of the machine basic blocks for the jump table.
5721 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling2acf6382011-10-07 23:18:02 +00005722 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling2a850152011-10-05 00:02:33 +00005723 LPadList.reserve(CallSiteNumToLPad.size());
5724 for (unsigned I = 1; I <= MaxCSNum; ++I) {
5725 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
5726 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00005727 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling2a850152011-10-05 00:02:33 +00005728 LPadList.push_back(*II);
Bill Wendling2acf6382011-10-07 23:18:02 +00005729 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
5730 }
Bill Wendling2a850152011-10-05 00:02:33 +00005731 }
5732
Bill Wendling5cbef192011-10-05 23:28:57 +00005733 assert(!LPadList.empty() &&
5734 "No landing pad destinations for the dispatch jump table!");
5735
Bill Wendling04f15b42011-10-06 21:29:56 +00005736 // Create the jump table and associated information.
Bill Wendling2a850152011-10-05 00:02:33 +00005737 MachineJumpTableInfo *JTI =
5738 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
5739 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
5740 unsigned UId = AFI->createJumpTableUId();
5741
Bill Wendling04f15b42011-10-06 21:29:56 +00005742 // Create the MBBs for the dispatch code.
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005743
5744 // Shove the dispatch's address into the return slot in the function context.
5745 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
5746 DispatchBB->setIsLandingPad();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005747
Bill Wendlingbb734682011-10-05 00:39:32 +00005748 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Bill Wendling083a8eb2011-10-06 23:37:36 +00005749 BuildMI(TrapBB, dl, TII->get(Subtarget->isThumb() ? ARM::tTRAP : ARM::TRAP));
Bill Wendlingbb734682011-10-05 00:39:32 +00005750 DispatchBB->addSuccessor(TrapBB);
5751
5752 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
5753 DispatchBB->addSuccessor(DispContBB);
Bill Wendling2a850152011-10-05 00:02:33 +00005754
Bill Wendlinga48ed4f2011-10-17 21:32:56 +00005755 // Insert and MBBs.
Bill Wendling930193c2011-10-06 00:53:33 +00005756 MF->insert(MF->end(), DispatchBB);
5757 MF->insert(MF->end(), DispContBB);
5758 MF->insert(MF->end(), TrapBB);
Bill Wendling930193c2011-10-06 00:53:33 +00005759
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005760 // Insert code into the entry block that creates and registers the function
5761 // context.
5762 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
5763
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005764 MachineMemOperand *FIMMOLd =
Bill Wendling04f15b42011-10-06 21:29:56 +00005765 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendling083a8eb2011-10-06 23:37:36 +00005766 MachineMemOperand::MOLoad |
5767 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling930193c2011-10-06 00:53:33 +00005768
Bob Wilsoneaab6ef2011-11-16 07:11:57 +00005769 BuildMI(DispatchBB, dl, TII->get(ARM::eh_sjlj_dispatchsetup));
5770
Bill Wendling952cb502011-10-18 22:49:07 +00005771 unsigned NumLPads = LPadList.size();
Bill Wendling95ce2e92011-10-06 22:53:00 +00005772 if (Subtarget->isThumb2()) {
5773 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5774 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
5775 .addFrameIndex(FI)
5776 .addImm(4)
5777 .addMemOperand(FIMMOLd));
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005778
Bill Wendling952cb502011-10-18 22:49:07 +00005779 if (NumLPads < 256) {
5780 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
5781 .addReg(NewVReg1)
5782 .addImm(LPadList.size()));
5783 } else {
5784 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5785 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00005786 .addImm(NumLPads & 0xFFFF));
5787
5788 unsigned VReg2 = VReg1;
5789 if ((NumLPads & 0xFFFF0000) != 0) {
5790 VReg2 = MRI->createVirtualRegister(TRC);
5791 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
5792 .addReg(VReg1)
5793 .addImm(NumLPads >> 16));
5794 }
5795
Bill Wendling952cb502011-10-18 22:49:07 +00005796 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
5797 .addReg(NewVReg1)
5798 .addReg(VReg2));
5799 }
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005800
Bill Wendling95ce2e92011-10-06 22:53:00 +00005801 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
5802 .addMBB(TrapBB)
5803 .addImm(ARMCC::HI)
5804 .addReg(ARM::CPSR);
Bill Wendlingbb734682011-10-05 00:39:32 +00005805
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005806 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5807 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005808 .addJumpTableIndex(MJTI)
5809 .addImm(UId));
Bill Wendling2a850152011-10-05 00:02:33 +00005810
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005811 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005812 AddDefaultCC(
5813 AddDefaultPred(
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005814 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
5815 .addReg(NewVReg3, RegState::Kill)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005816 .addReg(NewVReg1)
5817 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
5818
5819 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005820 .addReg(NewVReg4, RegState::Kill)
Bill Wendling2a850152011-10-05 00:02:33 +00005821 .addReg(NewVReg1)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005822 .addJumpTableIndex(MJTI)
5823 .addImm(UId);
5824 } else if (Subtarget->isThumb()) {
Bill Wendling083a8eb2011-10-06 23:37:36 +00005825 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5826 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
5827 .addFrameIndex(FI)
5828 .addImm(1)
5829 .addMemOperand(FIMMOLd));
Bill Wendlingf1083d42011-10-07 22:08:37 +00005830
Bill Wendlinga5871dc2011-10-18 23:11:05 +00005831 if (NumLPads < 256) {
5832 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
5833 .addReg(NewVReg1)
5834 .addImm(NumLPads));
5835 } else {
5836 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling922ad782011-10-19 09:24:02 +00005837 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
5838 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
5839
5840 // MachineConstantPool wants an explicit alignment.
5841 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
5842 if (Align == 0)
5843 Align = getTargetData()->getTypeAllocSize(C->getType());
5844 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendlinga5871dc2011-10-18 23:11:05 +00005845
5846 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5847 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
5848 .addReg(VReg1, RegState::Define)
5849 .addConstantPoolIndex(Idx));
5850 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
5851 .addReg(NewVReg1)
5852 .addReg(VReg1));
5853 }
5854
Bill Wendling083a8eb2011-10-06 23:37:36 +00005855 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
5856 .addMBB(TrapBB)
5857 .addImm(ARMCC::HI)
5858 .addReg(ARM::CPSR);
5859
5860 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5861 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
5862 .addReg(ARM::CPSR, RegState::Define)
5863 .addReg(NewVReg1)
5864 .addImm(2));
5865
5866 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling217f0e92011-10-06 23:41:14 +00005867 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendling083a8eb2011-10-06 23:37:36 +00005868 .addJumpTableIndex(MJTI)
5869 .addImm(UId));
5870
5871 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5872 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
5873 .addReg(ARM::CPSR, RegState::Define)
5874 .addReg(NewVReg2, RegState::Kill)
5875 .addReg(NewVReg3));
5876
5877 MachineMemOperand *JTMMOLd =
5878 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
5879 MachineMemOperand::MOLoad, 4, 4);
5880
5881 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5882 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
5883 .addReg(NewVReg4, RegState::Kill)
5884 .addImm(0)
5885 .addMemOperand(JTMMOLd));
5886
5887 unsigned NewVReg6 = MRI->createVirtualRegister(TRC);
5888 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
5889 .addReg(ARM::CPSR, RegState::Define)
5890 .addReg(NewVReg5, RegState::Kill)
5891 .addReg(NewVReg3));
5892
5893 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
5894 .addReg(NewVReg6, RegState::Kill)
5895 .addJumpTableIndex(MJTI)
5896 .addImm(UId);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005897 } else {
5898 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5899 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
5900 .addFrameIndex(FI)
5901 .addImm(4)
5902 .addMemOperand(FIMMOLd));
Bill Wendling564392b2011-10-18 22:11:18 +00005903
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005904 if (NumLPads < 256) {
5905 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
5906 .addReg(NewVReg1)
5907 .addImm(NumLPads));
Bill Wendling922ad782011-10-19 09:24:02 +00005908 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005909 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5910 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00005911 .addImm(NumLPads & 0xFFFF));
5912
5913 unsigned VReg2 = VReg1;
5914 if ((NumLPads & 0xFFFF0000) != 0) {
5915 VReg2 = MRI->createVirtualRegister(TRC);
5916 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
5917 .addReg(VReg1)
5918 .addImm(NumLPads >> 16));
5919 }
5920
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005921 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
5922 .addReg(NewVReg1)
5923 .addReg(VReg2));
Bill Wendling922ad782011-10-19 09:24:02 +00005924 } else {
5925 MachineConstantPool *ConstantPool = MF->getConstantPool();
5926 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
5927 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
5928
5929 // MachineConstantPool wants an explicit alignment.
5930 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
5931 if (Align == 0)
5932 Align = getTargetData()->getTypeAllocSize(C->getType());
5933 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
5934
5935 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5936 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
5937 .addReg(VReg1, RegState::Define)
Bill Wendling767f8be2011-10-20 20:37:11 +00005938 .addConstantPoolIndex(Idx)
5939 .addImm(0));
Bill Wendling922ad782011-10-19 09:24:02 +00005940 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
5941 .addReg(NewVReg1)
5942 .addReg(VReg1, RegState::Kill));
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005943 }
5944
Bill Wendling95ce2e92011-10-06 22:53:00 +00005945 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
5946 .addMBB(TrapBB)
5947 .addImm(ARMCC::HI)
5948 .addReg(ARM::CPSR);
Bill Wendling2a850152011-10-05 00:02:33 +00005949
Bill Wendling564392b2011-10-18 22:11:18 +00005950 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005951 AddDefaultCC(
Bill Wendling564392b2011-10-18 22:11:18 +00005952 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005953 .addReg(NewVReg1)
5954 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling564392b2011-10-18 22:11:18 +00005955 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5956 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005957 .addJumpTableIndex(MJTI)
5958 .addImm(UId));
5959
5960 MachineMemOperand *JTMMOLd =
5961 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
5962 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling564392b2011-10-18 22:11:18 +00005963 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005964 AddDefaultPred(
Bill Wendling564392b2011-10-18 22:11:18 +00005965 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
5966 .addReg(NewVReg3, RegState::Kill)
5967 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005968 .addImm(0)
5969 .addMemOperand(JTMMOLd));
5970
5971 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
Bill Wendling564392b2011-10-18 22:11:18 +00005972 .addReg(NewVReg5, RegState::Kill)
5973 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005974 .addJumpTableIndex(MJTI)
5975 .addImm(UId);
5976 }
Bill Wendling2a850152011-10-05 00:02:33 +00005977
Bill Wendlingbb734682011-10-05 00:39:32 +00005978 // Add the jump table entries as successors to the MBB.
Bill Wendling2acf6382011-10-07 23:18:02 +00005979 MachineBasicBlock *PrevMBB = 0;
Bill Wendlingbb734682011-10-05 00:39:32 +00005980 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00005981 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
5982 MachineBasicBlock *CurMBB = *I;
5983 if (PrevMBB != CurMBB)
5984 DispContBB->addSuccessor(CurMBB);
5985 PrevMBB = CurMBB;
5986 }
5987
Bill Wendling24bb9252011-10-17 05:25:09 +00005988 // N.B. the order the invoke BBs are processed in doesn't matter here.
Bill Wendling969c9ef2011-10-14 23:34:37 +00005989 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
5990 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
5991 const unsigned *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendlingf7b02072011-10-18 18:30:49 +00005992 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling2acf6382011-10-07 23:18:02 +00005993 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
5994 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
5995 MachineBasicBlock *BB = *I;
Bill Wendling969c9ef2011-10-14 23:34:37 +00005996
5997 // Remove the landing pad successor from the invoke block and replace it
5998 // with the new dispatch block.
Bill Wendlingde39d862011-10-26 07:16:18 +00005999 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6000 BB->succ_end());
6001 while (!Successors.empty()) {
6002 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling2acf6382011-10-07 23:18:02 +00006003 if (SMBB->isLandingPad()) {
6004 BB->removeSuccessor(SMBB);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006005 MBBLPads.push_back(SMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006006 }
6007 }
6008
6009 BB->addSuccessor(DispatchBB);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006010
6011 // Find the invoke call and mark all of the callee-saved registers as
6012 // 'implicit defined' so that they're spilled. This prevents code from
6013 // moving instructions to before the EH block, where they will never be
6014 // executed.
6015 for (MachineBasicBlock::reverse_iterator
6016 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006017 if (!II->isCall()) continue;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006018
6019 DenseMap<unsigned, bool> DefRegs;
6020 for (MachineInstr::mop_iterator
6021 OI = II->operands_begin(), OE = II->operands_end();
6022 OI != OE; ++OI) {
6023 if (!OI->isReg()) continue;
6024 DefRegs[OI->getReg()] = true;
6025 }
6026
6027 MachineInstrBuilder MIB(&*II);
6028
Bill Wendling5d798592011-10-14 23:55:44 +00006029 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006030 unsigned Reg = SavedRegs[i];
6031 if (Subtarget->isThumb2() &&
6032 !ARM::tGPRRegisterClass->contains(Reg) &&
6033 !ARM::hGPRRegisterClass->contains(Reg))
6034 continue;
6035 else if (Subtarget->isThumb1Only() &&
6036 !ARM::tGPRRegisterClass->contains(Reg))
6037 continue;
6038 else if (!Subtarget->isThumb() &&
6039 !ARM::GPRRegisterClass->contains(Reg))
6040 continue;
6041 if (!DefRegs[Reg])
6042 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling5d798592011-10-14 23:55:44 +00006043 }
Bill Wendling969c9ef2011-10-14 23:34:37 +00006044
6045 break;
6046 }
Bill Wendling2acf6382011-10-07 23:18:02 +00006047 }
Bill Wendlingbb734682011-10-05 00:39:32 +00006048
Bill Wendlingf7b02072011-10-18 18:30:49 +00006049 // Mark all former landing pads as non-landing pads. The dispatch is the only
6050 // landing pad now.
6051 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6052 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6053 (*I)->setIsLandingPad(false);
6054
Bill Wendlingbb734682011-10-05 00:39:32 +00006055 // The instruction is gone now.
6056 MI->eraseFromParent();
6057
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006058 return MBB;
6059}
6060
Evan Cheng218977b2010-07-13 19:27:42 +00006061static
6062MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6063 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6064 E = MBB->succ_end(); I != E; ++I)
6065 if (*I != Succ)
6066 return *I;
6067 llvm_unreachable("Expecting a BB with two successors!");
6068}
6069
Jim Grosbache801dc42009-12-12 01:40:06 +00006070MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006071ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00006072 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006073 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00006074 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006075 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00006076 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00006077 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00006078 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00006079 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00006080 }
Jim Grosbachee2c2a42011-09-16 21:55:56 +00006081 // The Thumb2 pre-indexed stores have the same MI operands, they just
6082 // define them differently in the .td files from the isel patterns, so
6083 // they need pseudos.
6084 case ARM::t2STR_preidx:
6085 MI->setDesc(TII->get(ARM::t2STR_PRE));
6086 return BB;
6087 case ARM::t2STRB_preidx:
6088 MI->setDesc(TII->get(ARM::t2STRB_PRE));
6089 return BB;
6090 case ARM::t2STRH_preidx:
6091 MI->setDesc(TII->get(ARM::t2STRH_PRE));
6092 return BB;
6093
Jim Grosbach19dec202011-08-05 20:35:44 +00006094 case ARM::STRi_preidx:
6095 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00006096 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00006097 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
6098 // Decode the offset.
6099 unsigned Offset = MI->getOperand(4).getImm();
6100 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
6101 Offset = ARM_AM::getAM2Offset(Offset);
6102 if (isSub)
6103 Offset = -Offset;
6104
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006105 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00006106 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00006107 .addOperand(MI->getOperand(0)) // Rn_wb
6108 .addOperand(MI->getOperand(1)) // Rt
6109 .addOperand(MI->getOperand(2)) // Rn
6110 .addImm(Offset) // offset (skip GPR==zero_reg)
6111 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006112 .addOperand(MI->getOperand(6))
6113 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00006114 MI->eraseFromParent();
6115 return BB;
6116 }
6117 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00006118 case ARM::STRBr_preidx:
6119 case ARM::STRH_preidx: {
6120 unsigned NewOpc;
6121 switch (MI->getOpcode()) {
6122 default: llvm_unreachable("unexpected opcode!");
6123 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
6124 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
6125 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
6126 }
Jim Grosbach19dec202011-08-05 20:35:44 +00006127 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
6128 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
6129 MIB.addOperand(MI->getOperand(i));
6130 MI->eraseFromParent();
6131 return BB;
6132 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006133 case ARM::ATOMIC_LOAD_ADD_I8:
6134 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6135 case ARM::ATOMIC_LOAD_ADD_I16:
6136 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6137 case ARM::ATOMIC_LOAD_ADD_I32:
6138 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006139
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006140 case ARM::ATOMIC_LOAD_AND_I8:
6141 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6142 case ARM::ATOMIC_LOAD_AND_I16:
6143 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6144 case ARM::ATOMIC_LOAD_AND_I32:
6145 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006146
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006147 case ARM::ATOMIC_LOAD_OR_I8:
6148 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6149 case ARM::ATOMIC_LOAD_OR_I16:
6150 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6151 case ARM::ATOMIC_LOAD_OR_I32:
6152 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006153
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006154 case ARM::ATOMIC_LOAD_XOR_I8:
6155 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6156 case ARM::ATOMIC_LOAD_XOR_I16:
6157 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6158 case ARM::ATOMIC_LOAD_XOR_I32:
6159 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006160
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006161 case ARM::ATOMIC_LOAD_NAND_I8:
6162 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6163 case ARM::ATOMIC_LOAD_NAND_I16:
6164 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6165 case ARM::ATOMIC_LOAD_NAND_I32:
6166 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006167
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006168 case ARM::ATOMIC_LOAD_SUB_I8:
6169 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6170 case ARM::ATOMIC_LOAD_SUB_I16:
6171 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6172 case ARM::ATOMIC_LOAD_SUB_I32:
6173 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006174
Jim Grosbachf7da8822011-04-26 19:44:18 +00006175 case ARM::ATOMIC_LOAD_MIN_I8:
6176 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
6177 case ARM::ATOMIC_LOAD_MIN_I16:
6178 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
6179 case ARM::ATOMIC_LOAD_MIN_I32:
6180 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
6181
6182 case ARM::ATOMIC_LOAD_MAX_I8:
6183 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
6184 case ARM::ATOMIC_LOAD_MAX_I16:
6185 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
6186 case ARM::ATOMIC_LOAD_MAX_I32:
6187 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
6188
6189 case ARM::ATOMIC_LOAD_UMIN_I8:
6190 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
6191 case ARM::ATOMIC_LOAD_UMIN_I16:
6192 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
6193 case ARM::ATOMIC_LOAD_UMIN_I32:
6194 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
6195
6196 case ARM::ATOMIC_LOAD_UMAX_I8:
6197 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
6198 case ARM::ATOMIC_LOAD_UMAX_I16:
6199 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
6200 case ARM::ATOMIC_LOAD_UMAX_I32:
6201 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
6202
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006203 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
6204 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
6205 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00006206
6207 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
6208 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
6209 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006210
Eli Friedman2bdffe42011-08-31 00:31:29 +00006211
6212 case ARM::ATOMADD6432:
6213 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006214 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr,
6215 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006216 case ARM::ATOMSUB6432:
6217 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006218 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6219 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006220 case ARM::ATOMOR6432:
6221 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006222 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006223 case ARM::ATOMXOR6432:
6224 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006225 isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006226 case ARM::ATOMAND6432:
6227 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006228 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006229 case ARM::ATOMSWAP6432:
6230 return EmitAtomicBinary64(MI, BB, 0, 0, false);
Eli Friedman4d3f3292011-08-31 17:52:22 +00006231 case ARM::ATOMCMPXCHG6432:
6232 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
6233 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6234 /*NeedsCarry*/ false, /*IsCmpxchg*/true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006235
Evan Cheng007ea272009-08-12 05:17:19 +00006236 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00006237 // To "insert" a SELECT_CC instruction, we actually have to insert the
6238 // diamond control-flow pattern. The incoming instruction knows the
6239 // destination vreg to set, the condition code register to branch on, the
6240 // true/false values to select between, and a branch opcode to use.
6241 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006242 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00006243 ++It;
6244
6245 // thisMBB:
6246 // ...
6247 // TrueVal = ...
6248 // cmpTY ccX, r1, r2
6249 // bCC copy1MBB
6250 // fallthrough --> copy0MBB
6251 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006252 MachineFunction *F = BB->getParent();
6253 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6254 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00006255 F->insert(It, copy0MBB);
6256 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006257
6258 // Transfer the remainder of BB and its successor edges to sinkMBB.
6259 sinkMBB->splice(sinkMBB->begin(), BB,
6260 llvm::next(MachineBasicBlock::iterator(MI)),
6261 BB->end());
6262 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6263
Dan Gohman258c58c2010-07-06 15:49:48 +00006264 BB->addSuccessor(copy0MBB);
6265 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00006266
Dan Gohman14152b42010-07-06 20:24:04 +00006267 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
6268 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
6269
Evan Chenga8e29892007-01-19 07:51:42 +00006270 // copy0MBB:
6271 // %FalseValue = ...
6272 // # fallthrough to sinkMBB
6273 BB = copy0MBB;
6274
6275 // Update machine-CFG edges
6276 BB->addSuccessor(sinkMBB);
6277
6278 // sinkMBB:
6279 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6280 // ...
6281 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00006282 BuildMI(*BB, BB->begin(), dl,
6283 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00006284 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6285 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6286
Dan Gohman14152b42010-07-06 20:24:04 +00006287 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00006288 return BB;
6289 }
Evan Cheng86198642009-08-07 00:34:42 +00006290
Evan Cheng218977b2010-07-13 19:27:42 +00006291 case ARM::BCCi64:
6292 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00006293 // If there is an unconditional branch to the other successor, remove it.
6294 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00006295
Evan Cheng218977b2010-07-13 19:27:42 +00006296 // Compare both parts that make up the double comparison separately for
6297 // equality.
6298 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
6299
6300 unsigned LHS1 = MI->getOperand(1).getReg();
6301 unsigned LHS2 = MI->getOperand(2).getReg();
6302 if (RHSisZero) {
6303 AddDefaultPred(BuildMI(BB, dl,
6304 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6305 .addReg(LHS1).addImm(0));
6306 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6307 .addReg(LHS2).addImm(0)
6308 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6309 } else {
6310 unsigned RHS1 = MI->getOperand(3).getReg();
6311 unsigned RHS2 = MI->getOperand(4).getReg();
6312 AddDefaultPred(BuildMI(BB, dl,
6313 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6314 .addReg(LHS1).addReg(RHS1));
6315 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6316 .addReg(LHS2).addReg(RHS2)
6317 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6318 }
6319
6320 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
6321 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
6322 if (MI->getOperand(0).getImm() == ARMCC::NE)
6323 std::swap(destMBB, exitMBB);
6324
6325 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6326 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson51f6a7a2011-09-09 21:48:23 +00006327 if (isThumb2)
6328 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
6329 else
6330 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng218977b2010-07-13 19:27:42 +00006331
6332 MI->eraseFromParent(); // The pseudo instruction is gone now.
6333 return BB;
6334 }
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006335
Bill Wendling5bc85282011-10-17 20:37:20 +00006336 case ARM::Int_eh_sjlj_setjmp:
6337 case ARM::Int_eh_sjlj_setjmp_nofp:
6338 case ARM::tInt_eh_sjlj_setjmp:
6339 case ARM::t2Int_eh_sjlj_setjmp:
6340 case ARM::t2Int_eh_sjlj_setjmp_nofp:
6341 EmitSjLjDispatchBlock(MI, BB);
6342 return BB;
6343
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006344 case ARM::ABS:
6345 case ARM::t2ABS: {
6346 // To insert an ABS instruction, we have to insert the
6347 // diamond control-flow pattern. The incoming instruction knows the
6348 // source vreg to test against 0, the destination vreg to set,
6349 // the condition code register to branch on, the
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006350 // true/false values to select between, and a branch opcode to use.
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006351 // It transforms
6352 // V1 = ABS V0
6353 // into
6354 // V2 = MOVS V0
6355 // BCC (branch to SinkBB if V0 >= 0)
6356 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006357 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006358 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6359 MachineFunction::iterator BBI = BB;
6360 ++BBI;
6361 MachineFunction *Fn = BB->getParent();
6362 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6363 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6364 Fn->insert(BBI, RSBBB);
6365 Fn->insert(BBI, SinkBB);
6366
6367 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
6368 unsigned int ABSDstReg = MI->getOperand(0).getReg();
6369 bool isThumb2 = Subtarget->isThumb2();
6370 MachineRegisterInfo &MRI = Fn->getRegInfo();
6371 // In Thumb mode S must not be specified if source register is the SP or
6372 // PC and if destination register is the SP, so restrict register class
6373 unsigned NewMovDstReg = MRI.createVirtualRegister(
6374 isThumb2 ? ARM::rGPRRegisterClass : ARM::GPRRegisterClass);
6375 unsigned NewRsbDstReg = MRI.createVirtualRegister(
6376 isThumb2 ? ARM::rGPRRegisterClass : ARM::GPRRegisterClass);
6377
6378 // Transfer the remainder of BB and its successor edges to sinkMBB.
6379 SinkBB->splice(SinkBB->begin(), BB,
6380 llvm::next(MachineBasicBlock::iterator(MI)),
6381 BB->end());
6382 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
6383
6384 BB->addSuccessor(RSBBB);
6385 BB->addSuccessor(SinkBB);
6386
6387 // fall through to SinkMBB
6388 RSBBB->addSuccessor(SinkBB);
6389
6390 // insert a movs at the end of BB
6391 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVr : ARM::MOVr),
6392 NewMovDstReg)
6393 .addReg(ABSSrcReg, RegState::Kill)
6394 .addImm((unsigned)ARMCC::AL).addReg(0)
6395 .addReg(ARM::CPSR, RegState::Define);
6396
6397 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006398 BuildMI(BB, dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006399 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
6400 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
6401
6402 // insert rsbri in RSBBB
6403 // Note: BCC and rsbri will be converted into predicated rsbmi
6404 // by if-conversion pass
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006405 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006406 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
6407 .addReg(NewMovDstReg, RegState::Kill)
6408 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
6409
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006410 // insert PHI in SinkBB,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006411 // reuse ABSDstReg to not change uses of ABS instruction
6412 BuildMI(*SinkBB, SinkBB->begin(), dl,
6413 TII->get(ARM::PHI), ABSDstReg)
6414 .addReg(NewRsbDstReg).addMBB(RSBBB)
6415 .addReg(NewMovDstReg).addMBB(BB);
6416
6417 // remove ABS instruction
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006418 MI->eraseFromParent();
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006419
6420 // return last added BB
6421 return SinkBB;
6422 }
Evan Chenga8e29892007-01-19 07:51:42 +00006423 }
6424}
6425
Evan Cheng37fefc22011-08-30 19:09:48 +00006426void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
6427 SDNode *Node) const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006428 if (!MI->hasPostISelHook()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006429 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
6430 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
6431 return;
6432 }
6433
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006434 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick4815d562011-09-20 03:17:40 +00006435 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
6436 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
6437 // operand is still set to noreg. If needed, set the optional operand's
6438 // register to CPSR, and remove the redundant implicit def.
Andrew Trick3be654f2011-09-21 02:20:46 +00006439 //
Andrew Trick90b7b122011-10-18 19:18:52 +00006440 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick4815d562011-09-20 03:17:40 +00006441
Andrew Trick3be654f2011-09-21 02:20:46 +00006442 // Rename pseudo opcodes.
6443 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
6444 if (NewOpc) {
6445 const ARMBaseInstrInfo *TII =
6446 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick90b7b122011-10-18 19:18:52 +00006447 MCID = &TII->get(NewOpc);
6448
6449 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
6450 "converted opcode should be the same except for cc_out");
6451
6452 MI->setDesc(*MCID);
6453
6454 // Add the optional cc_out operand
6455 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick3be654f2011-09-21 02:20:46 +00006456 }
Andrew Trick90b7b122011-10-18 19:18:52 +00006457 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick4815d562011-09-20 03:17:40 +00006458
6459 // Any ARM instruction that sets the 's' bit should specify an optional
6460 // "cc_out" operand in the last operand position.
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006461 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006462 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006463 return;
6464 }
Andrew Trick3be654f2011-09-21 02:20:46 +00006465 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
6466 // since we already have an optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006467 bool definesCPSR = false;
6468 bool deadCPSR = false;
Andrew Trick90b7b122011-10-18 19:18:52 +00006469 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick4815d562011-09-20 03:17:40 +00006470 i != e; ++i) {
6471 const MachineOperand &MO = MI->getOperand(i);
6472 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
6473 definesCPSR = true;
6474 if (MO.isDead())
6475 deadCPSR = true;
6476 MI->RemoveOperand(i);
6477 break;
Evan Cheng37fefc22011-08-30 19:09:48 +00006478 }
6479 }
Andrew Trick4815d562011-09-20 03:17:40 +00006480 if (!definesCPSR) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006481 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006482 return;
6483 }
6484 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick3be654f2011-09-21 02:20:46 +00006485 if (deadCPSR) {
6486 assert(!MI->getOperand(ccOutIdx).getReg() &&
6487 "expect uninitialized optional cc_out operand");
Andrew Trick4815d562011-09-20 03:17:40 +00006488 return;
Andrew Trick3be654f2011-09-21 02:20:46 +00006489 }
Andrew Trick4815d562011-09-20 03:17:40 +00006490
Andrew Trick3be654f2011-09-21 02:20:46 +00006491 // If this instruction was defined with an optional CPSR def and its dag node
6492 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006493 MachineOperand &MO = MI->getOperand(ccOutIdx);
6494 MO.setReg(ARM::CPSR);
6495 MO.setIsDef(true);
Evan Cheng37fefc22011-08-30 19:09:48 +00006496}
6497
Evan Chenga8e29892007-01-19 07:51:42 +00006498//===----------------------------------------------------------------------===//
6499// ARM Optimization Hooks
6500//===----------------------------------------------------------------------===//
6501
Chris Lattnerd1980a52009-03-12 06:52:53 +00006502static
6503SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
6504 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00006505 SelectionDAG &DAG = DCI.DAG;
6506 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00006507 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00006508 unsigned Opc = N->getOpcode();
6509 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
6510 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
6511 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
6512 ISD::CondCode CC = ISD::SETCC_INVALID;
6513
6514 if (isSlctCC) {
6515 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
6516 } else {
6517 SDValue CCOp = Slct.getOperand(0);
6518 if (CCOp.getOpcode() == ISD::SETCC)
6519 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
6520 }
6521
6522 bool DoXform = false;
6523 bool InvCC = false;
6524 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
6525 "Bad input!");
6526
6527 if (LHS.getOpcode() == ISD::Constant &&
6528 cast<ConstantSDNode>(LHS)->isNullValue()) {
6529 DoXform = true;
6530 } else if (CC != ISD::SETCC_INVALID &&
6531 RHS.getOpcode() == ISD::Constant &&
6532 cast<ConstantSDNode>(RHS)->isNullValue()) {
6533 std::swap(LHS, RHS);
6534 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006535 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00006536 Op0.getOperand(0).getValueType();
6537 bool isInt = OpVT.isInteger();
6538 CC = ISD::getSetCCInverse(CC, isInt);
6539
6540 if (!TLI.isCondCodeLegal(CC, OpVT))
6541 return SDValue(); // Inverse operator isn't legal.
6542
6543 DoXform = true;
6544 InvCC = true;
6545 }
6546
6547 if (DoXform) {
6548 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
6549 if (isSlctCC)
6550 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
6551 Slct.getOperand(0), Slct.getOperand(1), CC);
6552 SDValue CCOp = Slct.getOperand(0);
6553 if (InvCC)
6554 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
6555 CCOp.getOperand(0), CCOp.getOperand(1), CC);
6556 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
6557 CCOp, OtherOp, Result);
6558 }
6559 return SDValue();
6560}
6561
Eric Christopherfa6f5912011-06-29 21:10:36 +00006562// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00006563// (only after legalization).
6564static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
6565 TargetLowering::DAGCombinerInfo &DCI,
6566 const ARMSubtarget *Subtarget) {
6567
6568 // Only perform optimization if after legalize, and if NEON is available. We
6569 // also expected both operands to be BUILD_VECTORs.
6570 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
6571 || N0.getOpcode() != ISD::BUILD_VECTOR
6572 || N1.getOpcode() != ISD::BUILD_VECTOR)
6573 return SDValue();
6574
6575 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
6576 EVT VT = N->getValueType(0);
6577 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
6578 return SDValue();
6579
6580 // Check that the vector operands are of the right form.
6581 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
6582 // operands, where N is the size of the formed vector.
6583 // Each EXTRACT_VECTOR should have the same input vector and odd or even
6584 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00006585
6586 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00006587 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00006588 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00006589 SDValue Vec = N0->getOperand(0)->getOperand(0);
6590 SDNode *V = Vec.getNode();
6591 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00006592
Eric Christopherfa6f5912011-06-29 21:10:36 +00006593 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00006594 // check to see if each of their operands are an EXTRACT_VECTOR with
6595 // the same vector and appropriate index.
6596 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
6597 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
6598 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00006599
Tanya Lattner189531f2011-06-14 23:48:48 +00006600 SDValue ExtVec0 = N0->getOperand(i);
6601 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00006602
Tanya Lattner189531f2011-06-14 23:48:48 +00006603 // First operand is the vector, verify its the same.
6604 if (V != ExtVec0->getOperand(0).getNode() ||
6605 V != ExtVec1->getOperand(0).getNode())
6606 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00006607
Tanya Lattner189531f2011-06-14 23:48:48 +00006608 // Second is the constant, verify its correct.
6609 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
6610 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00006611
Tanya Lattner189531f2011-06-14 23:48:48 +00006612 // For the constant, we want to see all the even or all the odd.
6613 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
6614 || C1->getZExtValue() != nextIndex+1)
6615 return SDValue();
6616
6617 // Increment index.
6618 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00006619 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00006620 return SDValue();
6621 }
6622
6623 // Create VPADDL node.
6624 SelectionDAG &DAG = DCI.DAG;
6625 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00006626
6627 // Build operand list.
6628 SmallVector<SDValue, 8> Ops;
6629 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
6630 TLI.getPointerTy()));
6631
6632 // Input is the vector.
6633 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00006634
Tanya Lattner189531f2011-06-14 23:48:48 +00006635 // Get widened type and narrowed type.
6636 MVT widenType;
6637 unsigned numElem = VT.getVectorNumElements();
6638 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
6639 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
6640 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
6641 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
6642 default:
6643 assert(0 && "Invalid vector element type for padd optimization.");
6644 }
6645
6646 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
6647 widenType, &Ops[0], Ops.size());
6648 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
6649}
6650
Bob Wilson3d5792a2010-07-29 20:34:14 +00006651/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
6652/// operands N0 and N1. This is a helper for PerformADDCombine that is
6653/// called with the default operands, and if that fails, with commuted
6654/// operands.
6655static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00006656 TargetLowering::DAGCombinerInfo &DCI,
6657 const ARMSubtarget *Subtarget){
6658
6659 // Attempt to create vpaddl for this add.
6660 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
6661 if (Result.getNode())
6662 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00006663
Chris Lattnerd1980a52009-03-12 06:52:53 +00006664 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
6665 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
6666 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
6667 if (Result.getNode()) return Result;
6668 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00006669 return SDValue();
6670}
6671
Bob Wilson3d5792a2010-07-29 20:34:14 +00006672/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
6673///
6674static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00006675 TargetLowering::DAGCombinerInfo &DCI,
6676 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00006677 SDValue N0 = N->getOperand(0);
6678 SDValue N1 = N->getOperand(1);
6679
6680 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00006681 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00006682 if (Result.getNode())
6683 return Result;
6684
6685 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00006686 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00006687}
6688
Chris Lattnerd1980a52009-03-12 06:52:53 +00006689/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00006690///
Chris Lattnerd1980a52009-03-12 06:52:53 +00006691static SDValue PerformSUBCombine(SDNode *N,
6692 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00006693 SDValue N0 = N->getOperand(0);
6694 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00006695
Chris Lattnerd1980a52009-03-12 06:52:53 +00006696 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
6697 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
6698 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
6699 if (Result.getNode()) return Result;
6700 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00006701
Chris Lattnerd1980a52009-03-12 06:52:53 +00006702 return SDValue();
6703}
6704
Evan Cheng463d3582011-03-31 19:38:48 +00006705/// PerformVMULCombine
6706/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
6707/// special multiplier accumulator forwarding.
6708/// vmul d3, d0, d2
6709/// vmla d3, d1, d2
6710/// is faster than
6711/// vadd d3, d0, d1
6712/// vmul d3, d3, d2
6713static SDValue PerformVMULCombine(SDNode *N,
6714 TargetLowering::DAGCombinerInfo &DCI,
6715 const ARMSubtarget *Subtarget) {
6716 if (!Subtarget->hasVMLxForwarding())
6717 return SDValue();
6718
6719 SelectionDAG &DAG = DCI.DAG;
6720 SDValue N0 = N->getOperand(0);
6721 SDValue N1 = N->getOperand(1);
6722 unsigned Opcode = N0.getOpcode();
6723 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
6724 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00006725 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00006726 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
6727 Opcode != ISD::FADD && Opcode != ISD::FSUB)
6728 return SDValue();
6729 std::swap(N0, N1);
6730 }
6731
6732 EVT VT = N->getValueType(0);
6733 DebugLoc DL = N->getDebugLoc();
6734 SDValue N00 = N0->getOperand(0);
6735 SDValue N01 = N0->getOperand(1);
6736 return DAG.getNode(Opcode, DL, VT,
6737 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
6738 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
6739}
6740
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006741static SDValue PerformMULCombine(SDNode *N,
6742 TargetLowering::DAGCombinerInfo &DCI,
6743 const ARMSubtarget *Subtarget) {
6744 SelectionDAG &DAG = DCI.DAG;
6745
6746 if (Subtarget->isThumb1Only())
6747 return SDValue();
6748
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006749 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
6750 return SDValue();
6751
6752 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00006753 if (VT.is64BitVector() || VT.is128BitVector())
6754 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006755 if (VT != MVT::i32)
6756 return SDValue();
6757
6758 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
6759 if (!C)
6760 return SDValue();
6761
6762 uint64_t MulAmt = C->getZExtValue();
6763 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
6764 ShiftAmt = ShiftAmt & (32 - 1);
6765 SDValue V = N->getOperand(0);
6766 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006767
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006768 SDValue Res;
6769 MulAmt >>= ShiftAmt;
6770 if (isPowerOf2_32(MulAmt - 1)) {
6771 // (mul x, 2^N + 1) => (add (shl x, N), x)
6772 Res = DAG.getNode(ISD::ADD, DL, VT,
6773 V, DAG.getNode(ISD::SHL, DL, VT,
6774 V, DAG.getConstant(Log2_32(MulAmt-1),
6775 MVT::i32)));
6776 } else if (isPowerOf2_32(MulAmt + 1)) {
6777 // (mul x, 2^N - 1) => (sub (shl x, N), x)
6778 Res = DAG.getNode(ISD::SUB, DL, VT,
6779 DAG.getNode(ISD::SHL, DL, VT,
6780 V, DAG.getConstant(Log2_32(MulAmt+1),
6781 MVT::i32)),
6782 V);
6783 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006784 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006785
6786 if (ShiftAmt != 0)
6787 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
6788 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006789
6790 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006791 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006792 return SDValue();
6793}
6794
Owen Anderson080c0922010-11-05 19:27:46 +00006795static SDValue PerformANDCombine(SDNode *N,
6796 TargetLowering::DAGCombinerInfo &DCI) {
Owen Anderson76706012011-04-05 21:48:57 +00006797
Owen Anderson080c0922010-11-05 19:27:46 +00006798 // Attempt to use immediate-form VBIC
6799 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
6800 DebugLoc dl = N->getDebugLoc();
6801 EVT VT = N->getValueType(0);
6802 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006803
Tanya Lattner0433b212011-04-07 15:24:20 +00006804 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
6805 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00006806
Owen Anderson080c0922010-11-05 19:27:46 +00006807 APInt SplatBits, SplatUndef;
6808 unsigned SplatBitSize;
6809 bool HasAnyUndefs;
6810 if (BVN &&
6811 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
6812 if (SplatBitSize <= 64) {
6813 EVT VbicVT;
6814 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
6815 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006816 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00006817 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00006818 if (Val.getNode()) {
6819 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006820 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00006821 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006822 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00006823 }
6824 }
6825 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006826
Owen Anderson080c0922010-11-05 19:27:46 +00006827 return SDValue();
6828}
6829
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006830/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
6831static SDValue PerformORCombine(SDNode *N,
6832 TargetLowering::DAGCombinerInfo &DCI,
6833 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00006834 // Attempt to use immediate-form VORR
6835 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
6836 DebugLoc dl = N->getDebugLoc();
6837 EVT VT = N->getValueType(0);
6838 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006839
Tanya Lattner0433b212011-04-07 15:24:20 +00006840 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
6841 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00006842
Owen Anderson60f48702010-11-03 23:15:26 +00006843 APInt SplatBits, SplatUndef;
6844 unsigned SplatBitSize;
6845 bool HasAnyUndefs;
6846 if (BVN && Subtarget->hasNEON() &&
6847 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
6848 if (SplatBitSize <= 64) {
6849 EVT VorrVT;
6850 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
6851 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00006852 DAG, VorrVT, VT.is128BitVector(),
6853 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00006854 if (Val.getNode()) {
6855 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006856 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00006857 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006858 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00006859 }
6860 }
6861 }
6862
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00006863 SDValue N0 = N->getOperand(0);
6864 if (N0.getOpcode() != ISD::AND)
6865 return SDValue();
6866 SDValue N1 = N->getOperand(1);
6867
6868 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
6869 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
6870 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
6871 APInt SplatUndef;
6872 unsigned SplatBitSize;
6873 bool HasAnyUndefs;
6874
6875 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
6876 APInt SplatBits0;
6877 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
6878 HasAnyUndefs) && !HasAnyUndefs) {
6879 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
6880 APInt SplatBits1;
6881 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
6882 HasAnyUndefs) && !HasAnyUndefs &&
6883 SplatBits0 == ~SplatBits1) {
6884 // Canonicalize the vector type to make instruction selection simpler.
6885 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
6886 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
6887 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00006888 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00006889 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
6890 }
6891 }
6892 }
6893
Jim Grosbach54238562010-07-17 03:30:54 +00006894 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
6895 // reasonable.
6896
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006897 // BFI is only available on V6T2+
6898 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
6899 return SDValue();
6900
Jim Grosbach54238562010-07-17 03:30:54 +00006901 DebugLoc DL = N->getDebugLoc();
6902 // 1) or (and A, mask), val => ARMbfi A, val, mask
6903 // iff (val & mask) == val
6904 //
6905 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
6906 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00006907 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00006908 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00006909 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00006910 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006911
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006912 if (VT != MVT::i32)
6913 return SDValue();
6914
Evan Cheng30fb13f2010-12-13 20:32:54 +00006915 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00006916
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006917 // The value and the mask need to be constants so we can verify this is
6918 // actually a bitfield set. If the mask is 0xffff, we can do better
6919 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00006920 SDValue MaskOp = N0.getOperand(1);
6921 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
6922 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006923 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00006924 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006925 if (Mask == 0xffff)
6926 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006927 SDValue Res;
6928 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00006929 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
6930 if (N1C) {
6931 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00006932 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00006933 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006934
Evan Chenga9688c42010-12-11 04:11:38 +00006935 if (ARM::isBitFieldInvertedMask(Mask)) {
6936 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006937
Evan Cheng30fb13f2010-12-13 20:32:54 +00006938 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00006939 DAG.getConstant(Val, MVT::i32),
6940 DAG.getConstant(Mask, MVT::i32));
6941
6942 // Do not add new nodes to DAG combiner worklist.
6943 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00006944 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00006945 }
Jim Grosbach54238562010-07-17 03:30:54 +00006946 } else if (N1.getOpcode() == ISD::AND) {
6947 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00006948 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
6949 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00006950 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00006951 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006952
Eric Christopher29aeed12011-03-26 01:21:03 +00006953 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
6954 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00006955 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00006956 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00006957 // The pack halfword instruction works better for masks that fit it,
6958 // so use that when it's available.
6959 if (Subtarget->hasT2ExtractPack() &&
6960 (Mask == 0xffff || Mask == 0xffff0000))
6961 return SDValue();
6962 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00006963 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00006964 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00006965 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00006966 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00006967 DAG.getConstant(Mask, MVT::i32));
6968 // Do not add new nodes to DAG combiner worklist.
6969 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00006970 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006971 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00006972 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00006973 // The pack halfword instruction works better for masks that fit it,
6974 // so use that when it's available.
6975 if (Subtarget->hasT2ExtractPack() &&
6976 (Mask2 == 0xffff || Mask2 == 0xffff0000))
6977 return SDValue();
6978 // 2b
6979 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00006980 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00006981 DAG.getConstant(lsb, MVT::i32));
6982 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00006983 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00006984 // Do not add new nodes to DAG combiner worklist.
6985 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00006986 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006987 }
6988 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006989
Evan Cheng30fb13f2010-12-13 20:32:54 +00006990 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
6991 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
6992 ARM::isBitFieldInvertedMask(~Mask)) {
6993 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
6994 // where lsb(mask) == #shamt and masked bits of B are known zero.
6995 SDValue ShAmt = N00.getOperand(1);
6996 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
6997 unsigned LSB = CountTrailingZeros_32(Mask);
6998 if (ShAmtC != LSB)
6999 return SDValue();
7000
7001 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
7002 DAG.getConstant(~Mask, MVT::i32));
7003
7004 // Do not add new nodes to DAG combiner worklist.
7005 DCI.CombineTo(N, Res, false);
7006 }
7007
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007008 return SDValue();
7009}
7010
Evan Chengbf188ae2011-06-15 01:12:31 +00007011/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
7012/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00007013static SDValue PerformBFICombine(SDNode *N,
7014 TargetLowering::DAGCombinerInfo &DCI) {
7015 SDValue N1 = N->getOperand(1);
7016 if (N1.getOpcode() == ISD::AND) {
7017 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7018 if (!N11C)
7019 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007020 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
7021 unsigned LSB = CountTrailingZeros_32(~InvMask);
7022 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
7023 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00007024 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007025 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00007026 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
7027 N->getOperand(0), N1.getOperand(0),
7028 N->getOperand(2));
7029 }
7030 return SDValue();
7031}
7032
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007033/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
7034/// ARMISD::VMOVRRD.
7035static SDValue PerformVMOVRRDCombine(SDNode *N,
7036 TargetLowering::DAGCombinerInfo &DCI) {
7037 // vmovrrd(vmovdrr x, y) -> x,y
7038 SDValue InDouble = N->getOperand(0);
7039 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
7040 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00007041
7042 // vmovrrd(load f64) -> (load i32), (load i32)
7043 SDNode *InNode = InDouble.getNode();
7044 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
7045 InNode->getValueType(0) == MVT::f64 &&
7046 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
7047 !cast<LoadSDNode>(InNode)->isVolatile()) {
7048 // TODO: Should this be done for non-FrameIndex operands?
7049 LoadSDNode *LD = cast<LoadSDNode>(InNode);
7050
7051 SelectionDAG &DAG = DCI.DAG;
7052 DebugLoc DL = LD->getDebugLoc();
7053 SDValue BasePtr = LD->getBasePtr();
7054 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
7055 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007056 LD->isNonTemporal(), LD->isInvariant(),
7057 LD->getAlignment());
Cameron Zwarich4071a712011-04-02 02:40:43 +00007058
7059 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7060 DAG.getConstant(4, MVT::i32));
7061 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
7062 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007063 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich4071a712011-04-02 02:40:43 +00007064 std::min(4U, LD->getAlignment() / 2));
7065
7066 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
7067 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
7068 DCI.RemoveFromWorklist(LD);
7069 DAG.DeleteNode(LD);
7070 return Result;
7071 }
7072
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007073 return SDValue();
7074}
7075
7076/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
7077/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
7078static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
7079 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
7080 SDValue Op0 = N->getOperand(0);
7081 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007082 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007083 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007084 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007085 Op1 = Op1.getOperand(0);
7086 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
7087 Op0.getNode() == Op1.getNode() &&
7088 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007089 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007090 N->getValueType(0), Op0.getOperand(0));
7091 return SDValue();
7092}
7093
Bob Wilson31600902010-12-21 06:43:19 +00007094/// PerformSTORECombine - Target-specific dag combine xforms for
7095/// ISD::STORE.
7096static SDValue PerformSTORECombine(SDNode *N,
7097 TargetLowering::DAGCombinerInfo &DCI) {
7098 // Bitcast an i64 store extracted from a vector to f64.
7099 // Otherwise, the i64 value will be legalized to a pair of i32 values.
7100 StoreSDNode *St = cast<StoreSDNode>(N);
7101 SDValue StVal = St->getValue();
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007102 if (!ISD::isNormalStore(St) || St->isVolatile())
7103 return SDValue();
7104
7105 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
7106 StVal.getNode()->hasOneUse() && !St->isVolatile()) {
7107 SelectionDAG &DAG = DCI.DAG;
7108 DebugLoc DL = St->getDebugLoc();
7109 SDValue BasePtr = St->getBasePtr();
7110 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
7111 StVal.getNode()->getOperand(0), BasePtr,
7112 St->getPointerInfo(), St->isVolatile(),
7113 St->isNonTemporal(), St->getAlignment());
7114
7115 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7116 DAG.getConstant(4, MVT::i32));
7117 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
7118 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
7119 St->isNonTemporal(),
7120 std::min(4U, St->getAlignment() / 2));
7121 }
7122
7123 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00007124 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
7125 return SDValue();
7126
7127 SelectionDAG &DAG = DCI.DAG;
7128 DebugLoc dl = StVal.getDebugLoc();
7129 SDValue IntVec = StVal.getOperand(0);
7130 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7131 IntVec.getValueType().getVectorNumElements());
7132 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
7133 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
7134 Vec, StVal.getOperand(1));
7135 dl = N->getDebugLoc();
7136 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
7137 // Make the DAGCombiner fold the bitcasts.
7138 DCI.AddToWorklist(Vec.getNode());
7139 DCI.AddToWorklist(ExtElt.getNode());
7140 DCI.AddToWorklist(V.getNode());
7141 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
7142 St->getPointerInfo(), St->isVolatile(),
7143 St->isNonTemporal(), St->getAlignment(),
7144 St->getTBAAInfo());
7145}
7146
7147/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
7148/// are normal, non-volatile loads. If so, it is profitable to bitcast an
7149/// i64 vector to have f64 elements, since the value can then be loaded
7150/// directly into a VFP register.
7151static bool hasNormalLoadOperand(SDNode *N) {
7152 unsigned NumElts = N->getValueType(0).getVectorNumElements();
7153 for (unsigned i = 0; i < NumElts; ++i) {
7154 SDNode *Elt = N->getOperand(i).getNode();
7155 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
7156 return true;
7157 }
7158 return false;
7159}
7160
Bob Wilson75f02882010-09-17 22:59:05 +00007161/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
7162/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00007163static SDValue PerformBUILD_VECTORCombine(SDNode *N,
7164 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00007165 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
7166 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
7167 // into a pair of GPRs, which is fine when the value is used as a scalar,
7168 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00007169 SelectionDAG &DAG = DCI.DAG;
7170 if (N->getNumOperands() == 2) {
7171 SDValue RV = PerformVMOVDRRCombine(N, DAG);
7172 if (RV.getNode())
7173 return RV;
7174 }
Bob Wilson75f02882010-09-17 22:59:05 +00007175
Bob Wilson31600902010-12-21 06:43:19 +00007176 // Load i64 elements as f64 values so that type legalization does not split
7177 // them up into i32 values.
7178 EVT VT = N->getValueType(0);
7179 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
7180 return SDValue();
7181 DebugLoc dl = N->getDebugLoc();
7182 SmallVector<SDValue, 8> Ops;
7183 unsigned NumElts = VT.getVectorNumElements();
7184 for (unsigned i = 0; i < NumElts; ++i) {
7185 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
7186 Ops.push_back(V);
7187 // Make the DAGCombiner fold the bitcast.
7188 DCI.AddToWorklist(V.getNode());
7189 }
7190 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
7191 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
7192 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
7193}
7194
7195/// PerformInsertEltCombine - Target-specific dag combine xforms for
7196/// ISD::INSERT_VECTOR_ELT.
7197static SDValue PerformInsertEltCombine(SDNode *N,
7198 TargetLowering::DAGCombinerInfo &DCI) {
7199 // Bitcast an i64 load inserted into a vector to f64.
7200 // Otherwise, the i64 value will be legalized to a pair of i32 values.
7201 EVT VT = N->getValueType(0);
7202 SDNode *Elt = N->getOperand(1).getNode();
7203 if (VT.getVectorElementType() != MVT::i64 ||
7204 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
7205 return SDValue();
7206
7207 SelectionDAG &DAG = DCI.DAG;
7208 DebugLoc dl = N->getDebugLoc();
7209 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7210 VT.getVectorNumElements());
7211 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
7212 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
7213 // Make the DAGCombiner fold the bitcasts.
7214 DCI.AddToWorklist(Vec.getNode());
7215 DCI.AddToWorklist(V.getNode());
7216 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
7217 Vec, V, N->getOperand(2));
7218 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00007219}
7220
Bob Wilsonf20700c2010-10-27 20:38:28 +00007221/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
7222/// ISD::VECTOR_SHUFFLE.
7223static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
7224 // The LLVM shufflevector instruction does not require the shuffle mask
7225 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
7226 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
7227 // operands do not match the mask length, they are extended by concatenating
7228 // them with undef vectors. That is probably the right thing for other
7229 // targets, but for NEON it is better to concatenate two double-register
7230 // size vector operands into a single quad-register size vector. Do that
7231 // transformation here:
7232 // shuffle(concat(v1, undef), concat(v2, undef)) ->
7233 // shuffle(concat(v1, v2), undef)
7234 SDValue Op0 = N->getOperand(0);
7235 SDValue Op1 = N->getOperand(1);
7236 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
7237 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
7238 Op0.getNumOperands() != 2 ||
7239 Op1.getNumOperands() != 2)
7240 return SDValue();
7241 SDValue Concat0Op1 = Op0.getOperand(1);
7242 SDValue Concat1Op1 = Op1.getOperand(1);
7243 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
7244 Concat1Op1.getOpcode() != ISD::UNDEF)
7245 return SDValue();
7246 // Skip the transformation if any of the types are illegal.
7247 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7248 EVT VT = N->getValueType(0);
7249 if (!TLI.isTypeLegal(VT) ||
7250 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
7251 !TLI.isTypeLegal(Concat1Op1.getValueType()))
7252 return SDValue();
7253
7254 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
7255 Op0.getOperand(0), Op1.getOperand(0));
7256 // Translate the shuffle mask.
7257 SmallVector<int, 16> NewMask;
7258 unsigned NumElts = VT.getVectorNumElements();
7259 unsigned HalfElts = NumElts/2;
7260 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
7261 for (unsigned n = 0; n < NumElts; ++n) {
7262 int MaskElt = SVN->getMaskElt(n);
7263 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007264 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00007265 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007266 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00007267 NewElt = HalfElts + MaskElt - NumElts;
7268 NewMask.push_back(NewElt);
7269 }
7270 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
7271 DAG.getUNDEF(VT), NewMask.data());
7272}
7273
Bob Wilson1c3ef902011-02-07 17:43:21 +00007274/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
7275/// NEON load/store intrinsics to merge base address updates.
7276static SDValue CombineBaseUpdate(SDNode *N,
7277 TargetLowering::DAGCombinerInfo &DCI) {
7278 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7279 return SDValue();
7280
7281 SelectionDAG &DAG = DCI.DAG;
7282 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
7283 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
7284 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
7285 SDValue Addr = N->getOperand(AddrOpIdx);
7286
7287 // Search for a use of the address operand that is an increment.
7288 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
7289 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
7290 SDNode *User = *UI;
7291 if (User->getOpcode() != ISD::ADD ||
7292 UI.getUse().getResNo() != Addr.getResNo())
7293 continue;
7294
7295 // Check that the add is independent of the load/store. Otherwise, folding
7296 // it would create a cycle.
7297 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
7298 continue;
7299
7300 // Find the new opcode for the updating load/store.
7301 bool isLoad = true;
7302 bool isLaneOp = false;
7303 unsigned NewOpc = 0;
7304 unsigned NumVecs = 0;
7305 if (isIntrinsic) {
7306 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
7307 switch (IntNo) {
7308 default: assert(0 && "unexpected intrinsic for Neon base update");
7309 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
7310 NumVecs = 1; break;
7311 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
7312 NumVecs = 2; break;
7313 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
7314 NumVecs = 3; break;
7315 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
7316 NumVecs = 4; break;
7317 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
7318 NumVecs = 2; isLaneOp = true; break;
7319 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
7320 NumVecs = 3; isLaneOp = true; break;
7321 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
7322 NumVecs = 4; isLaneOp = true; break;
7323 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
7324 NumVecs = 1; isLoad = false; break;
7325 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
7326 NumVecs = 2; isLoad = false; break;
7327 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
7328 NumVecs = 3; isLoad = false; break;
7329 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
7330 NumVecs = 4; isLoad = false; break;
7331 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
7332 NumVecs = 2; isLoad = false; isLaneOp = true; break;
7333 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
7334 NumVecs = 3; isLoad = false; isLaneOp = true; break;
7335 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
7336 NumVecs = 4; isLoad = false; isLaneOp = true; break;
7337 }
7338 } else {
7339 isLaneOp = true;
7340 switch (N->getOpcode()) {
7341 default: assert(0 && "unexpected opcode for Neon base update");
7342 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
7343 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
7344 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
7345 }
7346 }
7347
7348 // Find the size of memory referenced by the load/store.
7349 EVT VecTy;
7350 if (isLoad)
7351 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00007352 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00007353 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
7354 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
7355 if (isLaneOp)
7356 NumBytes /= VecTy.getVectorNumElements();
7357
7358 // If the increment is a constant, it must match the memory ref size.
7359 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
7360 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
7361 uint64_t IncVal = CInc->getZExtValue();
7362 if (IncVal != NumBytes)
7363 continue;
7364 } else if (NumBytes >= 3 * 16) {
7365 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
7366 // separate instructions that make it harder to use a non-constant update.
7367 continue;
7368 }
7369
7370 // Create the new updating load/store node.
7371 EVT Tys[6];
7372 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
7373 unsigned n;
7374 for (n = 0; n < NumResultVecs; ++n)
7375 Tys[n] = VecTy;
7376 Tys[n++] = MVT::i32;
7377 Tys[n] = MVT::Other;
7378 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
7379 SmallVector<SDValue, 8> Ops;
7380 Ops.push_back(N->getOperand(0)); // incoming chain
7381 Ops.push_back(N->getOperand(AddrOpIdx));
7382 Ops.push_back(Inc);
7383 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
7384 Ops.push_back(N->getOperand(i));
7385 }
7386 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
7387 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
7388 Ops.data(), Ops.size(),
7389 MemInt->getMemoryVT(),
7390 MemInt->getMemOperand());
7391
7392 // Update the uses.
7393 std::vector<SDValue> NewResults;
7394 for (unsigned i = 0; i < NumResultVecs; ++i) {
7395 NewResults.push_back(SDValue(UpdN.getNode(), i));
7396 }
7397 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
7398 DCI.CombineTo(N, NewResults);
7399 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
7400
7401 break;
Owen Anderson76706012011-04-05 21:48:57 +00007402 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00007403 return SDValue();
7404}
7405
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007406/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
7407/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
7408/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
7409/// return true.
7410static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
7411 SelectionDAG &DAG = DCI.DAG;
7412 EVT VT = N->getValueType(0);
7413 // vldN-dup instructions only support 64-bit vectors for N > 1.
7414 if (!VT.is64BitVector())
7415 return false;
7416
7417 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
7418 SDNode *VLD = N->getOperand(0).getNode();
7419 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
7420 return false;
7421 unsigned NumVecs = 0;
7422 unsigned NewOpc = 0;
7423 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
7424 if (IntNo == Intrinsic::arm_neon_vld2lane) {
7425 NumVecs = 2;
7426 NewOpc = ARMISD::VLD2DUP;
7427 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
7428 NumVecs = 3;
7429 NewOpc = ARMISD::VLD3DUP;
7430 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
7431 NumVecs = 4;
7432 NewOpc = ARMISD::VLD4DUP;
7433 } else {
7434 return false;
7435 }
7436
7437 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
7438 // numbers match the load.
7439 unsigned VLDLaneNo =
7440 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
7441 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
7442 UI != UE; ++UI) {
7443 // Ignore uses of the chain result.
7444 if (UI.getUse().getResNo() == NumVecs)
7445 continue;
7446 SDNode *User = *UI;
7447 if (User->getOpcode() != ARMISD::VDUPLANE ||
7448 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
7449 return false;
7450 }
7451
7452 // Create the vldN-dup node.
7453 EVT Tys[5];
7454 unsigned n;
7455 for (n = 0; n < NumVecs; ++n)
7456 Tys[n] = VT;
7457 Tys[n] = MVT::Other;
7458 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
7459 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
7460 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
7461 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
7462 Ops, 2, VLDMemInt->getMemoryVT(),
7463 VLDMemInt->getMemOperand());
7464
7465 // Update the uses.
7466 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
7467 UI != UE; ++UI) {
7468 unsigned ResNo = UI.getUse().getResNo();
7469 // Ignore uses of the chain result.
7470 if (ResNo == NumVecs)
7471 continue;
7472 SDNode *User = *UI;
7473 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
7474 }
7475
7476 // Now the vldN-lane intrinsic is dead except for its chain result.
7477 // Update uses of the chain.
7478 std::vector<SDValue> VLDDupResults;
7479 for (unsigned n = 0; n < NumVecs; ++n)
7480 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
7481 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
7482 DCI.CombineTo(VLD, VLDDupResults);
7483
7484 return true;
7485}
7486
Bob Wilson9e82bf12010-07-14 01:22:12 +00007487/// PerformVDUPLANECombine - Target-specific dag combine xforms for
7488/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007489static SDValue PerformVDUPLANECombine(SDNode *N,
7490 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00007491 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007492
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007493 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
7494 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
7495 if (CombineVLDDUP(N, DCI))
7496 return SDValue(N, 0);
7497
7498 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
7499 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007500 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00007501 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00007502 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00007503 return SDValue();
7504
7505 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
7506 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
7507 // The canonical VMOV for a zero vector uses a 32-bit element size.
7508 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7509 unsigned EltBits;
7510 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
7511 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007512 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007513 if (EltSize > VT.getVectorElementType().getSizeInBits())
7514 return SDValue();
7515
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007516 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007517}
7518
Eric Christopherfa6f5912011-06-29 21:10:36 +00007519// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00007520// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
7521static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
7522{
Chad Rosier118c9a02011-06-28 17:26:57 +00007523 integerPart cN;
7524 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00007525 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
7526 I != E; I++) {
7527 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
7528 if (!C)
7529 return false;
7530
Eric Christopherfa6f5912011-06-29 21:10:36 +00007531 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00007532 APFloat APF = C->getValueAPF();
7533 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
7534 != APFloat::opOK || !isExact)
7535 return false;
7536
7537 c0 = (I == 0) ? cN : c0;
7538 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
7539 return false;
7540 }
7541 C = c0;
7542 return true;
7543}
7544
7545/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
7546/// can replace combinations of VMUL and VCVT (floating-point to integer)
7547/// when the VMUL has a constant operand that is a power of 2.
7548///
7549/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
7550/// vmul.f32 d16, d17, d16
7551/// vcvt.s32.f32 d16, d16
7552/// becomes:
7553/// vcvt.s32.f32 d16, d16, #3
7554static SDValue PerformVCVTCombine(SDNode *N,
7555 TargetLowering::DAGCombinerInfo &DCI,
7556 const ARMSubtarget *Subtarget) {
7557 SelectionDAG &DAG = DCI.DAG;
7558 SDValue Op = N->getOperand(0);
7559
7560 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
7561 Op.getOpcode() != ISD::FMUL)
7562 return SDValue();
7563
7564 uint64_t C;
7565 SDValue N0 = Op->getOperand(0);
7566 SDValue ConstVec = Op->getOperand(1);
7567 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
7568
Eric Christopherfa6f5912011-06-29 21:10:36 +00007569 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00007570 !isConstVecPow2(ConstVec, isSigned, C))
7571 return SDValue();
7572
7573 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
7574 Intrinsic::arm_neon_vcvtfp2fxu;
7575 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7576 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00007577 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00007578 DAG.getConstant(Log2_64(C), MVT::i32));
7579}
7580
7581/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
7582/// can replace combinations of VCVT (integer to floating-point) and VDIV
7583/// when the VDIV has a constant operand that is a power of 2.
7584///
7585/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
7586/// vcvt.f32.s32 d16, d16
7587/// vdiv.f32 d16, d17, d16
7588/// becomes:
7589/// vcvt.f32.s32 d16, d16, #3
7590static SDValue PerformVDIVCombine(SDNode *N,
7591 TargetLowering::DAGCombinerInfo &DCI,
7592 const ARMSubtarget *Subtarget) {
7593 SelectionDAG &DAG = DCI.DAG;
7594 SDValue Op = N->getOperand(0);
7595 unsigned OpOpcode = Op.getNode()->getOpcode();
7596
7597 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
7598 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
7599 return SDValue();
7600
7601 uint64_t C;
7602 SDValue ConstVec = N->getOperand(1);
7603 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
7604
7605 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
7606 !isConstVecPow2(ConstVec, isSigned, C))
7607 return SDValue();
7608
Eric Christopherfa6f5912011-06-29 21:10:36 +00007609 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00007610 Intrinsic::arm_neon_vcvtfxu2fp;
7611 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7612 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00007613 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00007614 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
7615}
7616
7617/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00007618/// operand of a vector shift operation, where all the elements of the
7619/// build_vector must have the same constant integer value.
7620static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
7621 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007622 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00007623 Op = Op.getOperand(0);
7624 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
7625 APInt SplatBits, SplatUndef;
7626 unsigned SplatBitSize;
7627 bool HasAnyUndefs;
7628 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
7629 HasAnyUndefs, ElementBits) ||
7630 SplatBitSize > ElementBits)
7631 return false;
7632 Cnt = SplatBits.getSExtValue();
7633 return true;
7634}
7635
7636/// isVShiftLImm - Check if this is a valid build_vector for the immediate
7637/// operand of a vector shift left operation. That value must be in the range:
7638/// 0 <= Value < ElementBits for a left shift; or
7639/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00007640static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00007641 assert(VT.isVector() && "vector shift count is not a vector type");
7642 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
7643 if (! getVShiftImm(Op, ElementBits, Cnt))
7644 return false;
7645 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
7646}
7647
7648/// isVShiftRImm - Check if this is a valid build_vector for the immediate
7649/// operand of a vector shift right operation. For a shift opcode, the value
7650/// is positive, but for an intrinsic the value count must be negative. The
7651/// absolute value must be in the range:
7652/// 1 <= |Value| <= ElementBits for a right shift; or
7653/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00007654static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00007655 int64_t &Cnt) {
7656 assert(VT.isVector() && "vector shift count is not a vector type");
7657 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
7658 if (! getVShiftImm(Op, ElementBits, Cnt))
7659 return false;
7660 if (isIntrinsic)
7661 Cnt = -Cnt;
7662 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
7663}
7664
7665/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
7666static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
7667 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
7668 switch (IntNo) {
7669 default:
7670 // Don't do anything for most intrinsics.
7671 break;
7672
7673 // Vector shifts: check for immediate versions and lower them.
7674 // Note: This is done during DAG combining instead of DAG legalizing because
7675 // the build_vectors for 64-bit vector element shift counts are generally
7676 // not legal, and it is hard to see their values after they get legalized to
7677 // loads from a constant pool.
7678 case Intrinsic::arm_neon_vshifts:
7679 case Intrinsic::arm_neon_vshiftu:
7680 case Intrinsic::arm_neon_vshiftls:
7681 case Intrinsic::arm_neon_vshiftlu:
7682 case Intrinsic::arm_neon_vshiftn:
7683 case Intrinsic::arm_neon_vrshifts:
7684 case Intrinsic::arm_neon_vrshiftu:
7685 case Intrinsic::arm_neon_vrshiftn:
7686 case Intrinsic::arm_neon_vqshifts:
7687 case Intrinsic::arm_neon_vqshiftu:
7688 case Intrinsic::arm_neon_vqshiftsu:
7689 case Intrinsic::arm_neon_vqshiftns:
7690 case Intrinsic::arm_neon_vqshiftnu:
7691 case Intrinsic::arm_neon_vqshiftnsu:
7692 case Intrinsic::arm_neon_vqrshiftns:
7693 case Intrinsic::arm_neon_vqrshiftnu:
7694 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00007695 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007696 int64_t Cnt;
7697 unsigned VShiftOpc = 0;
7698
7699 switch (IntNo) {
7700 case Intrinsic::arm_neon_vshifts:
7701 case Intrinsic::arm_neon_vshiftu:
7702 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
7703 VShiftOpc = ARMISD::VSHL;
7704 break;
7705 }
7706 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
7707 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
7708 ARMISD::VSHRs : ARMISD::VSHRu);
7709 break;
7710 }
7711 return SDValue();
7712
7713 case Intrinsic::arm_neon_vshiftls:
7714 case Intrinsic::arm_neon_vshiftlu:
7715 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
7716 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007717 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007718
7719 case Intrinsic::arm_neon_vrshifts:
7720 case Intrinsic::arm_neon_vrshiftu:
7721 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
7722 break;
7723 return SDValue();
7724
7725 case Intrinsic::arm_neon_vqshifts:
7726 case Intrinsic::arm_neon_vqshiftu:
7727 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
7728 break;
7729 return SDValue();
7730
7731 case Intrinsic::arm_neon_vqshiftsu:
7732 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
7733 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007734 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007735
7736 case Intrinsic::arm_neon_vshiftn:
7737 case Intrinsic::arm_neon_vrshiftn:
7738 case Intrinsic::arm_neon_vqshiftns:
7739 case Intrinsic::arm_neon_vqshiftnu:
7740 case Intrinsic::arm_neon_vqshiftnsu:
7741 case Intrinsic::arm_neon_vqrshiftns:
7742 case Intrinsic::arm_neon_vqrshiftnu:
7743 case Intrinsic::arm_neon_vqrshiftnsu:
7744 // Narrowing shifts require an immediate right shift.
7745 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
7746 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00007747 llvm_unreachable("invalid shift count for narrowing vector shift "
7748 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007749
7750 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00007751 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00007752 }
7753
7754 switch (IntNo) {
7755 case Intrinsic::arm_neon_vshifts:
7756 case Intrinsic::arm_neon_vshiftu:
7757 // Opcode already set above.
7758 break;
7759 case Intrinsic::arm_neon_vshiftls:
7760 case Intrinsic::arm_neon_vshiftlu:
7761 if (Cnt == VT.getVectorElementType().getSizeInBits())
7762 VShiftOpc = ARMISD::VSHLLi;
7763 else
7764 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
7765 ARMISD::VSHLLs : ARMISD::VSHLLu);
7766 break;
7767 case Intrinsic::arm_neon_vshiftn:
7768 VShiftOpc = ARMISD::VSHRN; break;
7769 case Intrinsic::arm_neon_vrshifts:
7770 VShiftOpc = ARMISD::VRSHRs; break;
7771 case Intrinsic::arm_neon_vrshiftu:
7772 VShiftOpc = ARMISD::VRSHRu; break;
7773 case Intrinsic::arm_neon_vrshiftn:
7774 VShiftOpc = ARMISD::VRSHRN; break;
7775 case Intrinsic::arm_neon_vqshifts:
7776 VShiftOpc = ARMISD::VQSHLs; break;
7777 case Intrinsic::arm_neon_vqshiftu:
7778 VShiftOpc = ARMISD::VQSHLu; break;
7779 case Intrinsic::arm_neon_vqshiftsu:
7780 VShiftOpc = ARMISD::VQSHLsu; break;
7781 case Intrinsic::arm_neon_vqshiftns:
7782 VShiftOpc = ARMISD::VQSHRNs; break;
7783 case Intrinsic::arm_neon_vqshiftnu:
7784 VShiftOpc = ARMISD::VQSHRNu; break;
7785 case Intrinsic::arm_neon_vqshiftnsu:
7786 VShiftOpc = ARMISD::VQSHRNsu; break;
7787 case Intrinsic::arm_neon_vqrshiftns:
7788 VShiftOpc = ARMISD::VQRSHRNs; break;
7789 case Intrinsic::arm_neon_vqrshiftnu:
7790 VShiftOpc = ARMISD::VQRSHRNu; break;
7791 case Intrinsic::arm_neon_vqrshiftnsu:
7792 VShiftOpc = ARMISD::VQRSHRNsu; break;
7793 }
7794
7795 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007796 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007797 }
7798
7799 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00007800 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007801 int64_t Cnt;
7802 unsigned VShiftOpc = 0;
7803
7804 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
7805 VShiftOpc = ARMISD::VSLI;
7806 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
7807 VShiftOpc = ARMISD::VSRI;
7808 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00007809 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007810 }
7811
7812 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
7813 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00007814 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007815 }
7816
7817 case Intrinsic::arm_neon_vqrshifts:
7818 case Intrinsic::arm_neon_vqrshiftu:
7819 // No immediate versions of these to check for.
7820 break;
7821 }
7822
7823 return SDValue();
7824}
7825
7826/// PerformShiftCombine - Checks for immediate versions of vector shifts and
7827/// lowers them. As with the vector shift intrinsics, this is done during DAG
7828/// combining instead of DAG legalizing because the build_vectors for 64-bit
7829/// vector element shift counts are generally not legal, and it is hard to see
7830/// their values after they get legalized to loads from a constant pool.
7831static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
7832 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00007833 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00007834
7835 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00007836 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7837 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00007838 return SDValue();
7839
7840 assert(ST->hasNEON() && "unexpected vector shift");
7841 int64_t Cnt;
7842
7843 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007844 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00007845
7846 case ISD::SHL:
7847 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
7848 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007849 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007850 break;
7851
7852 case ISD::SRA:
7853 case ISD::SRL:
7854 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
7855 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
7856 ARMISD::VSHRs : ARMISD::VSHRu);
7857 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007858 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007859 }
7860 }
7861 return SDValue();
7862}
7863
7864/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
7865/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
7866static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
7867 const ARMSubtarget *ST) {
7868 SDValue N0 = N->getOperand(0);
7869
7870 // Check for sign- and zero-extensions of vector extract operations of 8-
7871 // and 16-bit vector elements. NEON supports these directly. They are
7872 // handled during DAG combining because type legalization will promote them
7873 // to 32-bit types and it is messy to recognize the operations after that.
7874 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
7875 SDValue Vec = N0.getOperand(0);
7876 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00007877 EVT VT = N->getValueType(0);
7878 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007879 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7880
Owen Anderson825b72b2009-08-11 20:47:22 +00007881 if (VT == MVT::i32 &&
7882 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00007883 TLI.isTypeLegal(Vec.getValueType()) &&
7884 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00007885
7886 unsigned Opc = 0;
7887 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007888 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00007889 case ISD::SIGN_EXTEND:
7890 Opc = ARMISD::VGETLANEs;
7891 break;
7892 case ISD::ZERO_EXTEND:
7893 case ISD::ANY_EXTEND:
7894 Opc = ARMISD::VGETLANEu;
7895 break;
7896 }
7897 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
7898 }
7899 }
7900
7901 return SDValue();
7902}
7903
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007904/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
7905/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
7906static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
7907 const ARMSubtarget *ST) {
7908 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00007909 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007910 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
7911 // a NaN; only do the transformation when it matches that behavior.
7912
7913 // For now only do this when using NEON for FP operations; if using VFP, it
7914 // is not obvious that the benefit outweighs the cost of switching to the
7915 // NEON pipeline.
7916 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
7917 N->getValueType(0) != MVT::f32)
7918 return SDValue();
7919
7920 SDValue CondLHS = N->getOperand(0);
7921 SDValue CondRHS = N->getOperand(1);
7922 SDValue LHS = N->getOperand(2);
7923 SDValue RHS = N->getOperand(3);
7924 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
7925
7926 unsigned Opcode = 0;
7927 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00007928 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007929 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00007930 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007931 IsReversed = true ; // x CC y ? y : x
7932 } else {
7933 return SDValue();
7934 }
7935
Bob Wilsone742bb52010-02-24 22:15:53 +00007936 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007937 switch (CC) {
7938 default: break;
7939 case ISD::SETOLT:
7940 case ISD::SETOLE:
7941 case ISD::SETLT:
7942 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007943 case ISD::SETULT:
7944 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00007945 // If LHS is NaN, an ordered comparison will be false and the result will
7946 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
7947 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
7948 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
7949 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
7950 break;
7951 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
7952 // will return -0, so vmin can only be used for unsafe math or if one of
7953 // the operands is known to be nonzero.
7954 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00007955 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00007956 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
7957 break;
7958 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007959 break;
7960
7961 case ISD::SETOGT:
7962 case ISD::SETOGE:
7963 case ISD::SETGT:
7964 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007965 case ISD::SETUGT:
7966 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00007967 // If LHS is NaN, an ordered comparison will be false and the result will
7968 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
7969 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
7970 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
7971 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
7972 break;
7973 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
7974 // will return +0, so vmax can only be used for unsafe math or if one of
7975 // the operands is known to be nonzero.
7976 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00007977 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00007978 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
7979 break;
7980 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007981 break;
7982 }
7983
7984 if (!Opcode)
7985 return SDValue();
7986 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
7987}
7988
Evan Chenge721f5c2011-07-13 00:42:17 +00007989/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
7990SDValue
7991ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
7992 SDValue Cmp = N->getOperand(4);
7993 if (Cmp.getOpcode() != ARMISD::CMPZ)
7994 // Only looking at EQ and NE cases.
7995 return SDValue();
7996
7997 EVT VT = N->getValueType(0);
7998 DebugLoc dl = N->getDebugLoc();
7999 SDValue LHS = Cmp.getOperand(0);
8000 SDValue RHS = Cmp.getOperand(1);
8001 SDValue FalseVal = N->getOperand(0);
8002 SDValue TrueVal = N->getOperand(1);
8003 SDValue ARMcc = N->getOperand(2);
Jim Grosbachb04546f2011-09-13 20:30:37 +00008004 ARMCC::CondCodes CC =
8005 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chenge721f5c2011-07-13 00:42:17 +00008006
8007 // Simplify
8008 // mov r1, r0
8009 // cmp r1, x
8010 // mov r0, y
8011 // moveq r0, x
8012 // to
8013 // cmp r0, x
8014 // movne r0, y
8015 //
8016 // mov r1, r0
8017 // cmp r1, x
8018 // mov r0, x
8019 // movne r0, y
8020 // to
8021 // cmp r0, x
8022 // movne r0, y
8023 /// FIXME: Turn this into a target neutral optimization?
8024 SDValue Res;
Evan Cheng9b88d2d2011-09-28 23:16:31 +00008025 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chenge721f5c2011-07-13 00:42:17 +00008026 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
8027 N->getOperand(3), Cmp);
8028 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
8029 SDValue ARMcc;
8030 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
8031 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
8032 N->getOperand(3), NewCmp);
8033 }
8034
8035 if (Res.getNode()) {
8036 APInt KnownZero, KnownOne;
8037 APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits());
8038 DAG.ComputeMaskedBits(SDValue(N,0), Mask, KnownZero, KnownOne);
8039 // Capture demanded bits information that would be otherwise lost.
8040 if (KnownZero == 0xfffffffe)
8041 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8042 DAG.getValueType(MVT::i1));
8043 else if (KnownZero == 0xffffff00)
8044 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8045 DAG.getValueType(MVT::i8));
8046 else if (KnownZero == 0xffff0000)
8047 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8048 DAG.getValueType(MVT::i16));
8049 }
8050
8051 return Res;
8052}
8053
Dan Gohman475871a2008-07-27 21:46:04 +00008054SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008055 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008056 switch (N->getOpcode()) {
8057 default: break;
Tanya Lattner189531f2011-06-14 23:48:48 +00008058 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008059 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00008060 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008061 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00008062 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00008063 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00008064 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008065 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00008066 case ISD::STORE: return PerformSTORECombine(N, DCI);
8067 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
8068 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00008069 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008070 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00008071 case ISD::FP_TO_SINT:
8072 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
8073 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008074 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00008075 case ISD::SHL:
8076 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008077 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00008078 case ISD::SIGN_EXTEND:
8079 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008080 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
8081 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00008082 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00008083 case ARMISD::VLD2DUP:
8084 case ARMISD::VLD3DUP:
8085 case ARMISD::VLD4DUP:
8086 return CombineBaseUpdate(N, DCI);
8087 case ISD::INTRINSIC_VOID:
8088 case ISD::INTRINSIC_W_CHAIN:
8089 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
8090 case Intrinsic::arm_neon_vld1:
8091 case Intrinsic::arm_neon_vld2:
8092 case Intrinsic::arm_neon_vld3:
8093 case Intrinsic::arm_neon_vld4:
8094 case Intrinsic::arm_neon_vld2lane:
8095 case Intrinsic::arm_neon_vld3lane:
8096 case Intrinsic::arm_neon_vld4lane:
8097 case Intrinsic::arm_neon_vst1:
8098 case Intrinsic::arm_neon_vst2:
8099 case Intrinsic::arm_neon_vst3:
8100 case Intrinsic::arm_neon_vst4:
8101 case Intrinsic::arm_neon_vst2lane:
8102 case Intrinsic::arm_neon_vst3lane:
8103 case Intrinsic::arm_neon_vst4lane:
8104 return CombineBaseUpdate(N, DCI);
8105 default: break;
8106 }
8107 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008108 }
Dan Gohman475871a2008-07-27 21:46:04 +00008109 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008110}
8111
Evan Cheng31959b12011-02-02 01:06:55 +00008112bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
8113 EVT VT) const {
8114 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
8115}
8116
Bill Wendlingaf566342009-08-15 21:21:19 +00008117bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00008118 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00008119 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00008120
8121 switch (VT.getSimpleVT().SimpleTy) {
8122 default:
8123 return false;
8124 case MVT::i8:
8125 case MVT::i16:
8126 case MVT::i32:
8127 return true;
8128 // FIXME: VLD1 etc with standard alignment is legal.
8129 }
8130}
8131
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008132static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
8133 unsigned AlignCheck) {
8134 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
8135 (DstAlign == 0 || DstAlign % AlignCheck == 0));
8136}
8137
8138EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
8139 unsigned DstAlign, unsigned SrcAlign,
Lang Hamesa1e78882011-11-02 23:37:04 +00008140 bool IsZeroVal,
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008141 bool MemcpyStrSrc,
8142 MachineFunction &MF) const {
8143 const Function *F = MF.getFunction();
8144
8145 // See if we can use NEON instructions for this...
Lang Hamesa1e78882011-11-02 23:37:04 +00008146 if (IsZeroVal &&
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008147 !F->hasFnAttr(Attribute::NoImplicitFloat) &&
8148 Subtarget->hasNEON()) {
8149 if (memOpAlign(SrcAlign, DstAlign, 16) && Size >= 16) {
8150 return MVT::v4i32;
8151 } else if (memOpAlign(SrcAlign, DstAlign, 8) && Size >= 8) {
8152 return MVT::v2i32;
8153 }
8154 }
8155
Lang Hames5207bf22011-11-08 18:56:23 +00008156 // Lowering to i32/i16 if the size permits.
8157 if (Size >= 4) {
8158 return MVT::i32;
8159 } else if (Size >= 2) {
8160 return MVT::i16;
8161 }
8162
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008163 // Let the target-independent logic figure it out.
8164 return MVT::Other;
8165}
8166
Evan Chenge6c835f2009-08-14 20:09:37 +00008167static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
8168 if (V < 0)
8169 return false;
8170
8171 unsigned Scale = 1;
8172 switch (VT.getSimpleVT().SimpleTy) {
8173 default: return false;
8174 case MVT::i1:
8175 case MVT::i8:
8176 // Scale == 1;
8177 break;
8178 case MVT::i16:
8179 // Scale == 2;
8180 Scale = 2;
8181 break;
8182 case MVT::i32:
8183 // Scale == 4;
8184 Scale = 4;
8185 break;
8186 }
8187
8188 if ((V & (Scale - 1)) != 0)
8189 return false;
8190 V /= Scale;
8191 return V == (V & ((1LL << 5) - 1));
8192}
8193
8194static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
8195 const ARMSubtarget *Subtarget) {
8196 bool isNeg = false;
8197 if (V < 0) {
8198 isNeg = true;
8199 V = - V;
8200 }
8201
8202 switch (VT.getSimpleVT().SimpleTy) {
8203 default: return false;
8204 case MVT::i1:
8205 case MVT::i8:
8206 case MVT::i16:
8207 case MVT::i32:
8208 // + imm12 or - imm8
8209 if (isNeg)
8210 return V == (V & ((1LL << 8) - 1));
8211 return V == (V & ((1LL << 12) - 1));
8212 case MVT::f32:
8213 case MVT::f64:
8214 // Same as ARM mode. FIXME: NEON?
8215 if (!Subtarget->hasVFP2())
8216 return false;
8217 if ((V & 3) != 0)
8218 return false;
8219 V >>= 2;
8220 return V == (V & ((1LL << 8) - 1));
8221 }
8222}
8223
Evan Chengb01fad62007-03-12 23:30:29 +00008224/// isLegalAddressImmediate - Return true if the integer value can be used
8225/// as the offset of the target addressing mode for load / store of the
8226/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00008227static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00008228 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00008229 if (V == 0)
8230 return true;
8231
Evan Cheng65011532009-03-09 19:15:00 +00008232 if (!VT.isSimple())
8233 return false;
8234
Evan Chenge6c835f2009-08-14 20:09:37 +00008235 if (Subtarget->isThumb1Only())
8236 return isLegalT1AddressImmediate(V, VT);
8237 else if (Subtarget->isThumb2())
8238 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00008239
Evan Chenge6c835f2009-08-14 20:09:37 +00008240 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00008241 if (V < 0)
8242 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00008243 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00008244 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008245 case MVT::i1:
8246 case MVT::i8:
8247 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00008248 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008249 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008250 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00008251 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008252 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008253 case MVT::f32:
8254 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00008255 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00008256 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00008257 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00008258 return false;
8259 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008260 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00008261 }
Evan Chenga8e29892007-01-19 07:51:42 +00008262}
8263
Evan Chenge6c835f2009-08-14 20:09:37 +00008264bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
8265 EVT VT) const {
8266 int Scale = AM.Scale;
8267 if (Scale < 0)
8268 return false;
8269
8270 switch (VT.getSimpleVT().SimpleTy) {
8271 default: return false;
8272 case MVT::i1:
8273 case MVT::i8:
8274 case MVT::i16:
8275 case MVT::i32:
8276 if (Scale == 1)
8277 return true;
8278 // r + r << imm
8279 Scale = Scale & ~1;
8280 return Scale == 2 || Scale == 4 || Scale == 8;
8281 case MVT::i64:
8282 // r + r
8283 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
8284 return true;
8285 return false;
8286 case MVT::isVoid:
8287 // Note, we allow "void" uses (basically, uses that aren't loads or
8288 // stores), because arm allows folding a scale into many arithmetic
8289 // operations. This should be made more precise and revisited later.
8290
8291 // Allow r << imm, but the imm has to be a multiple of two.
8292 if (Scale & 1) return false;
8293 return isPowerOf2_32(Scale);
8294 }
8295}
8296
Chris Lattner37caf8c2007-04-09 23:33:39 +00008297/// isLegalAddressingMode - Return true if the addressing mode represented
8298/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008299bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008300 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008301 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00008302 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00008303 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008304
Chris Lattner37caf8c2007-04-09 23:33:39 +00008305 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008306 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008307 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008308
Chris Lattner37caf8c2007-04-09 23:33:39 +00008309 switch (AM.Scale) {
8310 case 0: // no scale reg, must be "r+i" or "r", or "i".
8311 break;
8312 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00008313 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00008314 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008315 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00008316 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008317 // ARM doesn't support any R+R*scale+imm addr modes.
8318 if (AM.BaseOffs)
8319 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008320
Bob Wilson2c7dab12009-04-08 17:55:28 +00008321 if (!VT.isSimple())
8322 return false;
8323
Evan Chenge6c835f2009-08-14 20:09:37 +00008324 if (Subtarget->isThumb2())
8325 return isLegalT2ScaledAddressingMode(AM, VT);
8326
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008327 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00008328 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00008329 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008330 case MVT::i1:
8331 case MVT::i8:
8332 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008333 if (Scale < 0) Scale = -Scale;
8334 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008335 return true;
8336 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00008337 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008338 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00008339 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00008340 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008341 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008342 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00008343 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008344
Owen Anderson825b72b2009-08-11 20:47:22 +00008345 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00008346 // Note, we allow "void" uses (basically, uses that aren't loads or
8347 // stores), because arm allows folding a scale into many arithmetic
8348 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008349
Chris Lattner37caf8c2007-04-09 23:33:39 +00008350 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00008351 if (Scale & 1) return false;
8352 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00008353 }
8354 break;
Evan Chengb01fad62007-03-12 23:30:29 +00008355 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00008356 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00008357}
8358
Evan Cheng77e47512009-11-11 19:05:52 +00008359/// isLegalICmpImmediate - Return true if the specified immediate is legal
8360/// icmp immediate, that is the target has icmp instructions which can compare
8361/// a register against the immediate without having to materialize the
8362/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00008363bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00008364 if (!Subtarget->isThumb())
8365 return ARM_AM::getSOImmVal(Imm) != -1;
8366 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00008367 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00008368 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00008369}
8370
Dan Gohmancca82142011-05-03 00:46:49 +00008371/// isLegalAddImmediate - Return true if the specified immediate is legal
8372/// add immediate, that is the target has add instructions which can add
8373/// a register with the immediate without having to materialize the
8374/// immediate into a register.
8375bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
8376 return ARM_AM::getSOImmVal(Imm) != -1;
8377}
8378
Owen Andersone50ed302009-08-10 22:56:29 +00008379static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00008380 bool isSEXTLoad, SDValue &Base,
8381 SDValue &Offset, bool &isInc,
8382 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00008383 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
8384 return false;
8385
Owen Anderson825b72b2009-08-11 20:47:22 +00008386 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00008387 // AddressingMode 3
8388 Base = Ptr->getOperand(0);
8389 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008390 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00008391 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008392 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00008393 isInc = false;
8394 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8395 return true;
8396 }
8397 }
8398 isInc = (Ptr->getOpcode() == ISD::ADD);
8399 Offset = Ptr->getOperand(1);
8400 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00008401 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00008402 // AddressingMode 2
8403 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008404 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00008405 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008406 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00008407 isInc = false;
8408 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8409 Base = Ptr->getOperand(0);
8410 return true;
8411 }
8412 }
8413
8414 if (Ptr->getOpcode() == ISD::ADD) {
8415 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00008416 ARM_AM::ShiftOpc ShOpcVal=
8417 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00008418 if (ShOpcVal != ARM_AM::no_shift) {
8419 Base = Ptr->getOperand(1);
8420 Offset = Ptr->getOperand(0);
8421 } else {
8422 Base = Ptr->getOperand(0);
8423 Offset = Ptr->getOperand(1);
8424 }
8425 return true;
8426 }
8427
8428 isInc = (Ptr->getOpcode() == ISD::ADD);
8429 Base = Ptr->getOperand(0);
8430 Offset = Ptr->getOperand(1);
8431 return true;
8432 }
8433
Jim Grosbache5165492009-11-09 00:11:35 +00008434 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00008435 return false;
8436}
8437
Owen Andersone50ed302009-08-10 22:56:29 +00008438static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00008439 bool isSEXTLoad, SDValue &Base,
8440 SDValue &Offset, bool &isInc,
8441 SelectionDAG &DAG) {
8442 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
8443 return false;
8444
8445 Base = Ptr->getOperand(0);
8446 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
8447 int RHSC = (int)RHS->getZExtValue();
8448 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
8449 assert(Ptr->getOpcode() == ISD::ADD);
8450 isInc = false;
8451 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8452 return true;
8453 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
8454 isInc = Ptr->getOpcode() == ISD::ADD;
8455 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
8456 return true;
8457 }
8458 }
8459
8460 return false;
8461}
8462
Evan Chenga8e29892007-01-19 07:51:42 +00008463/// getPreIndexedAddressParts - returns true by value, base pointer and
8464/// offset pointer and addressing mode by reference if the node's address
8465/// can be legally represented as pre-indexed load / store address.
8466bool
Dan Gohman475871a2008-07-27 21:46:04 +00008467ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
8468 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00008469 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00008470 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008471 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00008472 return false;
8473
Owen Andersone50ed302009-08-10 22:56:29 +00008474 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00008475 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00008476 bool isSEXTLoad = false;
8477 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
8478 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008479 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00008480 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
8481 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
8482 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008483 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00008484 } else
8485 return false;
8486
8487 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00008488 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00008489 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00008490 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
8491 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00008492 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00008493 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00008494 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00008495 if (!isLegal)
8496 return false;
8497
8498 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
8499 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00008500}
8501
8502/// getPostIndexedAddressParts - returns true by value, base pointer and
8503/// offset pointer and addressing mode by reference if this node can be
8504/// combined with a load / store to form a post-indexed load / store.
8505bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00008506 SDValue &Base,
8507 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00008508 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00008509 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008510 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00008511 return false;
8512
Owen Andersone50ed302009-08-10 22:56:29 +00008513 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00008514 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00008515 bool isSEXTLoad = false;
8516 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008517 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00008518 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00008519 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
8520 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008521 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00008522 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00008523 } else
8524 return false;
8525
8526 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00008527 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00008528 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00008529 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00008530 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00008531 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00008532 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
8533 isInc, DAG);
8534 if (!isLegal)
8535 return false;
8536
Evan Cheng28dad2a2010-05-18 21:31:17 +00008537 if (Ptr != Base) {
8538 // Swap base ptr and offset to catch more post-index load / store when
8539 // it's legal. In Thumb2 mode, offset must be an immediate.
8540 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
8541 !Subtarget->isThumb2())
8542 std::swap(Base, Offset);
8543
8544 // Post-indexed load / store update the base pointer.
8545 if (Ptr != Base)
8546 return false;
8547 }
8548
Evan Chenge88d5ce2009-07-02 07:28:31 +00008549 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
8550 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00008551}
8552
Dan Gohman475871a2008-07-27 21:46:04 +00008553void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00008554 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008555 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008556 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008557 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00008558 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008559 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00008560 switch (Op.getOpcode()) {
8561 default: break;
8562 case ARMISD::CMOV: {
8563 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00008564 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00008565 if (KnownZero == 0 && KnownOne == 0) return;
8566
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008567 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00008568 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
8569 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00008570 KnownZero &= KnownZeroRHS;
8571 KnownOne &= KnownOneRHS;
8572 return;
8573 }
8574 }
8575}
8576
8577//===----------------------------------------------------------------------===//
8578// ARM Inline Assembly Support
8579//===----------------------------------------------------------------------===//
8580
Evan Cheng55d42002011-01-08 01:24:27 +00008581bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
8582 // Looking for "rev" which is V6+.
8583 if (!Subtarget->hasV6Ops())
8584 return false;
8585
8586 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
8587 std::string AsmStr = IA->getAsmString();
8588 SmallVector<StringRef, 4> AsmPieces;
8589 SplitString(AsmStr, AsmPieces, ";\n");
8590
8591 switch (AsmPieces.size()) {
8592 default: return false;
8593 case 1:
8594 AsmStr = AsmPieces[0];
8595 AsmPieces.clear();
8596 SplitString(AsmStr, AsmPieces, " \t,");
8597
8598 // rev $0, $1
8599 if (AsmPieces.size() == 3 &&
8600 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
8601 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008602 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00008603 if (Ty && Ty->getBitWidth() == 32)
8604 return IntrinsicLowering::LowerToByteSwap(CI);
8605 }
8606 break;
8607 }
8608
8609 return false;
8610}
8611
Evan Chenga8e29892007-01-19 07:51:42 +00008612/// getConstraintType - Given a constraint letter, return the type of
8613/// constraint it is for this target.
8614ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00008615ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
8616 if (Constraint.size() == 1) {
8617 switch (Constraint[0]) {
8618 default: break;
8619 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008620 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00008621 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00008622 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008623 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00008624 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00008625 // An address with a single base register. Due to the way we
8626 // currently handle addresses it is the same as an 'r' memory constraint.
8627 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00008628 }
Eric Christopher1312ca82011-06-21 22:10:57 +00008629 } else if (Constraint.size() == 2) {
8630 switch (Constraint[0]) {
8631 default: break;
8632 // All 'U+' constraints are addresses.
8633 case 'U': return C_Memory;
8634 }
Evan Chenga8e29892007-01-19 07:51:42 +00008635 }
Chris Lattner4234f572007-03-25 02:14:49 +00008636 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00008637}
8638
John Thompson44ab89e2010-10-29 17:29:13 +00008639/// Examine constraint type and operand type and determine a weight value.
8640/// This object must already have been set up with the operand type
8641/// and the current alternative constraint selected.
8642TargetLowering::ConstraintWeight
8643ARMTargetLowering::getSingleConstraintMatchWeight(
8644 AsmOperandInfo &info, const char *constraint) const {
8645 ConstraintWeight weight = CW_Invalid;
8646 Value *CallOperandVal = info.CallOperandVal;
8647 // If we don't have a value, we can't do a match,
8648 // but allow it at the lowest weight.
8649 if (CallOperandVal == NULL)
8650 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008651 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00008652 // Look at the constraint type.
8653 switch (*constraint) {
8654 default:
8655 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
8656 break;
8657 case 'l':
8658 if (type->isIntegerTy()) {
8659 if (Subtarget->isThumb())
8660 weight = CW_SpecificReg;
8661 else
8662 weight = CW_Register;
8663 }
8664 break;
8665 case 'w':
8666 if (type->isFloatingPointTy())
8667 weight = CW_Register;
8668 break;
8669 }
8670 return weight;
8671}
8672
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008673typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
8674RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00008675ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00008676 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00008677 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00008678 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00008679 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00008680 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00008681 if (Subtarget->isThumb())
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008682 return RCPair(0U, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +00008683 else
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008684 return RCPair(0U, ARM::GPRRegisterClass);
Eric Christopher73744df2011-06-30 23:23:01 +00008685 case 'h': // High regs or no regs.
8686 if (Subtarget->isThumb())
Andrew Trick3af7a672011-09-20 03:06:13 +00008687 return RCPair(0U, ARM::hGPRRegisterClass);
Eric Christopher1070f822011-07-01 00:19:27 +00008688 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008689 case 'r':
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008690 return RCPair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008691 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00008692 if (VT == MVT::f32)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008693 return RCPair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00008694 if (VT.getSizeInBits() == 64)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008695 return RCPair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00008696 if (VT.getSizeInBits() == 128)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008697 return RCPair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008698 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00008699 case 'x':
8700 if (VT == MVT::f32)
Andrew Trick3af7a672011-09-20 03:06:13 +00008701 return RCPair(0U, ARM::SPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008702 if (VT.getSizeInBits() == 64)
Andrew Trick3af7a672011-09-20 03:06:13 +00008703 return RCPair(0U, ARM::DPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008704 if (VT.getSizeInBits() == 128)
Andrew Trick3af7a672011-09-20 03:06:13 +00008705 return RCPair(0U, ARM::QPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008706 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008707 case 't':
8708 if (VT == MVT::f32)
Andrew Trick3af7a672011-09-20 03:06:13 +00008709 return RCPair(0U, ARM::SPRRegisterClass);
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008710 break;
Evan Chenga8e29892007-01-19 07:51:42 +00008711 }
8712 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00008713 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00008714 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00008715
Evan Chenga8e29892007-01-19 07:51:42 +00008716 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
8717}
8718
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008719/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
8720/// vector. If it is invalid, don't add anything to Ops.
8721void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00008722 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008723 std::vector<SDValue>&Ops,
8724 SelectionDAG &DAG) const {
8725 SDValue Result(0, 0);
8726
Eric Christopher100c8332011-06-02 23:16:42 +00008727 // Currently only support length 1 constraints.
8728 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00008729
Eric Christopher100c8332011-06-02 23:16:42 +00008730 char ConstraintLetter = Constraint[0];
8731 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008732 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00008733 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008734 case 'I': case 'J': case 'K': case 'L':
8735 case 'M': case 'N': case 'O':
8736 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
8737 if (!C)
8738 return;
8739
8740 int64_t CVal64 = C->getSExtValue();
8741 int CVal = (int) CVal64;
8742 // None of these constraints allow values larger than 32 bits. Check
8743 // that the value fits in an int.
8744 if (CVal != CVal64)
8745 return;
8746
Eric Christopher100c8332011-06-02 23:16:42 +00008747 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00008748 case 'j':
Andrew Trick3af7a672011-09-20 03:06:13 +00008749 // Constant suitable for movw, must be between 0 and
8750 // 65535.
8751 if (Subtarget->hasV6T2Ops())
8752 if (CVal >= 0 && CVal <= 65535)
8753 break;
8754 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008755 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008756 if (Subtarget->isThumb1Only()) {
8757 // This must be a constant between 0 and 255, for ADD
8758 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008759 if (CVal >= 0 && CVal <= 255)
8760 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008761 } else if (Subtarget->isThumb2()) {
8762 // A constant that can be used as an immediate value in a
8763 // data-processing instruction.
8764 if (ARM_AM::getT2SOImmVal(CVal) != -1)
8765 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008766 } else {
8767 // A constant that can be used as an immediate value in a
8768 // data-processing instruction.
8769 if (ARM_AM::getSOImmVal(CVal) != -1)
8770 break;
8771 }
8772 return;
8773
8774 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008775 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008776 // This must be a constant between -255 and -1, for negated ADD
8777 // immediates. This can be used in GCC with an "n" modifier that
8778 // prints the negated value, for use with SUB instructions. It is
8779 // not useful otherwise but is implemented for compatibility.
8780 if (CVal >= -255 && CVal <= -1)
8781 break;
8782 } else {
8783 // This must be a constant between -4095 and 4095. It is not clear
8784 // what this constraint is intended for. Implemented for
8785 // compatibility with GCC.
8786 if (CVal >= -4095 && CVal <= 4095)
8787 break;
8788 }
8789 return;
8790
8791 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008792 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008793 // A 32-bit value where only one byte has a nonzero value. Exclude
8794 // zero to match GCC. This constraint is used by GCC internally for
8795 // constants that can be loaded with a move/shift combination.
8796 // It is not useful otherwise but is implemented for compatibility.
8797 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
8798 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008799 } else if (Subtarget->isThumb2()) {
8800 // A constant whose bitwise inverse can be used as an immediate
8801 // value in a data-processing instruction. This can be used in GCC
8802 // with a "B" modifier that prints the inverted value, for use with
8803 // BIC and MVN instructions. It is not useful otherwise but is
8804 // implemented for compatibility.
8805 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
8806 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008807 } else {
8808 // A constant whose bitwise inverse can be used as an immediate
8809 // value in a data-processing instruction. This can be used in GCC
8810 // with a "B" modifier that prints the inverted value, for use with
8811 // BIC and MVN instructions. It is not useful otherwise but is
8812 // implemented for compatibility.
8813 if (ARM_AM::getSOImmVal(~CVal) != -1)
8814 break;
8815 }
8816 return;
8817
8818 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008819 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008820 // This must be a constant between -7 and 7,
8821 // for 3-operand ADD/SUB immediate instructions.
8822 if (CVal >= -7 && CVal < 7)
8823 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008824 } else if (Subtarget->isThumb2()) {
8825 // A constant whose negation can be used as an immediate value in a
8826 // data-processing instruction. This can be used in GCC with an "n"
8827 // modifier that prints the negated value, for use with SUB
8828 // instructions. It is not useful otherwise but is implemented for
8829 // compatibility.
8830 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
8831 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008832 } else {
8833 // A constant whose negation can be used as an immediate value in a
8834 // data-processing instruction. This can be used in GCC with an "n"
8835 // modifier that prints the negated value, for use with SUB
8836 // instructions. It is not useful otherwise but is implemented for
8837 // compatibility.
8838 if (ARM_AM::getSOImmVal(-CVal) != -1)
8839 break;
8840 }
8841 return;
8842
8843 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008844 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008845 // This must be a multiple of 4 between 0 and 1020, for
8846 // ADD sp + immediate.
8847 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
8848 break;
8849 } else {
8850 // A power of two or a constant between 0 and 32. This is used in
8851 // GCC for the shift amount on shifted register operands, but it is
8852 // useful in general for any shift amounts.
8853 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
8854 break;
8855 }
8856 return;
8857
8858 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008859 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008860 // This must be a constant between 0 and 31, for shift amounts.
8861 if (CVal >= 0 && CVal <= 31)
8862 break;
8863 }
8864 return;
8865
8866 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008867 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008868 // This must be a multiple of 4 between -508 and 508, for
8869 // ADD/SUB sp = sp + immediate.
8870 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
8871 break;
8872 }
8873 return;
8874 }
8875 Result = DAG.getTargetConstant(CVal, Op.getValueType());
8876 break;
8877 }
8878
8879 if (Result.getNode()) {
8880 Ops.push_back(Result);
8881 return;
8882 }
Dale Johannesen1784d162010-06-25 21:55:36 +00008883 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008884}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00008885
8886bool
8887ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
8888 // The ARM target isn't yet aware of offsets.
8889 return false;
8890}
Evan Cheng39382422009-10-28 01:44:26 +00008891
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008892bool ARM::isBitFieldInvertedMask(unsigned v) {
8893 if (v == 0xffffffff)
8894 return 0;
8895 // there can be 1's on either or both "outsides", all the "inside"
8896 // bits must be 0's
8897 unsigned int lsb = 0, msb = 31;
8898 while (v & (1 << msb)) --msb;
8899 while (v & (1 << lsb)) ++lsb;
8900 for (unsigned int i = lsb; i <= msb; ++i) {
8901 if (v & (1 << i))
8902 return 0;
8903 }
8904 return 1;
8905}
8906
Evan Cheng39382422009-10-28 01:44:26 +00008907/// isFPImmLegal - Returns true if the target can instruction select the
8908/// specified FP immediate natively. If false, the legalizer will
8909/// materialize the FP immediate as a load from a constant pool.
8910bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
8911 if (!Subtarget->hasVFP3())
8912 return false;
8913 if (VT == MVT::f32)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00008914 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00008915 if (VT == MVT::f64)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00008916 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00008917 return false;
8918}
Bob Wilson65ffec42010-09-21 17:56:22 +00008919
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008920/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00008921/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
8922/// specified in the intrinsic calls.
8923bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
8924 const CallInst &I,
8925 unsigned Intrinsic) const {
8926 switch (Intrinsic) {
8927 case Intrinsic::arm_neon_vld1:
8928 case Intrinsic::arm_neon_vld2:
8929 case Intrinsic::arm_neon_vld3:
8930 case Intrinsic::arm_neon_vld4:
8931 case Intrinsic::arm_neon_vld2lane:
8932 case Intrinsic::arm_neon_vld3lane:
8933 case Intrinsic::arm_neon_vld4lane: {
8934 Info.opc = ISD::INTRINSIC_W_CHAIN;
8935 // Conservatively set memVT to the entire set of vectors loaded.
8936 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
8937 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
8938 Info.ptrVal = I.getArgOperand(0);
8939 Info.offset = 0;
8940 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
8941 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
8942 Info.vol = false; // volatile loads with NEON intrinsics not supported
8943 Info.readMem = true;
8944 Info.writeMem = false;
8945 return true;
8946 }
8947 case Intrinsic::arm_neon_vst1:
8948 case Intrinsic::arm_neon_vst2:
8949 case Intrinsic::arm_neon_vst3:
8950 case Intrinsic::arm_neon_vst4:
8951 case Intrinsic::arm_neon_vst2lane:
8952 case Intrinsic::arm_neon_vst3lane:
8953 case Intrinsic::arm_neon_vst4lane: {
8954 Info.opc = ISD::INTRINSIC_VOID;
8955 // Conservatively set memVT to the entire set of vectors stored.
8956 unsigned NumElts = 0;
8957 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008958 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +00008959 if (!ArgTy->isVectorTy())
8960 break;
8961 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
8962 }
8963 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
8964 Info.ptrVal = I.getArgOperand(0);
8965 Info.offset = 0;
8966 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
8967 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
8968 Info.vol = false; // volatile stores with NEON intrinsics not supported
8969 Info.readMem = false;
8970 Info.writeMem = true;
8971 return true;
8972 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00008973 case Intrinsic::arm_strexd: {
8974 Info.opc = ISD::INTRINSIC_W_CHAIN;
8975 Info.memVT = MVT::i64;
8976 Info.ptrVal = I.getArgOperand(2);
8977 Info.offset = 0;
8978 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00008979 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00008980 Info.readMem = false;
8981 Info.writeMem = true;
8982 return true;
8983 }
8984 case Intrinsic::arm_ldrexd: {
8985 Info.opc = ISD::INTRINSIC_W_CHAIN;
8986 Info.memVT = MVT::i64;
8987 Info.ptrVal = I.getArgOperand(0);
8988 Info.offset = 0;
8989 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00008990 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00008991 Info.readMem = true;
8992 Info.writeMem = false;
8993 return true;
8994 }
Bob Wilson65ffec42010-09-21 17:56:22 +00008995 default:
8996 break;
8997 }
8998
8999 return false;
9000}