blob: d146930a39821643e1f747fc5e4048ea54c66010 [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"
Chris Lattner84bc5422007-12-31 04:13:23 +000041#include "llvm/CodeGen/MachineRegisterInfo.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000042#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chenga8e29892007-01-19 07:51:42 +000043#include "llvm/CodeGen/SelectionDAG.h"
Bill Wendling94a1c632010-03-09 02:46:12 +000044#include "llvm/MC/MCSectionMachO.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000045#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000046#include "llvm/ADT/VectorExtras.h"
Evan Cheng55d42002011-01-08 01:24:27 +000047#include "llvm/ADT/StringExtras.h"
Dale Johannesen51e28e62010-06-03 21:09:53 +000048#include "llvm/ADT/Statistic.h"
Jim Grosbache7b52522010-04-14 22:28:31 +000049#include "llvm/Support/CommandLine.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000050#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000051#include "llvm/Support/MathExtras.h"
Jim Grosbache801dc42009-12-12 01:40:06 +000052#include "llvm/Support/raw_ostream.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000053#include <sstream>
Evan Chenga8e29892007-01-19 07:51:42 +000054using namespace llvm;
55
Dale Johannesen51e28e62010-06-03 21:09:53 +000056STATISTIC(NumTailCalls, "Number of tail calls");
Evan Chengfc8475b2011-01-19 02:16:49 +000057STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Dale Johannesen51e28e62010-06-03 21:09:53 +000058
Bob Wilson703af3a2010-08-13 22:43:33 +000059// This option should go away when tail calls fully work.
60static cl::opt<bool>
61EnableARMTailCalls("arm-tail-calls", cl::Hidden,
62 cl::desc("Generate tail calls (TEMPORARY OPTION)."),
63 cl::init(false));
64
Eric Christopher836c6242010-12-15 23:47:29 +000065cl::opt<bool>
Jim Grosbache7b52522010-04-14 22:28:31 +000066EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng515fe3a2010-07-08 02:08:50 +000067 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbache7b52522010-04-14 22:28:31 +000068 cl::init(false));
69
Evan Cheng46df4eb2010-06-16 07:35:02 +000070static cl::opt<bool>
71ARMInterworking("arm-interworking", cl::Hidden,
72 cl::desc("Enable / disable ARM interworking (for debugging only)"),
73 cl::init(true));
74
Cameron Zwaricha86686e2011-06-10 20:59:24 +000075namespace llvm {
76 class ARMCCState : public CCState {
77 public:
78 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
79 const TargetMachine &TM, SmallVector<CCValAssign, 16> &locs,
80 LLVMContext &C, ParmContext PC)
81 : CCState(CC, isVarArg, MF, TM, locs, C) {
82 assert(((PC == Call) || (PC == Prologue)) &&
83 "ARMCCState users must specify whether their context is call"
84 "or prologue generation.");
85 CallOrPrologue = PC;
86 }
87 };
88}
89
Stuart Hastingsc7315872011-04-20 16:47:52 +000090// The APCS parameter registers.
91static const unsigned GPRArgRegs[] = {
92 ARM::R0, ARM::R1, ARM::R2, ARM::R3
93};
94
Owen Andersone50ed302009-08-10 22:56:29 +000095void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
96 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000097 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000098 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000099 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
100 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000101
Owen Anderson70671842009-08-10 20:18:46 +0000102 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000103 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000104 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000105 }
106
Owen Andersone50ed302009-08-10 22:56:29 +0000107 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000108 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Duncan Sands28b77e92011-09-06 19:07:46 +0000109 setOperationAction(ISD::SETCC, VT.getSimpleVT(), Custom);
Bob Wilson3468c2e2010-11-03 16:24:50 +0000110 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson0696fdf2009-09-16 20:20:44 +0000111 if (ElemTy != MVT::i32) {
112 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
113 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
114 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
115 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
116 }
Owen Anderson70671842009-08-10 20:18:46 +0000117 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
118 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Bob Wilson07f6e802010-06-16 21:34:01 +0000119 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
Bob Wilson5e8b8332011-01-07 04:59:04 +0000120 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Legal);
Bob Wilsond0910c42010-04-06 22:02:24 +0000121 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
122 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000123 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000124 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
125 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
126 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000127 setLoadExtAction(ISD::SEXTLOAD, VT.getSimpleVT(), Expand);
128 setLoadExtAction(ISD::ZEXTLOAD, VT.getSimpleVT(), Expand);
Bob Wilson24645a12010-11-01 18:31:39 +0000129 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
130 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
131 setTruncStoreAction(VT.getSimpleVT(),
132 (MVT::SimpleValueType)InnerVT, Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000133 }
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000134 setLoadExtAction(ISD::EXTLOAD, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000135
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
David Goodwinf1daf7d2009-07-08 23:10:31 +0000424 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000425 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000426 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000427 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000428 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000429 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000430 if (!Subtarget->isFPOnlySP())
431 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000432
Owen Anderson825b72b2009-08-11 20:47:22 +0000433 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000434 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000435
436 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000437 addDRTypeForNEON(MVT::v2f32);
438 addDRTypeForNEON(MVT::v8i8);
439 addDRTypeForNEON(MVT::v4i16);
440 addDRTypeForNEON(MVT::v2i32);
441 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000442
Owen Anderson825b72b2009-08-11 20:47:22 +0000443 addQRTypeForNEON(MVT::v4f32);
444 addQRTypeForNEON(MVT::v2f64);
445 addQRTypeForNEON(MVT::v16i8);
446 addQRTypeForNEON(MVT::v8i16);
447 addQRTypeForNEON(MVT::v4i32);
448 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000449
Bob Wilson74dc72e2009-09-15 23:55:57 +0000450 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
451 // neither Neon nor VFP support any arithmetic operations on it.
452 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
453 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
454 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
455 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
456 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
457 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000458 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Bob Wilson74dc72e2009-09-15 23:55:57 +0000459 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
460 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
461 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
462 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
463 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
464 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
465 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
466 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
467 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
468 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
469 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
470 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
471 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
472 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
473 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
474 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
475 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
476
Bob Wilsonb31a11b2010-08-20 04:54:02 +0000477 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand);
478
Bob Wilson642b3292009-09-16 00:32:15 +0000479 // Neon does not support some operations on v1i64 and v2i64 types.
480 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000481 // Custom handling for some quad-vector types to detect VMULL.
482 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
483 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
484 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begeman7973f352011-02-11 20:53:29 +0000485 // Custom handling for some vector types to avoid expensive expansions
486 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
487 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
488 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
489 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000490 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
491 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000492 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
493 // a destination type that is wider than the source.
494 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
495 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000496
Bob Wilson1c3ef902011-02-07 17:43:21 +0000497 setTargetDAGCombine(ISD::INTRINSIC_VOID);
498 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000499 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
500 setTargetDAGCombine(ISD::SHL);
501 setTargetDAGCombine(ISD::SRL);
502 setTargetDAGCombine(ISD::SRA);
503 setTargetDAGCombine(ISD::SIGN_EXTEND);
504 setTargetDAGCombine(ISD::ZERO_EXTEND);
505 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000506 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000507 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000508 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000509 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
510 setTargetDAGCombine(ISD::STORE);
Chad Rosieref01edf2011-06-24 19:23:04 +0000511 setTargetDAGCombine(ISD::FP_TO_SINT);
512 setTargetDAGCombine(ISD::FP_TO_UINT);
513 setTargetDAGCombine(ISD::FDIV);
Bob Wilson5bafff32009-06-22 23:27:02 +0000514 }
515
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000516 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000517
518 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000519 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000520
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000521 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000522 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000523
Evan Chenga8e29892007-01-19 07:51:42 +0000524 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000525 if (!Subtarget->isThumb1Only()) {
526 for (unsigned im = (unsigned)ISD::PRE_INC;
527 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000528 setIndexedLoadAction(im, MVT::i1, Legal);
529 setIndexedLoadAction(im, MVT::i8, Legal);
530 setIndexedLoadAction(im, MVT::i16, Legal);
531 setIndexedLoadAction(im, MVT::i32, Legal);
532 setIndexedStoreAction(im, MVT::i1, Legal);
533 setIndexedStoreAction(im, MVT::i8, Legal);
534 setIndexedStoreAction(im, MVT::i16, Legal);
535 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000536 }
Evan Chenga8e29892007-01-19 07:51:42 +0000537 }
538
539 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000540 setOperationAction(ISD::MUL, MVT::i64, Expand);
541 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000542 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000543 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
544 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000545 }
Jim Grosbacha7603982011-07-01 21:12:19 +0000546 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
547 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopher2cc40132011-04-19 18:49:19 +0000548 setOperationAction(ISD::MULHS, MVT::i32, Expand);
549
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000550 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000551 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000552 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000553 setOperationAction(ISD::SRL, MVT::i64, Custom);
554 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000555
Evan Cheng342e3162011-08-30 01:34:54 +0000556 if (!Subtarget->isThumb1Only()) {
557 // FIXME: We should do this for Thumb1 as well.
558 setOperationAction(ISD::ADDC, MVT::i32, Custom);
559 setOperationAction(ISD::ADDE, MVT::i32, Custom);
560 setOperationAction(ISD::SUBC, MVT::i32, Custom);
561 setOperationAction(ISD::SUBE, MVT::i32, Custom);
562 }
563
Evan Chenga8e29892007-01-19 07:51:42 +0000564 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000565 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000566 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000567 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000568 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000569 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000570
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000571 // Only ARMv6 has BSWAP.
572 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000573 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000574
Evan Chenga8e29892007-01-19 07:51:42 +0000575 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000576 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000577 // v7M has a hardware divider
578 setOperationAction(ISD::SDIV, MVT::i32, Expand);
579 setOperationAction(ISD::UDIV, MVT::i32, Expand);
580 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000581 setOperationAction(ISD::SREM, MVT::i32, Expand);
582 setOperationAction(ISD::UREM, MVT::i32, Expand);
583 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
584 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000585
Owen Anderson825b72b2009-08-11 20:47:22 +0000586 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
587 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
588 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
589 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000590 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000591
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000592 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000593
Evan Chenga8e29892007-01-19 07:51:42 +0000594 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000595 setOperationAction(ISD::VASTART, MVT::Other, Custom);
596 setOperationAction(ISD::VAARG, MVT::Other, Expand);
597 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
598 setOperationAction(ISD::VAEND, MVT::Other, Expand);
599 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
600 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000601 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000602 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
603 setExceptionPointerRegister(ARM::R0);
604 setExceptionSelectorRegister(ARM::R1);
605
Evan Cheng3a1588a2010-04-15 22:20:34 +0000606 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000607 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
608 // the default expansion.
Eli Friedman4db5aca2011-08-29 18:23:02 +0000609 // FIXME: This should be checking for v6k, not just v6.
Evan Cheng11db0682010-08-11 06:22:01 +0000610 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000611 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000612 // membarrier needs custom lowering; the rest are legal and handled
613 // normally.
614 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000615 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Eli Friedman2bdffe42011-08-31 00:31:29 +0000616 // Custom lowering for 64-bit ops
617 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
618 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
619 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
620 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
621 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
622 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Eli Friedman4d3f3292011-08-31 17:52:22 +0000623 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Eli Friedman26689ac2011-08-03 21:06:02 +0000624 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
625 setInsertFencesForAtomic(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000626 } else {
627 // Set them all for expansion, which will force libcalls.
628 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000629 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000630 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000631 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000632 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000633 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000634 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000635 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000636 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000637 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000638 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000639 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000640 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000641 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedman7cc15662011-09-15 22:18:49 +0000642 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
643 // Unordered/Monotonic case.
644 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
645 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000646 // Since the libcalls include locking, fold in the fences
647 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000648 }
Evan Chenga8e29892007-01-19 07:51:42 +0000649
Evan Cheng416941d2010-11-04 05:19:35 +0000650 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000651
Eli Friedmana2c6f452010-06-26 04:36:50 +0000652 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
653 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000654 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
655 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000656 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000657 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000658
Nate Begemand1fb5832010-08-03 21:31:55 +0000659 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000660 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
661 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000662 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000663 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
664 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000665
666 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000667 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000668 if (Subtarget->isTargetDarwin()) {
669 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
670 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Jim Grosbache4ad3872010-10-19 23:27:08 +0000671 setOperationAction(ISD::EH_SJLJ_DISPATCHSETUP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000672 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000673 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000674
Owen Anderson825b72b2009-08-11 20:47:22 +0000675 setOperationAction(ISD::SETCC, MVT::i32, Expand);
676 setOperationAction(ISD::SETCC, MVT::f32, Expand);
677 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000678 setOperationAction(ISD::SELECT, MVT::i32, Custom);
679 setOperationAction(ISD::SELECT, MVT::f32, Custom);
680 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000681 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
682 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
683 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000684
Owen Anderson825b72b2009-08-11 20:47:22 +0000685 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
686 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
687 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
688 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
689 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000690
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000691 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000692 setOperationAction(ISD::FSIN, MVT::f64, Expand);
693 setOperationAction(ISD::FSIN, MVT::f32, Expand);
694 setOperationAction(ISD::FCOS, MVT::f32, Expand);
695 setOperationAction(ISD::FCOS, MVT::f64, Expand);
696 setOperationAction(ISD::FREM, MVT::f64, Expand);
697 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000698 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000699 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
700 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000701 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000702 setOperationAction(ISD::FPOW, MVT::f64, Expand);
703 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000704
Cameron Zwarich33390842011-07-08 21:39:21 +0000705 setOperationAction(ISD::FMA, MVT::f64, Expand);
706 setOperationAction(ISD::FMA, MVT::f32, Expand);
707
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000708 // Various VFP goodness
709 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000710 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
711 if (Subtarget->hasVFP2()) {
712 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
713 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
714 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
715 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
716 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000717 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000718 if (!Subtarget->hasFP16()) {
719 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
720 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000721 }
Evan Cheng110cf482008-04-01 01:50:16 +0000722 }
Evan Chenga8e29892007-01-19 07:51:42 +0000723
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000724 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000725 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000726 setTargetDAGCombine(ISD::ADD);
727 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000728 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000729
Owen Anderson080c0922010-11-05 19:27:46 +0000730 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON())
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000731 setTargetDAGCombine(ISD::OR);
Owen Anderson080c0922010-11-05 19:27:46 +0000732 if (Subtarget->hasNEON())
733 setTargetDAGCombine(ISD::AND);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000734
Evan Chenga8e29892007-01-19 07:51:42 +0000735 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000736
Evan Chengf7d87ee2010-05-21 00:43:17 +0000737 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
738 setSchedulingPreference(Sched::RegPressure);
739 else
740 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000741
Evan Cheng05219282011-01-06 06:52:41 +0000742 //// temporary - rewrite interface to use type
743 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Evan Chengf6799392010-06-26 01:52:05 +0000744
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000745 // On ARM arguments smaller than 4 bytes are extended, so all arguments
746 // are at least 4 bytes aligned.
747 setMinStackArgumentAlignment(4);
748
Evan Chengfff606d2010-09-24 19:07:23 +0000749 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000750
751 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000752}
753
Andrew Trick32cec0a2011-01-19 02:35:27 +0000754// FIXME: It might make sense to define the representative register class as the
755// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
756// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
757// SPR's representative would be DPR_VFP2. This should work well if register
758// pressure tracking were modified such that a register use would increment the
759// pressure of the register class's representative and all of it's super
760// classes' representatives transitively. We have not implemented this because
761// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000762// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000763// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000764std::pair<const TargetRegisterClass*, uint8_t>
765ARMTargetLowering::findRepresentativeClass(EVT VT) const{
766 const TargetRegisterClass *RRC = 0;
767 uint8_t Cost = 1;
768 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000769 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000770 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000771 // Use DPR as representative register class for all floating point
772 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
773 // the cost is 1 for both f32 and f64.
774 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000775 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000776 RRC = ARM::DPRRegisterClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000777 // When NEON is used for SP, only half of the register file is available
778 // because operations that define both SP and DP results will be constrained
779 // to the VFP2 class (D0-D15). We currently model this constraint prior to
780 // coalescing by double-counting the SP regs. See the FIXME above.
781 if (Subtarget->useNEONForSinglePrecisionFP())
782 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000783 break;
784 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
785 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000786 RRC = ARM::DPRRegisterClass;
787 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000788 break;
789 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000790 RRC = ARM::DPRRegisterClass;
791 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000792 break;
793 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000794 RRC = ARM::DPRRegisterClass;
795 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000796 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000797 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000798 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000799}
800
Evan Chenga8e29892007-01-19 07:51:42 +0000801const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
802 switch (Opcode) {
803 default: return 0;
804 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000805 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000806 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000807 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
808 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000809 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000810 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
811 case ARMISD::tCALL: return "ARMISD::tCALL";
812 case ARMISD::BRCOND: return "ARMISD::BRCOND";
813 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000814 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000815 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
816 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
817 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000818 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000819 case ARMISD::CMPFP: return "ARMISD::CMPFP";
820 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000821 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000822 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
823 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000824
Jim Grosbach3482c802010-01-18 19:58:49 +0000825 case ARMISD::RBIT: return "ARMISD::RBIT";
826
Bob Wilson76a312b2010-03-19 22:51:32 +0000827 case ARMISD::FTOSI: return "ARMISD::FTOSI";
828 case ARMISD::FTOUI: return "ARMISD::FTOUI";
829 case ARMISD::SITOF: return "ARMISD::SITOF";
830 case ARMISD::UITOF: return "ARMISD::UITOF";
831
Evan Chenga8e29892007-01-19 07:51:42 +0000832 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
833 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
834 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000835
Evan Cheng342e3162011-08-30 01:34:54 +0000836 case ARMISD::ADDC: return "ARMISD::ADDC";
837 case ARMISD::ADDE: return "ARMISD::ADDE";
838 case ARMISD::SUBC: return "ARMISD::SUBC";
839 case ARMISD::SUBE: return "ARMISD::SUBE";
840
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000841 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
842 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000843
Evan Chengc5942082009-10-28 06:55:03 +0000844 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
845 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
Jim Grosbache4ad3872010-10-19 23:27:08 +0000846 case ARMISD::EH_SJLJ_DISPATCHSETUP:return "ARMISD::EH_SJLJ_DISPATCHSETUP";
Evan Chengc5942082009-10-28 06:55:03 +0000847
Dale Johannesen51e28e62010-06-03 21:09:53 +0000848 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000849
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000850 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000851
Evan Cheng86198642009-08-07 00:34:42 +0000852 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
853
Jim Grosbach3728e962009-12-10 00:11:09 +0000854 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000855 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000856
Evan Chengdfed19f2010-11-03 06:34:55 +0000857 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
858
Bob Wilson5bafff32009-06-22 23:27:02 +0000859 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000860 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000861 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000862 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
863 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000864 case ARMISD::VCGEU: return "ARMISD::VCGEU";
865 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000866 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
867 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000868 case ARMISD::VCGTU: return "ARMISD::VCGTU";
869 case ARMISD::VTST: return "ARMISD::VTST";
870
871 case ARMISD::VSHL: return "ARMISD::VSHL";
872 case ARMISD::VSHRs: return "ARMISD::VSHRs";
873 case ARMISD::VSHRu: return "ARMISD::VSHRu";
874 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
875 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
876 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
877 case ARMISD::VSHRN: return "ARMISD::VSHRN";
878 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
879 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
880 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
881 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
882 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
883 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
884 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
885 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
886 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
887 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
888 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
889 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
890 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
891 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000892 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000893 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000894 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000895 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000896 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000897 case ARMISD::VREV64: return "ARMISD::VREV64";
898 case ARMISD::VREV32: return "ARMISD::VREV32";
899 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000900 case ARMISD::VZIP: return "ARMISD::VZIP";
901 case ARMISD::VUZP: return "ARMISD::VUZP";
902 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000903 case ARMISD::VTBL1: return "ARMISD::VTBL1";
904 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000905 case ARMISD::VMULLs: return "ARMISD::VMULLs";
906 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000907 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000908 case ARMISD::FMAX: return "ARMISD::FMAX";
909 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000910 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000911 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
912 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000913 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000914 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
915 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
916 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +0000917 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
918 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
919 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
920 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
921 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
922 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
923 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
924 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
925 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
926 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
927 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
928 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
929 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
930 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
931 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
932 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
933 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +0000934 }
935}
936
Duncan Sands28b77e92011-09-06 19:07:46 +0000937EVT ARMTargetLowering::getSetCCResultType(EVT VT) const {
938 if (!VT.isVector()) return getPointerTy();
939 return VT.changeVectorElementTypeToInteger();
940}
941
Evan Cheng06b666c2010-05-15 02:18:07 +0000942/// getRegClassFor - Return the register class that should be used for the
943/// specified value type.
944TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
945 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
946 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
947 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000948 if (Subtarget->hasNEON()) {
949 if (VT == MVT::v4i64)
950 return ARM::QQPRRegisterClass;
951 else if (VT == MVT::v8i64)
952 return ARM::QQQQPRRegisterClass;
953 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000954 return TargetLowering::getRegClassFor(VT);
955}
956
Eric Christopherab695882010-07-21 22:26:11 +0000957// Create a fast isel object.
958FastISel *
959ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
960 return ARM::createFastISel(funcInfo);
961}
962
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000963/// getMaximalGlobalOffset - Returns the maximal possible offset which can
964/// be used for loads / stores from the global.
965unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
966 return (Subtarget->isThumb1Only() ? 127 : 4095);
967}
968
Evan Cheng1cc39842010-05-20 23:26:43 +0000969Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000970 unsigned NumVals = N->getNumValues();
971 if (!NumVals)
972 return Sched::RegPressure;
973
974 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000975 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000976 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +0000977 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +0000978 if (VT.isFloatingPoint() || VT.isVector())
979 return Sched::Latency;
980 }
Evan Chengc10f5432010-05-28 23:25:23 +0000981
982 if (!N->isMachineOpcode())
983 return Sched::RegPressure;
984
985 // Load are scheduled for latency even if there instruction itinerary
986 // is not available.
987 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +0000988 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +0000989
Evan Chenge837dea2011-06-28 19:10:37 +0000990 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +0000991 return Sched::RegPressure;
992 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +0000993 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Evan Chengc10f5432010-05-28 23:25:23 +0000994 return Sched::Latency;
995
Evan Cheng1cc39842010-05-20 23:26:43 +0000996 return Sched::RegPressure;
997}
998
Evan Chenga8e29892007-01-19 07:51:42 +0000999//===----------------------------------------------------------------------===//
1000// Lowering Code
1001//===----------------------------------------------------------------------===//
1002
Evan Chenga8e29892007-01-19 07:51:42 +00001003/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1004static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1005 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001006 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +00001007 case ISD::SETNE: return ARMCC::NE;
1008 case ISD::SETEQ: return ARMCC::EQ;
1009 case ISD::SETGT: return ARMCC::GT;
1010 case ISD::SETGE: return ARMCC::GE;
1011 case ISD::SETLT: return ARMCC::LT;
1012 case ISD::SETLE: return ARMCC::LE;
1013 case ISD::SETUGT: return ARMCC::HI;
1014 case ISD::SETUGE: return ARMCC::HS;
1015 case ISD::SETULT: return ARMCC::LO;
1016 case ISD::SETULE: return ARMCC::LS;
1017 }
1018}
1019
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001020/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1021static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001022 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001023 CondCode2 = ARMCC::AL;
1024 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001025 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001026 case ISD::SETEQ:
1027 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1028 case ISD::SETGT:
1029 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1030 case ISD::SETGE:
1031 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1032 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001033 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001034 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1035 case ISD::SETO: CondCode = ARMCC::VC; break;
1036 case ISD::SETUO: CondCode = ARMCC::VS; break;
1037 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1038 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1039 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1040 case ISD::SETLT:
1041 case ISD::SETULT: CondCode = ARMCC::LT; break;
1042 case ISD::SETLE:
1043 case ISD::SETULE: CondCode = ARMCC::LE; break;
1044 case ISD::SETNE:
1045 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1046 }
Evan Chenga8e29892007-01-19 07:51:42 +00001047}
1048
Bob Wilson1f595bb2009-04-17 19:07:39 +00001049//===----------------------------------------------------------------------===//
1050// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001051//===----------------------------------------------------------------------===//
1052
1053#include "ARMGenCallingConv.inc"
1054
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001055/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1056/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001057CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001058 bool Return,
1059 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001060 switch (CC) {
1061 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001062 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001063 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001064 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001065 if (!Subtarget->isAAPCS_ABI())
1066 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1067 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1068 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1069 }
1070 // Fallthrough
1071 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001072 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001073 if (!Subtarget->isAAPCS_ABI())
1074 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1075 else if (Subtarget->hasVFP2() &&
1076 FloatABIType == FloatABI::Hard && !isVarArg)
1077 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1078 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1079 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001080 case CallingConv::ARM_AAPCS_VFP:
Evan Cheng76f920d2010-10-22 18:23:05 +00001081 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001082 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001083 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001084 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001085 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001086 }
1087}
1088
Dan Gohman98ca4f22009-08-05 01:29:28 +00001089/// LowerCallResult - Lower the result values of a call into the
1090/// appropriate copies out of appropriate physical registers.
1091SDValue
1092ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001093 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001094 const SmallVectorImpl<ISD::InputArg> &Ins,
1095 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001096 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001097
Bob Wilson1f595bb2009-04-17 19:07:39 +00001098 // Assign locations to each value returned by this call.
1099 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001100 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1101 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001102 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001103 CCAssignFnForNode(CallConv, /* Return*/ true,
1104 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001105
1106 // Copy all of the result registers out of their specified physreg.
1107 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1108 CCValAssign VA = RVLocs[i];
1109
Bob Wilson80915242009-04-25 00:33:20 +00001110 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001111 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001112 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001113 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001114 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001115 Chain = Lo.getValue(1);
1116 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001117 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001118 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001119 InFlag);
1120 Chain = Hi.getValue(1);
1121 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001122 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001123
Owen Anderson825b72b2009-08-11 20:47:22 +00001124 if (VA.getLocVT() == MVT::v2f64) {
1125 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1126 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1127 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001128
1129 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001130 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001131 Chain = Lo.getValue(1);
1132 InFlag = Lo.getValue(2);
1133 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001134 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001135 Chain = Hi.getValue(1);
1136 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001137 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001138 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1139 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001140 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001141 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001142 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1143 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001144 Chain = Val.getValue(1);
1145 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001146 }
Bob Wilson80915242009-04-25 00:33:20 +00001147
1148 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001149 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001150 case CCValAssign::Full: break;
1151 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001152 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001153 break;
1154 }
1155
Dan Gohman98ca4f22009-08-05 01:29:28 +00001156 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001157 }
1158
Dan Gohman98ca4f22009-08-05 01:29:28 +00001159 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001160}
1161
Bob Wilsondee46d72009-04-17 20:35:10 +00001162/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001163SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001164ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1165 SDValue StackPtr, SDValue Arg,
1166 DebugLoc dl, SelectionDAG &DAG,
1167 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001168 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001169 unsigned LocMemOffset = VA.getLocMemOffset();
1170 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1171 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001172 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001173 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001174 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001175}
1176
Dan Gohman98ca4f22009-08-05 01:29:28 +00001177void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001178 SDValue Chain, SDValue &Arg,
1179 RegsToPassVector &RegsToPass,
1180 CCValAssign &VA, CCValAssign &NextVA,
1181 SDValue &StackPtr,
1182 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001183 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001184
Jim Grosbache5165492009-11-09 00:11:35 +00001185 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001186 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001187 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1188
1189 if (NextVA.isRegLoc())
1190 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1191 else {
1192 assert(NextVA.isMemLoc());
1193 if (StackPtr.getNode() == 0)
1194 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1195
Dan Gohman98ca4f22009-08-05 01:29:28 +00001196 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1197 dl, DAG, NextVA,
1198 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001199 }
1200}
1201
Dan Gohman98ca4f22009-08-05 01:29:28 +00001202/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001203/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1204/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001205SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001206ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001207 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001208 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001209 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001210 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001211 const SmallVectorImpl<ISD::InputArg> &Ins,
1212 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001213 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001214 MachineFunction &MF = DAG.getMachineFunction();
1215 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1216 bool IsSibCall = false;
Bob Wilson703af3a2010-08-13 22:43:33 +00001217 // Temporarily disable tail calls so things don't break.
Evan Cheng0b655992011-05-20 17:38:48 +00001218 if (!EnableARMTailCalls)
Bob Wilson703af3a2010-08-13 22:43:33 +00001219 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001220 if (isTailCall) {
1221 // Check if it's really possible to do a tail call.
1222 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1223 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001224 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001225 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1226 // detected sibcalls.
1227 if (isTailCall) {
1228 ++NumTailCalls;
1229 IsSibCall = true;
1230 }
1231 }
Evan Chenga8e29892007-01-19 07:51:42 +00001232
Bob Wilson1f595bb2009-04-17 19:07:39 +00001233 // Analyze operands of the call, assigning locations to each operand.
1234 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001235 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1236 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001237 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001238 CCAssignFnForNode(CallConv, /* Return*/ false,
1239 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001240
Bob Wilson1f595bb2009-04-17 19:07:39 +00001241 // Get a count of how many bytes are to be pushed on the stack.
1242 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001243
Dale Johannesen51e28e62010-06-03 21:09:53 +00001244 // For tail calls, memory operands are available in our caller's stack.
1245 if (IsSibCall)
1246 NumBytes = 0;
1247
Evan Chenga8e29892007-01-19 07:51:42 +00001248 // Adjust the stack pointer for the new arguments...
1249 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001250 if (!IsSibCall)
1251 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001252
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001253 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001254
Bob Wilson5bafff32009-06-22 23:27:02 +00001255 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001256 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001257
Bob Wilson1f595bb2009-04-17 19:07:39 +00001258 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001259 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001260 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1261 i != e;
1262 ++i, ++realArgIdx) {
1263 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001264 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001265 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001266 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001267
Bob Wilson1f595bb2009-04-17 19:07:39 +00001268 // Promote the value if needed.
1269 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001270 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001271 case CCValAssign::Full: break;
1272 case CCValAssign::SExt:
1273 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1274 break;
1275 case CCValAssign::ZExt:
1276 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1277 break;
1278 case CCValAssign::AExt:
1279 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1280 break;
1281 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001282 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001283 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001284 }
1285
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001286 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001287 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001288 if (VA.getLocVT() == MVT::v2f64) {
1289 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1290 DAG.getConstant(0, MVT::i32));
1291 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1292 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001293
Dan Gohman98ca4f22009-08-05 01:29:28 +00001294 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001295 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1296
1297 VA = ArgLocs[++i]; // skip ahead to next loc
1298 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001299 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001300 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1301 } else {
1302 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001303
Dan Gohman98ca4f22009-08-05 01:29:28 +00001304 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1305 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001306 }
1307 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001308 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001309 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001310 }
1311 } else if (VA.isRegLoc()) {
1312 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001313 } else if (isByVal) {
1314 assert(VA.isMemLoc());
1315 unsigned offset = 0;
1316
1317 // True if this byval aggregate will be split between registers
1318 // and memory.
1319 if (CCInfo.isFirstByValRegValid()) {
1320 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1321 unsigned int i, j;
1322 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1323 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1324 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1325 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1326 MachinePointerInfo(),
1327 false, false, 0);
1328 MemOpChains.push_back(Load.getValue(1));
1329 RegsToPass.push_back(std::make_pair(j, Load));
1330 }
1331 offset = ARM::R4 - CCInfo.getFirstByValReg();
1332 CCInfo.clearFirstByValReg();
1333 }
1334
1335 unsigned LocMemOffset = VA.getLocMemOffset();
1336 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1337 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1338 StkPtrOff);
1339 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1340 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1341 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1342 MVT::i32);
1343 MemOpChains.push_back(DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode,
1344 Flags.getByValAlign(),
1345 /*isVolatile=*/false,
1346 /*AlwaysInline=*/false,
1347 MachinePointerInfo(0),
1348 MachinePointerInfo(0)));
1349
1350 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001351 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001352
Dan Gohman98ca4f22009-08-05 01:29:28 +00001353 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1354 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001355 }
Evan Chenga8e29892007-01-19 07:51:42 +00001356 }
1357
1358 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001359 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001360 &MemOpChains[0], MemOpChains.size());
1361
1362 // Build a sequence of copy-to-reg nodes chained together with token chain
1363 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001364 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001365 // Tail call byval lowering might overwrite argument registers so in case of
1366 // tail call optimization the copies to registers are lowered later.
1367 if (!isTailCall)
1368 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1369 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1370 RegsToPass[i].second, InFlag);
1371 InFlag = Chain.getValue(1);
1372 }
Evan Chenga8e29892007-01-19 07:51:42 +00001373
Dale Johannesen51e28e62010-06-03 21:09:53 +00001374 // For tail calls lower the arguments to the 'real' stack slot.
1375 if (isTailCall) {
1376 // Force all the incoming stack arguments to be loaded from the stack
1377 // before any new outgoing arguments are stored to the stack, because the
1378 // outgoing stack slots may alias the incoming argument stack slots, and
1379 // the alias isn't otherwise explicit. This is slightly more conservative
1380 // than necessary, because it means that each store effectively depends
1381 // on every argument instead of just those arguments it would clobber.
1382
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001383 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001384 InFlag = SDValue();
1385 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1386 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1387 RegsToPass[i].second, InFlag);
1388 InFlag = Chain.getValue(1);
1389 }
1390 InFlag =SDValue();
1391 }
1392
Bill Wendling056292f2008-09-16 21:48:12 +00001393 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1394 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1395 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001396 bool isDirect = false;
1397 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001398 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001399 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001400
1401 if (EnableARMLongCalls) {
1402 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1403 && "long-calls with non-static relocation model!");
1404 // Handle a global address or an external symbol. If it's not one of
1405 // those, the target's already in a register, so we don't need to do
1406 // anything extra.
1407 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001408 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001409 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001410 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001411 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
1412 ARMPCLabelIndex,
1413 ARMCP::CPValue, 0);
1414 // Get the address of the callee into a register
1415 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1416 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1417 Callee = DAG.getLoad(getPointerTy(), dl,
1418 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001419 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001420 false, false, 0);
1421 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1422 const char *Sym = S->getSymbol();
1423
1424 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001425 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbache7b52522010-04-14 22:28:31 +00001426 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
1427 Sym, ARMPCLabelIndex, 0);
1428 // Get the address of the callee into a register
1429 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1430 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1431 Callee = DAG.getLoad(getPointerTy(), dl,
1432 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001433 MachinePointerInfo::getConstantPool(),
Jim Grosbache7b52522010-04-14 22:28:31 +00001434 false, false, 0);
1435 }
1436 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001437 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001438 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001439 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001440 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001441 getTargetMachine().getRelocationModel() != Reloc::Static;
1442 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001443 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001444 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001445 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001446 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001447 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00001448 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00001449 ARMPCLabelIndex,
1450 ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001451 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001452 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001453 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001454 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001455 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001456 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001457 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001458 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001459 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001460 } else {
1461 // On ELF targets for PIC code, direct calls should go through the PLT
1462 unsigned OpFlags = 0;
1463 if (Subtarget->isTargetELF() &&
1464 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1465 OpFlags = ARMII::MO_PLT;
1466 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1467 }
Bill Wendling056292f2008-09-16 21:48:12 +00001468 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001469 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001470 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001471 getTargetMachine().getRelocationModel() != Reloc::Static;
1472 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001473 // tBX takes a register source operand.
1474 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001475 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001476 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson1d0be152009-08-13 21:58:54 +00001477 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
Evan Chenge4e4ed32009-08-28 23:18:09 +00001478 Sym, ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001479 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001480 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001481 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001482 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001483 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001484 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001485 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001486 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001487 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001488 } else {
1489 unsigned OpFlags = 0;
1490 // On ELF targets for PIC code, direct calls should go through the PLT
1491 if (Subtarget->isTargetELF() &&
1492 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1493 OpFlags = ARMII::MO_PLT;
1494 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1495 }
Evan Chenga8e29892007-01-19 07:51:42 +00001496 }
1497
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001498 // FIXME: handle tail calls differently.
1499 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001500 if (Subtarget->isThumb()) {
1501 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001502 CallOpc = ARMISD::CALL_NOLINK;
1503 else
1504 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1505 } else {
1506 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001507 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1508 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001509 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001510
Dan Gohman475871a2008-07-27 21:46:04 +00001511 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001512 Ops.push_back(Chain);
1513 Ops.push_back(Callee);
1514
1515 // Add argument registers to the end of the list so that they are known live
1516 // into the call.
1517 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1518 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1519 RegsToPass[i].second.getValueType()));
1520
Gabor Greifba36cb52008-08-28 21:40:38 +00001521 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001522 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001523
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001524 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001525 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001526 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001527
Duncan Sands4bdcb612008-07-02 17:40:58 +00001528 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001529 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001530 InFlag = Chain.getValue(1);
1531
Chris Lattnere563bbc2008-10-11 22:08:30 +00001532 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1533 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001534 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001535 InFlag = Chain.getValue(1);
1536
Bob Wilson1f595bb2009-04-17 19:07:39 +00001537 // Handle result values, copying them out of physregs into vregs that we
1538 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001539 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1540 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001541}
1542
Stuart Hastingsf222e592011-02-28 17:17:53 +00001543/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001544/// on the stack. Remember the next parameter register to allocate,
1545/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001546/// this.
1547void
Stuart Hastingsc7315872011-04-20 16:47:52 +00001548llvm::ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
1549 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1550 assert((State->getCallOrPrologue() == Prologue ||
1551 State->getCallOrPrologue() == Call) &&
1552 "unhandled ParmContext");
1553 if ((!State->isFirstByValRegValid()) &&
1554 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1555 State->setFirstByValReg(reg);
1556 // At a call site, a byval parameter that is split between
1557 // registers and memory needs its size truncated here. In a
1558 // function prologue, such byval parameters are reassembled in
1559 // memory, and are not truncated.
1560 if (State->getCallOrPrologue() == Call) {
1561 unsigned excess = 4 * (ARM::R4 - reg);
1562 assert(size >= excess && "expected larger existing stack allocation");
1563 size -= excess;
1564 }
1565 }
1566 // Confiscate any remaining parameter registers to preclude their
1567 // assignment to subsequent parameters.
1568 while (State->AllocateReg(GPRArgRegs, 4))
1569 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001570}
1571
Dale Johannesen51e28e62010-06-03 21:09:53 +00001572/// MatchingStackOffset - Return true if the given stack call argument is
1573/// already available in the same position (relatively) of the caller's
1574/// incoming argument stack.
1575static
1576bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1577 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1578 const ARMInstrInfo *TII) {
1579 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1580 int FI = INT_MAX;
1581 if (Arg.getOpcode() == ISD::CopyFromReg) {
1582 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001583 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001584 return false;
1585 MachineInstr *Def = MRI->getVRegDef(VR);
1586 if (!Def)
1587 return false;
1588 if (!Flags.isByVal()) {
1589 if (!TII->isLoadFromStackSlot(Def, FI))
1590 return false;
1591 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001592 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001593 }
1594 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1595 if (Flags.isByVal())
1596 // ByVal argument is passed in as a pointer but it's now being
1597 // dereferenced. e.g.
1598 // define @foo(%struct.X* %A) {
1599 // tail call @bar(%struct.X* byval %A)
1600 // }
1601 return false;
1602 SDValue Ptr = Ld->getBasePtr();
1603 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1604 if (!FINode)
1605 return false;
1606 FI = FINode->getIndex();
1607 } else
1608 return false;
1609
1610 assert(FI != INT_MAX);
1611 if (!MFI->isFixedObjectIndex(FI))
1612 return false;
1613 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1614}
1615
1616/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1617/// for tail call optimization. Targets which want to do tail call
1618/// optimization should implement this function.
1619bool
1620ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1621 CallingConv::ID CalleeCC,
1622 bool isVarArg,
1623 bool isCalleeStructRet,
1624 bool isCallerStructRet,
1625 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001626 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001627 const SmallVectorImpl<ISD::InputArg> &Ins,
1628 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001629 const Function *CallerF = DAG.getMachineFunction().getFunction();
1630 CallingConv::ID CallerCC = CallerF->getCallingConv();
1631 bool CCMatch = CallerCC == CalleeCC;
1632
1633 // Look for obvious safe cases to perform tail call optimization that do not
1634 // require ABI changes. This is what gcc calls sibcall.
1635
Jim Grosbach7616b642010-06-16 23:45:49 +00001636 // Do not sibcall optimize vararg calls unless the call site is not passing
1637 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001638 if (isVarArg && !Outs.empty())
1639 return false;
1640
1641 // Also avoid sibcall optimization if either caller or callee uses struct
1642 // return semantics.
1643 if (isCalleeStructRet || isCallerStructRet)
1644 return false;
1645
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001646 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001647 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1648 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1649 // support in the assembler and linker to be used. This would need to be
1650 // fixed to fully support tail calls in Thumb1.
1651 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001652 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1653 // LR. This means if we need to reload LR, it takes an extra instructions,
1654 // which outweighs the value of the tail call; but here we don't know yet
1655 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001656 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001657 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001658
1659 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1660 // but we need to make sure there are enough registers; the only valid
1661 // registers are the 4 used for parameters. We don't currently do this
1662 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001663 if (Subtarget->isThumb1Only())
1664 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001665
Dale Johannesen51e28e62010-06-03 21:09:53 +00001666 // If the calling conventions do not match, then we'd better make sure the
1667 // results are returned in the same way as what the caller expects.
1668 if (!CCMatch) {
1669 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001670 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1671 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001672 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1673
1674 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001675 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1676 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001677 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1678
1679 if (RVLocs1.size() != RVLocs2.size())
1680 return false;
1681 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1682 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1683 return false;
1684 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1685 return false;
1686 if (RVLocs1[i].isRegLoc()) {
1687 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1688 return false;
1689 } else {
1690 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1691 return false;
1692 }
1693 }
1694 }
1695
1696 // If the callee takes no arguments then go on to check the results of the
1697 // call.
1698 if (!Outs.empty()) {
1699 // Check if stack adjustment is needed. For now, do not do this if any
1700 // argument is passed on the stack.
1701 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001702 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1703 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001704 CCInfo.AnalyzeCallOperands(Outs,
1705 CCAssignFnForNode(CalleeCC, false, isVarArg));
1706 if (CCInfo.getNextStackOffset()) {
1707 MachineFunction &MF = DAG.getMachineFunction();
1708
1709 // Check if the arguments are already laid out in the right way as
1710 // the caller's fixed stack objects.
1711 MachineFrameInfo *MFI = MF.getFrameInfo();
1712 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1713 const ARMInstrInfo *TII =
1714 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001715 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1716 i != e;
1717 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001718 CCValAssign &VA = ArgLocs[i];
1719 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001720 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001721 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001722 if (VA.getLocInfo() == CCValAssign::Indirect)
1723 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001724 if (VA.needsCustom()) {
1725 // f64 and vector types are split into multiple registers or
1726 // register/stack-slot combinations. The types will not match
1727 // the registers; give up on memory f64 refs until we figure
1728 // out what to do about this.
1729 if (!VA.isRegLoc())
1730 return false;
1731 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001732 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001733 if (RegVT == MVT::v2f64) {
1734 if (!ArgLocs[++i].isRegLoc())
1735 return false;
1736 if (!ArgLocs[++i].isRegLoc())
1737 return false;
1738 }
1739 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001740 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1741 MFI, MRI, TII))
1742 return false;
1743 }
1744 }
1745 }
1746 }
1747
1748 return true;
1749}
1750
Dan Gohman98ca4f22009-08-05 01:29:28 +00001751SDValue
1752ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001753 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001754 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001755 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001756 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001757
Bob Wilsondee46d72009-04-17 20:35:10 +00001758 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001759 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001760
Bob Wilsondee46d72009-04-17 20:35:10 +00001761 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001762 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1763 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001764
Dan Gohman98ca4f22009-08-05 01:29:28 +00001765 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001766 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1767 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001768
1769 // If this is the first return lowered for this function, add
1770 // the regs to the liveout set for the function.
1771 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1772 for (unsigned i = 0; i != RVLocs.size(); ++i)
1773 if (RVLocs[i].isRegLoc())
1774 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001775 }
1776
Bob Wilson1f595bb2009-04-17 19:07:39 +00001777 SDValue Flag;
1778
1779 // Copy the result values into the output registers.
1780 for (unsigned i = 0, realRVLocIdx = 0;
1781 i != RVLocs.size();
1782 ++i, ++realRVLocIdx) {
1783 CCValAssign &VA = RVLocs[i];
1784 assert(VA.isRegLoc() && "Can only return in registers!");
1785
Dan Gohmanc9403652010-07-07 15:54:55 +00001786 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001787
1788 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001789 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001790 case CCValAssign::Full: break;
1791 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001792 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001793 break;
1794 }
1795
Bob Wilson1f595bb2009-04-17 19:07:39 +00001796 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001797 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001798 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001799 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1800 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001801 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001802 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001803
1804 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1805 Flag = Chain.getValue(1);
1806 VA = RVLocs[++i]; // skip ahead to next loc
1807 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1808 HalfGPRs.getValue(1), Flag);
1809 Flag = Chain.getValue(1);
1810 VA = RVLocs[++i]; // skip ahead to next loc
1811
1812 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001813 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1814 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001815 }
1816 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1817 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001818 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001819 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001820 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001821 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001822 VA = RVLocs[++i]; // skip ahead to next loc
1823 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1824 Flag);
1825 } else
1826 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1827
Bob Wilsondee46d72009-04-17 20:35:10 +00001828 // Guarantee that all emitted copies are
1829 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001830 Flag = Chain.getValue(1);
1831 }
1832
1833 SDValue result;
1834 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001835 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001836 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001837 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001838
1839 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001840}
1841
Evan Cheng3d2125c2010-11-30 23:55:39 +00001842bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1843 if (N->getNumValues() != 1)
1844 return false;
1845 if (!N->hasNUsesOfValue(1, 0))
1846 return false;
1847
1848 unsigned NumCopies = 0;
1849 SDNode* Copies[2];
1850 SDNode *Use = *N->use_begin();
1851 if (Use->getOpcode() == ISD::CopyToReg) {
1852 Copies[NumCopies++] = Use;
1853 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1854 // f64 returned in a pair of GPRs.
1855 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1856 UI != UE; ++UI) {
1857 if (UI->getOpcode() != ISD::CopyToReg)
1858 return false;
1859 Copies[UI.getUse().getResNo()] = *UI;
1860 ++NumCopies;
1861 }
1862 } else if (Use->getOpcode() == ISD::BITCAST) {
1863 // f32 returned in a single GPR.
1864 if (!Use->hasNUsesOfValue(1, 0))
1865 return false;
1866 Use = *Use->use_begin();
1867 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1868 return false;
1869 Copies[NumCopies++] = Use;
1870 } else {
1871 return false;
1872 }
1873
1874 if (NumCopies != 1 && NumCopies != 2)
1875 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001876
1877 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001878 for (unsigned i = 0; i < NumCopies; ++i) {
1879 SDNode *Copy = Copies[i];
1880 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1881 UI != UE; ++UI) {
1882 if (UI->getOpcode() == ISD::CopyToReg) {
1883 SDNode *Use = *UI;
1884 if (Use == Copies[0] || Use == Copies[1])
1885 continue;
1886 return false;
1887 }
1888 if (UI->getOpcode() != ARMISD::RET_FLAG)
1889 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001890 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001891 }
1892 }
1893
Evan Cheng1bf891a2010-12-01 22:59:46 +00001894 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001895}
1896
Evan Cheng485fafc2011-03-21 01:19:09 +00001897bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1898 if (!EnableARMTailCalls)
1899 return false;
1900
1901 if (!CI->isTailCall())
1902 return false;
1903
1904 return !Subtarget->isThumb1Only();
1905}
1906
Bob Wilsonb62d2572009-11-03 00:02:05 +00001907// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1908// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1909// one of the above mentioned nodes. It has to be wrapped because otherwise
1910// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1911// be used to form addressing mode. These wrapped nodes will be selected
1912// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001913static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001914 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001915 // FIXME there is no actual debug info here
1916 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001917 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001918 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001919 if (CP->isMachineConstantPoolEntry())
1920 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1921 CP->getAlignment());
1922 else
1923 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1924 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001925 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001926}
1927
Jim Grosbache1102ca2010-07-19 17:20:38 +00001928unsigned ARMTargetLowering::getJumpTableEncoding() const {
1929 return MachineJumpTableInfo::EK_Inline;
1930}
1931
Dan Gohmand858e902010-04-17 15:26:15 +00001932SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1933 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001934 MachineFunction &MF = DAG.getMachineFunction();
1935 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1936 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001937 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001938 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001939 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001940 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1941 SDValue CPAddr;
1942 if (RelocM == Reloc::Static) {
1943 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1944 } else {
1945 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001946 ARMPCLabelIndex = AFI->createPICLabelUId();
Bob Wilson907eebd2009-11-02 20:59:23 +00001947 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(BA, ARMPCLabelIndex,
1948 ARMCP::CPBlockAddress,
1949 PCAdj);
1950 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1951 }
1952 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1953 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001954 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001955 false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001956 if (RelocM == Reloc::Static)
1957 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001958 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001959 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001960}
1961
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001962// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001963SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001964ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001965 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001966 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001967 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001968 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001969 MachineFunction &MF = DAG.getMachineFunction();
1970 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001971 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001972 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00001973 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00001974 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001975 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001976 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001977 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001978 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00001979 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001980 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001981
Evan Chenge7e0d622009-11-06 22:24:13 +00001982 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001983 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001984
1985 // call __tls_get_addr.
1986 ArgListTy Args;
1987 ArgListEntry Entry;
1988 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001989 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001990 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001991 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00001992 std::pair<SDValue, SDValue> CallResult =
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001993 LowerCallTo(Chain, (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00001994 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001995 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00001996 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001997 return CallResult.first;
1998}
1999
2000// Lower ISD::GlobalTLSAddress using the "initial exec" or
2001// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00002002SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002003ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002004 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002005 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002006 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002007 SDValue Offset;
2008 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002009 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002010 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002011 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002012
Chris Lattner4fb63d02009-07-15 04:12:33 +00002013 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002014 MachineFunction &MF = DAG.getMachineFunction();
2015 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002016 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002017 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002018 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2019 ARMConstantPoolValue *CPV =
Evan Chenge4e4ed32009-08-28 23:18:09 +00002020 new ARMConstantPoolValue(GA->getGlobal(), ARMPCLabelIndex,
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002021 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002022 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002023 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002024 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002025 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002026 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002027 Chain = Offset.getValue(1);
2028
Evan Chenge7e0d622009-11-06 22:24:13 +00002029 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002030 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002031
Evan Cheng9eda6892009-10-31 03:39:36 +00002032 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002033 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002034 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002035 } else {
2036 // local exec model
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002037 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002038 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002039 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002040 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002041 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002042 false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002043 }
2044
2045 // The address of the thread local variable is the add of the thread
2046 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002047 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002048}
2049
Dan Gohman475871a2008-07-27 21:46:04 +00002050SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002051ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002052 // TODO: implement the "local dynamic" model
2053 assert(Subtarget->isTargetELF() &&
2054 "TLS not implemented for non-ELF targets");
2055 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2056 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
2057 // otherwise use the "Local Exec" TLS Model
2058 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
2059 return LowerToTLSGeneralDynamicModel(GA, DAG);
2060 else
2061 return LowerToTLSExecModels(GA, DAG);
2062}
2063
Dan Gohman475871a2008-07-27 21:46:04 +00002064SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002065 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002066 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002067 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002068 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002069 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2070 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002071 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002072 ARMConstantPoolValue *CPV =
Jim Grosbach3a2429a2010-11-09 21:36:17 +00002073 new ARMConstantPoolValue(GV, UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002074 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002075 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002076 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002077 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002078 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002079 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002080 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002081 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002082 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002083 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002084 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002085 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002086 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002087 }
2088
2089 // If we have T2 ops, we can materialize the address directly via movt/movw
2090 // pair. This is always cheaper.
2091 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002092 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002093 // FIXME: Once remat is capable of dealing with instructions with register
2094 // operands, expand this into two nodes.
2095 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2096 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002097 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002098 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2099 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2100 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2101 MachinePointerInfo::getConstantPool(),
2102 false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002103 }
2104}
2105
Dan Gohman475871a2008-07-27 21:46:04 +00002106SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002107 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002108 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002109 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002110 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002111 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002112 MachineFunction &MF = DAG.getMachineFunction();
2113 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2114
Evan Cheng4abce0c2011-05-27 20:11:27 +00002115 // FIXME: Enable this for static codegen when tool issues are fixed.
2116 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002117 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002118 // FIXME: Once remat is capable of dealing with instructions with register
2119 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002120 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002121 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2122 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2123
Evan Cheng53519f02011-01-21 18:55:51 +00002124 unsigned Wrapper = (RelocM == Reloc::PIC_)
2125 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2126 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002127 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002128 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2129 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2130 MachinePointerInfo::getGOT(), false, false, 0);
2131 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002132 }
2133
2134 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002135 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002136 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002137 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002138 } else {
2139 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002140 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2141 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002142 new ARMConstantPoolValue(GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002143 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002144 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002145 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002146
Evan Cheng9eda6892009-10-31 03:39:36 +00002147 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002148 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002149 false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002150 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002151
2152 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002153 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002154 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002155 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002156
Evan Cheng63476a82009-09-03 07:04:02 +00002157 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002158 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
David Greene1b58cab2010-02-15 16:55:24 +00002159 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002160
2161 return Result;
2162}
2163
Dan Gohman475871a2008-07-27 21:46:04 +00002164SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002165 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002166 assert(Subtarget->isTargetELF() &&
2167 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002168 MachineFunction &MF = DAG.getMachineFunction();
2169 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002170 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002171 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002172 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002173 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Owen Anderson1d0be152009-08-13 21:58:54 +00002174 ARMConstantPoolValue *CPV = new ARMConstantPoolValue(*DAG.getContext(),
2175 "_GLOBAL_OFFSET_TABLE_",
Evan Chenge4e4ed32009-08-28 23:18:09 +00002176 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002177 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002178 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002179 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002180 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002181 false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002182 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002183 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002184}
2185
Jim Grosbach0e0da732009-05-12 23:59:14 +00002186SDValue
Jim Grosbache4ad3872010-10-19 23:27:08 +00002187ARMTargetLowering::LowerEH_SJLJ_DISPATCHSETUP(SDValue Op, SelectionDAG &DAG)
2188 const {
2189 DebugLoc dl = Op.getDebugLoc();
2190 return DAG.getNode(ARMISD::EH_SJLJ_DISPATCHSETUP, dl, MVT::Other,
Bill Wendling61512ba2011-05-11 01:11:55 +00002191 Op.getOperand(0), Op.getOperand(1));
Jim Grosbache4ad3872010-10-19 23:27:08 +00002192}
2193
2194SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002195ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2196 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002197 SDValue Val = DAG.getConstant(0, MVT::i32);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002198 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl, MVT::i32, Op.getOperand(0),
2199 Op.getOperand(1), Val);
2200}
2201
2202SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002203ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2204 DebugLoc dl = Op.getDebugLoc();
2205 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2206 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2207}
2208
2209SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002210ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002211 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002212 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002213 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002214 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002215 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002216 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002217 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002218 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2219 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002220 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002221 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002222 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002223 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002224 EVT PtrVT = getPointerTy();
2225 DebugLoc dl = Op.getDebugLoc();
2226 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2227 SDValue CPAddr;
2228 unsigned PCAdj = (RelocM != Reloc::PIC_)
2229 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002230 ARMConstantPoolValue *CPV =
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +00002231 new ARMConstantPoolValue(MF.getFunction(), ARMPCLabelIndex,
2232 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002233 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002234 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002235 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002236 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002237 MachinePointerInfo::getConstantPool(),
David Greene1b58cab2010-02-15 16:55:24 +00002238 false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002239
2240 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002241 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002242 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2243 }
2244 return Result;
2245 }
Evan Cheng92e39162011-03-29 23:06:19 +00002246 case Intrinsic::arm_neon_vmulls:
2247 case Intrinsic::arm_neon_vmullu: {
2248 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2249 ? ARMISD::VMULLs : ARMISD::VMULLu;
2250 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2251 Op.getOperand(1), Op.getOperand(2));
2252 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002253 }
2254}
2255
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002256static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002257 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002258 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002259 if (!Subtarget->hasDataBarrier()) {
2260 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2261 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2262 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002263 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002264 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002265 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002266 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002267 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002268
2269 SDValue Op5 = Op.getOperand(5);
2270 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2271 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2272 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2273 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2274
2275 ARM_MB::MemBOpt DMBOpt;
2276 if (isDeviceBarrier)
2277 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2278 else
2279 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2280 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2281 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002282}
2283
Eli Friedman26689ac2011-08-03 21:06:02 +00002284
2285static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2286 const ARMSubtarget *Subtarget) {
2287 // FIXME: handle "fence singlethread" more efficiently.
2288 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002289 if (!Subtarget->hasDataBarrier()) {
2290 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2291 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2292 // here.
2293 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2294 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002295 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002296 DAG.getConstant(0, MVT::i32));
2297 }
2298
Eli Friedman26689ac2011-08-03 21:06:02 +00002299 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002300 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002301}
2302
Evan Chengdfed19f2010-11-03 06:34:55 +00002303static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2304 const ARMSubtarget *Subtarget) {
2305 // ARM pre v5TE and Thumb1 does not have preload instructions.
2306 if (!(Subtarget->isThumb2() ||
2307 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2308 // Just preserve the chain.
2309 return Op.getOperand(0);
2310
2311 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002312 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2313 if (!isRead &&
2314 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2315 // ARMv7 with MP extension has PLDW.
2316 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002317
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002318 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2319 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002320 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002321 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002322 isData = ~isData & 1;
2323 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002324
2325 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002326 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2327 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002328}
2329
Dan Gohman1e93df62010-04-17 14:41:14 +00002330static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2331 MachineFunction &MF = DAG.getMachineFunction();
2332 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2333
Evan Chenga8e29892007-01-19 07:51:42 +00002334 // vastart just stores the address of the VarArgsFrameIndex slot into the
2335 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002336 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002337 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002338 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002339 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002340 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2341 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002342}
2343
Dan Gohman475871a2008-07-27 21:46:04 +00002344SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002345ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2346 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002347 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002348 MachineFunction &MF = DAG.getMachineFunction();
2349 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2350
2351 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002352 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002353 RC = ARM::tGPRRegisterClass;
2354 else
2355 RC = ARM::GPRRegisterClass;
2356
2357 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002358 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002359 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002360
2361 SDValue ArgValue2;
2362 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002363 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002364 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002365
2366 // Create load node to retrieve arguments from the stack.
2367 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002368 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002369 MachinePointerInfo::getFixedStack(FI),
David Greene1b58cab2010-02-15 16:55:24 +00002370 false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002371 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002372 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002373 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002374 }
2375
Jim Grosbache5165492009-11-09 00:11:35 +00002376 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002377}
2378
Stuart Hastingsc7315872011-04-20 16:47:52 +00002379void
2380ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2381 unsigned &VARegSize, unsigned &VARegSaveSize)
2382 const {
2383 unsigned NumGPRs;
2384 if (CCInfo.isFirstByValRegValid())
2385 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2386 else {
2387 unsigned int firstUnalloced;
2388 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2389 sizeof(GPRArgRegs) /
2390 sizeof(GPRArgRegs[0]));
2391 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2392 }
2393
2394 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2395 VARegSize = NumGPRs * 4;
2396 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2397}
2398
2399// The remaining GPRs hold either the beginning of variable-argument
2400// data, or the beginning of an aggregate passed by value (usuall
2401// byval). Either way, we allocate stack slots adjacent to the data
2402// provided by our caller, and store the unallocated registers there.
2403// If this is a variadic function, the va_list pointer will begin with
2404// these values; otherwise, this reassembles a (byval) structure that
2405// was split between registers and memory.
2406void
2407ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2408 DebugLoc dl, SDValue &Chain,
2409 unsigned ArgOffset) const {
2410 MachineFunction &MF = DAG.getMachineFunction();
2411 MachineFrameInfo *MFI = MF.getFrameInfo();
2412 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2413 unsigned firstRegToSaveIndex;
2414 if (CCInfo.isFirstByValRegValid())
2415 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2416 else {
2417 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2418 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2419 }
2420
2421 unsigned VARegSize, VARegSaveSize;
2422 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2423 if (VARegSaveSize) {
2424 // If this function is vararg, store any remaining integer argument regs
2425 // to their spots on the stack so that they may be loaded by deferencing
2426 // the result of va_next.
2427 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002428 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2429 ArgOffset + VARegSaveSize
2430 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002431 false));
2432 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2433 getPointerTy());
2434
2435 SmallVector<SDValue, 4> MemOps;
2436 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
2437 TargetRegisterClass *RC;
2438 if (AFI->isThumb1OnlyFunction())
2439 RC = ARM::tGPRRegisterClass;
2440 else
2441 RC = ARM::GPRRegisterClass;
2442
2443 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2444 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2445 SDValue Store =
2446 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002447 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002448 false, false, 0);
2449 MemOps.push_back(Store);
2450 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2451 DAG.getConstant(4, getPointerTy()));
2452 }
2453 if (!MemOps.empty())
2454 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2455 &MemOps[0], MemOps.size());
2456 } else
2457 // This will point to the next argument passed via stack.
2458 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2459}
2460
Bob Wilson5bafff32009-06-22 23:27:02 +00002461SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002462ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002463 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002464 const SmallVectorImpl<ISD::InputArg>
2465 &Ins,
2466 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002467 SmallVectorImpl<SDValue> &InVals)
2468 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002469 MachineFunction &MF = DAG.getMachineFunction();
2470 MachineFrameInfo *MFI = MF.getFrameInfo();
2471
Bob Wilson1f595bb2009-04-17 19:07:39 +00002472 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2473
2474 // Assign locations to all of the incoming arguments.
2475 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002476 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2477 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002478 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002479 CCAssignFnForNode(CallConv, /* Return*/ false,
2480 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002481
2482 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002483 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002484
Stuart Hastingsf222e592011-02-28 17:17:53 +00002485 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002486 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2487 CCValAssign &VA = ArgLocs[i];
2488
Bob Wilsondee46d72009-04-17 20:35:10 +00002489 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002490 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002491 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002492
Bob Wilson1f595bb2009-04-17 19:07:39 +00002493 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002494 // f64 and vector types are split up into multiple registers or
2495 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002496 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002497 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002498 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002499 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002500 SDValue ArgValue2;
2501 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002502 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002503 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2504 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002505 MachinePointerInfo::getFixedStack(FI),
Bob Wilson6a234f02010-04-13 22:03:22 +00002506 false, false, 0);
2507 } else {
2508 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2509 Chain, DAG, dl);
2510 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002511 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2512 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002513 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002514 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002515 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2516 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002517 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002518
Bob Wilson5bafff32009-06-22 23:27:02 +00002519 } else {
2520 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002521
Owen Anderson825b72b2009-08-11 20:47:22 +00002522 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002523 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002524 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002525 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002526 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002527 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002528 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002529 RC = (AFI->isThumb1OnlyFunction() ?
2530 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002531 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002532 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002533
2534 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002535 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002536 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002537 }
2538
2539 // If this is an 8 or 16-bit value, it is really passed promoted
2540 // to 32 bits. Insert an assert[sz]ext to capture this, then
2541 // truncate to the right size.
2542 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002543 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002544 case CCValAssign::Full: break;
2545 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002546 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002547 break;
2548 case CCValAssign::SExt:
2549 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2550 DAG.getValueType(VA.getValVT()));
2551 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2552 break;
2553 case CCValAssign::ZExt:
2554 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2555 DAG.getValueType(VA.getValVT()));
2556 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2557 break;
2558 }
2559
Dan Gohman98ca4f22009-08-05 01:29:28 +00002560 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002561
2562 } else { // VA.isRegLoc()
2563
2564 // sanity check
2565 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002566 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002567
Stuart Hastingsf222e592011-02-28 17:17:53 +00002568 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002569
Stuart Hastingsf222e592011-02-28 17:17:53 +00002570 // Some Ins[] entries become multiple ArgLoc[] entries.
2571 // Process them only once.
2572 if (index != lastInsIndex)
2573 {
2574 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002575 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002576 // This can be changed with more analysis.
2577 // In case of tail call optimization mark all arguments mutable.
2578 // Since they could be overwritten by lowering of arguments in case of
2579 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002580 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002581 unsigned VARegSize, VARegSaveSize;
2582 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2583 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2584 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002585 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002586 int FI = MFI->CreateFixedObject(Bytes,
2587 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002588 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2589 } else {
2590 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2591 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002592
Stuart Hastingsf222e592011-02-28 17:17:53 +00002593 // Create load nodes to retrieve arguments from the stack.
2594 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2595 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2596 MachinePointerInfo::getFixedStack(FI),
2597 false, false, 0));
2598 }
2599 lastInsIndex = index;
2600 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002601 }
2602 }
2603
2604 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002605 if (isVarArg)
2606 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002607
Dan Gohman98ca4f22009-08-05 01:29:28 +00002608 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002609}
2610
2611/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002612static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002613 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002614 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002615 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002616 // Maybe this has already been legalized into the constant pool?
2617 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002618 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002619 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002620 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002621 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002622 }
2623 }
2624 return false;
2625}
2626
Evan Chenga8e29892007-01-19 07:51:42 +00002627/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2628/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002629SDValue
2630ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002631 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002632 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002633 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002634 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002635 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002636 // Constant does not fit, try adjusting it by one?
2637 switch (CC) {
2638 default: break;
2639 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002640 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002641 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002642 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002643 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002644 }
2645 break;
2646 case ISD::SETULT:
2647 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002648 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002649 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002650 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002651 }
2652 break;
2653 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002654 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002655 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002656 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002657 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002658 }
2659 break;
2660 case ISD::SETULE:
2661 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002662 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002663 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002664 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002665 }
2666 break;
2667 }
2668 }
2669 }
2670
2671 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002672 ARMISD::NodeType CompareType;
2673 switch (CondCode) {
2674 default:
2675 CompareType = ARMISD::CMP;
2676 break;
2677 case ARMCC::EQ:
2678 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002679 // Uses only Z Flag
2680 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002681 break;
2682 }
Evan Cheng218977b2010-07-13 19:27:42 +00002683 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002684 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002685}
2686
2687/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002688SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002689ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002690 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002691 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002692 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002693 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002694 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002695 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2696 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002697}
2698
Bob Wilson79f56c92011-03-08 01:17:20 +00002699/// duplicateCmp - Glue values can have only one use, so this function
2700/// duplicates a comparison node.
2701SDValue
2702ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2703 unsigned Opc = Cmp.getOpcode();
2704 DebugLoc DL = Cmp.getDebugLoc();
2705 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2706 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2707
2708 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2709 Cmp = Cmp.getOperand(0);
2710 Opc = Cmp.getOpcode();
2711 if (Opc == ARMISD::CMPFP)
2712 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2713 else {
2714 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2715 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2716 }
2717 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2718}
2719
Bill Wendlingde2b1512010-08-11 08:43:16 +00002720SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2721 SDValue Cond = Op.getOperand(0);
2722 SDValue SelectTrue = Op.getOperand(1);
2723 SDValue SelectFalse = Op.getOperand(2);
2724 DebugLoc dl = Op.getDebugLoc();
2725
2726 // Convert:
2727 //
2728 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2729 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2730 //
2731 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2732 const ConstantSDNode *CMOVTrue =
2733 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2734 const ConstantSDNode *CMOVFalse =
2735 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2736
2737 if (CMOVTrue && CMOVFalse) {
2738 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2739 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2740
2741 SDValue True;
2742 SDValue False;
2743 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2744 True = SelectTrue;
2745 False = SelectFalse;
2746 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2747 True = SelectFalse;
2748 False = SelectTrue;
2749 }
2750
2751 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002752 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002753 SDValue ARMcc = Cond.getOperand(2);
2754 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002755 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002756 assert(True.getValueType() == VT);
2757 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002758 }
2759 }
2760 }
2761
2762 return DAG.getSelectCC(dl, Cond,
2763 DAG.getConstant(0, Cond.getValueType()),
2764 SelectTrue, SelectFalse, ISD::SETNE);
2765}
2766
Dan Gohmand858e902010-04-17 15:26:15 +00002767SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002768 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002769 SDValue LHS = Op.getOperand(0);
2770 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002771 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002772 SDValue TrueVal = Op.getOperand(2);
2773 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002774 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002775
Owen Anderson825b72b2009-08-11 20:47:22 +00002776 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002777 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002778 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002779 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Jim Grosbachb04546f2011-09-13 20:30:37 +00002780 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002781 }
2782
2783 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002784 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002785
Evan Cheng218977b2010-07-13 19:27:42 +00002786 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2787 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002788 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002789 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002790 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002791 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002792 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002793 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002794 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002795 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002796 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002797 }
2798 return Result;
2799}
2800
Evan Cheng218977b2010-07-13 19:27:42 +00002801/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2802/// to morph to an integer compare sequence.
2803static bool canChangeToInt(SDValue Op, bool &SeenZero,
2804 const ARMSubtarget *Subtarget) {
2805 SDNode *N = Op.getNode();
2806 if (!N->hasOneUse())
2807 // Otherwise it requires moving the value from fp to integer registers.
2808 return false;
2809 if (!N->getNumValues())
2810 return false;
2811 EVT VT = Op.getValueType();
2812 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2813 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2814 // vmrs are very slow, e.g. cortex-a8.
2815 return false;
2816
2817 if (isFloatingPointZero(Op)) {
2818 SeenZero = true;
2819 return true;
2820 }
2821 return ISD::isNormalLoad(N);
2822}
2823
2824static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2825 if (isFloatingPointZero(Op))
2826 return DAG.getConstant(0, MVT::i32);
2827
2828 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2829 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002830 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002831 Ld->isVolatile(), Ld->isNonTemporal(),
2832 Ld->getAlignment());
2833
2834 llvm_unreachable("Unknown VFP cmp argument!");
2835}
2836
2837static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2838 SDValue &RetVal1, SDValue &RetVal2) {
2839 if (isFloatingPointZero(Op)) {
2840 RetVal1 = DAG.getConstant(0, MVT::i32);
2841 RetVal2 = DAG.getConstant(0, MVT::i32);
2842 return;
2843 }
2844
2845 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2846 SDValue Ptr = Ld->getBasePtr();
2847 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2848 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002849 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002850 Ld->isVolatile(), Ld->isNonTemporal(),
2851 Ld->getAlignment());
2852
2853 EVT PtrType = Ptr.getValueType();
2854 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2855 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2856 PtrType, Ptr, DAG.getConstant(4, PtrType));
2857 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2858 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002859 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002860 Ld->isVolatile(), Ld->isNonTemporal(),
2861 NewAlign);
2862 return;
2863 }
2864
2865 llvm_unreachable("Unknown VFP cmp argument!");
2866}
2867
2868/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2869/// f32 and even f64 comparisons to integer ones.
2870SDValue
2871ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2872 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002873 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002874 SDValue LHS = Op.getOperand(2);
2875 SDValue RHS = Op.getOperand(3);
2876 SDValue Dest = Op.getOperand(4);
2877 DebugLoc dl = Op.getDebugLoc();
2878
2879 bool SeenZero = false;
2880 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2881 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002882 // If one of the operand is zero, it's safe to ignore the NaN case since
2883 // we only care about equality comparisons.
2884 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Bob Wilson1b772f92011-03-08 01:17:16 +00002885 // If unsafe fp math optimization is enabled and there are no other uses of
2886 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00002887 // to an integer comparison.
2888 if (CC == ISD::SETOEQ)
2889 CC = ISD::SETEQ;
2890 else if (CC == ISD::SETUNE)
2891 CC = ISD::SETNE;
2892
2893 SDValue ARMcc;
2894 if (LHS.getValueType() == MVT::f32) {
2895 LHS = bitcastf32Toi32(LHS, DAG);
2896 RHS = bitcastf32Toi32(RHS, DAG);
2897 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2898 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2899 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2900 Chain, Dest, ARMcc, CCR, Cmp);
2901 }
2902
2903 SDValue LHS1, LHS2;
2904 SDValue RHS1, RHS2;
2905 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2906 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2907 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2908 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002909 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002910 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2911 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2912 }
2913
2914 return SDValue();
2915}
2916
2917SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2918 SDValue Chain = Op.getOperand(0);
2919 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2920 SDValue LHS = Op.getOperand(2);
2921 SDValue RHS = Op.getOperand(3);
2922 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002923 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002924
Owen Anderson825b72b2009-08-11 20:47:22 +00002925 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002926 SDValue ARMcc;
2927 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002928 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002929 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002930 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002931 }
2932
Owen Anderson825b72b2009-08-11 20:47:22 +00002933 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002934
2935 if (UnsafeFPMath &&
2936 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2937 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2938 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2939 if (Result.getNode())
2940 return Result;
2941 }
2942
Evan Chenga8e29892007-01-19 07:51:42 +00002943 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002944 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002945
Evan Cheng218977b2010-07-13 19:27:42 +00002946 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2947 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002948 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002949 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002950 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002951 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002952 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002953 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2954 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002955 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002956 }
2957 return Res;
2958}
2959
Dan Gohmand858e902010-04-17 15:26:15 +00002960SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002961 SDValue Chain = Op.getOperand(0);
2962 SDValue Table = Op.getOperand(1);
2963 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002964 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002965
Owen Andersone50ed302009-08-10 22:56:29 +00002966 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002967 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2968 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002969 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002970 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002971 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002972 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2973 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002974 if (Subtarget->isThumb2()) {
2975 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
2976 // which does another jump to the destination. This also makes it easier
2977 // to translate it to TBB / TBH later.
2978 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00002979 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00002980 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002981 }
Evan Cheng66ac5312009-07-25 00:33:29 +00002982 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00002983 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002984 MachinePointerInfo::getJumpTable(),
David Greene1b58cab2010-02-15 16:55:24 +00002985 false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002986 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002987 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00002988 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002989 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00002990 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002991 MachinePointerInfo::getJumpTable(), false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00002992 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00002993 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00002994 }
Evan Chenga8e29892007-01-19 07:51:42 +00002995}
2996
Bob Wilson76a312b2010-03-19 22:51:32 +00002997static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
2998 DebugLoc dl = Op.getDebugLoc();
2999 unsigned Opc;
3000
3001 switch (Op.getOpcode()) {
3002 default:
3003 assert(0 && "Invalid opcode!");
3004 case ISD::FP_TO_SINT:
3005 Opc = ARMISD::FTOSI;
3006 break;
3007 case ISD::FP_TO_UINT:
3008 Opc = ARMISD::FTOUI;
3009 break;
3010 }
3011 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003012 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003013}
3014
Cameron Zwarich3007d332011-03-29 21:41:55 +00003015static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3016 EVT VT = Op.getValueType();
3017 DebugLoc dl = Op.getDebugLoc();
3018
Duncan Sands1f6a3292011-08-12 14:54:45 +00003019 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3020 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003021 if (VT != MVT::v4f32)
3022 return DAG.UnrollVectorOp(Op.getNode());
3023
3024 unsigned CastOpc;
3025 unsigned Opc;
3026 switch (Op.getOpcode()) {
3027 default:
3028 assert(0 && "Invalid opcode!");
3029 case ISD::SINT_TO_FP:
3030 CastOpc = ISD::SIGN_EXTEND;
3031 Opc = ISD::SINT_TO_FP;
3032 break;
3033 case ISD::UINT_TO_FP:
3034 CastOpc = ISD::ZERO_EXTEND;
3035 Opc = ISD::UINT_TO_FP;
3036 break;
3037 }
3038
3039 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3040 return DAG.getNode(Opc, dl, VT, Op);
3041}
3042
Bob Wilson76a312b2010-03-19 22:51:32 +00003043static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3044 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003045 if (VT.isVector())
3046 return LowerVectorINT_TO_FP(Op, DAG);
3047
Bob Wilson76a312b2010-03-19 22:51:32 +00003048 DebugLoc dl = Op.getDebugLoc();
3049 unsigned Opc;
3050
3051 switch (Op.getOpcode()) {
3052 default:
3053 assert(0 && "Invalid opcode!");
3054 case ISD::SINT_TO_FP:
3055 Opc = ARMISD::SITOF;
3056 break;
3057 case ISD::UINT_TO_FP:
3058 Opc = ARMISD::UITOF;
3059 break;
3060 }
3061
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003062 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003063 return DAG.getNode(Opc, dl, VT, Op);
3064}
3065
Evan Cheng515fe3a2010-07-08 02:08:50 +00003066SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003067 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003068 SDValue Tmp0 = Op.getOperand(0);
3069 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003070 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003071 EVT VT = Op.getValueType();
3072 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003073 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3074 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3075 bool UseNEON = !InGPR && Subtarget->hasNEON();
3076
3077 if (UseNEON) {
3078 // Use VBSL to copy the sign bit.
3079 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3080 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3081 DAG.getTargetConstant(EncodedVal, MVT::i32));
3082 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3083 if (VT == MVT::f64)
3084 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3085 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3086 DAG.getConstant(32, MVT::i32));
3087 else /*if (VT == MVT::f32)*/
3088 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3089 if (SrcVT == MVT::f32) {
3090 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3091 if (VT == MVT::f64)
3092 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3093 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3094 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003095 } else if (VT == MVT::f32)
3096 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3097 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3098 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003099 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3100 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3101
3102 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3103 MVT::i32);
3104 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3105 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3106 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003107
Evan Chenge573fb32011-02-23 02:24:55 +00003108 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3109 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3110 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003111 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003112 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3113 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3114 DAG.getConstant(0, MVT::i32));
3115 } else {
3116 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3117 }
3118
3119 return Res;
3120 }
Evan Chengc143dd42011-02-11 02:28:55 +00003121
3122 // Bitcast operand 1 to i32.
3123 if (SrcVT == MVT::f64)
3124 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3125 &Tmp1, 1).getValue(1);
3126 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3127
Evan Chenge573fb32011-02-23 02:24:55 +00003128 // Or in the signbit with integer operations.
3129 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3130 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3131 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3132 if (VT == MVT::f32) {
3133 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3134 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3135 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3136 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003137 }
3138
Evan Chenge573fb32011-02-23 02:24:55 +00003139 // f64: Or the high part with signbit and then combine two parts.
3140 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3141 &Tmp0, 1);
3142 SDValue Lo = Tmp0.getValue(0);
3143 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3144 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3145 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003146}
3147
Evan Cheng2457f2c2010-05-22 01:47:14 +00003148SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3149 MachineFunction &MF = DAG.getMachineFunction();
3150 MachineFrameInfo *MFI = MF.getFrameInfo();
3151 MFI->setReturnAddressIsTaken(true);
3152
3153 EVT VT = Op.getValueType();
3154 DebugLoc dl = Op.getDebugLoc();
3155 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3156 if (Depth) {
3157 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3158 SDValue Offset = DAG.getConstant(4, MVT::i32);
3159 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3160 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003161 MachinePointerInfo(), false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003162 }
3163
3164 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003165 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003166 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3167}
3168
Dan Gohmand858e902010-04-17 15:26:15 +00003169SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003170 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3171 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003172
Owen Andersone50ed302009-08-10 22:56:29 +00003173 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003174 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3175 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003176 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003177 ? ARM::R7 : ARM::R11;
3178 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3179 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003180 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3181 MachinePointerInfo(),
David Greene1b58cab2010-02-15 16:55:24 +00003182 false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003183 return FrameAddr;
3184}
3185
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003186/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003187/// expand a bit convert where either the source or destination type is i64 to
3188/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3189/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3190/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003191static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003192 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3193 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003194 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003195
Bob Wilson9f3f0612010-04-17 05:30:19 +00003196 // This function is only supposed to be called for i64 types, either as the
3197 // source or destination of the bit convert.
3198 EVT SrcVT = Op.getValueType();
3199 EVT DstVT = N->getValueType(0);
3200 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003201 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003202
Bob Wilson9f3f0612010-04-17 05:30:19 +00003203 // Turn i64->f64 into VMOVDRR.
3204 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003205 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3206 DAG.getConstant(0, MVT::i32));
3207 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3208 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003209 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003210 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003211 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003212
Jim Grosbache5165492009-11-09 00:11:35 +00003213 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003214 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3215 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3216 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3217 // Merge the pieces into a single i64 value.
3218 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3219 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003220
Bob Wilson9f3f0612010-04-17 05:30:19 +00003221 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003222}
3223
Bob Wilson5bafff32009-06-22 23:27:02 +00003224/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003225/// Zero vectors are used to represent vector negation and in those cases
3226/// will be implemented with the NEON VNEG instruction. However, VNEG does
3227/// not support i64 elements, so sometimes the zero vectors will need to be
3228/// explicitly constructed. Regardless, use a canonical VMOV to create the
3229/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003230static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003231 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003232 // The canonical modified immediate encoding of a zero vector is....0!
3233 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3234 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3235 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003236 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003237}
3238
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003239/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3240/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003241SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3242 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003243 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3244 EVT VT = Op.getValueType();
3245 unsigned VTBits = VT.getSizeInBits();
3246 DebugLoc dl = Op.getDebugLoc();
3247 SDValue ShOpLo = Op.getOperand(0);
3248 SDValue ShOpHi = Op.getOperand(1);
3249 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003250 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003251 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003252
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003253 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3254
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003255 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3256 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3257 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3258 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3259 DAG.getConstant(VTBits, MVT::i32));
3260 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3261 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003262 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003263
3264 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3265 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003266 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003267 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003268 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003269 CCR, Cmp);
3270
3271 SDValue Ops[2] = { Lo, Hi };
3272 return DAG.getMergeValues(Ops, 2, dl);
3273}
3274
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003275/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3276/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003277SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3278 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003279 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3280 EVT VT = Op.getValueType();
3281 unsigned VTBits = VT.getSizeInBits();
3282 DebugLoc dl = Op.getDebugLoc();
3283 SDValue ShOpLo = Op.getOperand(0);
3284 SDValue ShOpHi = Op.getOperand(1);
3285 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003286 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003287
3288 assert(Op.getOpcode() == ISD::SHL_PARTS);
3289 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3290 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3291 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3292 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3293 DAG.getConstant(VTBits, MVT::i32));
3294 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3295 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3296
3297 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3298 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3299 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003300 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003301 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003302 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003303 CCR, Cmp);
3304
3305 SDValue Ops[2] = { Lo, Hi };
3306 return DAG.getMergeValues(Ops, 2, dl);
3307}
3308
Jim Grosbach4725ca72010-09-08 03:54:02 +00003309SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003310 SelectionDAG &DAG) const {
3311 // The rounding mode is in bits 23:22 of the FPSCR.
3312 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3313 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3314 // so that the shift + and get folded into a bitfield extract.
3315 DebugLoc dl = Op.getDebugLoc();
3316 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3317 DAG.getConstant(Intrinsic::arm_get_fpscr,
3318 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003319 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003320 DAG.getConstant(1U << 22, MVT::i32));
3321 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3322 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003323 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003324 DAG.getConstant(3, MVT::i32));
3325}
3326
Jim Grosbach3482c802010-01-18 19:58:49 +00003327static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3328 const ARMSubtarget *ST) {
3329 EVT VT = N->getValueType(0);
3330 DebugLoc dl = N->getDebugLoc();
3331
3332 if (!ST->hasV6T2Ops())
3333 return SDValue();
3334
3335 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3336 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3337}
3338
Bob Wilson5bafff32009-06-22 23:27:02 +00003339static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3340 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003341 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003342 DebugLoc dl = N->getDebugLoc();
3343
Bob Wilsond5448bb2010-11-18 21:16:28 +00003344 if (!VT.isVector())
3345 return SDValue();
3346
Bob Wilson5bafff32009-06-22 23:27:02 +00003347 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003348 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003349
Bob Wilsond5448bb2010-11-18 21:16:28 +00003350 // Left shifts translate directly to the vshiftu intrinsic.
3351 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003352 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003353 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3354 N->getOperand(0), N->getOperand(1));
3355
3356 assert((N->getOpcode() == ISD::SRA ||
3357 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3358
3359 // NEON uses the same intrinsics for both left and right shifts. For
3360 // right shifts, the shift amounts are negative, so negate the vector of
3361 // shift amounts.
3362 EVT ShiftVT = N->getOperand(1).getValueType();
3363 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3364 getZeroVector(ShiftVT, DAG, dl),
3365 N->getOperand(1));
3366 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3367 Intrinsic::arm_neon_vshifts :
3368 Intrinsic::arm_neon_vshiftu);
3369 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3370 DAG.getConstant(vshiftInt, MVT::i32),
3371 N->getOperand(0), NegatedCount);
3372}
3373
3374static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3375 const ARMSubtarget *ST) {
3376 EVT VT = N->getValueType(0);
3377 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003378
Eli Friedmance392eb2009-08-22 03:13:10 +00003379 // We can get here for a node like i32 = ISD::SHL i32, i64
3380 if (VT != MVT::i64)
3381 return SDValue();
3382
3383 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003384 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003385
Chris Lattner27a6c732007-11-24 07:07:01 +00003386 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3387 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003388 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003389 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003390
Chris Lattner27a6c732007-11-24 07:07:01 +00003391 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003392 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003393
Chris Lattner27a6c732007-11-24 07:07:01 +00003394 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003395 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003396 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003397 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003398 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003399
Chris Lattner27a6c732007-11-24 07:07:01 +00003400 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3401 // captures the result into a carry flag.
3402 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003403 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003404
Chris Lattner27a6c732007-11-24 07:07:01 +00003405 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003406 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003407
Chris Lattner27a6c732007-11-24 07:07:01 +00003408 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003409 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003410}
3411
Bob Wilson5bafff32009-06-22 23:27:02 +00003412static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3413 SDValue TmpOp0, TmpOp1;
3414 bool Invert = false;
3415 bool Swap = false;
3416 unsigned Opc = 0;
3417
3418 SDValue Op0 = Op.getOperand(0);
3419 SDValue Op1 = Op.getOperand(1);
3420 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003421 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003422 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3423 DebugLoc dl = Op.getDebugLoc();
3424
3425 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3426 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003427 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003428 case ISD::SETUNE:
3429 case ISD::SETNE: Invert = true; // Fallthrough
3430 case ISD::SETOEQ:
3431 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3432 case ISD::SETOLT:
3433 case ISD::SETLT: Swap = true; // Fallthrough
3434 case ISD::SETOGT:
3435 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3436 case ISD::SETOLE:
3437 case ISD::SETLE: Swap = true; // Fallthrough
3438 case ISD::SETOGE:
3439 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3440 case ISD::SETUGE: Swap = true; // Fallthrough
3441 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3442 case ISD::SETUGT: Swap = true; // Fallthrough
3443 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3444 case ISD::SETUEQ: Invert = true; // Fallthrough
3445 case ISD::SETONE:
3446 // Expand this to (OLT | OGT).
3447 TmpOp0 = Op0;
3448 TmpOp1 = Op1;
3449 Opc = ISD::OR;
3450 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3451 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3452 break;
3453 case ISD::SETUO: Invert = true; // Fallthrough
3454 case ISD::SETO:
3455 // Expand this to (OLT | OGE).
3456 TmpOp0 = Op0;
3457 TmpOp1 = Op1;
3458 Opc = ISD::OR;
3459 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3460 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3461 break;
3462 }
3463 } else {
3464 // Integer comparisons.
3465 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003466 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003467 case ISD::SETNE: Invert = true;
3468 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3469 case ISD::SETLT: Swap = true;
3470 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3471 case ISD::SETLE: Swap = true;
3472 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3473 case ISD::SETULT: Swap = true;
3474 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3475 case ISD::SETULE: Swap = true;
3476 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3477 }
3478
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003479 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003480 if (Opc == ARMISD::VCEQ) {
3481
3482 SDValue AndOp;
3483 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3484 AndOp = Op0;
3485 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3486 AndOp = Op1;
3487
3488 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003489 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003490 AndOp = AndOp.getOperand(0);
3491
3492 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3493 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003494 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3495 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003496 Invert = !Invert;
3497 }
3498 }
3499 }
3500
3501 if (Swap)
3502 std::swap(Op0, Op1);
3503
Owen Andersonc24cb352010-11-08 23:21:22 +00003504 // If one of the operands is a constant vector zero, attempt to fold the
3505 // comparison to a specialized compare-against-zero form.
3506 SDValue SingleOp;
3507 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3508 SingleOp = Op0;
3509 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3510 if (Opc == ARMISD::VCGE)
3511 Opc = ARMISD::VCLEZ;
3512 else if (Opc == ARMISD::VCGT)
3513 Opc = ARMISD::VCLTZ;
3514 SingleOp = Op1;
3515 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003516
Owen Andersonc24cb352010-11-08 23:21:22 +00003517 SDValue Result;
3518 if (SingleOp.getNode()) {
3519 switch (Opc) {
3520 case ARMISD::VCEQ:
3521 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3522 case ARMISD::VCGE:
3523 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3524 case ARMISD::VCLEZ:
3525 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3526 case ARMISD::VCGT:
3527 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3528 case ARMISD::VCLTZ:
3529 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3530 default:
3531 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3532 }
3533 } else {
3534 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3535 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003536
3537 if (Invert)
3538 Result = DAG.getNOT(dl, Result, VT);
3539
3540 return Result;
3541}
3542
Bob Wilsond3c42842010-06-14 22:19:57 +00003543/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3544/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003545/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003546static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3547 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003548 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003549 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003550
Bob Wilson827b2102010-06-15 19:05:35 +00003551 // SplatBitSize is set to the smallest size that splats the vector, so a
3552 // zero vector will always have SplatBitSize == 8. However, NEON modified
3553 // immediate instructions others than VMOV do not support the 8-bit encoding
3554 // of a zero vector, and the default encoding of zero is supposed to be the
3555 // 32-bit version.
3556 if (SplatBits == 0)
3557 SplatBitSize = 32;
3558
Bob Wilson5bafff32009-06-22 23:27:02 +00003559 switch (SplatBitSize) {
3560 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003561 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003562 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003563 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003564 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003565 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003566 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003567 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003568 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003569
3570 case 16:
3571 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003572 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003573 if ((SplatBits & ~0xff) == 0) {
3574 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003575 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003576 Imm = SplatBits;
3577 break;
3578 }
3579 if ((SplatBits & ~0xff00) == 0) {
3580 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003581 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003582 Imm = SplatBits >> 8;
3583 break;
3584 }
3585 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003586
3587 case 32:
3588 // NEON's 32-bit VMOV supports splat values where:
3589 // * only one byte is nonzero, or
3590 // * the least significant byte is 0xff and the second byte is nonzero, or
3591 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003592 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003593 if ((SplatBits & ~0xff) == 0) {
3594 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003595 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003596 Imm = SplatBits;
3597 break;
3598 }
3599 if ((SplatBits & ~0xff00) == 0) {
3600 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003601 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003602 Imm = SplatBits >> 8;
3603 break;
3604 }
3605 if ((SplatBits & ~0xff0000) == 0) {
3606 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003607 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003608 Imm = SplatBits >> 16;
3609 break;
3610 }
3611 if ((SplatBits & ~0xff000000) == 0) {
3612 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003613 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003614 Imm = SplatBits >> 24;
3615 break;
3616 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003617
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003618 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3619 if (type == OtherModImm) return SDValue();
3620
Bob Wilson5bafff32009-06-22 23:27:02 +00003621 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003622 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3623 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003624 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003625 Imm = SplatBits >> 8;
3626 SplatBits |= 0xff;
3627 break;
3628 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003629
3630 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003631 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3632 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003633 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003634 Imm = SplatBits >> 16;
3635 SplatBits |= 0xffff;
3636 break;
3637 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003638
3639 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3640 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3641 // VMOV.I32. A (very) minor optimization would be to replicate the value
3642 // and fall through here to test for a valid 64-bit splat. But, then the
3643 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003644 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003645
3646 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003647 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003648 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003649 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003650 uint64_t BitMask = 0xff;
3651 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003652 unsigned ImmMask = 1;
3653 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003654 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003655 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003656 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003657 Imm |= ImmMask;
3658 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003659 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003660 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003661 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003662 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003663 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003664 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003665 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003666 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003667 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003668 break;
3669 }
3670
Bob Wilson1a913ed2010-06-11 21:34:50 +00003671 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003672 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003673 return SDValue();
3674 }
3675
Bob Wilsoncba270d2010-07-13 21:16:48 +00003676 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3677 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003678}
3679
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003680static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3681 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003682 unsigned NumElts = VT.getVectorNumElements();
3683 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003684
3685 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3686 if (M[0] < 0)
3687 return false;
3688
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003689 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003690
3691 // If this is a VEXT shuffle, the immediate value is the index of the first
3692 // element. The other shuffle indices must be the successive elements after
3693 // the first one.
3694 unsigned ExpectedElt = Imm;
3695 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003696 // Increment the expected index. If it wraps around, it may still be
3697 // a VEXT but the source vectors must be swapped.
3698 ExpectedElt += 1;
3699 if (ExpectedElt == NumElts * 2) {
3700 ExpectedElt = 0;
3701 ReverseVEXT = true;
3702 }
3703
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003704 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003705 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003706 return false;
3707 }
3708
3709 // Adjust the index value if the source operands will be swapped.
3710 if (ReverseVEXT)
3711 Imm -= NumElts;
3712
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003713 return true;
3714}
3715
Bob Wilson8bb9e482009-07-26 00:39:34 +00003716/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3717/// instruction with the specified blocksize. (The order of the elements
3718/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003719static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3720 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003721 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3722 "Only possible block sizes for VREV are: 16, 32, 64");
3723
Bob Wilson8bb9e482009-07-26 00:39:34 +00003724 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003725 if (EltSz == 64)
3726 return false;
3727
3728 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003729 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003730 // If the first shuffle index is UNDEF, be optimistic.
3731 if (M[0] < 0)
3732 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003733
3734 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3735 return false;
3736
3737 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003738 if (M[i] < 0) continue; // ignore UNDEF indices
3739 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003740 return false;
3741 }
3742
3743 return true;
3744}
3745
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003746static bool isVTBLMask(const SmallVectorImpl<int> &M, EVT VT) {
3747 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3748 // range, then 0 is placed into the resulting vector. So pretty much any mask
3749 // of 8 elements can work here.
3750 return VT == MVT::v8i8 && M.size() == 8;
3751}
3752
Bob Wilsonc692cb72009-08-21 20:54:19 +00003753static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3754 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003755 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3756 if (EltSz == 64)
3757 return false;
3758
Bob Wilsonc692cb72009-08-21 20:54:19 +00003759 unsigned NumElts = VT.getVectorNumElements();
3760 WhichResult = (M[0] == 0 ? 0 : 1);
3761 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003762 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3763 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003764 return false;
3765 }
3766 return true;
3767}
3768
Bob Wilson324f4f12009-12-03 06:40:55 +00003769/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3770/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3771/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3772static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3773 unsigned &WhichResult) {
3774 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3775 if (EltSz == 64)
3776 return false;
3777
3778 unsigned NumElts = VT.getVectorNumElements();
3779 WhichResult = (M[0] == 0 ? 0 : 1);
3780 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003781 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3782 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003783 return false;
3784 }
3785 return true;
3786}
3787
Bob Wilsonc692cb72009-08-21 20:54:19 +00003788static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3789 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003790 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3791 if (EltSz == 64)
3792 return false;
3793
Bob Wilsonc692cb72009-08-21 20:54:19 +00003794 unsigned NumElts = VT.getVectorNumElements();
3795 WhichResult = (M[0] == 0 ? 0 : 1);
3796 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003797 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003798 if ((unsigned) M[i] != 2 * i + WhichResult)
3799 return false;
3800 }
3801
3802 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003803 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003804 return false;
3805
3806 return true;
3807}
3808
Bob Wilson324f4f12009-12-03 06:40:55 +00003809/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3810/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3811/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3812static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3813 unsigned &WhichResult) {
3814 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3815 if (EltSz == 64)
3816 return false;
3817
3818 unsigned Half = VT.getVectorNumElements() / 2;
3819 WhichResult = (M[0] == 0 ? 0 : 1);
3820 for (unsigned j = 0; j != 2; ++j) {
3821 unsigned Idx = WhichResult;
3822 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003823 int MIdx = M[i + j * Half];
3824 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003825 return false;
3826 Idx += 2;
3827 }
3828 }
3829
3830 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3831 if (VT.is64BitVector() && EltSz == 32)
3832 return false;
3833
3834 return true;
3835}
3836
Bob Wilsonc692cb72009-08-21 20:54:19 +00003837static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3838 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003839 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3840 if (EltSz == 64)
3841 return false;
3842
Bob Wilsonc692cb72009-08-21 20:54:19 +00003843 unsigned NumElts = VT.getVectorNumElements();
3844 WhichResult = (M[0] == 0 ? 0 : 1);
3845 unsigned Idx = WhichResult * NumElts / 2;
3846 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003847 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3848 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003849 return false;
3850 Idx += 1;
3851 }
3852
3853 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003854 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003855 return false;
3856
3857 return true;
3858}
3859
Bob Wilson324f4f12009-12-03 06:40:55 +00003860/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3861/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3862/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3863static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3864 unsigned &WhichResult) {
3865 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3866 if (EltSz == 64)
3867 return false;
3868
3869 unsigned NumElts = VT.getVectorNumElements();
3870 WhichResult = (M[0] == 0 ? 0 : 1);
3871 unsigned Idx = WhichResult * NumElts / 2;
3872 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003873 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3874 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003875 return false;
3876 Idx += 1;
3877 }
3878
3879 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3880 if (VT.is64BitVector() && EltSz == 32)
3881 return false;
3882
3883 return true;
3884}
3885
Dale Johannesenf630c712010-07-29 20:10:08 +00003886// If N is an integer constant that can be moved into a register in one
3887// instruction, return an SDValue of such a constant (will become a MOV
3888// instruction). Otherwise return null.
3889static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3890 const ARMSubtarget *ST, DebugLoc dl) {
3891 uint64_t Val;
3892 if (!isa<ConstantSDNode>(N))
3893 return SDValue();
3894 Val = cast<ConstantSDNode>(N)->getZExtValue();
3895
3896 if (ST->isThumb1Only()) {
3897 if (Val <= 255 || ~Val <= 255)
3898 return DAG.getConstant(Val, MVT::i32);
3899 } else {
3900 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3901 return DAG.getConstant(Val, MVT::i32);
3902 }
3903 return SDValue();
3904}
3905
Bob Wilson5bafff32009-06-22 23:27:02 +00003906// If this is a case we can't handle, return null and let the default
3907// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003908SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3909 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003910 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003911 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003912 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003913
3914 APInt SplatBits, SplatUndef;
3915 unsigned SplatBitSize;
3916 bool HasAnyUndefs;
3917 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003918 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003919 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003920 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003921 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003922 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003923 DAG, VmovVT, VT.is128BitVector(),
3924 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003925 if (Val.getNode()) {
3926 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003927 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003928 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003929
3930 // Try an immediate VMVN.
3931 uint64_t NegatedImm = (SplatBits.getZExtValue() ^
3932 ((1LL << SplatBitSize) - 1));
3933 Val = isNEONModifiedImm(NegatedImm,
3934 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003935 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003936 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003937 if (Val.getNode()) {
3938 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003939 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003940 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003941 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003942 }
3943
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003944 // Scan through the operands to see if only one value is used.
3945 unsigned NumElts = VT.getVectorNumElements();
3946 bool isOnlyLowElement = true;
3947 bool usesOnlyOneValue = true;
3948 bool isConstant = true;
3949 SDValue Value;
3950 for (unsigned i = 0; i < NumElts; ++i) {
3951 SDValue V = Op.getOperand(i);
3952 if (V.getOpcode() == ISD::UNDEF)
3953 continue;
3954 if (i > 0)
3955 isOnlyLowElement = false;
3956 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
3957 isConstant = false;
3958
3959 if (!Value.getNode())
3960 Value = V;
3961 else if (V != Value)
3962 usesOnlyOneValue = false;
3963 }
3964
3965 if (!Value.getNode())
3966 return DAG.getUNDEF(VT);
3967
3968 if (isOnlyLowElement)
3969 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
3970
Dale Johannesenf630c712010-07-29 20:10:08 +00003971 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3972
Dale Johannesen575cd142010-10-19 20:00:17 +00003973 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
3974 // i32 and try again.
3975 if (usesOnlyOneValue && EltSize <= 32) {
3976 if (!isConstant)
3977 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
3978 if (VT.getVectorElementType().isFloatingPoint()) {
3979 SmallVector<SDValue, 8> Ops;
3980 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003981 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00003982 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00003983 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
3984 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00003985 Val = LowerBUILD_VECTOR(Val, DAG, ST);
3986 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003987 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003988 }
Dale Johannesen575cd142010-10-19 20:00:17 +00003989 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
3990 if (Val.getNode())
3991 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00003992 }
3993
3994 // If all elements are constants and the case above didn't get hit, fall back
3995 // to the default expansion, which will generate a load from the constant
3996 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003997 if (isConstant)
3998 return SDValue();
3999
Bob Wilson11a1dff2011-01-07 21:37:30 +00004000 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4001 if (NumElts >= 4) {
4002 SDValue shuffle = ReconstructShuffle(Op, DAG);
4003 if (shuffle != SDValue())
4004 return shuffle;
4005 }
4006
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004007 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004008 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4009 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004010 if (EltSize >= 32) {
4011 // Do the expansion with floating-point types, since that is what the VFP
4012 // registers are defined to use, and since i64 is not legal.
4013 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4014 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004015 SmallVector<SDValue, 8> Ops;
4016 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004017 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004018 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004019 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004020 }
4021
4022 return SDValue();
4023}
4024
Bob Wilson11a1dff2011-01-07 21:37:30 +00004025// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004026// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004027SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4028 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004029 DebugLoc dl = Op.getDebugLoc();
4030 EVT VT = Op.getValueType();
4031 unsigned NumElts = VT.getVectorNumElements();
4032
4033 SmallVector<SDValue, 2> SourceVecs;
4034 SmallVector<unsigned, 2> MinElts;
4035 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004036
Bob Wilson11a1dff2011-01-07 21:37:30 +00004037 for (unsigned i = 0; i < NumElts; ++i) {
4038 SDValue V = Op.getOperand(i);
4039 if (V.getOpcode() == ISD::UNDEF)
4040 continue;
4041 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4042 // A shuffle can only come from building a vector from various
4043 // elements of other vectors.
4044 return SDValue();
4045 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004046
Bob Wilson11a1dff2011-01-07 21:37:30 +00004047 // Record this extraction against the appropriate vector if possible...
4048 SDValue SourceVec = V.getOperand(0);
4049 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4050 bool FoundSource = false;
4051 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4052 if (SourceVecs[j] == SourceVec) {
4053 if (MinElts[j] > EltNo)
4054 MinElts[j] = EltNo;
4055 if (MaxElts[j] < EltNo)
4056 MaxElts[j] = EltNo;
4057 FoundSource = true;
4058 break;
4059 }
4060 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004061
Bob Wilson11a1dff2011-01-07 21:37:30 +00004062 // Or record a new source if not...
4063 if (!FoundSource) {
4064 SourceVecs.push_back(SourceVec);
4065 MinElts.push_back(EltNo);
4066 MaxElts.push_back(EltNo);
4067 }
4068 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004069
Bob Wilson11a1dff2011-01-07 21:37:30 +00004070 // Currently only do something sane when at most two source vectors
4071 // involved.
4072 if (SourceVecs.size() > 2)
4073 return SDValue();
4074
4075 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4076 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004077
Bob Wilson11a1dff2011-01-07 21:37:30 +00004078 // This loop extracts the usage patterns of the source vectors
4079 // and prepares appropriate SDValues for a shuffle if possible.
4080 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4081 if (SourceVecs[i].getValueType() == VT) {
4082 // No VEXT necessary
4083 ShuffleSrcs[i] = SourceVecs[i];
4084 VEXTOffsets[i] = 0;
4085 continue;
4086 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4087 // It probably isn't worth padding out a smaller vector just to
4088 // break it down again in a shuffle.
4089 return SDValue();
4090 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004091
Bob Wilson11a1dff2011-01-07 21:37:30 +00004092 // Since only 64-bit and 128-bit vectors are legal on ARM and
4093 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004094 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4095 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004096
Bob Wilson11a1dff2011-01-07 21:37:30 +00004097 if (MaxElts[i] - MinElts[i] >= NumElts) {
4098 // Span too large for a VEXT to cope
4099 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004100 }
4101
Bob Wilson11a1dff2011-01-07 21:37:30 +00004102 if (MinElts[i] >= NumElts) {
4103 // The extraction can just take the second half
4104 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004105 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4106 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004107 DAG.getIntPtrConstant(NumElts));
4108 } else if (MaxElts[i] < NumElts) {
4109 // The extraction can just take the first half
4110 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004111 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4112 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004113 DAG.getIntPtrConstant(0));
4114 } else {
4115 // An actual VEXT is needed
4116 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004117 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4118 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004119 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004120 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4121 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004122 DAG.getIntPtrConstant(NumElts));
4123 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4124 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4125 }
4126 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004127
Bob Wilson11a1dff2011-01-07 21:37:30 +00004128 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004129
Bob Wilson11a1dff2011-01-07 21:37:30 +00004130 for (unsigned i = 0; i < NumElts; ++i) {
4131 SDValue Entry = Op.getOperand(i);
4132 if (Entry.getOpcode() == ISD::UNDEF) {
4133 Mask.push_back(-1);
4134 continue;
4135 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004136
Bob Wilson11a1dff2011-01-07 21:37:30 +00004137 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004138 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4139 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004140 if (ExtractVec == SourceVecs[0]) {
4141 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4142 } else {
4143 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4144 }
4145 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004146
Bob Wilson11a1dff2011-01-07 21:37:30 +00004147 // Final check before we try to produce nonsense...
4148 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004149 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4150 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004151
Bob Wilson11a1dff2011-01-07 21:37:30 +00004152 return SDValue();
4153}
4154
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004155/// isShuffleMaskLegal - Targets can use this to indicate that they only
4156/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4157/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4158/// are assumed to be legal.
4159bool
4160ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4161 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004162 if (VT.getVectorNumElements() == 4 &&
4163 (VT.is128BitVector() || VT.is64BitVector())) {
4164 unsigned PFIndexes[4];
4165 for (unsigned i = 0; i != 4; ++i) {
4166 if (M[i] < 0)
4167 PFIndexes[i] = 8;
4168 else
4169 PFIndexes[i] = M[i];
4170 }
4171
4172 // Compute the index in the perfect shuffle table.
4173 unsigned PFTableIndex =
4174 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4175 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4176 unsigned Cost = (PFEntry >> 30);
4177
4178 if (Cost <= 4)
4179 return true;
4180 }
4181
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004182 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004183 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004184
Bob Wilson53dd2452010-06-07 23:53:38 +00004185 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4186 return (EltSize >= 32 ||
4187 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004188 isVREVMask(M, VT, 64) ||
4189 isVREVMask(M, VT, 32) ||
4190 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004191 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004192 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004193 isVTRNMask(M, VT, WhichResult) ||
4194 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004195 isVZIPMask(M, VT, WhichResult) ||
4196 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4197 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4198 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004199}
4200
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004201/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4202/// the specified operations to build the shuffle.
4203static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4204 SDValue RHS, SelectionDAG &DAG,
4205 DebugLoc dl) {
4206 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4207 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4208 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4209
4210 enum {
4211 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4212 OP_VREV,
4213 OP_VDUP0,
4214 OP_VDUP1,
4215 OP_VDUP2,
4216 OP_VDUP3,
4217 OP_VEXT1,
4218 OP_VEXT2,
4219 OP_VEXT3,
4220 OP_VUZPL, // VUZP, left result
4221 OP_VUZPR, // VUZP, right result
4222 OP_VZIPL, // VZIP, left result
4223 OP_VZIPR, // VZIP, right result
4224 OP_VTRNL, // VTRN, left result
4225 OP_VTRNR // VTRN, right result
4226 };
4227
4228 if (OpNum == OP_COPY) {
4229 if (LHSID == (1*9+2)*9+3) return LHS;
4230 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4231 return RHS;
4232 }
4233
4234 SDValue OpLHS, OpRHS;
4235 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4236 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4237 EVT VT = OpLHS.getValueType();
4238
4239 switch (OpNum) {
4240 default: llvm_unreachable("Unknown shuffle opcode!");
4241 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004242 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004243 if (VT.getVectorElementType() == MVT::i32 ||
4244 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004245 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4246 // vrev <4 x i16> -> VREV32
4247 if (VT.getVectorElementType() == MVT::i16)
4248 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4249 // vrev <4 x i8> -> VREV16
4250 assert(VT.getVectorElementType() == MVT::i8);
4251 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004252 case OP_VDUP0:
4253 case OP_VDUP1:
4254 case OP_VDUP2:
4255 case OP_VDUP3:
4256 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004257 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004258 case OP_VEXT1:
4259 case OP_VEXT2:
4260 case OP_VEXT3:
4261 return DAG.getNode(ARMISD::VEXT, dl, VT,
4262 OpLHS, OpRHS,
4263 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4264 case OP_VUZPL:
4265 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004266 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004267 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4268 case OP_VZIPL:
4269 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004270 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004271 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4272 case OP_VTRNL:
4273 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004274 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4275 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004276 }
4277}
4278
Bill Wendling69a05a72011-03-14 23:02:38 +00004279static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
4280 SmallVectorImpl<int> &ShuffleMask,
4281 SelectionDAG &DAG) {
4282 // Check to see if we can use the VTBL instruction.
4283 SDValue V1 = Op.getOperand(0);
4284 SDValue V2 = Op.getOperand(1);
4285 DebugLoc DL = Op.getDebugLoc();
4286
4287 SmallVector<SDValue, 8> VTBLMask;
4288 for (SmallVectorImpl<int>::iterator
4289 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4290 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4291
4292 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4293 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4294 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4295 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004296
Owen Anderson76706012011-04-05 21:48:57 +00004297 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004298 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4299 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004300}
4301
Bob Wilson5bafff32009-06-22 23:27:02 +00004302static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004303 SDValue V1 = Op.getOperand(0);
4304 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004305 DebugLoc dl = Op.getDebugLoc();
4306 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004307 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004308 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00004309
Bob Wilson28865062009-08-13 02:13:04 +00004310 // Convert shuffles that are directly supported on NEON to target-specific
4311 // DAG nodes, instead of keeping them as shuffles and matching them again
4312 // during code selection. This is more efficient and avoids the possibility
4313 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004314 // FIXME: floating-point vectors should be canonicalized to integer vectors
4315 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004316 SVN->getMask(ShuffleMask);
4317
Bob Wilson53dd2452010-06-07 23:53:38 +00004318 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4319 if (EltSize <= 32) {
4320 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4321 int Lane = SVN->getSplatIndex();
4322 // If this is undef splat, generate it via "just" vdup, if possible.
4323 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004324
Bob Wilson53dd2452010-06-07 23:53:38 +00004325 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4326 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4327 }
4328 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4329 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004330 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004331
4332 bool ReverseVEXT;
4333 unsigned Imm;
4334 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4335 if (ReverseVEXT)
4336 std::swap(V1, V2);
4337 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4338 DAG.getConstant(Imm, MVT::i32));
4339 }
4340
4341 if (isVREVMask(ShuffleMask, VT, 64))
4342 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4343 if (isVREVMask(ShuffleMask, VT, 32))
4344 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4345 if (isVREVMask(ShuffleMask, VT, 16))
4346 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4347
4348 // Check for Neon shuffles that modify both input vectors in place.
4349 // If both results are used, i.e., if there are two shuffles with the same
4350 // source operands and with masks corresponding to both results of one of
4351 // these operations, DAG memoization will ensure that a single node is
4352 // used for both shuffles.
4353 unsigned WhichResult;
4354 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4355 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4356 V1, V2).getValue(WhichResult);
4357 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4358 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4359 V1, V2).getValue(WhichResult);
4360 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4361 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4362 V1, V2).getValue(WhichResult);
4363
4364 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4365 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4366 V1, V1).getValue(WhichResult);
4367 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4368 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4369 V1, V1).getValue(WhichResult);
4370 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4371 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4372 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004373 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004374
Bob Wilsonc692cb72009-08-21 20:54:19 +00004375 // If the shuffle is not directly supported and it has 4 elements, use
4376 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004377 unsigned NumElts = VT.getVectorNumElements();
4378 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004379 unsigned PFIndexes[4];
4380 for (unsigned i = 0; i != 4; ++i) {
4381 if (ShuffleMask[i] < 0)
4382 PFIndexes[i] = 8;
4383 else
4384 PFIndexes[i] = ShuffleMask[i];
4385 }
4386
4387 // Compute the index in the perfect shuffle table.
4388 unsigned PFTableIndex =
4389 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004390 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4391 unsigned Cost = (PFEntry >> 30);
4392
4393 if (Cost <= 4)
4394 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4395 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004396
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004397 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004398 if (EltSize >= 32) {
4399 // Do the expansion with floating-point types, since that is what the VFP
4400 // registers are defined to use, and since i64 is not legal.
4401 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4402 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004403 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4404 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004405 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004406 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004407 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004408 Ops.push_back(DAG.getUNDEF(EltVT));
4409 else
4410 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4411 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4412 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4413 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004414 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004415 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004416 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004417 }
4418
Bill Wendling69a05a72011-03-14 23:02:38 +00004419 if (VT == MVT::v8i8) {
4420 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4421 if (NewOp.getNode())
4422 return NewOp;
4423 }
4424
Bob Wilson22cac0d2009-08-14 05:16:33 +00004425 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004426}
4427
Bob Wilson5bafff32009-06-22 23:27:02 +00004428static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004429 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004430 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004431 if (!isa<ConstantSDNode>(Lane))
4432 return SDValue();
4433
4434 SDValue Vec = Op.getOperand(0);
4435 if (Op.getValueType() == MVT::i32 &&
4436 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4437 DebugLoc dl = Op.getDebugLoc();
4438 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4439 }
4440
4441 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004442}
4443
Bob Wilsona6d65862009-08-03 20:36:38 +00004444static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4445 // The only time a CONCAT_VECTORS operation can have legal types is when
4446 // two 64-bit vectors are concatenated to a 128-bit vector.
4447 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4448 "unexpected CONCAT_VECTORS");
4449 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004450 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004451 SDValue Op0 = Op.getOperand(0);
4452 SDValue Op1 = Op.getOperand(1);
4453 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004454 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004455 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004456 DAG.getIntPtrConstant(0));
4457 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004458 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004459 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004460 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004461 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004462}
4463
Bob Wilson626613d2010-11-23 19:38:38 +00004464/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4465/// element has been zero/sign-extended, depending on the isSigned parameter,
4466/// from an integer type half its size.
4467static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4468 bool isSigned) {
4469 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4470 EVT VT = N->getValueType(0);
4471 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4472 SDNode *BVN = N->getOperand(0).getNode();
4473 if (BVN->getValueType(0) != MVT::v4i32 ||
4474 BVN->getOpcode() != ISD::BUILD_VECTOR)
4475 return false;
4476 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4477 unsigned HiElt = 1 - LoElt;
4478 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4479 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4480 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4481 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4482 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4483 return false;
4484 if (isSigned) {
4485 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4486 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4487 return true;
4488 } else {
4489 if (Hi0->isNullValue() && Hi1->isNullValue())
4490 return true;
4491 }
4492 return false;
4493 }
4494
4495 if (N->getOpcode() != ISD::BUILD_VECTOR)
4496 return false;
4497
4498 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4499 SDNode *Elt = N->getOperand(i).getNode();
4500 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4501 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4502 unsigned HalfSize = EltSize / 2;
4503 if (isSigned) {
4504 int64_t SExtVal = C->getSExtValue();
4505 if ((SExtVal >> HalfSize) != (SExtVal >> EltSize))
4506 return false;
4507 } else {
4508 if ((C->getZExtValue() >> HalfSize) != 0)
4509 return false;
4510 }
4511 continue;
4512 }
4513 return false;
4514 }
4515
4516 return true;
4517}
4518
4519/// isSignExtended - Check if a node is a vector value that is sign-extended
4520/// or a constant BUILD_VECTOR with sign-extended elements.
4521static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4522 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4523 return true;
4524 if (isExtendedBUILD_VECTOR(N, DAG, true))
4525 return true;
4526 return false;
4527}
4528
4529/// isZeroExtended - Check if a node is a vector value that is zero-extended
4530/// or a constant BUILD_VECTOR with zero-extended elements.
4531static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4532 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4533 return true;
4534 if (isExtendedBUILD_VECTOR(N, DAG, false))
4535 return true;
4536 return false;
4537}
4538
4539/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4540/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004541static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4542 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4543 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004544 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4545 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4546 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
4547 LD->isNonTemporal(), LD->getAlignment());
4548 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4549 // have been legalized as a BITCAST from v4i32.
4550 if (N->getOpcode() == ISD::BITCAST) {
4551 SDNode *BVN = N->getOperand(0).getNode();
4552 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4553 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4554 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4555 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4556 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4557 }
4558 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4559 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4560 EVT VT = N->getValueType(0);
4561 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4562 unsigned NumElts = VT.getVectorNumElements();
4563 MVT TruncVT = MVT::getIntegerVT(EltSize);
4564 SmallVector<SDValue, 8> Ops;
4565 for (unsigned i = 0; i != NumElts; ++i) {
4566 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4567 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004568 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004569 }
4570 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4571 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004572}
4573
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004574static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4575 unsigned Opcode = N->getOpcode();
4576 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4577 SDNode *N0 = N->getOperand(0).getNode();
4578 SDNode *N1 = N->getOperand(1).getNode();
4579 return N0->hasOneUse() && N1->hasOneUse() &&
4580 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4581 }
4582 return false;
4583}
4584
4585static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4586 unsigned Opcode = N->getOpcode();
4587 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4588 SDNode *N0 = N->getOperand(0).getNode();
4589 SDNode *N1 = N->getOperand(1).getNode();
4590 return N0->hasOneUse() && N1->hasOneUse() &&
4591 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4592 }
4593 return false;
4594}
4595
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004596static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4597 // Multiplications are only custom-lowered for 128-bit vectors so that
4598 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4599 EVT VT = Op.getValueType();
4600 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4601 SDNode *N0 = Op.getOperand(0).getNode();
4602 SDNode *N1 = Op.getOperand(1).getNode();
4603 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004604 bool isMLA = false;
4605 bool isN0SExt = isSignExtended(N0, DAG);
4606 bool isN1SExt = isSignExtended(N1, DAG);
4607 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004608 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004609 else {
4610 bool isN0ZExt = isZeroExtended(N0, DAG);
4611 bool isN1ZExt = isZeroExtended(N1, DAG);
4612 if (isN0ZExt && isN1ZExt)
4613 NewOpc = ARMISD::VMULLu;
4614 else if (isN1SExt || isN1ZExt) {
4615 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4616 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4617 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4618 NewOpc = ARMISD::VMULLs;
4619 isMLA = true;
4620 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4621 NewOpc = ARMISD::VMULLu;
4622 isMLA = true;
4623 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4624 std::swap(N0, N1);
4625 NewOpc = ARMISD::VMULLu;
4626 isMLA = true;
4627 }
4628 }
4629
4630 if (!NewOpc) {
4631 if (VT == MVT::v2i64)
4632 // Fall through to expand this. It is not legal.
4633 return SDValue();
4634 else
4635 // Other vector multiplications are legal.
4636 return Op;
4637 }
4638 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004639
4640 // Legalize to a VMULL instruction.
4641 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004642 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004643 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004644 if (!isMLA) {
4645 Op0 = SkipExtension(N0, DAG);
4646 assert(Op0.getValueType().is64BitVector() &&
4647 Op1.getValueType().is64BitVector() &&
4648 "unexpected types for extended operands to VMULL");
4649 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4650 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004651
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004652 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4653 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4654 // vmull q0, d4, d6
4655 // vmlal q0, d5, d6
4656 // is faster than
4657 // vaddl q0, d4, d5
4658 // vmovl q1, d6
4659 // vmul q0, q0, q1
4660 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4661 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4662 EVT Op1VT = Op1.getValueType();
4663 return DAG.getNode(N0->getOpcode(), DL, VT,
4664 DAG.getNode(NewOpc, DL, VT,
4665 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4666 DAG.getNode(NewOpc, DL, VT,
4667 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004668}
4669
Owen Anderson76706012011-04-05 21:48:57 +00004670static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004671LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4672 // Convert to float
4673 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4674 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4675 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4676 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4677 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4678 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4679 // Get reciprocal estimate.
4680 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004681 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004682 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4683 // Because char has a smaller range than uchar, we can actually get away
4684 // without any newton steps. This requires that we use a weird bias
4685 // of 0xb000, however (again, this has been exhaustively tested).
4686 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4687 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4688 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4689 Y = DAG.getConstant(0xb000, MVT::i32);
4690 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4691 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4692 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4693 // Convert back to short.
4694 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4695 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4696 return X;
4697}
4698
Owen Anderson76706012011-04-05 21:48:57 +00004699static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004700LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4701 SDValue N2;
4702 // Convert to float.
4703 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4704 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4705 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4706 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4707 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4708 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004709
Nate Begeman7973f352011-02-11 20:53:29 +00004710 // Use reciprocal estimate and one refinement step.
4711 // float4 recip = vrecpeq_f32(yf);
4712 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004713 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004714 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004715 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004716 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4717 N1, N2);
4718 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4719 // Because short has a smaller range than ushort, we can actually get away
4720 // with only a single newton step. This requires that we use a weird bias
4721 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004722 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004723 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4724 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004725 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004726 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4727 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4728 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4729 // Convert back to integer and return.
4730 // return vmovn_s32(vcvt_s32_f32(result));
4731 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4732 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4733 return N0;
4734}
4735
4736static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4737 EVT VT = Op.getValueType();
4738 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4739 "unexpected type for custom-lowering ISD::SDIV");
4740
4741 DebugLoc dl = Op.getDebugLoc();
4742 SDValue N0 = Op.getOperand(0);
4743 SDValue N1 = Op.getOperand(1);
4744 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004745
Nate Begeman7973f352011-02-11 20:53:29 +00004746 if (VT == MVT::v8i8) {
4747 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4748 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004749
Nate Begeman7973f352011-02-11 20:53:29 +00004750 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4751 DAG.getIntPtrConstant(4));
4752 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004753 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004754 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4755 DAG.getIntPtrConstant(0));
4756 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4757 DAG.getIntPtrConstant(0));
4758
4759 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4760 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4761
4762 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4763 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004764
Nate Begeman7973f352011-02-11 20:53:29 +00004765 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
4766 return N0;
4767 }
4768 return LowerSDIV_v4i16(N0, N1, dl, DAG);
4769}
4770
4771static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
4772 EVT VT = Op.getValueType();
4773 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4774 "unexpected type for custom-lowering ISD::UDIV");
4775
4776 DebugLoc dl = Op.getDebugLoc();
4777 SDValue N0 = Op.getOperand(0);
4778 SDValue N1 = Op.getOperand(1);
4779 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004780
Nate Begeman7973f352011-02-11 20:53:29 +00004781 if (VT == MVT::v8i8) {
4782 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
4783 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004784
Nate Begeman7973f352011-02-11 20:53:29 +00004785 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4786 DAG.getIntPtrConstant(4));
4787 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004788 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004789 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4790 DAG.getIntPtrConstant(0));
4791 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4792 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00004793
Nate Begeman7973f352011-02-11 20:53:29 +00004794 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
4795 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00004796
Nate Begeman7973f352011-02-11 20:53:29 +00004797 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4798 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004799
4800 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00004801 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
4802 N0);
4803 return N0;
4804 }
Owen Anderson76706012011-04-05 21:48:57 +00004805
Nate Begeman7973f352011-02-11 20:53:29 +00004806 // v4i16 sdiv ... Convert to float.
4807 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
4808 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
4809 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
4810 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
4811 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004812 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00004813
4814 // Use reciprocal estimate and two refinement steps.
4815 // float4 recip = vrecpeq_f32(yf);
4816 // recip *= vrecpsq_f32(yf, recip);
4817 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004818 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004819 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00004820 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004821 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004822 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004823 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00004824 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004825 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004826 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004827 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4828 // Simply multiplying by the reciprocal estimate can leave us a few ulps
4829 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
4830 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004831 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00004832 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4833 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4834 N1 = DAG.getConstant(2, MVT::i32);
4835 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4836 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4837 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4838 // Convert back to integer and return.
4839 // return vmovn_u32(vcvt_s32_f32(result));
4840 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4841 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4842 return N0;
4843}
4844
Evan Cheng342e3162011-08-30 01:34:54 +00004845static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
4846 EVT VT = Op.getNode()->getValueType(0);
4847 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
4848
4849 unsigned Opc;
4850 bool ExtraOp = false;
4851 switch (Op.getOpcode()) {
4852 default: assert(0 && "Invalid code");
4853 case ISD::ADDC: Opc = ARMISD::ADDC; break;
4854 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
4855 case ISD::SUBC: Opc = ARMISD::SUBC; break;
4856 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
4857 }
4858
4859 if (!ExtraOp)
4860 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
4861 Op.getOperand(1));
4862 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
4863 Op.getOperand(1), Op.getOperand(2));
4864}
4865
Eli Friedman74bf18c2011-09-15 22:26:18 +00004866static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedman7cc15662011-09-15 22:18:49 +00004867 // Monotonic load/store is legal for all targets
4868 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
4869 return Op;
4870
4871 // Aquire/Release load/store is not legal for targets without a
4872 // dmb or equivalent available.
4873 return SDValue();
4874}
4875
4876
Eli Friedman2bdffe42011-08-31 00:31:29 +00004877static void
Eli Friedman4d3f3292011-08-31 17:52:22 +00004878ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
4879 SelectionDAG &DAG, unsigned NewOp) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00004880 EVT T = Node->getValueType(0);
4881 DebugLoc dl = Node->getDebugLoc();
4882 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
4883
Eli Friedman4d3f3292011-08-31 17:52:22 +00004884 SmallVector<SDValue, 6> Ops;
4885 Ops.push_back(Node->getOperand(0)); // Chain
4886 Ops.push_back(Node->getOperand(1)); // Ptr
4887 // Low part of Val1
4888 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4889 Node->getOperand(2), DAG.getIntPtrConstant(0)));
4890 // High part of Val1
4891 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4892 Node->getOperand(2), DAG.getIntPtrConstant(1)));
4893 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) {
4894 // High part of Val1
4895 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4896 Node->getOperand(3), DAG.getIntPtrConstant(0)));
4897 // High part of Val2
4898 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4899 Node->getOperand(3), DAG.getIntPtrConstant(1)));
4900 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00004901 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
4902 SDValue Result =
Eli Friedman4d3f3292011-08-31 17:52:22 +00004903 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64,
Eli Friedman2bdffe42011-08-31 00:31:29 +00004904 cast<MemSDNode>(Node)->getMemOperand());
Eli Friedman4d3f3292011-08-31 17:52:22 +00004905 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) };
Eli Friedman2bdffe42011-08-31 00:31:29 +00004906 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
4907 Results.push_back(Result.getValue(2));
4908}
4909
Dan Gohmand858e902010-04-17 15:26:15 +00004910SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004911 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004912 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00004913 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00004914 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004915 case ISD::GlobalAddress:
4916 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
4917 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00004918 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00004919 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00004920 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
4921 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004922 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00004923 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00004924 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00004925 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00004926 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00004927 case ISD::SINT_TO_FP:
4928 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
4929 case ISD::FP_TO_SINT:
4930 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004931 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00004932 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00004933 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004934 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004935 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00004936 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbache4ad3872010-10-19 23:27:08 +00004937 case ISD::EH_SJLJ_DISPATCHSETUP: return LowerEH_SJLJ_DISPATCHSETUP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00004938 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
4939 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00004940 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004941 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00004942 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00004943 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00004944 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00004945 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00004946 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00004947 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Duncan Sands28b77e92011-09-06 19:07:46 +00004948 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00004949 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00004950 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00004951 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00004952 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00004953 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004954 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00004955 case ISD::SDIV: return LowerSDIV(Op, DAG);
4956 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00004957 case ISD::ADDC:
4958 case ISD::ADDE:
4959 case ISD::SUBC:
4960 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Eli Friedman7cc15662011-09-15 22:18:49 +00004961 case ISD::ATOMIC_LOAD:
Eli Friedman74bf18c2011-09-15 22:26:18 +00004962 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00004963 }
Dan Gohman475871a2008-07-27 21:46:04 +00004964 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00004965}
4966
Duncan Sands1607f052008-12-01 11:39:25 +00004967/// ReplaceNodeResults - Replace the results of node with an illegal result
4968/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00004969void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
4970 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00004971 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00004972 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00004973 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00004974 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00004975 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00004976 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004977 case ISD::BITCAST:
4978 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004979 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00004980 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00004981 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00004982 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00004983 break;
Eli Friedman2bdffe42011-08-31 00:31:29 +00004984 case ISD::ATOMIC_LOAD_ADD:
Eli Friedman4d3f3292011-08-31 17:52:22 +00004985 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00004986 return;
4987 case ISD::ATOMIC_LOAD_AND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00004988 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00004989 return;
4990 case ISD::ATOMIC_LOAD_NAND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00004991 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00004992 return;
4993 case ISD::ATOMIC_LOAD_OR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00004994 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00004995 return;
4996 case ISD::ATOMIC_LOAD_SUB:
Eli Friedman4d3f3292011-08-31 17:52:22 +00004997 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00004998 return;
4999 case ISD::ATOMIC_LOAD_XOR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005000 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005001 return;
5002 case ISD::ATOMIC_SWAP:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005003 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005004 return;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005005 case ISD::ATOMIC_CMP_SWAP:
5006 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG);
5007 return;
Duncan Sands1607f052008-12-01 11:39:25 +00005008 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00005009 if (Res.getNode())
5010 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00005011}
Chris Lattner27a6c732007-11-24 07:07:01 +00005012
Evan Chenga8e29892007-01-19 07:51:42 +00005013//===----------------------------------------------------------------------===//
5014// ARM Scheduler Hooks
5015//===----------------------------------------------------------------------===//
5016
5017MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005018ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
5019 MachineBasicBlock *BB,
5020 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005021 unsigned dest = MI->getOperand(0).getReg();
5022 unsigned ptr = MI->getOperand(1).getReg();
5023 unsigned oldval = MI->getOperand(2).getReg();
5024 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005025 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5026 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005027 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005028
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005029 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5030 unsigned scratch =
Cameron Zwarich141ec632011-05-18 02:29:50 +00005031 MRI.createVirtualRegister(isThumb2 ? ARM::rGPRRegisterClass
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005032 : ARM::GPRRegisterClass);
5033
5034 if (isThumb2) {
Cameron Zwarich141ec632011-05-18 02:29:50 +00005035 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5036 MRI.constrainRegClass(oldval, ARM::rGPRRegisterClass);
5037 MRI.constrainRegClass(newval, ARM::rGPRRegisterClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005038 }
5039
Jim Grosbach5278eb82009-12-11 01:42:04 +00005040 unsigned ldrOpc, strOpc;
5041 switch (Size) {
5042 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005043 case 1:
5044 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00005045 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005046 break;
5047 case 2:
5048 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5049 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5050 break;
5051 case 4:
5052 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5053 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5054 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00005055 }
5056
5057 MachineFunction *MF = BB->getParent();
5058 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5059 MachineFunction::iterator It = BB;
5060 ++It; // insert the new blocks after the current block
5061
5062 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5063 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5064 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5065 MF->insert(It, loop1MBB);
5066 MF->insert(It, loop2MBB);
5067 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005068
5069 // Transfer the remainder of BB and its successor edges to exitMBB.
5070 exitMBB->splice(exitMBB->begin(), BB,
5071 llvm::next(MachineBasicBlock::iterator(MI)),
5072 BB->end());
5073 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005074
5075 // thisMBB:
5076 // ...
5077 // fallthrough --> loop1MBB
5078 BB->addSuccessor(loop1MBB);
5079
5080 // loop1MBB:
5081 // ldrex dest, [ptr]
5082 // cmp dest, oldval
5083 // bne exitMBB
5084 BB = loop1MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005085 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5086 if (ldrOpc == ARM::t2LDREX)
5087 MIB.addImm(0);
5088 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005089 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005090 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005091 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5092 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005093 BB->addSuccessor(loop2MBB);
5094 BB->addSuccessor(exitMBB);
5095
5096 // loop2MBB:
5097 // strex scratch, newval, [ptr]
5098 // cmp scratch, #0
5099 // bne loop1MBB
5100 BB = loop2MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005101 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr);
5102 if (strOpc == ARM::t2STREX)
5103 MIB.addImm(0);
5104 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005105 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005106 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005107 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5108 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005109 BB->addSuccessor(loop1MBB);
5110 BB->addSuccessor(exitMBB);
5111
5112 // exitMBB:
5113 // ...
5114 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005115
Dan Gohman14152b42010-07-06 20:24:04 +00005116 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005117
Jim Grosbach5278eb82009-12-11 01:42:04 +00005118 return BB;
5119}
5120
5121MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005122ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5123 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005124 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5125 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5126
5127 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005128 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005129 MachineFunction::iterator It = BB;
5130 ++It;
5131
5132 unsigned dest = MI->getOperand(0).getReg();
5133 unsigned ptr = MI->getOperand(1).getReg();
5134 unsigned incr = MI->getOperand(2).getReg();
5135 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005136 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005137
5138 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5139 if (isThumb2) {
5140 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5141 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5142 }
5143
Jim Grosbachc3c23542009-12-14 04:22:04 +00005144 unsigned ldrOpc, strOpc;
5145 switch (Size) {
5146 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005147 case 1:
5148 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005149 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005150 break;
5151 case 2:
5152 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5153 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5154 break;
5155 case 4:
5156 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5157 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5158 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005159 }
5160
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005161 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5162 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5163 MF->insert(It, loopMBB);
5164 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005165
5166 // Transfer the remainder of BB and its successor edges to exitMBB.
5167 exitMBB->splice(exitMBB->begin(), BB,
5168 llvm::next(MachineBasicBlock::iterator(MI)),
5169 BB->end());
5170 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005171
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005172 TargetRegisterClass *TRC =
5173 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5174 unsigned scratch = MRI.createVirtualRegister(TRC);
5175 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005176
5177 // thisMBB:
5178 // ...
5179 // fallthrough --> loopMBB
5180 BB->addSuccessor(loopMBB);
5181
5182 // loopMBB:
5183 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005184 // <binop> scratch2, dest, incr
5185 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005186 // cmp scratch, #0
5187 // bne- loopMBB
5188 // fallthrough --> exitMBB
5189 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005190 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5191 if (ldrOpc == ARM::t2LDREX)
5192 MIB.addImm(0);
5193 AddDefaultPred(MIB);
Jim Grosbachc67b5562009-12-15 00:12:35 +00005194 if (BinOpcode) {
5195 // operand order needs to go the other way for NAND
5196 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5197 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5198 addReg(incr).addReg(dest)).addReg(0);
5199 else
5200 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5201 addReg(dest).addReg(incr)).addReg(0);
5202 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005203
Jim Grosbachb6aed502011-09-09 18:37:27 +00005204 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5205 if (strOpc == ARM::t2STREX)
5206 MIB.addImm(0);
5207 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005208 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005209 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005210 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5211 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005212
5213 BB->addSuccessor(loopMBB);
5214 BB->addSuccessor(exitMBB);
5215
5216 // exitMBB:
5217 // ...
5218 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005219
Dan Gohman14152b42010-07-06 20:24:04 +00005220 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005221
Jim Grosbachc3c23542009-12-14 04:22:04 +00005222 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005223}
5224
Jim Grosbachf7da8822011-04-26 19:44:18 +00005225MachineBasicBlock *
5226ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5227 MachineBasicBlock *BB,
5228 unsigned Size,
5229 bool signExtend,
5230 ARMCC::CondCodes Cond) const {
5231 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5232
5233 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5234 MachineFunction *MF = BB->getParent();
5235 MachineFunction::iterator It = BB;
5236 ++It;
5237
5238 unsigned dest = MI->getOperand(0).getReg();
5239 unsigned ptr = MI->getOperand(1).getReg();
5240 unsigned incr = MI->getOperand(2).getReg();
5241 unsigned oldval = dest;
5242 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005243 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005244
5245 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5246 if (isThumb2) {
5247 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5248 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5249 }
5250
Jim Grosbachf7da8822011-04-26 19:44:18 +00005251 unsigned ldrOpc, strOpc, extendOpc;
5252 switch (Size) {
5253 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5254 case 1:
5255 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5256 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005257 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005258 break;
5259 case 2:
5260 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5261 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005262 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005263 break;
5264 case 4:
5265 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5266 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5267 extendOpc = 0;
5268 break;
5269 }
5270
5271 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5272 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5273 MF->insert(It, loopMBB);
5274 MF->insert(It, exitMBB);
5275
5276 // Transfer the remainder of BB and its successor edges to exitMBB.
5277 exitMBB->splice(exitMBB->begin(), BB,
5278 llvm::next(MachineBasicBlock::iterator(MI)),
5279 BB->end());
5280 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5281
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005282 TargetRegisterClass *TRC =
5283 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5284 unsigned scratch = MRI.createVirtualRegister(TRC);
5285 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005286
5287 // thisMBB:
5288 // ...
5289 // fallthrough --> loopMBB
5290 BB->addSuccessor(loopMBB);
5291
5292 // loopMBB:
5293 // ldrex dest, ptr
5294 // (sign extend dest, if required)
5295 // cmp dest, incr
5296 // cmov.cond scratch2, dest, incr
5297 // strex scratch, scratch2, ptr
5298 // cmp scratch, #0
5299 // bne- loopMBB
5300 // fallthrough --> exitMBB
5301 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005302 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5303 if (ldrOpc == ARM::t2LDREX)
5304 MIB.addImm(0);
5305 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005306
5307 // Sign extend the value, if necessary.
5308 if (signExtend && extendOpc) {
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005309 oldval = MRI.createVirtualRegister(ARM::GPRRegisterClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005310 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5311 .addReg(dest)
5312 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005313 }
5314
5315 // Build compare and cmov instructions.
5316 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5317 .addReg(oldval).addReg(incr));
5318 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5319 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5320
Jim Grosbachb6aed502011-09-09 18:37:27 +00005321 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5322 if (strOpc == ARM::t2STREX)
5323 MIB.addImm(0);
5324 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005325 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5326 .addReg(scratch).addImm(0));
5327 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5328 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5329
5330 BB->addSuccessor(loopMBB);
5331 BB->addSuccessor(exitMBB);
5332
5333 // exitMBB:
5334 // ...
5335 BB = exitMBB;
5336
5337 MI->eraseFromParent(); // The instruction is gone now.
5338
5339 return BB;
5340}
5341
Eli Friedman2bdffe42011-08-31 00:31:29 +00005342MachineBasicBlock *
5343ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
5344 unsigned Op1, unsigned Op2,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005345 bool NeedsCarry, bool IsCmpxchg) const {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005346 // This also handles ATOMIC_SWAP, indicated by Op1==0.
5347 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5348
5349 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5350 MachineFunction *MF = BB->getParent();
5351 MachineFunction::iterator It = BB;
5352 ++It;
5353
5354 unsigned destlo = MI->getOperand(0).getReg();
5355 unsigned desthi = MI->getOperand(1).getReg();
5356 unsigned ptr = MI->getOperand(2).getReg();
5357 unsigned vallo = MI->getOperand(3).getReg();
5358 unsigned valhi = MI->getOperand(4).getReg();
5359 DebugLoc dl = MI->getDebugLoc();
5360 bool isThumb2 = Subtarget->isThumb2();
5361
5362 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5363 if (isThumb2) {
5364 MRI.constrainRegClass(destlo, ARM::rGPRRegisterClass);
5365 MRI.constrainRegClass(desthi, ARM::rGPRRegisterClass);
5366 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5367 }
5368
5369 unsigned ldrOpc = isThumb2 ? ARM::t2LDREXD : ARM::LDREXD;
5370 unsigned strOpc = isThumb2 ? ARM::t2STREXD : ARM::STREXD;
5371
5372 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman7df496d2011-09-01 22:27:41 +00005373 MachineBasicBlock *contBB = 0, *cont2BB = 0;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005374 if (IsCmpxchg) {
5375 contBB = MF->CreateMachineBasicBlock(LLVM_BB);
5376 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB);
5377 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005378 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5379 MF->insert(It, loopMBB);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005380 if (IsCmpxchg) {
5381 MF->insert(It, contBB);
5382 MF->insert(It, cont2BB);
5383 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005384 MF->insert(It, exitMBB);
5385
5386 // Transfer the remainder of BB and its successor edges to exitMBB.
5387 exitMBB->splice(exitMBB->begin(), BB,
5388 llvm::next(MachineBasicBlock::iterator(MI)),
5389 BB->end());
5390 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5391
5392 TargetRegisterClass *TRC =
5393 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5394 unsigned storesuccess = MRI.createVirtualRegister(TRC);
5395
5396 // thisMBB:
5397 // ...
5398 // fallthrough --> loopMBB
5399 BB->addSuccessor(loopMBB);
5400
5401 // loopMBB:
5402 // ldrexd r2, r3, ptr
5403 // <binopa> r0, r2, incr
5404 // <binopb> r1, r3, incr
5405 // strexd storesuccess, r0, r1, ptr
5406 // cmp storesuccess, #0
5407 // bne- loopMBB
5408 // fallthrough --> exitMBB
5409 //
5410 // Note that the registers are explicitly specified because there is not any
5411 // way to force the register allocator to allocate a register pair.
5412 //
5413 // FIXME: The hardcoded registers are not necessary for Thumb2, but we
5414 // need to properly enforce the restriction that the two output registers
5415 // for ldrexd must be different.
5416 BB = loopMBB;
5417 // Load
5418 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc))
5419 .addReg(ARM::R2, RegState::Define)
5420 .addReg(ARM::R3, RegState::Define).addReg(ptr));
5421 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
5422 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo).addReg(ARM::R2);
5423 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi).addReg(ARM::R3);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005424
5425 if (IsCmpxchg) {
5426 // Add early exit
5427 for (unsigned i = 0; i < 2; i++) {
5428 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr :
5429 ARM::CMPrr))
5430 .addReg(i == 0 ? destlo : desthi)
5431 .addReg(i == 0 ? vallo : valhi));
5432 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5433 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5434 BB->addSuccessor(exitMBB);
5435 BB->addSuccessor(i == 0 ? contBB : cont2BB);
5436 BB = (i == 0 ? contBB : cont2BB);
5437 }
5438
5439 // Copy to physregs for strexd
5440 unsigned setlo = MI->getOperand(5).getReg();
5441 unsigned sethi = MI->getOperand(6).getReg();
5442 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(setlo);
5443 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(sethi);
5444 } else if (Op1) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005445 // Perform binary operation
5446 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), ARM::R0)
5447 .addReg(destlo).addReg(vallo))
5448 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
5449 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), ARM::R1)
5450 .addReg(desthi).addReg(valhi)).addReg(0);
5451 } else {
5452 // Copy to physregs for strexd
5453 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(vallo);
5454 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(valhi);
5455 }
5456
5457 // Store
5458 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), storesuccess)
5459 .addReg(ARM::R0).addReg(ARM::R1).addReg(ptr));
5460 // Cmp+jump
5461 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5462 .addReg(storesuccess).addImm(0));
5463 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5464 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5465
5466 BB->addSuccessor(loopMBB);
5467 BB->addSuccessor(exitMBB);
5468
5469 // exitMBB:
5470 // ...
5471 BB = exitMBB;
5472
5473 MI->eraseFromParent(); // The instruction is gone now.
5474
5475 return BB;
5476}
5477
Evan Cheng218977b2010-07-13 19:27:42 +00005478static
5479MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
5480 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
5481 E = MBB->succ_end(); I != E; ++I)
5482 if (*I != Succ)
5483 return *I;
5484 llvm_unreachable("Expecting a BB with two successors!");
5485}
5486
Jim Grosbache801dc42009-12-12 01:40:06 +00005487MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00005488ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00005489 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005490 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00005491 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005492 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00005493 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00005494 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005495 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00005496 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00005497 }
Jim Grosbachee2c2a42011-09-16 21:55:56 +00005498 // The Thumb2 pre-indexed stores have the same MI operands, they just
5499 // define them differently in the .td files from the isel patterns, so
5500 // they need pseudos.
5501 case ARM::t2STR_preidx:
5502 MI->setDesc(TII->get(ARM::t2STR_PRE));
5503 return BB;
5504 case ARM::t2STRB_preidx:
5505 MI->setDesc(TII->get(ARM::t2STRB_PRE));
5506 return BB;
5507 case ARM::t2STRH_preidx:
5508 MI->setDesc(TII->get(ARM::t2STRH_PRE));
5509 return BB;
5510
Jim Grosbach19dec202011-08-05 20:35:44 +00005511 case ARM::STRi_preidx:
5512 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00005513 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00005514 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
5515 // Decode the offset.
5516 unsigned Offset = MI->getOperand(4).getImm();
5517 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
5518 Offset = ARM_AM::getAM2Offset(Offset);
5519 if (isSub)
5520 Offset = -Offset;
5521
Jim Grosbach4dfe2202011-08-12 21:02:34 +00005522 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00005523 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00005524 .addOperand(MI->getOperand(0)) // Rn_wb
5525 .addOperand(MI->getOperand(1)) // Rt
5526 .addOperand(MI->getOperand(2)) // Rn
5527 .addImm(Offset) // offset (skip GPR==zero_reg)
5528 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00005529 .addOperand(MI->getOperand(6))
5530 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00005531 MI->eraseFromParent();
5532 return BB;
5533 }
5534 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00005535 case ARM::STRBr_preidx:
5536 case ARM::STRH_preidx: {
5537 unsigned NewOpc;
5538 switch (MI->getOpcode()) {
5539 default: llvm_unreachable("unexpected opcode!");
5540 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
5541 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
5542 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
5543 }
Jim Grosbach19dec202011-08-05 20:35:44 +00005544 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
5545 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
5546 MIB.addOperand(MI->getOperand(i));
5547 MI->eraseFromParent();
5548 return BB;
5549 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005550 case ARM::ATOMIC_LOAD_ADD_I8:
5551 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
5552 case ARM::ATOMIC_LOAD_ADD_I16:
5553 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
5554 case ARM::ATOMIC_LOAD_ADD_I32:
5555 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005556
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005557 case ARM::ATOMIC_LOAD_AND_I8:
5558 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
5559 case ARM::ATOMIC_LOAD_AND_I16:
5560 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
5561 case ARM::ATOMIC_LOAD_AND_I32:
5562 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005563
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005564 case ARM::ATOMIC_LOAD_OR_I8:
5565 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
5566 case ARM::ATOMIC_LOAD_OR_I16:
5567 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
5568 case ARM::ATOMIC_LOAD_OR_I32:
5569 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005570
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005571 case ARM::ATOMIC_LOAD_XOR_I8:
5572 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
5573 case ARM::ATOMIC_LOAD_XOR_I16:
5574 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
5575 case ARM::ATOMIC_LOAD_XOR_I32:
5576 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005577
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005578 case ARM::ATOMIC_LOAD_NAND_I8:
5579 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5580 case ARM::ATOMIC_LOAD_NAND_I16:
5581 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
5582 case ARM::ATOMIC_LOAD_NAND_I32:
5583 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005584
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005585 case ARM::ATOMIC_LOAD_SUB_I8:
5586 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5587 case ARM::ATOMIC_LOAD_SUB_I16:
5588 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
5589 case ARM::ATOMIC_LOAD_SUB_I32:
5590 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00005591
Jim Grosbachf7da8822011-04-26 19:44:18 +00005592 case ARM::ATOMIC_LOAD_MIN_I8:
5593 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
5594 case ARM::ATOMIC_LOAD_MIN_I16:
5595 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
5596 case ARM::ATOMIC_LOAD_MIN_I32:
5597 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
5598
5599 case ARM::ATOMIC_LOAD_MAX_I8:
5600 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
5601 case ARM::ATOMIC_LOAD_MAX_I16:
5602 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
5603 case ARM::ATOMIC_LOAD_MAX_I32:
5604 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
5605
5606 case ARM::ATOMIC_LOAD_UMIN_I8:
5607 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
5608 case ARM::ATOMIC_LOAD_UMIN_I16:
5609 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
5610 case ARM::ATOMIC_LOAD_UMIN_I32:
5611 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
5612
5613 case ARM::ATOMIC_LOAD_UMAX_I8:
5614 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
5615 case ARM::ATOMIC_LOAD_UMAX_I16:
5616 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
5617 case ARM::ATOMIC_LOAD_UMAX_I32:
5618 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
5619
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005620 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
5621 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
5622 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00005623
5624 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
5625 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
5626 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005627
Eli Friedman2bdffe42011-08-31 00:31:29 +00005628
5629 case ARM::ATOMADD6432:
5630 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005631 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr,
5632 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005633 case ARM::ATOMSUB6432:
5634 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005635 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
5636 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005637 case ARM::ATOMOR6432:
5638 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005639 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005640 case ARM::ATOMXOR6432:
5641 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005642 isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005643 case ARM::ATOMAND6432:
5644 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005645 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005646 case ARM::ATOMSWAP6432:
5647 return EmitAtomicBinary64(MI, BB, 0, 0, false);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005648 case ARM::ATOMCMPXCHG6432:
5649 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
5650 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
5651 /*NeedsCarry*/ false, /*IsCmpxchg*/true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005652
Evan Cheng007ea272009-08-12 05:17:19 +00005653 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00005654 // To "insert" a SELECT_CC instruction, we actually have to insert the
5655 // diamond control-flow pattern. The incoming instruction knows the
5656 // destination vreg to set, the condition code register to branch on, the
5657 // true/false values to select between, and a branch opcode to use.
5658 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005659 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00005660 ++It;
5661
5662 // thisMBB:
5663 // ...
5664 // TrueVal = ...
5665 // cmpTY ccX, r1, r2
5666 // bCC copy1MBB
5667 // fallthrough --> copy0MBB
5668 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005669 MachineFunction *F = BB->getParent();
5670 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
5671 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00005672 F->insert(It, copy0MBB);
5673 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005674
5675 // Transfer the remainder of BB and its successor edges to sinkMBB.
5676 sinkMBB->splice(sinkMBB->begin(), BB,
5677 llvm::next(MachineBasicBlock::iterator(MI)),
5678 BB->end());
5679 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
5680
Dan Gohman258c58c2010-07-06 15:49:48 +00005681 BB->addSuccessor(copy0MBB);
5682 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00005683
Dan Gohman14152b42010-07-06 20:24:04 +00005684 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
5685 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
5686
Evan Chenga8e29892007-01-19 07:51:42 +00005687 // copy0MBB:
5688 // %FalseValue = ...
5689 // # fallthrough to sinkMBB
5690 BB = copy0MBB;
5691
5692 // Update machine-CFG edges
5693 BB->addSuccessor(sinkMBB);
5694
5695 // sinkMBB:
5696 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5697 // ...
5698 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00005699 BuildMI(*BB, BB->begin(), dl,
5700 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00005701 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5702 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5703
Dan Gohman14152b42010-07-06 20:24:04 +00005704 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00005705 return BB;
5706 }
Evan Cheng86198642009-08-07 00:34:42 +00005707
Evan Cheng218977b2010-07-13 19:27:42 +00005708 case ARM::BCCi64:
5709 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00005710 // If there is an unconditional branch to the other successor, remove it.
5711 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00005712
Evan Cheng218977b2010-07-13 19:27:42 +00005713 // Compare both parts that make up the double comparison separately for
5714 // equality.
5715 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
5716
5717 unsigned LHS1 = MI->getOperand(1).getReg();
5718 unsigned LHS2 = MI->getOperand(2).getReg();
5719 if (RHSisZero) {
5720 AddDefaultPred(BuildMI(BB, dl,
5721 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5722 .addReg(LHS1).addImm(0));
5723 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5724 .addReg(LHS2).addImm(0)
5725 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5726 } else {
5727 unsigned RHS1 = MI->getOperand(3).getReg();
5728 unsigned RHS2 = MI->getOperand(4).getReg();
5729 AddDefaultPred(BuildMI(BB, dl,
5730 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5731 .addReg(LHS1).addReg(RHS1));
5732 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5733 .addReg(LHS2).addReg(RHS2)
5734 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
5735 }
5736
5737 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
5738 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
5739 if (MI->getOperand(0).getImm() == ARMCC::NE)
5740 std::swap(destMBB, exitMBB);
5741
5742 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5743 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson51f6a7a2011-09-09 21:48:23 +00005744 if (isThumb2)
5745 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
5746 else
5747 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng218977b2010-07-13 19:27:42 +00005748
5749 MI->eraseFromParent(); // The pseudo instruction is gone now.
5750 return BB;
5751 }
Evan Chenga8e29892007-01-19 07:51:42 +00005752 }
5753}
5754
Evan Cheng37fefc22011-08-30 19:09:48 +00005755void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
5756 SDNode *Node) const {
5757 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC,
5758 // RSB, RSC. Coming out of isel, they have an implicit CPSR def, but the
5759 // optional operand is not filled in. If the carry bit is used, then change
5760 // the optional operand to CPSR. Otherwise, remove the CPSR implicit def.
5761 const MCInstrDesc &MCID = MI->getDesc();
5762 if (Node->hasAnyUseOfValue(1)) {
Evan Cheng4a517082011-09-06 18:52:20 +00005763 MachineOperand &MO = MI->getOperand(MCID.getNumOperands() - 1);
Evan Cheng37fefc22011-08-30 19:09:48 +00005764 MO.setReg(ARM::CPSR);
5765 MO.setIsDef(true);
5766 } else {
5767 for (unsigned i = MCID.getNumOperands(), e = MI->getNumOperands();
5768 i != e; ++i) {
5769 const MachineOperand &MO = MI->getOperand(i);
5770 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
5771 MI->RemoveOperand(i);
5772 break;
5773 }
5774 }
5775 }
5776}
5777
Evan Chenga8e29892007-01-19 07:51:42 +00005778//===----------------------------------------------------------------------===//
5779// ARM Optimization Hooks
5780//===----------------------------------------------------------------------===//
5781
Chris Lattnerd1980a52009-03-12 06:52:53 +00005782static
5783SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
5784 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00005785 SelectionDAG &DAG = DCI.DAG;
5786 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00005787 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00005788 unsigned Opc = N->getOpcode();
5789 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
5790 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
5791 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
5792 ISD::CondCode CC = ISD::SETCC_INVALID;
5793
5794 if (isSlctCC) {
5795 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
5796 } else {
5797 SDValue CCOp = Slct.getOperand(0);
5798 if (CCOp.getOpcode() == ISD::SETCC)
5799 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
5800 }
5801
5802 bool DoXform = false;
5803 bool InvCC = false;
5804 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
5805 "Bad input!");
5806
5807 if (LHS.getOpcode() == ISD::Constant &&
5808 cast<ConstantSDNode>(LHS)->isNullValue()) {
5809 DoXform = true;
5810 } else if (CC != ISD::SETCC_INVALID &&
5811 RHS.getOpcode() == ISD::Constant &&
5812 cast<ConstantSDNode>(RHS)->isNullValue()) {
5813 std::swap(LHS, RHS);
5814 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005815 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00005816 Op0.getOperand(0).getValueType();
5817 bool isInt = OpVT.isInteger();
5818 CC = ISD::getSetCCInverse(CC, isInt);
5819
5820 if (!TLI.isCondCodeLegal(CC, OpVT))
5821 return SDValue(); // Inverse operator isn't legal.
5822
5823 DoXform = true;
5824 InvCC = true;
5825 }
5826
5827 if (DoXform) {
5828 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
5829 if (isSlctCC)
5830 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
5831 Slct.getOperand(0), Slct.getOperand(1), CC);
5832 SDValue CCOp = Slct.getOperand(0);
5833 if (InvCC)
5834 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
5835 CCOp.getOperand(0), CCOp.getOperand(1), CC);
5836 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
5837 CCOp, OtherOp, Result);
5838 }
5839 return SDValue();
5840}
5841
Eric Christopherfa6f5912011-06-29 21:10:36 +00005842// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00005843// (only after legalization).
5844static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
5845 TargetLowering::DAGCombinerInfo &DCI,
5846 const ARMSubtarget *Subtarget) {
5847
5848 // Only perform optimization if after legalize, and if NEON is available. We
5849 // also expected both operands to be BUILD_VECTORs.
5850 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
5851 || N0.getOpcode() != ISD::BUILD_VECTOR
5852 || N1.getOpcode() != ISD::BUILD_VECTOR)
5853 return SDValue();
5854
5855 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
5856 EVT VT = N->getValueType(0);
5857 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
5858 return SDValue();
5859
5860 // Check that the vector operands are of the right form.
5861 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
5862 // operands, where N is the size of the formed vector.
5863 // Each EXTRACT_VECTOR should have the same input vector and odd or even
5864 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00005865
5866 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00005867 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00005868 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00005869 SDValue Vec = N0->getOperand(0)->getOperand(0);
5870 SDNode *V = Vec.getNode();
5871 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00005872
Eric Christopherfa6f5912011-06-29 21:10:36 +00005873 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00005874 // check to see if each of their operands are an EXTRACT_VECTOR with
5875 // the same vector and appropriate index.
5876 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
5877 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
5878 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00005879
Tanya Lattner189531f2011-06-14 23:48:48 +00005880 SDValue ExtVec0 = N0->getOperand(i);
5881 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00005882
Tanya Lattner189531f2011-06-14 23:48:48 +00005883 // First operand is the vector, verify its the same.
5884 if (V != ExtVec0->getOperand(0).getNode() ||
5885 V != ExtVec1->getOperand(0).getNode())
5886 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00005887
Tanya Lattner189531f2011-06-14 23:48:48 +00005888 // Second is the constant, verify its correct.
5889 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
5890 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00005891
Tanya Lattner189531f2011-06-14 23:48:48 +00005892 // For the constant, we want to see all the even or all the odd.
5893 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
5894 || C1->getZExtValue() != nextIndex+1)
5895 return SDValue();
5896
5897 // Increment index.
5898 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00005899 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00005900 return SDValue();
5901 }
5902
5903 // Create VPADDL node.
5904 SelectionDAG &DAG = DCI.DAG;
5905 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00005906
5907 // Build operand list.
5908 SmallVector<SDValue, 8> Ops;
5909 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
5910 TLI.getPointerTy()));
5911
5912 // Input is the vector.
5913 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00005914
Tanya Lattner189531f2011-06-14 23:48:48 +00005915 // Get widened type and narrowed type.
5916 MVT widenType;
5917 unsigned numElem = VT.getVectorNumElements();
5918 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
5919 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
5920 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
5921 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
5922 default:
5923 assert(0 && "Invalid vector element type for padd optimization.");
5924 }
5925
5926 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
5927 widenType, &Ops[0], Ops.size());
5928 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
5929}
5930
Bob Wilson3d5792a2010-07-29 20:34:14 +00005931/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
5932/// operands N0 and N1. This is a helper for PerformADDCombine that is
5933/// called with the default operands, and if that fails, with commuted
5934/// operands.
5935static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00005936 TargetLowering::DAGCombinerInfo &DCI,
5937 const ARMSubtarget *Subtarget){
5938
5939 // Attempt to create vpaddl for this add.
5940 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
5941 if (Result.getNode())
5942 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00005943
Chris Lattnerd1980a52009-03-12 06:52:53 +00005944 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
5945 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
5946 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
5947 if (Result.getNode()) return Result;
5948 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00005949 return SDValue();
5950}
5951
Bob Wilson3d5792a2010-07-29 20:34:14 +00005952/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
5953///
5954static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00005955 TargetLowering::DAGCombinerInfo &DCI,
5956 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00005957 SDValue N0 = N->getOperand(0);
5958 SDValue N1 = N->getOperand(1);
5959
5960 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00005961 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00005962 if (Result.getNode())
5963 return Result;
5964
5965 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00005966 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00005967}
5968
Chris Lattnerd1980a52009-03-12 06:52:53 +00005969/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00005970///
Chris Lattnerd1980a52009-03-12 06:52:53 +00005971static SDValue PerformSUBCombine(SDNode *N,
5972 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00005973 SDValue N0 = N->getOperand(0);
5974 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00005975
Chris Lattnerd1980a52009-03-12 06:52:53 +00005976 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
5977 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
5978 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
5979 if (Result.getNode()) return Result;
5980 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00005981
Chris Lattnerd1980a52009-03-12 06:52:53 +00005982 return SDValue();
5983}
5984
Evan Cheng463d3582011-03-31 19:38:48 +00005985/// PerformVMULCombine
5986/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
5987/// special multiplier accumulator forwarding.
5988/// vmul d3, d0, d2
5989/// vmla d3, d1, d2
5990/// is faster than
5991/// vadd d3, d0, d1
5992/// vmul d3, d3, d2
5993static SDValue PerformVMULCombine(SDNode *N,
5994 TargetLowering::DAGCombinerInfo &DCI,
5995 const ARMSubtarget *Subtarget) {
5996 if (!Subtarget->hasVMLxForwarding())
5997 return SDValue();
5998
5999 SelectionDAG &DAG = DCI.DAG;
6000 SDValue N0 = N->getOperand(0);
6001 SDValue N1 = N->getOperand(1);
6002 unsigned Opcode = N0.getOpcode();
6003 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
6004 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00006005 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00006006 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
6007 Opcode != ISD::FADD && Opcode != ISD::FSUB)
6008 return SDValue();
6009 std::swap(N0, N1);
6010 }
6011
6012 EVT VT = N->getValueType(0);
6013 DebugLoc DL = N->getDebugLoc();
6014 SDValue N00 = N0->getOperand(0);
6015 SDValue N01 = N0->getOperand(1);
6016 return DAG.getNode(Opcode, DL, VT,
6017 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
6018 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
6019}
6020
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006021static SDValue PerformMULCombine(SDNode *N,
6022 TargetLowering::DAGCombinerInfo &DCI,
6023 const ARMSubtarget *Subtarget) {
6024 SelectionDAG &DAG = DCI.DAG;
6025
6026 if (Subtarget->isThumb1Only())
6027 return SDValue();
6028
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006029 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
6030 return SDValue();
6031
6032 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00006033 if (VT.is64BitVector() || VT.is128BitVector())
6034 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006035 if (VT != MVT::i32)
6036 return SDValue();
6037
6038 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
6039 if (!C)
6040 return SDValue();
6041
6042 uint64_t MulAmt = C->getZExtValue();
6043 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
6044 ShiftAmt = ShiftAmt & (32 - 1);
6045 SDValue V = N->getOperand(0);
6046 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006047
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006048 SDValue Res;
6049 MulAmt >>= ShiftAmt;
6050 if (isPowerOf2_32(MulAmt - 1)) {
6051 // (mul x, 2^N + 1) => (add (shl x, N), x)
6052 Res = DAG.getNode(ISD::ADD, DL, VT,
6053 V, DAG.getNode(ISD::SHL, DL, VT,
6054 V, DAG.getConstant(Log2_32(MulAmt-1),
6055 MVT::i32)));
6056 } else if (isPowerOf2_32(MulAmt + 1)) {
6057 // (mul x, 2^N - 1) => (sub (shl x, N), x)
6058 Res = DAG.getNode(ISD::SUB, DL, VT,
6059 DAG.getNode(ISD::SHL, DL, VT,
6060 V, DAG.getConstant(Log2_32(MulAmt+1),
6061 MVT::i32)),
6062 V);
6063 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006064 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006065
6066 if (ShiftAmt != 0)
6067 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
6068 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006069
6070 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006071 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006072 return SDValue();
6073}
6074
Owen Anderson080c0922010-11-05 19:27:46 +00006075static SDValue PerformANDCombine(SDNode *N,
6076 TargetLowering::DAGCombinerInfo &DCI) {
Owen Anderson76706012011-04-05 21:48:57 +00006077
Owen Anderson080c0922010-11-05 19:27:46 +00006078 // Attempt to use immediate-form VBIC
6079 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
6080 DebugLoc dl = N->getDebugLoc();
6081 EVT VT = N->getValueType(0);
6082 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006083
Tanya Lattner0433b212011-04-07 15:24:20 +00006084 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
6085 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00006086
Owen Anderson080c0922010-11-05 19:27:46 +00006087 APInt SplatBits, SplatUndef;
6088 unsigned SplatBitSize;
6089 bool HasAnyUndefs;
6090 if (BVN &&
6091 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
6092 if (SplatBitSize <= 64) {
6093 EVT VbicVT;
6094 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
6095 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006096 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00006097 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00006098 if (Val.getNode()) {
6099 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006100 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00006101 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006102 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00006103 }
6104 }
6105 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006106
Owen Anderson080c0922010-11-05 19:27:46 +00006107 return SDValue();
6108}
6109
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006110/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
6111static SDValue PerformORCombine(SDNode *N,
6112 TargetLowering::DAGCombinerInfo &DCI,
6113 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00006114 // Attempt to use immediate-form VORR
6115 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
6116 DebugLoc dl = N->getDebugLoc();
6117 EVT VT = N->getValueType(0);
6118 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006119
Tanya Lattner0433b212011-04-07 15:24:20 +00006120 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
6121 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00006122
Owen Anderson60f48702010-11-03 23:15:26 +00006123 APInt SplatBits, SplatUndef;
6124 unsigned SplatBitSize;
6125 bool HasAnyUndefs;
6126 if (BVN && Subtarget->hasNEON() &&
6127 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
6128 if (SplatBitSize <= 64) {
6129 EVT VorrVT;
6130 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
6131 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00006132 DAG, VorrVT, VT.is128BitVector(),
6133 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00006134 if (Val.getNode()) {
6135 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006136 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00006137 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006138 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00006139 }
6140 }
6141 }
6142
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00006143 SDValue N0 = N->getOperand(0);
6144 if (N0.getOpcode() != ISD::AND)
6145 return SDValue();
6146 SDValue N1 = N->getOperand(1);
6147
6148 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
6149 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
6150 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
6151 APInt SplatUndef;
6152 unsigned SplatBitSize;
6153 bool HasAnyUndefs;
6154
6155 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
6156 APInt SplatBits0;
6157 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
6158 HasAnyUndefs) && !HasAnyUndefs) {
6159 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
6160 APInt SplatBits1;
6161 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
6162 HasAnyUndefs) && !HasAnyUndefs &&
6163 SplatBits0 == ~SplatBits1) {
6164 // Canonicalize the vector type to make instruction selection simpler.
6165 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
6166 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
6167 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00006168 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00006169 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
6170 }
6171 }
6172 }
6173
Jim Grosbach54238562010-07-17 03:30:54 +00006174 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
6175 // reasonable.
6176
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006177 // BFI is only available on V6T2+
6178 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
6179 return SDValue();
6180
Jim Grosbach54238562010-07-17 03:30:54 +00006181 DebugLoc DL = N->getDebugLoc();
6182 // 1) or (and A, mask), val => ARMbfi A, val, mask
6183 // iff (val & mask) == val
6184 //
6185 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
6186 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00006187 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00006188 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00006189 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00006190 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006191
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006192 if (VT != MVT::i32)
6193 return SDValue();
6194
Evan Cheng30fb13f2010-12-13 20:32:54 +00006195 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00006196
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006197 // The value and the mask need to be constants so we can verify this is
6198 // actually a bitfield set. If the mask is 0xffff, we can do better
6199 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00006200 SDValue MaskOp = N0.getOperand(1);
6201 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
6202 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006203 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00006204 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006205 if (Mask == 0xffff)
6206 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006207 SDValue Res;
6208 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00006209 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
6210 if (N1C) {
6211 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00006212 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00006213 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006214
Evan Chenga9688c42010-12-11 04:11:38 +00006215 if (ARM::isBitFieldInvertedMask(Mask)) {
6216 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006217
Evan Cheng30fb13f2010-12-13 20:32:54 +00006218 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00006219 DAG.getConstant(Val, MVT::i32),
6220 DAG.getConstant(Mask, MVT::i32));
6221
6222 // Do not add new nodes to DAG combiner worklist.
6223 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00006224 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00006225 }
Jim Grosbach54238562010-07-17 03:30:54 +00006226 } else if (N1.getOpcode() == ISD::AND) {
6227 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00006228 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
6229 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00006230 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00006231 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006232
Eric Christopher29aeed12011-03-26 01:21:03 +00006233 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
6234 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00006235 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00006236 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00006237 // The pack halfword instruction works better for masks that fit it,
6238 // so use that when it's available.
6239 if (Subtarget->hasT2ExtractPack() &&
6240 (Mask == 0xffff || Mask == 0xffff0000))
6241 return SDValue();
6242 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00006243 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00006244 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00006245 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00006246 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00006247 DAG.getConstant(Mask, MVT::i32));
6248 // Do not add new nodes to DAG combiner worklist.
6249 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00006250 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006251 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00006252 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00006253 // The pack halfword instruction works better for masks that fit it,
6254 // so use that when it's available.
6255 if (Subtarget->hasT2ExtractPack() &&
6256 (Mask2 == 0xffff || Mask2 == 0xffff0000))
6257 return SDValue();
6258 // 2b
6259 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00006260 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00006261 DAG.getConstant(lsb, MVT::i32));
6262 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00006263 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00006264 // Do not add new nodes to DAG combiner worklist.
6265 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00006266 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006267 }
6268 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006269
Evan Cheng30fb13f2010-12-13 20:32:54 +00006270 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
6271 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
6272 ARM::isBitFieldInvertedMask(~Mask)) {
6273 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
6274 // where lsb(mask) == #shamt and masked bits of B are known zero.
6275 SDValue ShAmt = N00.getOperand(1);
6276 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
6277 unsigned LSB = CountTrailingZeros_32(Mask);
6278 if (ShAmtC != LSB)
6279 return SDValue();
6280
6281 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
6282 DAG.getConstant(~Mask, MVT::i32));
6283
6284 // Do not add new nodes to DAG combiner worklist.
6285 DCI.CombineTo(N, Res, false);
6286 }
6287
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006288 return SDValue();
6289}
6290
Evan Chengbf188ae2011-06-15 01:12:31 +00006291/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
6292/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00006293static SDValue PerformBFICombine(SDNode *N,
6294 TargetLowering::DAGCombinerInfo &DCI) {
6295 SDValue N1 = N->getOperand(1);
6296 if (N1.getOpcode() == ISD::AND) {
6297 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
6298 if (!N11C)
6299 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00006300 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
6301 unsigned LSB = CountTrailingZeros_32(~InvMask);
6302 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
6303 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00006304 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00006305 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00006306 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
6307 N->getOperand(0), N1.getOperand(0),
6308 N->getOperand(2));
6309 }
6310 return SDValue();
6311}
6312
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006313/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
6314/// ARMISD::VMOVRRD.
6315static SDValue PerformVMOVRRDCombine(SDNode *N,
6316 TargetLowering::DAGCombinerInfo &DCI) {
6317 // vmovrrd(vmovdrr x, y) -> x,y
6318 SDValue InDouble = N->getOperand(0);
6319 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
6320 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00006321
6322 // vmovrrd(load f64) -> (load i32), (load i32)
6323 SDNode *InNode = InDouble.getNode();
6324 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
6325 InNode->getValueType(0) == MVT::f64 &&
6326 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
6327 !cast<LoadSDNode>(InNode)->isVolatile()) {
6328 // TODO: Should this be done for non-FrameIndex operands?
6329 LoadSDNode *LD = cast<LoadSDNode>(InNode);
6330
6331 SelectionDAG &DAG = DCI.DAG;
6332 DebugLoc DL = LD->getDebugLoc();
6333 SDValue BasePtr = LD->getBasePtr();
6334 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
6335 LD->getPointerInfo(), LD->isVolatile(),
6336 LD->isNonTemporal(), LD->getAlignment());
6337
6338 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
6339 DAG.getConstant(4, MVT::i32));
6340 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
6341 LD->getPointerInfo(), LD->isVolatile(),
6342 LD->isNonTemporal(),
6343 std::min(4U, LD->getAlignment() / 2));
6344
6345 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
6346 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
6347 DCI.RemoveFromWorklist(LD);
6348 DAG.DeleteNode(LD);
6349 return Result;
6350 }
6351
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006352 return SDValue();
6353}
6354
6355/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
6356/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
6357static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
6358 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
6359 SDValue Op0 = N->getOperand(0);
6360 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006361 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006362 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006363 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006364 Op1 = Op1.getOperand(0);
6365 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
6366 Op0.getNode() == Op1.getNode() &&
6367 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006368 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00006369 N->getValueType(0), Op0.getOperand(0));
6370 return SDValue();
6371}
6372
Bob Wilson31600902010-12-21 06:43:19 +00006373/// PerformSTORECombine - Target-specific dag combine xforms for
6374/// ISD::STORE.
6375static SDValue PerformSTORECombine(SDNode *N,
6376 TargetLowering::DAGCombinerInfo &DCI) {
6377 // Bitcast an i64 store extracted from a vector to f64.
6378 // Otherwise, the i64 value will be legalized to a pair of i32 values.
6379 StoreSDNode *St = cast<StoreSDNode>(N);
6380 SDValue StVal = St->getValue();
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00006381 if (!ISD::isNormalStore(St) || St->isVolatile())
6382 return SDValue();
6383
6384 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
6385 StVal.getNode()->hasOneUse() && !St->isVolatile()) {
6386 SelectionDAG &DAG = DCI.DAG;
6387 DebugLoc DL = St->getDebugLoc();
6388 SDValue BasePtr = St->getBasePtr();
6389 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
6390 StVal.getNode()->getOperand(0), BasePtr,
6391 St->getPointerInfo(), St->isVolatile(),
6392 St->isNonTemporal(), St->getAlignment());
6393
6394 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
6395 DAG.getConstant(4, MVT::i32));
6396 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
6397 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
6398 St->isNonTemporal(),
6399 std::min(4U, St->getAlignment() / 2));
6400 }
6401
6402 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00006403 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
6404 return SDValue();
6405
6406 SelectionDAG &DAG = DCI.DAG;
6407 DebugLoc dl = StVal.getDebugLoc();
6408 SDValue IntVec = StVal.getOperand(0);
6409 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
6410 IntVec.getValueType().getVectorNumElements());
6411 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
6412 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
6413 Vec, StVal.getOperand(1));
6414 dl = N->getDebugLoc();
6415 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
6416 // Make the DAGCombiner fold the bitcasts.
6417 DCI.AddToWorklist(Vec.getNode());
6418 DCI.AddToWorklist(ExtElt.getNode());
6419 DCI.AddToWorklist(V.getNode());
6420 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
6421 St->getPointerInfo(), St->isVolatile(),
6422 St->isNonTemporal(), St->getAlignment(),
6423 St->getTBAAInfo());
6424}
6425
6426/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
6427/// are normal, non-volatile loads. If so, it is profitable to bitcast an
6428/// i64 vector to have f64 elements, since the value can then be loaded
6429/// directly into a VFP register.
6430static bool hasNormalLoadOperand(SDNode *N) {
6431 unsigned NumElts = N->getValueType(0).getVectorNumElements();
6432 for (unsigned i = 0; i < NumElts; ++i) {
6433 SDNode *Elt = N->getOperand(i).getNode();
6434 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
6435 return true;
6436 }
6437 return false;
6438}
6439
Bob Wilson75f02882010-09-17 22:59:05 +00006440/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
6441/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00006442static SDValue PerformBUILD_VECTORCombine(SDNode *N,
6443 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00006444 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
6445 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
6446 // into a pair of GPRs, which is fine when the value is used as a scalar,
6447 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00006448 SelectionDAG &DAG = DCI.DAG;
6449 if (N->getNumOperands() == 2) {
6450 SDValue RV = PerformVMOVDRRCombine(N, DAG);
6451 if (RV.getNode())
6452 return RV;
6453 }
Bob Wilson75f02882010-09-17 22:59:05 +00006454
Bob Wilson31600902010-12-21 06:43:19 +00006455 // Load i64 elements as f64 values so that type legalization does not split
6456 // them up into i32 values.
6457 EVT VT = N->getValueType(0);
6458 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
6459 return SDValue();
6460 DebugLoc dl = N->getDebugLoc();
6461 SmallVector<SDValue, 8> Ops;
6462 unsigned NumElts = VT.getVectorNumElements();
6463 for (unsigned i = 0; i < NumElts; ++i) {
6464 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
6465 Ops.push_back(V);
6466 // Make the DAGCombiner fold the bitcast.
6467 DCI.AddToWorklist(V.getNode());
6468 }
6469 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
6470 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
6471 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
6472}
6473
6474/// PerformInsertEltCombine - Target-specific dag combine xforms for
6475/// ISD::INSERT_VECTOR_ELT.
6476static SDValue PerformInsertEltCombine(SDNode *N,
6477 TargetLowering::DAGCombinerInfo &DCI) {
6478 // Bitcast an i64 load inserted into a vector to f64.
6479 // Otherwise, the i64 value will be legalized to a pair of i32 values.
6480 EVT VT = N->getValueType(0);
6481 SDNode *Elt = N->getOperand(1).getNode();
6482 if (VT.getVectorElementType() != MVT::i64 ||
6483 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
6484 return SDValue();
6485
6486 SelectionDAG &DAG = DCI.DAG;
6487 DebugLoc dl = N->getDebugLoc();
6488 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
6489 VT.getVectorNumElements());
6490 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
6491 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
6492 // Make the DAGCombiner fold the bitcasts.
6493 DCI.AddToWorklist(Vec.getNode());
6494 DCI.AddToWorklist(V.getNode());
6495 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
6496 Vec, V, N->getOperand(2));
6497 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00006498}
6499
Bob Wilsonf20700c2010-10-27 20:38:28 +00006500/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
6501/// ISD::VECTOR_SHUFFLE.
6502static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
6503 // The LLVM shufflevector instruction does not require the shuffle mask
6504 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
6505 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
6506 // operands do not match the mask length, they are extended by concatenating
6507 // them with undef vectors. That is probably the right thing for other
6508 // targets, but for NEON it is better to concatenate two double-register
6509 // size vector operands into a single quad-register size vector. Do that
6510 // transformation here:
6511 // shuffle(concat(v1, undef), concat(v2, undef)) ->
6512 // shuffle(concat(v1, v2), undef)
6513 SDValue Op0 = N->getOperand(0);
6514 SDValue Op1 = N->getOperand(1);
6515 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
6516 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
6517 Op0.getNumOperands() != 2 ||
6518 Op1.getNumOperands() != 2)
6519 return SDValue();
6520 SDValue Concat0Op1 = Op0.getOperand(1);
6521 SDValue Concat1Op1 = Op1.getOperand(1);
6522 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
6523 Concat1Op1.getOpcode() != ISD::UNDEF)
6524 return SDValue();
6525 // Skip the transformation if any of the types are illegal.
6526 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6527 EVT VT = N->getValueType(0);
6528 if (!TLI.isTypeLegal(VT) ||
6529 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
6530 !TLI.isTypeLegal(Concat1Op1.getValueType()))
6531 return SDValue();
6532
6533 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
6534 Op0.getOperand(0), Op1.getOperand(0));
6535 // Translate the shuffle mask.
6536 SmallVector<int, 16> NewMask;
6537 unsigned NumElts = VT.getVectorNumElements();
6538 unsigned HalfElts = NumElts/2;
6539 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
6540 for (unsigned n = 0; n < NumElts; ++n) {
6541 int MaskElt = SVN->getMaskElt(n);
6542 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00006543 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00006544 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00006545 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00006546 NewElt = HalfElts + MaskElt - NumElts;
6547 NewMask.push_back(NewElt);
6548 }
6549 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
6550 DAG.getUNDEF(VT), NewMask.data());
6551}
6552
Bob Wilson1c3ef902011-02-07 17:43:21 +00006553/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
6554/// NEON load/store intrinsics to merge base address updates.
6555static SDValue CombineBaseUpdate(SDNode *N,
6556 TargetLowering::DAGCombinerInfo &DCI) {
6557 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
6558 return SDValue();
6559
6560 SelectionDAG &DAG = DCI.DAG;
6561 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
6562 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
6563 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
6564 SDValue Addr = N->getOperand(AddrOpIdx);
6565
6566 // Search for a use of the address operand that is an increment.
6567 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
6568 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
6569 SDNode *User = *UI;
6570 if (User->getOpcode() != ISD::ADD ||
6571 UI.getUse().getResNo() != Addr.getResNo())
6572 continue;
6573
6574 // Check that the add is independent of the load/store. Otherwise, folding
6575 // it would create a cycle.
6576 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
6577 continue;
6578
6579 // Find the new opcode for the updating load/store.
6580 bool isLoad = true;
6581 bool isLaneOp = false;
6582 unsigned NewOpc = 0;
6583 unsigned NumVecs = 0;
6584 if (isIntrinsic) {
6585 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
6586 switch (IntNo) {
6587 default: assert(0 && "unexpected intrinsic for Neon base update");
6588 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
6589 NumVecs = 1; break;
6590 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
6591 NumVecs = 2; break;
6592 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
6593 NumVecs = 3; break;
6594 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
6595 NumVecs = 4; break;
6596 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
6597 NumVecs = 2; isLaneOp = true; break;
6598 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
6599 NumVecs = 3; isLaneOp = true; break;
6600 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
6601 NumVecs = 4; isLaneOp = true; break;
6602 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
6603 NumVecs = 1; isLoad = false; break;
6604 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
6605 NumVecs = 2; isLoad = false; break;
6606 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
6607 NumVecs = 3; isLoad = false; break;
6608 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
6609 NumVecs = 4; isLoad = false; break;
6610 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
6611 NumVecs = 2; isLoad = false; isLaneOp = true; break;
6612 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
6613 NumVecs = 3; isLoad = false; isLaneOp = true; break;
6614 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
6615 NumVecs = 4; isLoad = false; isLaneOp = true; break;
6616 }
6617 } else {
6618 isLaneOp = true;
6619 switch (N->getOpcode()) {
6620 default: assert(0 && "unexpected opcode for Neon base update");
6621 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
6622 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
6623 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
6624 }
6625 }
6626
6627 // Find the size of memory referenced by the load/store.
6628 EVT VecTy;
6629 if (isLoad)
6630 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00006631 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00006632 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
6633 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
6634 if (isLaneOp)
6635 NumBytes /= VecTy.getVectorNumElements();
6636
6637 // If the increment is a constant, it must match the memory ref size.
6638 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
6639 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
6640 uint64_t IncVal = CInc->getZExtValue();
6641 if (IncVal != NumBytes)
6642 continue;
6643 } else if (NumBytes >= 3 * 16) {
6644 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
6645 // separate instructions that make it harder to use a non-constant update.
6646 continue;
6647 }
6648
6649 // Create the new updating load/store node.
6650 EVT Tys[6];
6651 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
6652 unsigned n;
6653 for (n = 0; n < NumResultVecs; ++n)
6654 Tys[n] = VecTy;
6655 Tys[n++] = MVT::i32;
6656 Tys[n] = MVT::Other;
6657 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
6658 SmallVector<SDValue, 8> Ops;
6659 Ops.push_back(N->getOperand(0)); // incoming chain
6660 Ops.push_back(N->getOperand(AddrOpIdx));
6661 Ops.push_back(Inc);
6662 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
6663 Ops.push_back(N->getOperand(i));
6664 }
6665 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
6666 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
6667 Ops.data(), Ops.size(),
6668 MemInt->getMemoryVT(),
6669 MemInt->getMemOperand());
6670
6671 // Update the uses.
6672 std::vector<SDValue> NewResults;
6673 for (unsigned i = 0; i < NumResultVecs; ++i) {
6674 NewResults.push_back(SDValue(UpdN.getNode(), i));
6675 }
6676 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
6677 DCI.CombineTo(N, NewResults);
6678 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
6679
6680 break;
Owen Anderson76706012011-04-05 21:48:57 +00006681 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00006682 return SDValue();
6683}
6684
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006685/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
6686/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
6687/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
6688/// return true.
6689static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
6690 SelectionDAG &DAG = DCI.DAG;
6691 EVT VT = N->getValueType(0);
6692 // vldN-dup instructions only support 64-bit vectors for N > 1.
6693 if (!VT.is64BitVector())
6694 return false;
6695
6696 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
6697 SDNode *VLD = N->getOperand(0).getNode();
6698 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
6699 return false;
6700 unsigned NumVecs = 0;
6701 unsigned NewOpc = 0;
6702 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
6703 if (IntNo == Intrinsic::arm_neon_vld2lane) {
6704 NumVecs = 2;
6705 NewOpc = ARMISD::VLD2DUP;
6706 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
6707 NumVecs = 3;
6708 NewOpc = ARMISD::VLD3DUP;
6709 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
6710 NumVecs = 4;
6711 NewOpc = ARMISD::VLD4DUP;
6712 } else {
6713 return false;
6714 }
6715
6716 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
6717 // numbers match the load.
6718 unsigned VLDLaneNo =
6719 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
6720 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
6721 UI != UE; ++UI) {
6722 // Ignore uses of the chain result.
6723 if (UI.getUse().getResNo() == NumVecs)
6724 continue;
6725 SDNode *User = *UI;
6726 if (User->getOpcode() != ARMISD::VDUPLANE ||
6727 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
6728 return false;
6729 }
6730
6731 // Create the vldN-dup node.
6732 EVT Tys[5];
6733 unsigned n;
6734 for (n = 0; n < NumVecs; ++n)
6735 Tys[n] = VT;
6736 Tys[n] = MVT::Other;
6737 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
6738 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
6739 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
6740 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
6741 Ops, 2, VLDMemInt->getMemoryVT(),
6742 VLDMemInt->getMemOperand());
6743
6744 // Update the uses.
6745 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
6746 UI != UE; ++UI) {
6747 unsigned ResNo = UI.getUse().getResNo();
6748 // Ignore uses of the chain result.
6749 if (ResNo == NumVecs)
6750 continue;
6751 SDNode *User = *UI;
6752 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
6753 }
6754
6755 // Now the vldN-lane intrinsic is dead except for its chain result.
6756 // Update uses of the chain.
6757 std::vector<SDValue> VLDDupResults;
6758 for (unsigned n = 0; n < NumVecs; ++n)
6759 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
6760 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
6761 DCI.CombineTo(VLD, VLDDupResults);
6762
6763 return true;
6764}
6765
Bob Wilson9e82bf12010-07-14 01:22:12 +00006766/// PerformVDUPLANECombine - Target-specific dag combine xforms for
6767/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006768static SDValue PerformVDUPLANECombine(SDNode *N,
6769 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00006770 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006771
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006772 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
6773 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
6774 if (CombineVLDDUP(N, DCI))
6775 return SDValue(N, 0);
6776
6777 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
6778 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006779 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00006780 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00006781 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00006782 return SDValue();
6783
6784 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
6785 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
6786 // The canonical VMOV for a zero vector uses a 32-bit element size.
6787 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6788 unsigned EltBits;
6789 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
6790 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006791 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006792 if (EltSize > VT.getVectorElementType().getSizeInBits())
6793 return SDValue();
6794
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00006795 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00006796}
6797
Eric Christopherfa6f5912011-06-29 21:10:36 +00006798// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00006799// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
6800static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
6801{
Chad Rosier118c9a02011-06-28 17:26:57 +00006802 integerPart cN;
6803 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00006804 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
6805 I != E; I++) {
6806 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
6807 if (!C)
6808 return false;
6809
Eric Christopherfa6f5912011-06-29 21:10:36 +00006810 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00006811 APFloat APF = C->getValueAPF();
6812 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
6813 != APFloat::opOK || !isExact)
6814 return false;
6815
6816 c0 = (I == 0) ? cN : c0;
6817 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
6818 return false;
6819 }
6820 C = c0;
6821 return true;
6822}
6823
6824/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
6825/// can replace combinations of VMUL and VCVT (floating-point to integer)
6826/// when the VMUL has a constant operand that is a power of 2.
6827///
6828/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
6829/// vmul.f32 d16, d17, d16
6830/// vcvt.s32.f32 d16, d16
6831/// becomes:
6832/// vcvt.s32.f32 d16, d16, #3
6833static SDValue PerformVCVTCombine(SDNode *N,
6834 TargetLowering::DAGCombinerInfo &DCI,
6835 const ARMSubtarget *Subtarget) {
6836 SelectionDAG &DAG = DCI.DAG;
6837 SDValue Op = N->getOperand(0);
6838
6839 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
6840 Op.getOpcode() != ISD::FMUL)
6841 return SDValue();
6842
6843 uint64_t C;
6844 SDValue N0 = Op->getOperand(0);
6845 SDValue ConstVec = Op->getOperand(1);
6846 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
6847
Eric Christopherfa6f5912011-06-29 21:10:36 +00006848 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00006849 !isConstVecPow2(ConstVec, isSigned, C))
6850 return SDValue();
6851
6852 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
6853 Intrinsic::arm_neon_vcvtfp2fxu;
6854 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
6855 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00006856 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00006857 DAG.getConstant(Log2_64(C), MVT::i32));
6858}
6859
6860/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
6861/// can replace combinations of VCVT (integer to floating-point) and VDIV
6862/// when the VDIV has a constant operand that is a power of 2.
6863///
6864/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
6865/// vcvt.f32.s32 d16, d16
6866/// vdiv.f32 d16, d17, d16
6867/// becomes:
6868/// vcvt.f32.s32 d16, d16, #3
6869static SDValue PerformVDIVCombine(SDNode *N,
6870 TargetLowering::DAGCombinerInfo &DCI,
6871 const ARMSubtarget *Subtarget) {
6872 SelectionDAG &DAG = DCI.DAG;
6873 SDValue Op = N->getOperand(0);
6874 unsigned OpOpcode = Op.getNode()->getOpcode();
6875
6876 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
6877 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
6878 return SDValue();
6879
6880 uint64_t C;
6881 SDValue ConstVec = N->getOperand(1);
6882 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
6883
6884 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
6885 !isConstVecPow2(ConstVec, isSigned, C))
6886 return SDValue();
6887
Eric Christopherfa6f5912011-06-29 21:10:36 +00006888 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00006889 Intrinsic::arm_neon_vcvtfxu2fp;
6890 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
6891 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00006892 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00006893 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
6894}
6895
6896/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00006897/// operand of a vector shift operation, where all the elements of the
6898/// build_vector must have the same constant integer value.
6899static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
6900 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006901 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00006902 Op = Op.getOperand(0);
6903 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
6904 APInt SplatBits, SplatUndef;
6905 unsigned SplatBitSize;
6906 bool HasAnyUndefs;
6907 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
6908 HasAnyUndefs, ElementBits) ||
6909 SplatBitSize > ElementBits)
6910 return false;
6911 Cnt = SplatBits.getSExtValue();
6912 return true;
6913}
6914
6915/// isVShiftLImm - Check if this is a valid build_vector for the immediate
6916/// operand of a vector shift left operation. That value must be in the range:
6917/// 0 <= Value < ElementBits for a left shift; or
6918/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006919static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00006920 assert(VT.isVector() && "vector shift count is not a vector type");
6921 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6922 if (! getVShiftImm(Op, ElementBits, Cnt))
6923 return false;
6924 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
6925}
6926
6927/// isVShiftRImm - Check if this is a valid build_vector for the immediate
6928/// operand of a vector shift right operation. For a shift opcode, the value
6929/// is positive, but for an intrinsic the value count must be negative. The
6930/// absolute value must be in the range:
6931/// 1 <= |Value| <= ElementBits for a right shift; or
6932/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00006933static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00006934 int64_t &Cnt) {
6935 assert(VT.isVector() && "vector shift count is not a vector type");
6936 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
6937 if (! getVShiftImm(Op, ElementBits, Cnt))
6938 return false;
6939 if (isIntrinsic)
6940 Cnt = -Cnt;
6941 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
6942}
6943
6944/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
6945static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
6946 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
6947 switch (IntNo) {
6948 default:
6949 // Don't do anything for most intrinsics.
6950 break;
6951
6952 // Vector shifts: check for immediate versions and lower them.
6953 // Note: This is done during DAG combining instead of DAG legalizing because
6954 // the build_vectors for 64-bit vector element shift counts are generally
6955 // not legal, and it is hard to see their values after they get legalized to
6956 // loads from a constant pool.
6957 case Intrinsic::arm_neon_vshifts:
6958 case Intrinsic::arm_neon_vshiftu:
6959 case Intrinsic::arm_neon_vshiftls:
6960 case Intrinsic::arm_neon_vshiftlu:
6961 case Intrinsic::arm_neon_vshiftn:
6962 case Intrinsic::arm_neon_vrshifts:
6963 case Intrinsic::arm_neon_vrshiftu:
6964 case Intrinsic::arm_neon_vrshiftn:
6965 case Intrinsic::arm_neon_vqshifts:
6966 case Intrinsic::arm_neon_vqshiftu:
6967 case Intrinsic::arm_neon_vqshiftsu:
6968 case Intrinsic::arm_neon_vqshiftns:
6969 case Intrinsic::arm_neon_vqshiftnu:
6970 case Intrinsic::arm_neon_vqshiftnsu:
6971 case Intrinsic::arm_neon_vqrshiftns:
6972 case Intrinsic::arm_neon_vqrshiftnu:
6973 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00006974 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00006975 int64_t Cnt;
6976 unsigned VShiftOpc = 0;
6977
6978 switch (IntNo) {
6979 case Intrinsic::arm_neon_vshifts:
6980 case Intrinsic::arm_neon_vshiftu:
6981 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
6982 VShiftOpc = ARMISD::VSHL;
6983 break;
6984 }
6985 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
6986 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
6987 ARMISD::VSHRs : ARMISD::VSHRu);
6988 break;
6989 }
6990 return SDValue();
6991
6992 case Intrinsic::arm_neon_vshiftls:
6993 case Intrinsic::arm_neon_vshiftlu:
6994 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
6995 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006996 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00006997
6998 case Intrinsic::arm_neon_vrshifts:
6999 case Intrinsic::arm_neon_vrshiftu:
7000 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
7001 break;
7002 return SDValue();
7003
7004 case Intrinsic::arm_neon_vqshifts:
7005 case Intrinsic::arm_neon_vqshiftu:
7006 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
7007 break;
7008 return SDValue();
7009
7010 case Intrinsic::arm_neon_vqshiftsu:
7011 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
7012 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007013 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007014
7015 case Intrinsic::arm_neon_vshiftn:
7016 case Intrinsic::arm_neon_vrshiftn:
7017 case Intrinsic::arm_neon_vqshiftns:
7018 case Intrinsic::arm_neon_vqshiftnu:
7019 case Intrinsic::arm_neon_vqshiftnsu:
7020 case Intrinsic::arm_neon_vqrshiftns:
7021 case Intrinsic::arm_neon_vqrshiftnu:
7022 case Intrinsic::arm_neon_vqrshiftnsu:
7023 // Narrowing shifts require an immediate right shift.
7024 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
7025 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00007026 llvm_unreachable("invalid shift count for narrowing vector shift "
7027 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007028
7029 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00007030 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00007031 }
7032
7033 switch (IntNo) {
7034 case Intrinsic::arm_neon_vshifts:
7035 case Intrinsic::arm_neon_vshiftu:
7036 // Opcode already set above.
7037 break;
7038 case Intrinsic::arm_neon_vshiftls:
7039 case Intrinsic::arm_neon_vshiftlu:
7040 if (Cnt == VT.getVectorElementType().getSizeInBits())
7041 VShiftOpc = ARMISD::VSHLLi;
7042 else
7043 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
7044 ARMISD::VSHLLs : ARMISD::VSHLLu);
7045 break;
7046 case Intrinsic::arm_neon_vshiftn:
7047 VShiftOpc = ARMISD::VSHRN; break;
7048 case Intrinsic::arm_neon_vrshifts:
7049 VShiftOpc = ARMISD::VRSHRs; break;
7050 case Intrinsic::arm_neon_vrshiftu:
7051 VShiftOpc = ARMISD::VRSHRu; break;
7052 case Intrinsic::arm_neon_vrshiftn:
7053 VShiftOpc = ARMISD::VRSHRN; break;
7054 case Intrinsic::arm_neon_vqshifts:
7055 VShiftOpc = ARMISD::VQSHLs; break;
7056 case Intrinsic::arm_neon_vqshiftu:
7057 VShiftOpc = ARMISD::VQSHLu; break;
7058 case Intrinsic::arm_neon_vqshiftsu:
7059 VShiftOpc = ARMISD::VQSHLsu; break;
7060 case Intrinsic::arm_neon_vqshiftns:
7061 VShiftOpc = ARMISD::VQSHRNs; break;
7062 case Intrinsic::arm_neon_vqshiftnu:
7063 VShiftOpc = ARMISD::VQSHRNu; break;
7064 case Intrinsic::arm_neon_vqshiftnsu:
7065 VShiftOpc = ARMISD::VQSHRNsu; break;
7066 case Intrinsic::arm_neon_vqrshiftns:
7067 VShiftOpc = ARMISD::VQRSHRNs; break;
7068 case Intrinsic::arm_neon_vqrshiftnu:
7069 VShiftOpc = ARMISD::VQRSHRNu; break;
7070 case Intrinsic::arm_neon_vqrshiftnsu:
7071 VShiftOpc = ARMISD::VQRSHRNsu; break;
7072 }
7073
7074 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007075 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007076 }
7077
7078 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00007079 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007080 int64_t Cnt;
7081 unsigned VShiftOpc = 0;
7082
7083 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
7084 VShiftOpc = ARMISD::VSLI;
7085 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
7086 VShiftOpc = ARMISD::VSRI;
7087 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00007088 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007089 }
7090
7091 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
7092 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00007093 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007094 }
7095
7096 case Intrinsic::arm_neon_vqrshifts:
7097 case Intrinsic::arm_neon_vqrshiftu:
7098 // No immediate versions of these to check for.
7099 break;
7100 }
7101
7102 return SDValue();
7103}
7104
7105/// PerformShiftCombine - Checks for immediate versions of vector shifts and
7106/// lowers them. As with the vector shift intrinsics, this is done during DAG
7107/// combining instead of DAG legalizing because the build_vectors for 64-bit
7108/// vector element shift counts are generally not legal, and it is hard to see
7109/// their values after they get legalized to loads from a constant pool.
7110static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
7111 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00007112 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00007113
7114 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00007115 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7116 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00007117 return SDValue();
7118
7119 assert(ST->hasNEON() && "unexpected vector shift");
7120 int64_t Cnt;
7121
7122 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007123 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00007124
7125 case ISD::SHL:
7126 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
7127 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007128 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007129 break;
7130
7131 case ISD::SRA:
7132 case ISD::SRL:
7133 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
7134 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
7135 ARMISD::VSHRs : ARMISD::VSHRu);
7136 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007137 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007138 }
7139 }
7140 return SDValue();
7141}
7142
7143/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
7144/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
7145static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
7146 const ARMSubtarget *ST) {
7147 SDValue N0 = N->getOperand(0);
7148
7149 // Check for sign- and zero-extensions of vector extract operations of 8-
7150 // and 16-bit vector elements. NEON supports these directly. They are
7151 // handled during DAG combining because type legalization will promote them
7152 // to 32-bit types and it is messy to recognize the operations after that.
7153 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
7154 SDValue Vec = N0.getOperand(0);
7155 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00007156 EVT VT = N->getValueType(0);
7157 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007158 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7159
Owen Anderson825b72b2009-08-11 20:47:22 +00007160 if (VT == MVT::i32 &&
7161 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00007162 TLI.isTypeLegal(Vec.getValueType()) &&
7163 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00007164
7165 unsigned Opc = 0;
7166 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007167 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00007168 case ISD::SIGN_EXTEND:
7169 Opc = ARMISD::VGETLANEs;
7170 break;
7171 case ISD::ZERO_EXTEND:
7172 case ISD::ANY_EXTEND:
7173 Opc = ARMISD::VGETLANEu;
7174 break;
7175 }
7176 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
7177 }
7178 }
7179
7180 return SDValue();
7181}
7182
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007183/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
7184/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
7185static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
7186 const ARMSubtarget *ST) {
7187 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00007188 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007189 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
7190 // a NaN; only do the transformation when it matches that behavior.
7191
7192 // For now only do this when using NEON for FP operations; if using VFP, it
7193 // is not obvious that the benefit outweighs the cost of switching to the
7194 // NEON pipeline.
7195 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
7196 N->getValueType(0) != MVT::f32)
7197 return SDValue();
7198
7199 SDValue CondLHS = N->getOperand(0);
7200 SDValue CondRHS = N->getOperand(1);
7201 SDValue LHS = N->getOperand(2);
7202 SDValue RHS = N->getOperand(3);
7203 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
7204
7205 unsigned Opcode = 0;
7206 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00007207 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007208 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00007209 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007210 IsReversed = true ; // x CC y ? y : x
7211 } else {
7212 return SDValue();
7213 }
7214
Bob Wilsone742bb52010-02-24 22:15:53 +00007215 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007216 switch (CC) {
7217 default: break;
7218 case ISD::SETOLT:
7219 case ISD::SETOLE:
7220 case ISD::SETLT:
7221 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007222 case ISD::SETULT:
7223 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00007224 // If LHS is NaN, an ordered comparison will be false and the result will
7225 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
7226 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
7227 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
7228 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
7229 break;
7230 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
7231 // will return -0, so vmin can only be used for unsafe math or if one of
7232 // the operands is known to be nonzero.
7233 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
7234 !UnsafeFPMath &&
7235 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
7236 break;
7237 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007238 break;
7239
7240 case ISD::SETOGT:
7241 case ISD::SETOGE:
7242 case ISD::SETGT:
7243 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007244 case ISD::SETUGT:
7245 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00007246 // If LHS is NaN, an ordered comparison will be false and the result will
7247 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
7248 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
7249 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
7250 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
7251 break;
7252 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
7253 // will return +0, so vmax can only be used for unsafe math or if one of
7254 // the operands is known to be nonzero.
7255 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
7256 !UnsafeFPMath &&
7257 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
7258 break;
7259 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007260 break;
7261 }
7262
7263 if (!Opcode)
7264 return SDValue();
7265 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
7266}
7267
Evan Chenge721f5c2011-07-13 00:42:17 +00007268/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
7269SDValue
7270ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
7271 SDValue Cmp = N->getOperand(4);
7272 if (Cmp.getOpcode() != ARMISD::CMPZ)
7273 // Only looking at EQ and NE cases.
7274 return SDValue();
7275
7276 EVT VT = N->getValueType(0);
7277 DebugLoc dl = N->getDebugLoc();
7278 SDValue LHS = Cmp.getOperand(0);
7279 SDValue RHS = Cmp.getOperand(1);
7280 SDValue FalseVal = N->getOperand(0);
7281 SDValue TrueVal = N->getOperand(1);
7282 SDValue ARMcc = N->getOperand(2);
Jim Grosbachb04546f2011-09-13 20:30:37 +00007283 ARMCC::CondCodes CC =
7284 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chenge721f5c2011-07-13 00:42:17 +00007285
7286 // Simplify
7287 // mov r1, r0
7288 // cmp r1, x
7289 // mov r0, y
7290 // moveq r0, x
7291 // to
7292 // cmp r0, x
7293 // movne r0, y
7294 //
7295 // mov r1, r0
7296 // cmp r1, x
7297 // mov r0, x
7298 // movne r0, y
7299 // to
7300 // cmp r0, x
7301 // movne r0, y
7302 /// FIXME: Turn this into a target neutral optimization?
7303 SDValue Res;
7304 if (CC == ARMCC::NE && FalseVal == RHS) {
7305 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
7306 N->getOperand(3), Cmp);
7307 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
7308 SDValue ARMcc;
7309 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
7310 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
7311 N->getOperand(3), NewCmp);
7312 }
7313
7314 if (Res.getNode()) {
7315 APInt KnownZero, KnownOne;
7316 APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits());
7317 DAG.ComputeMaskedBits(SDValue(N,0), Mask, KnownZero, KnownOne);
7318 // Capture demanded bits information that would be otherwise lost.
7319 if (KnownZero == 0xfffffffe)
7320 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
7321 DAG.getValueType(MVT::i1));
7322 else if (KnownZero == 0xffffff00)
7323 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
7324 DAG.getValueType(MVT::i8));
7325 else if (KnownZero == 0xffff0000)
7326 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
7327 DAG.getValueType(MVT::i16));
7328 }
7329
7330 return Res;
7331}
7332
Dan Gohman475871a2008-07-27 21:46:04 +00007333SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00007334 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00007335 switch (N->getOpcode()) {
7336 default: break;
Tanya Lattner189531f2011-06-14 23:48:48 +00007337 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007338 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007339 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007340 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00007341 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00007342 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00007343 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007344 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00007345 case ISD::STORE: return PerformSTORECombine(N, DCI);
7346 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
7347 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00007348 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007349 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00007350 case ISD::FP_TO_SINT:
7351 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
7352 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007353 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00007354 case ISD::SHL:
7355 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007356 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00007357 case ISD::SIGN_EXTEND:
7358 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007359 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
7360 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00007361 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00007362 case ARMISD::VLD2DUP:
7363 case ARMISD::VLD3DUP:
7364 case ARMISD::VLD4DUP:
7365 return CombineBaseUpdate(N, DCI);
7366 case ISD::INTRINSIC_VOID:
7367 case ISD::INTRINSIC_W_CHAIN:
7368 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
7369 case Intrinsic::arm_neon_vld1:
7370 case Intrinsic::arm_neon_vld2:
7371 case Intrinsic::arm_neon_vld3:
7372 case Intrinsic::arm_neon_vld4:
7373 case Intrinsic::arm_neon_vld2lane:
7374 case Intrinsic::arm_neon_vld3lane:
7375 case Intrinsic::arm_neon_vld4lane:
7376 case Intrinsic::arm_neon_vst1:
7377 case Intrinsic::arm_neon_vst2:
7378 case Intrinsic::arm_neon_vst3:
7379 case Intrinsic::arm_neon_vst4:
7380 case Intrinsic::arm_neon_vst2lane:
7381 case Intrinsic::arm_neon_vst3lane:
7382 case Intrinsic::arm_neon_vst4lane:
7383 return CombineBaseUpdate(N, DCI);
7384 default: break;
7385 }
7386 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00007387 }
Dan Gohman475871a2008-07-27 21:46:04 +00007388 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00007389}
7390
Evan Cheng31959b12011-02-02 01:06:55 +00007391bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
7392 EVT VT) const {
7393 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
7394}
7395
Bill Wendlingaf566342009-08-15 21:21:19 +00007396bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00007397 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00007398 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00007399
7400 switch (VT.getSimpleVT().SimpleTy) {
7401 default:
7402 return false;
7403 case MVT::i8:
7404 case MVT::i16:
7405 case MVT::i32:
7406 return true;
7407 // FIXME: VLD1 etc with standard alignment is legal.
7408 }
7409}
7410
Evan Chenge6c835f2009-08-14 20:09:37 +00007411static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
7412 if (V < 0)
7413 return false;
7414
7415 unsigned Scale = 1;
7416 switch (VT.getSimpleVT().SimpleTy) {
7417 default: return false;
7418 case MVT::i1:
7419 case MVT::i8:
7420 // Scale == 1;
7421 break;
7422 case MVT::i16:
7423 // Scale == 2;
7424 Scale = 2;
7425 break;
7426 case MVT::i32:
7427 // Scale == 4;
7428 Scale = 4;
7429 break;
7430 }
7431
7432 if ((V & (Scale - 1)) != 0)
7433 return false;
7434 V /= Scale;
7435 return V == (V & ((1LL << 5) - 1));
7436}
7437
7438static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
7439 const ARMSubtarget *Subtarget) {
7440 bool isNeg = false;
7441 if (V < 0) {
7442 isNeg = true;
7443 V = - V;
7444 }
7445
7446 switch (VT.getSimpleVT().SimpleTy) {
7447 default: return false;
7448 case MVT::i1:
7449 case MVT::i8:
7450 case MVT::i16:
7451 case MVT::i32:
7452 // + imm12 or - imm8
7453 if (isNeg)
7454 return V == (V & ((1LL << 8) - 1));
7455 return V == (V & ((1LL << 12) - 1));
7456 case MVT::f32:
7457 case MVT::f64:
7458 // Same as ARM mode. FIXME: NEON?
7459 if (!Subtarget->hasVFP2())
7460 return false;
7461 if ((V & 3) != 0)
7462 return false;
7463 V >>= 2;
7464 return V == (V & ((1LL << 8) - 1));
7465 }
7466}
7467
Evan Chengb01fad62007-03-12 23:30:29 +00007468/// isLegalAddressImmediate - Return true if the integer value can be used
7469/// as the offset of the target addressing mode for load / store of the
7470/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00007471static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00007472 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00007473 if (V == 0)
7474 return true;
7475
Evan Cheng65011532009-03-09 19:15:00 +00007476 if (!VT.isSimple())
7477 return false;
7478
Evan Chenge6c835f2009-08-14 20:09:37 +00007479 if (Subtarget->isThumb1Only())
7480 return isLegalT1AddressImmediate(V, VT);
7481 else if (Subtarget->isThumb2())
7482 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00007483
Evan Chenge6c835f2009-08-14 20:09:37 +00007484 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00007485 if (V < 0)
7486 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00007487 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00007488 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00007489 case MVT::i1:
7490 case MVT::i8:
7491 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00007492 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00007493 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007494 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00007495 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00007496 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007497 case MVT::f32:
7498 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00007499 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00007500 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00007501 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00007502 return false;
7503 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00007504 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00007505 }
Evan Chenga8e29892007-01-19 07:51:42 +00007506}
7507
Evan Chenge6c835f2009-08-14 20:09:37 +00007508bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
7509 EVT VT) const {
7510 int Scale = AM.Scale;
7511 if (Scale < 0)
7512 return false;
7513
7514 switch (VT.getSimpleVT().SimpleTy) {
7515 default: return false;
7516 case MVT::i1:
7517 case MVT::i8:
7518 case MVT::i16:
7519 case MVT::i32:
7520 if (Scale == 1)
7521 return true;
7522 // r + r << imm
7523 Scale = Scale & ~1;
7524 return Scale == 2 || Scale == 4 || Scale == 8;
7525 case MVT::i64:
7526 // r + r
7527 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
7528 return true;
7529 return false;
7530 case MVT::isVoid:
7531 // Note, we allow "void" uses (basically, uses that aren't loads or
7532 // stores), because arm allows folding a scale into many arithmetic
7533 // operations. This should be made more precise and revisited later.
7534
7535 // Allow r << imm, but the imm has to be a multiple of two.
7536 if (Scale & 1) return false;
7537 return isPowerOf2_32(Scale);
7538 }
7539}
7540
Chris Lattner37caf8c2007-04-09 23:33:39 +00007541/// isLegalAddressingMode - Return true if the addressing mode represented
7542/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00007543bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007544 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007545 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00007546 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00007547 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007548
Chris Lattner37caf8c2007-04-09 23:33:39 +00007549 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00007550 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00007551 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007552
Chris Lattner37caf8c2007-04-09 23:33:39 +00007553 switch (AM.Scale) {
7554 case 0: // no scale reg, must be "r+i" or "r", or "i".
7555 break;
7556 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00007557 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00007558 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00007559 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00007560 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00007561 // ARM doesn't support any R+R*scale+imm addr modes.
7562 if (AM.BaseOffs)
7563 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007564
Bob Wilson2c7dab12009-04-08 17:55:28 +00007565 if (!VT.isSimple())
7566 return false;
7567
Evan Chenge6c835f2009-08-14 20:09:37 +00007568 if (Subtarget->isThumb2())
7569 return isLegalT2ScaledAddressingMode(AM, VT);
7570
Chris Lattnereb13d1b2007-04-10 03:48:29 +00007571 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00007572 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00007573 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00007574 case MVT::i1:
7575 case MVT::i8:
7576 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00007577 if (Scale < 0) Scale = -Scale;
7578 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00007579 return true;
7580 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00007581 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007582 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00007583 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00007584 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00007585 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00007586 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00007587 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00007588
Owen Anderson825b72b2009-08-11 20:47:22 +00007589 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00007590 // Note, we allow "void" uses (basically, uses that aren't loads or
7591 // stores), because arm allows folding a scale into many arithmetic
7592 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00007593
Chris Lattner37caf8c2007-04-09 23:33:39 +00007594 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00007595 if (Scale & 1) return false;
7596 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00007597 }
7598 break;
Evan Chengb01fad62007-03-12 23:30:29 +00007599 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00007600 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00007601}
7602
Evan Cheng77e47512009-11-11 19:05:52 +00007603/// isLegalICmpImmediate - Return true if the specified immediate is legal
7604/// icmp immediate, that is the target has icmp instructions which can compare
7605/// a register against the immediate without having to materialize the
7606/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00007607bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00007608 if (!Subtarget->isThumb())
7609 return ARM_AM::getSOImmVal(Imm) != -1;
7610 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00007611 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00007612 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00007613}
7614
Dan Gohmancca82142011-05-03 00:46:49 +00007615/// isLegalAddImmediate - Return true if the specified immediate is legal
7616/// add immediate, that is the target has add instructions which can add
7617/// a register with the immediate without having to materialize the
7618/// immediate into a register.
7619bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
7620 return ARM_AM::getSOImmVal(Imm) != -1;
7621}
7622
Owen Andersone50ed302009-08-10 22:56:29 +00007623static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00007624 bool isSEXTLoad, SDValue &Base,
7625 SDValue &Offset, bool &isInc,
7626 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00007627 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
7628 return false;
7629
Owen Anderson825b72b2009-08-11 20:47:22 +00007630 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00007631 // AddressingMode 3
7632 Base = Ptr->getOperand(0);
7633 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007634 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00007635 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007636 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00007637 isInc = false;
7638 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7639 return true;
7640 }
7641 }
7642 isInc = (Ptr->getOpcode() == ISD::ADD);
7643 Offset = Ptr->getOperand(1);
7644 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00007645 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00007646 // AddressingMode 2
7647 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007648 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00007649 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007650 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00007651 isInc = false;
7652 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7653 Base = Ptr->getOperand(0);
7654 return true;
7655 }
7656 }
7657
7658 if (Ptr->getOpcode() == ISD::ADD) {
7659 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00007660 ARM_AM::ShiftOpc ShOpcVal=
7661 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00007662 if (ShOpcVal != ARM_AM::no_shift) {
7663 Base = Ptr->getOperand(1);
7664 Offset = Ptr->getOperand(0);
7665 } else {
7666 Base = Ptr->getOperand(0);
7667 Offset = Ptr->getOperand(1);
7668 }
7669 return true;
7670 }
7671
7672 isInc = (Ptr->getOpcode() == ISD::ADD);
7673 Base = Ptr->getOperand(0);
7674 Offset = Ptr->getOperand(1);
7675 return true;
7676 }
7677
Jim Grosbache5165492009-11-09 00:11:35 +00007678 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00007679 return false;
7680}
7681
Owen Andersone50ed302009-08-10 22:56:29 +00007682static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00007683 bool isSEXTLoad, SDValue &Base,
7684 SDValue &Offset, bool &isInc,
7685 SelectionDAG &DAG) {
7686 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
7687 return false;
7688
7689 Base = Ptr->getOperand(0);
7690 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
7691 int RHSC = (int)RHS->getZExtValue();
7692 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
7693 assert(Ptr->getOpcode() == ISD::ADD);
7694 isInc = false;
7695 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
7696 return true;
7697 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
7698 isInc = Ptr->getOpcode() == ISD::ADD;
7699 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
7700 return true;
7701 }
7702 }
7703
7704 return false;
7705}
7706
Evan Chenga8e29892007-01-19 07:51:42 +00007707/// getPreIndexedAddressParts - returns true by value, base pointer and
7708/// offset pointer and addressing mode by reference if the node's address
7709/// can be legally represented as pre-indexed load / store address.
7710bool
Dan Gohman475871a2008-07-27 21:46:04 +00007711ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
7712 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00007713 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00007714 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007715 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00007716 return false;
7717
Owen Andersone50ed302009-08-10 22:56:29 +00007718 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00007719 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00007720 bool isSEXTLoad = false;
7721 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
7722 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007723 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00007724 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
7725 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
7726 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007727 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00007728 } else
7729 return false;
7730
7731 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00007732 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00007733 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00007734 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
7735 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00007736 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00007737 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00007738 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00007739 if (!isLegal)
7740 return false;
7741
7742 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
7743 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00007744}
7745
7746/// getPostIndexedAddressParts - returns true by value, base pointer and
7747/// offset pointer and addressing mode by reference if this node can be
7748/// combined with a load / store to form a post-indexed load / store.
7749bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00007750 SDValue &Base,
7751 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00007752 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00007753 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00007754 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00007755 return false;
7756
Owen Andersone50ed302009-08-10 22:56:29 +00007757 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00007758 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00007759 bool isSEXTLoad = false;
7760 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007761 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00007762 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00007763 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
7764 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007765 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00007766 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00007767 } else
7768 return false;
7769
7770 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00007771 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00007772 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00007773 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00007774 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00007775 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00007776 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
7777 isInc, DAG);
7778 if (!isLegal)
7779 return false;
7780
Evan Cheng28dad2a2010-05-18 21:31:17 +00007781 if (Ptr != Base) {
7782 // Swap base ptr and offset to catch more post-index load / store when
7783 // it's legal. In Thumb2 mode, offset must be an immediate.
7784 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
7785 !Subtarget->isThumb2())
7786 std::swap(Base, Offset);
7787
7788 // Post-indexed load / store update the base pointer.
7789 if (Ptr != Base)
7790 return false;
7791 }
7792
Evan Chenge88d5ce2009-07-02 07:28:31 +00007793 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
7794 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00007795}
7796
Dan Gohman475871a2008-07-27 21:46:04 +00007797void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00007798 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00007799 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007800 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00007801 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00007802 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007803 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00007804 switch (Op.getOpcode()) {
7805 default: break;
7806 case ARMISD::CMOV: {
7807 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00007808 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00007809 if (KnownZero == 0 && KnownOne == 0) return;
7810
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007811 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00007812 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
7813 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00007814 KnownZero &= KnownZeroRHS;
7815 KnownOne &= KnownOneRHS;
7816 return;
7817 }
7818 }
7819}
7820
7821//===----------------------------------------------------------------------===//
7822// ARM Inline Assembly Support
7823//===----------------------------------------------------------------------===//
7824
Evan Cheng55d42002011-01-08 01:24:27 +00007825bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
7826 // Looking for "rev" which is V6+.
7827 if (!Subtarget->hasV6Ops())
7828 return false;
7829
7830 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
7831 std::string AsmStr = IA->getAsmString();
7832 SmallVector<StringRef, 4> AsmPieces;
7833 SplitString(AsmStr, AsmPieces, ";\n");
7834
7835 switch (AsmPieces.size()) {
7836 default: return false;
7837 case 1:
7838 AsmStr = AsmPieces[0];
7839 AsmPieces.clear();
7840 SplitString(AsmStr, AsmPieces, " \t,");
7841
7842 // rev $0, $1
7843 if (AsmPieces.size() == 3 &&
7844 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
7845 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007846 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00007847 if (Ty && Ty->getBitWidth() == 32)
7848 return IntrinsicLowering::LowerToByteSwap(CI);
7849 }
7850 break;
7851 }
7852
7853 return false;
7854}
7855
Evan Chenga8e29892007-01-19 07:51:42 +00007856/// getConstraintType - Given a constraint letter, return the type of
7857/// constraint it is for this target.
7858ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00007859ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
7860 if (Constraint.size() == 1) {
7861 switch (Constraint[0]) {
7862 default: break;
7863 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007864 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00007865 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00007866 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00007867 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00007868 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00007869 // An address with a single base register. Due to the way we
7870 // currently handle addresses it is the same as an 'r' memory constraint.
7871 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00007872 }
Eric Christopher1312ca82011-06-21 22:10:57 +00007873 } else if (Constraint.size() == 2) {
7874 switch (Constraint[0]) {
7875 default: break;
7876 // All 'U+' constraints are addresses.
7877 case 'U': return C_Memory;
7878 }
Evan Chenga8e29892007-01-19 07:51:42 +00007879 }
Chris Lattner4234f572007-03-25 02:14:49 +00007880 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00007881}
7882
John Thompson44ab89e2010-10-29 17:29:13 +00007883/// Examine constraint type and operand type and determine a weight value.
7884/// This object must already have been set up with the operand type
7885/// and the current alternative constraint selected.
7886TargetLowering::ConstraintWeight
7887ARMTargetLowering::getSingleConstraintMatchWeight(
7888 AsmOperandInfo &info, const char *constraint) const {
7889 ConstraintWeight weight = CW_Invalid;
7890 Value *CallOperandVal = info.CallOperandVal;
7891 // If we don't have a value, we can't do a match,
7892 // but allow it at the lowest weight.
7893 if (CallOperandVal == NULL)
7894 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007895 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00007896 // Look at the constraint type.
7897 switch (*constraint) {
7898 default:
7899 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
7900 break;
7901 case 'l':
7902 if (type->isIntegerTy()) {
7903 if (Subtarget->isThumb())
7904 weight = CW_SpecificReg;
7905 else
7906 weight = CW_Register;
7907 }
7908 break;
7909 case 'w':
7910 if (type->isFloatingPointTy())
7911 weight = CW_Register;
7912 break;
7913 }
7914 return weight;
7915}
7916
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007917typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
7918RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00007919ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00007920 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00007921 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007922 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00007923 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00007924 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00007925 if (Subtarget->isThumb())
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007926 return RCPair(0U, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +00007927 else
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007928 return RCPair(0U, ARM::GPRRegisterClass);
Eric Christopher73744df2011-06-30 23:23:01 +00007929 case 'h': // High regs or no regs.
7930 if (Subtarget->isThumb())
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007931 return RCPair(0U, ARM::hGPRRegisterClass);
Eric Christopher1070f822011-07-01 00:19:27 +00007932 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007933 case 'r':
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007934 return RCPair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007935 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00007936 if (VT == MVT::f32)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007937 return RCPair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00007938 if (VT.getSizeInBits() == 64)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007939 return RCPair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00007940 if (VT.getSizeInBits() == 128)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00007941 return RCPair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00007942 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00007943 case 'x':
7944 if (VT == MVT::f32)
7945 return RCPair(0U, ARM::SPR_8RegisterClass);
7946 if (VT.getSizeInBits() == 64)
7947 return RCPair(0U, ARM::DPR_8RegisterClass);
7948 if (VT.getSizeInBits() == 128)
7949 return RCPair(0U, ARM::QPR_8RegisterClass);
7950 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00007951 case 't':
7952 if (VT == MVT::f32)
7953 return RCPair(0U, ARM::SPRRegisterClass);
7954 break;
Evan Chenga8e29892007-01-19 07:51:42 +00007955 }
7956 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007957 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00007958 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00007959
Evan Chenga8e29892007-01-19 07:51:42 +00007960 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7961}
7962
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007963/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7964/// vector. If it is invalid, don't add anything to Ops.
7965void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00007966 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007967 std::vector<SDValue>&Ops,
7968 SelectionDAG &DAG) const {
7969 SDValue Result(0, 0);
7970
Eric Christopher100c8332011-06-02 23:16:42 +00007971 // Currently only support length 1 constraints.
7972 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00007973
Eric Christopher100c8332011-06-02 23:16:42 +00007974 char ConstraintLetter = Constraint[0];
7975 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007976 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00007977 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007978 case 'I': case 'J': case 'K': case 'L':
7979 case 'M': case 'N': case 'O':
7980 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
7981 if (!C)
7982 return;
7983
7984 int64_t CVal64 = C->getSExtValue();
7985 int CVal = (int) CVal64;
7986 // None of these constraints allow values larger than 32 bits. Check
7987 // that the value fits in an int.
7988 if (CVal != CVal64)
7989 return;
7990
Eric Christopher100c8332011-06-02 23:16:42 +00007991 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00007992 case 'j':
7993 // Constant suitable for movw, must be between 0 and
7994 // 65535.
7995 if (Subtarget->hasV6T2Ops())
7996 if (CVal >= 0 && CVal <= 65535)
7997 break;
7998 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00007999 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008000 if (Subtarget->isThumb1Only()) {
8001 // This must be a constant between 0 and 255, for ADD
8002 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008003 if (CVal >= 0 && CVal <= 255)
8004 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008005 } else if (Subtarget->isThumb2()) {
8006 // A constant that can be used as an immediate value in a
8007 // data-processing instruction.
8008 if (ARM_AM::getT2SOImmVal(CVal) != -1)
8009 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008010 } else {
8011 // A constant that can be used as an immediate value in a
8012 // data-processing instruction.
8013 if (ARM_AM::getSOImmVal(CVal) != -1)
8014 break;
8015 }
8016 return;
8017
8018 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008019 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008020 // This must be a constant between -255 and -1, for negated ADD
8021 // immediates. This can be used in GCC with an "n" modifier that
8022 // prints the negated value, for use with SUB instructions. It is
8023 // not useful otherwise but is implemented for compatibility.
8024 if (CVal >= -255 && CVal <= -1)
8025 break;
8026 } else {
8027 // This must be a constant between -4095 and 4095. It is not clear
8028 // what this constraint is intended for. Implemented for
8029 // compatibility with GCC.
8030 if (CVal >= -4095 && CVal <= 4095)
8031 break;
8032 }
8033 return;
8034
8035 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008036 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008037 // A 32-bit value where only one byte has a nonzero value. Exclude
8038 // zero to match GCC. This constraint is used by GCC internally for
8039 // constants that can be loaded with a move/shift combination.
8040 // It is not useful otherwise but is implemented for compatibility.
8041 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
8042 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008043 } else if (Subtarget->isThumb2()) {
8044 // A constant whose bitwise inverse can be used as an immediate
8045 // value in a data-processing instruction. This can be used in GCC
8046 // with a "B" modifier that prints the inverted value, for use with
8047 // BIC and MVN instructions. It is not useful otherwise but is
8048 // implemented for compatibility.
8049 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
8050 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008051 } else {
8052 // A constant whose bitwise inverse can be used as an immediate
8053 // value in a data-processing instruction. This can be used in GCC
8054 // with a "B" modifier that prints the inverted value, for use with
8055 // BIC and MVN instructions. It is not useful otherwise but is
8056 // implemented for compatibility.
8057 if (ARM_AM::getSOImmVal(~CVal) != -1)
8058 break;
8059 }
8060 return;
8061
8062 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008063 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008064 // This must be a constant between -7 and 7,
8065 // for 3-operand ADD/SUB immediate instructions.
8066 if (CVal >= -7 && CVal < 7)
8067 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008068 } else if (Subtarget->isThumb2()) {
8069 // A constant whose negation can be used as an immediate value in a
8070 // data-processing instruction. This can be used in GCC with an "n"
8071 // modifier that prints the negated value, for use with SUB
8072 // instructions. It is not useful otherwise but is implemented for
8073 // compatibility.
8074 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
8075 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008076 } else {
8077 // A constant whose negation can be used as an immediate value in a
8078 // data-processing instruction. This can be used in GCC with an "n"
8079 // modifier that prints the negated value, for use with SUB
8080 // instructions. It is not useful otherwise but is implemented for
8081 // compatibility.
8082 if (ARM_AM::getSOImmVal(-CVal) != -1)
8083 break;
8084 }
8085 return;
8086
8087 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008088 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008089 // This must be a multiple of 4 between 0 and 1020, for
8090 // ADD sp + immediate.
8091 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
8092 break;
8093 } else {
8094 // A power of two or a constant between 0 and 32. This is used in
8095 // GCC for the shift amount on shifted register operands, but it is
8096 // useful in general for any shift amounts.
8097 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
8098 break;
8099 }
8100 return;
8101
8102 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008103 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008104 // This must be a constant between 0 and 31, for shift amounts.
8105 if (CVal >= 0 && CVal <= 31)
8106 break;
8107 }
8108 return;
8109
8110 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008111 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008112 // This must be a multiple of 4 between -508 and 508, for
8113 // ADD/SUB sp = sp + immediate.
8114 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
8115 break;
8116 }
8117 return;
8118 }
8119 Result = DAG.getTargetConstant(CVal, Op.getValueType());
8120 break;
8121 }
8122
8123 if (Result.getNode()) {
8124 Ops.push_back(Result);
8125 return;
8126 }
Dale Johannesen1784d162010-06-25 21:55:36 +00008127 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008128}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00008129
8130bool
8131ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
8132 // The ARM target isn't yet aware of offsets.
8133 return false;
8134}
Evan Cheng39382422009-10-28 01:44:26 +00008135
8136int ARM::getVFPf32Imm(const APFloat &FPImm) {
8137 APInt Imm = FPImm.bitcastToAPInt();
8138 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
8139 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
8140 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
8141
8142 // We can handle 4 bits of mantissa.
8143 // mantissa = (16+UInt(e:f:g:h))/16.
8144 if (Mantissa & 0x7ffff)
8145 return -1;
8146 Mantissa >>= 19;
8147 if ((Mantissa & 0xf) != Mantissa)
8148 return -1;
8149
8150 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
8151 if (Exp < -3 || Exp > 4)
8152 return -1;
8153 Exp = ((Exp+3) & 0x7) ^ 4;
8154
8155 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
8156}
8157
8158int ARM::getVFPf64Imm(const APFloat &FPImm) {
8159 APInt Imm = FPImm.bitcastToAPInt();
8160 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
8161 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
8162 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffLL;
8163
8164 // We can handle 4 bits of mantissa.
8165 // mantissa = (16+UInt(e:f:g:h))/16.
8166 if (Mantissa & 0xffffffffffffLL)
8167 return -1;
8168 Mantissa >>= 48;
8169 if ((Mantissa & 0xf) != Mantissa)
8170 return -1;
8171
8172 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
8173 if (Exp < -3 || Exp > 4)
8174 return -1;
8175 Exp = ((Exp+3) & 0x7) ^ 4;
8176
8177 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
8178}
8179
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008180bool ARM::isBitFieldInvertedMask(unsigned v) {
8181 if (v == 0xffffffff)
8182 return 0;
8183 // there can be 1's on either or both "outsides", all the "inside"
8184 // bits must be 0's
8185 unsigned int lsb = 0, msb = 31;
8186 while (v & (1 << msb)) --msb;
8187 while (v & (1 << lsb)) ++lsb;
8188 for (unsigned int i = lsb; i <= msb; ++i) {
8189 if (v & (1 << i))
8190 return 0;
8191 }
8192 return 1;
8193}
8194
Evan Cheng39382422009-10-28 01:44:26 +00008195/// isFPImmLegal - Returns true if the target can instruction select the
8196/// specified FP immediate natively. If false, the legalizer will
8197/// materialize the FP immediate as a load from a constant pool.
8198bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
8199 if (!Subtarget->hasVFP3())
8200 return false;
8201 if (VT == MVT::f32)
8202 return ARM::getVFPf32Imm(Imm) != -1;
8203 if (VT == MVT::f64)
8204 return ARM::getVFPf64Imm(Imm) != -1;
8205 return false;
8206}
Bob Wilson65ffec42010-09-21 17:56:22 +00008207
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008208/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00008209/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
8210/// specified in the intrinsic calls.
8211bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
8212 const CallInst &I,
8213 unsigned Intrinsic) const {
8214 switch (Intrinsic) {
8215 case Intrinsic::arm_neon_vld1:
8216 case Intrinsic::arm_neon_vld2:
8217 case Intrinsic::arm_neon_vld3:
8218 case Intrinsic::arm_neon_vld4:
8219 case Intrinsic::arm_neon_vld2lane:
8220 case Intrinsic::arm_neon_vld3lane:
8221 case Intrinsic::arm_neon_vld4lane: {
8222 Info.opc = ISD::INTRINSIC_W_CHAIN;
8223 // Conservatively set memVT to the entire set of vectors loaded.
8224 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
8225 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
8226 Info.ptrVal = I.getArgOperand(0);
8227 Info.offset = 0;
8228 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
8229 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
8230 Info.vol = false; // volatile loads with NEON intrinsics not supported
8231 Info.readMem = true;
8232 Info.writeMem = false;
8233 return true;
8234 }
8235 case Intrinsic::arm_neon_vst1:
8236 case Intrinsic::arm_neon_vst2:
8237 case Intrinsic::arm_neon_vst3:
8238 case Intrinsic::arm_neon_vst4:
8239 case Intrinsic::arm_neon_vst2lane:
8240 case Intrinsic::arm_neon_vst3lane:
8241 case Intrinsic::arm_neon_vst4lane: {
8242 Info.opc = ISD::INTRINSIC_VOID;
8243 // Conservatively set memVT to the entire set of vectors stored.
8244 unsigned NumElts = 0;
8245 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008246 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +00008247 if (!ArgTy->isVectorTy())
8248 break;
8249 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
8250 }
8251 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
8252 Info.ptrVal = I.getArgOperand(0);
8253 Info.offset = 0;
8254 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
8255 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
8256 Info.vol = false; // volatile stores with NEON intrinsics not supported
8257 Info.readMem = false;
8258 Info.writeMem = true;
8259 return true;
8260 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00008261 case Intrinsic::arm_strexd: {
8262 Info.opc = ISD::INTRINSIC_W_CHAIN;
8263 Info.memVT = MVT::i64;
8264 Info.ptrVal = I.getArgOperand(2);
8265 Info.offset = 0;
8266 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00008267 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00008268 Info.readMem = false;
8269 Info.writeMem = true;
8270 return true;
8271 }
8272 case Intrinsic::arm_ldrexd: {
8273 Info.opc = ISD::INTRINSIC_W_CHAIN;
8274 Info.memVT = MVT::i64;
8275 Info.ptrVal = I.getArgOperand(0);
8276 Info.offset = 0;
8277 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00008278 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00008279 Info.readMem = true;
8280 Info.writeMem = false;
8281 return true;
8282 }
Bob Wilson65ffec42010-09-21 17:56:22 +00008283 default:
8284 break;
8285 }
8286
8287 return false;
8288}