blob: e9f70d5aceaaab3c9a9eff4d51557554a57e222d [file] [log] [blame]
Evan Chenga8e29892007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chenga8e29892007-01-19 07:51:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that ARM uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Dale Johannesen51e28e62010-06-03 21:09:53 +000015#define DEBUG_TYPE "arm-isel"
Evan Chenga8e29892007-01-19 07:51:42 +000016#include "ARM.h"
Eric Christopher6f2ccef2010-09-10 22:42:06 +000017#include "ARMCallingConv.h"
Evan Chenga8e29892007-01-19 07:51:42 +000018#include "ARMConstantPoolValue.h"
19#include "ARMISelLowering.h"
20#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +000021#include "ARMPerfectShuffle.h"
Evan Chenga8e29892007-01-19 07:51:42 +000022#include "ARMRegisterInfo.h"
23#include "ARMSubtarget.h"
24#include "ARMTargetMachine.h"
Chris Lattner80ec2792009-08-02 00:34:36 +000025#include "ARMTargetObjectFile.h"
Evan Chengee04a6d2011-07-20 23:34:39 +000026#include "MCTargetDesc/ARMAddressingModes.h"
Evan Chenga8e29892007-01-19 07:51:42 +000027#include "llvm/CallingConv.h"
28#include "llvm/Constants.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000029#include "llvm/Function.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000030#include "llvm/GlobalValue.h"
Evan Cheng27707472007-03-16 08:43:56 +000031#include "llvm/Instruction.h"
Bob Wilson65ffec42010-09-21 17:56:22 +000032#include "llvm/Instructions.h"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000033#include "llvm/Intrinsics.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000034#include "llvm/Type.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000035#include "llvm/CodeGen/CallingConvLower.h"
Evan Cheng55d42002011-01-08 01:24:27 +000036#include "llvm/CodeGen/IntrinsicLowering.h"
Evan Chenga8e29892007-01-19 07:51:42 +000037#include "llvm/CodeGen/MachineBasicBlock.h"
38#include "llvm/CodeGen/MachineFrameInfo.h"
39#include "llvm/CodeGen/MachineFunction.h"
40#include "llvm/CodeGen/MachineInstrBuilder.h"
Bill Wendling2a850152011-10-05 00:02:33 +000041#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000042#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chenga8e29892007-01-19 07:51:42 +000043#include "llvm/CodeGen/SelectionDAG.h"
Bill Wendling94a1c632010-03-09 02:46:12 +000044#include "llvm/MC/MCSectionMachO.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000045#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000046#include "llvm/ADT/VectorExtras.h"
Evan Cheng55d42002011-01-08 01:24:27 +000047#include "llvm/ADT/StringExtras.h"
Dale Johannesen51e28e62010-06-03 21:09:53 +000048#include "llvm/ADT/Statistic.h"
Jim Grosbache7b52522010-04-14 22:28:31 +000049#include "llvm/Support/CommandLine.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000050#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000051#include "llvm/Support/MathExtras.h"
Jim Grosbache801dc42009-12-12 01:40:06 +000052#include "llvm/Support/raw_ostream.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000053#include <sstream>
Evan Chenga8e29892007-01-19 07:51:42 +000054using namespace llvm;
55
Dale Johannesen51e28e62010-06-03 21:09:53 +000056STATISTIC(NumTailCalls, "Number of tail calls");
Evan Chengfc8475b2011-01-19 02:16:49 +000057STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Dale Johannesen51e28e62010-06-03 21:09:53 +000058
Bob Wilson703af3a2010-08-13 22:43:33 +000059// This option should go away when tail calls fully work.
60static cl::opt<bool>
61EnableARMTailCalls("arm-tail-calls", cl::Hidden,
62 cl::desc("Generate tail calls (TEMPORARY OPTION)."),
63 cl::init(false));
64
Eric Christopher836c6242010-12-15 23:47:29 +000065cl::opt<bool>
Jim Grosbache7b52522010-04-14 22:28:31 +000066EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng515fe3a2010-07-08 02:08:50 +000067 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbache7b52522010-04-14 22:28:31 +000068 cl::init(false));
69
Evan Cheng46df4eb2010-06-16 07:35:02 +000070static cl::opt<bool>
71ARMInterworking("arm-interworking", cl::Hidden,
72 cl::desc("Enable / disable ARM interworking (for debugging only)"),
73 cl::init(true));
74
Benjamin Kramer0861f572011-11-26 23:01:57 +000075namespace {
Cameron Zwaricha86686e2011-06-10 20:59:24 +000076 class ARMCCState : public CCState {
77 public:
78 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
79 const TargetMachine &TM, SmallVector<CCValAssign, 16> &locs,
80 LLVMContext &C, ParmContext PC)
81 : CCState(CC, isVarArg, MF, TM, locs, C) {
82 assert(((PC == Call) || (PC == Prologue)) &&
83 "ARMCCState users must specify whether their context is call"
84 "or prologue generation.");
85 CallOrPrologue = PC;
86 }
87 };
88}
89
Stuart Hastingsc7315872011-04-20 16:47:52 +000090// The APCS parameter registers.
91static const unsigned GPRArgRegs[] = {
92 ARM::R0, ARM::R1, ARM::R2, ARM::R3
93};
94
Owen Andersone50ed302009-08-10 22:56:29 +000095void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
96 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000097 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000098 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000099 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
100 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000101
Owen Anderson70671842009-08-10 20:18:46 +0000102 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000103 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000104 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000105 }
106
Owen Andersone50ed302009-08-10 22:56:29 +0000107 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000108 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Duncan Sands28b77e92011-09-06 19:07:46 +0000109 setOperationAction(ISD::SETCC, VT.getSimpleVT(), Custom);
Eli Friedman5c89cb82011-10-24 23:08:52 +0000110 setOperationAction(ISD::INSERT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson3468c2e2010-11-03 16:24:50 +0000111 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Eli Friedman14e809c2011-11-09 23:36:02 +0000112 if (ElemTy == MVT::i32) {
113 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Custom);
114 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Custom);
115 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Custom);
116 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Custom);
117 } else {
Bob Wilson0696fdf2009-09-16 20:20:44 +0000118 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
119 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
120 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
121 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
122 }
Owen Anderson70671842009-08-10 20:18:46 +0000123 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
124 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Bob Wilson07f6e802010-06-16 21:34:01 +0000125 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
Bob Wilson5e8b8332011-01-07 04:59:04 +0000126 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Legal);
Bob Wilsond0910c42010-04-06 22:02:24 +0000127 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
128 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Eli Friedman15f58c52011-11-11 03:16:38 +0000129 setOperationAction(ISD::SIGN_EXTEND_INREG, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000130 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000131 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
132 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
133 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +0000134 }
135
136 // Promote all bit-wise operations.
137 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000138 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000139 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
140 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000141 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000142 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000143 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000144 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000145 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000146 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000147 }
Bob Wilson16330762009-09-16 00:17:28 +0000148
149 // Neon does not support vector divide/remainder operations.
150 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
151 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
152 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
153 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
154 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
155 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000156}
157
Owen Andersone50ed302009-08-10 22:56:29 +0000158void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000159 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000160 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000161}
162
Owen Andersone50ed302009-08-10 22:56:29 +0000163void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000164 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000165 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000166}
167
Chris Lattnerf0144122009-07-28 03:13:23 +0000168static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
169 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000170 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000171
Chris Lattner80ec2792009-08-02 00:34:36 +0000172 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000173}
174
Evan Chenga8e29892007-01-19 07:51:42 +0000175ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000176 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000177 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng31446872010-07-23 22:39:59 +0000178 RegInfo = TM.getRegisterInfo();
Evan Cheng3ef1c872010-09-10 01:29:16 +0000179 Itins = TM.getInstrItineraryData();
Evan Chenga8e29892007-01-19 07:51:42 +0000180
Duncan Sands28b77e92011-09-06 19:07:46 +0000181 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
182
Evan Chengb1df8f22007-04-27 08:15:43 +0000183 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000184 // Uses VFP for Thumb libfuncs if available.
185 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
186 // Single-precision floating-point arithmetic.
187 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
188 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
189 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
190 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000191
Evan Chengb1df8f22007-04-27 08:15:43 +0000192 // Double-precision floating-point arithmetic.
193 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
194 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
195 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
196 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000197
Evan Chengb1df8f22007-04-27 08:15:43 +0000198 // Single-precision comparisons.
199 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
200 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
201 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
202 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
203 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
204 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
205 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
206 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000207
Evan Chengb1df8f22007-04-27 08:15:43 +0000208 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
209 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
210 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
211 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
212 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
213 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
214 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
215 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000216
Evan Chengb1df8f22007-04-27 08:15:43 +0000217 // Double-precision comparisons.
218 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
219 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
220 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
221 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
222 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
223 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
224 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
225 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000226
Evan Chengb1df8f22007-04-27 08:15:43 +0000227 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
228 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
229 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
230 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
231 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
232 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
233 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
234 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000235
Evan Chengb1df8f22007-04-27 08:15:43 +0000236 // Floating-point to integer conversions.
237 // i64 conversions are done via library routines even when generating VFP
238 // instructions, so use the same ones.
239 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
240 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
241 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
242 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000243
Evan Chengb1df8f22007-04-27 08:15:43 +0000244 // Conversions between floating types.
245 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
246 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
247
248 // Integer to floating-point conversions.
249 // i64 conversions are done via library routines even when generating VFP
250 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000251 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
252 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000253 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
254 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
255 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
256 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
257 }
Evan Chenga8e29892007-01-19 07:51:42 +0000258 }
259
Bob Wilson2f954612009-05-22 17:38:41 +0000260 // These libcalls are not available in 32-bit.
261 setLibcallName(RTLIB::SHL_I128, 0);
262 setLibcallName(RTLIB::SRL_I128, 0);
263 setLibcallName(RTLIB::SRA_I128, 0);
264
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000265 if (Subtarget->isAAPCS_ABI()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000266 // Double-precision floating-point arithmetic helper functions
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000267 // RTABI chapter 4.1.2, Table 2
268 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd");
269 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv");
270 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul");
271 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub");
272 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS);
273 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS);
274 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS);
275 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS);
276
277 // Double-precision floating-point comparison helper functions
278 // RTABI chapter 4.1.2, Table 3
279 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq");
280 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
281 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq");
282 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ);
283 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt");
284 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
285 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple");
286 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
287 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge");
288 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
289 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt");
290 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
291 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun");
292 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
293 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun");
294 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
295 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS);
296 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS);
297 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS);
298 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS);
299 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS);
300 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS);
301 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS);
302 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS);
303
304 // Single-precision floating-point arithmetic helper functions
305 // RTABI chapter 4.1.2, Table 4
306 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd");
307 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv");
308 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul");
309 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub");
310 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS);
311 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS);
312 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS);
313 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS);
314
315 // Single-precision floating-point comparison helper functions
316 // RTABI chapter 4.1.2, Table 5
317 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq");
318 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
319 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq");
320 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ);
321 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt");
322 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
323 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple");
324 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
325 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge");
326 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
327 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt");
328 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
329 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun");
330 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
331 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun");
332 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
333 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS);
334 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS);
335 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS);
336 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS);
337 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS);
338 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS);
339 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS);
340 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS);
341
342 // Floating-point to integer conversions.
343 // RTABI chapter 4.1.2, Table 6
344 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz");
345 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz");
346 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz");
347 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz");
348 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz");
349 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz");
350 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz");
351 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz");
352 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS);
353 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS);
354 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS);
355 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS);
356 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS);
357 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS);
358 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS);
359 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS);
360
361 // Conversions between floating types.
362 // RTABI chapter 4.1.2, Table 7
363 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f");
364 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d");
365 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000366 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000367
368 // Integer to floating-point conversions.
369 // RTABI chapter 4.1.2, Table 8
370 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d");
371 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d");
372 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d");
373 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d");
374 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f");
375 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f");
376 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f");
377 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f");
378 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
379 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
380 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
381 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
382 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
383 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
384 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
385 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
386
387 // Long long helper functions
388 // RTABI chapter 4.2, Table 9
389 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul");
390 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
391 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
392 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
393 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
394 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
395 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
396 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
397 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
398 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
399 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
400 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
401
402 // Integer division functions
403 // RTABI chapter 4.3.1
404 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
405 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
406 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
407 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
408 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
409 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
410 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
411 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
412 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
413 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS);
414 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000415 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS);
Renato Golin1ec11fb2011-05-22 21:41:23 +0000416
417 // Memory operations
418 // RTABI chapter 4.3.4
419 setLibcallName(RTLIB::MEMCPY, "__aeabi_memcpy");
420 setLibcallName(RTLIB::MEMMOVE, "__aeabi_memmove");
421 setLibcallName(RTLIB::MEMSET, "__aeabi_memset");
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000422 }
423
Bob Wilson2fef4572011-10-07 16:59:21 +0000424 // Use divmod compiler-rt calls for iOS 5.0 and later.
425 if (Subtarget->getTargetTriple().getOS() == Triple::IOS &&
426 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
427 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
428 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
429 }
430
David Goodwinf1daf7d2009-07-08 23:10:31 +0000431 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000432 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000433 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000434 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000435 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
436 !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000437 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000438 if (!Subtarget->isFPOnlySP())
439 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000440
Owen Anderson825b72b2009-08-11 20:47:22 +0000441 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000442 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000443
Eli Friedman9f1f26a2011-11-08 01:43:53 +0000444 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
445 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
446 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
447 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
448 setTruncStoreAction((MVT::SimpleValueType)VT,
449 (MVT::SimpleValueType)InnerVT, Expand);
450 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
451 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
452 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
453 }
454
Bob Wilson5bafff32009-06-22 23:27:02 +0000455 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000456 addDRTypeForNEON(MVT::v2f32);
457 addDRTypeForNEON(MVT::v8i8);
458 addDRTypeForNEON(MVT::v4i16);
459 addDRTypeForNEON(MVT::v2i32);
460 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000461
Owen Anderson825b72b2009-08-11 20:47:22 +0000462 addQRTypeForNEON(MVT::v4f32);
463 addQRTypeForNEON(MVT::v2f64);
464 addQRTypeForNEON(MVT::v16i8);
465 addQRTypeForNEON(MVT::v8i16);
466 addQRTypeForNEON(MVT::v4i32);
467 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000468
Bob Wilson74dc72e2009-09-15 23:55:57 +0000469 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
470 // neither Neon nor VFP support any arithmetic operations on it.
471 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
472 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
473 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
474 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
475 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
476 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000477 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Bob Wilson74dc72e2009-09-15 23:55:57 +0000478 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
479 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
480 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
481 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
482 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
483 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
484 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
485 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
486 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
487 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
488 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
489 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
490 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
491 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
492 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
493 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
494 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
495
Bob Wilson642b3292009-09-16 00:32:15 +0000496 // Neon does not support some operations on v1i64 and v2i64 types.
497 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000498 // Custom handling for some quad-vector types to detect VMULL.
499 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
500 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
501 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begeman7973f352011-02-11 20:53:29 +0000502 // Custom handling for some vector types to avoid expensive expansions
503 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
504 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
505 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
506 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000507 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
508 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000509 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
510 // a destination type that is wider than the source.
511 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
512 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000513
Bob Wilson1c3ef902011-02-07 17:43:21 +0000514 setTargetDAGCombine(ISD::INTRINSIC_VOID);
515 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000516 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
517 setTargetDAGCombine(ISD::SHL);
518 setTargetDAGCombine(ISD::SRL);
519 setTargetDAGCombine(ISD::SRA);
520 setTargetDAGCombine(ISD::SIGN_EXTEND);
521 setTargetDAGCombine(ISD::ZERO_EXTEND);
522 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000523 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000524 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000525 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000526 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
527 setTargetDAGCombine(ISD::STORE);
Chad Rosieref01edf2011-06-24 19:23:04 +0000528 setTargetDAGCombine(ISD::FP_TO_SINT);
529 setTargetDAGCombine(ISD::FP_TO_UINT);
530 setTargetDAGCombine(ISD::FDIV);
Nadav Rotem004a24b2011-10-15 20:03:12 +0000531
532 setLoadExtAction(ISD::EXTLOAD, MVT::v4i8, Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000533 }
534
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000535 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000536
537 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000538 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000539
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000540 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000542
Evan Chenga8e29892007-01-19 07:51:42 +0000543 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000544 if (!Subtarget->isThumb1Only()) {
545 for (unsigned im = (unsigned)ISD::PRE_INC;
546 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000547 setIndexedLoadAction(im, MVT::i1, Legal);
548 setIndexedLoadAction(im, MVT::i8, Legal);
549 setIndexedLoadAction(im, MVT::i16, Legal);
550 setIndexedLoadAction(im, MVT::i32, Legal);
551 setIndexedStoreAction(im, MVT::i1, Legal);
552 setIndexedStoreAction(im, MVT::i8, Legal);
553 setIndexedStoreAction(im, MVT::i16, Legal);
554 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000555 }
Evan Chenga8e29892007-01-19 07:51:42 +0000556 }
557
558 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000559 setOperationAction(ISD::MUL, MVT::i64, Expand);
560 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000561 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000562 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
563 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000564 }
Jim Grosbacha7603982011-07-01 21:12:19 +0000565 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
566 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopher2cc40132011-04-19 18:49:19 +0000567 setOperationAction(ISD::MULHS, MVT::i32, Expand);
568
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000569 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000570 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000571 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000572 setOperationAction(ISD::SRL, MVT::i64, Custom);
573 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000574
Evan Cheng342e3162011-08-30 01:34:54 +0000575 if (!Subtarget->isThumb1Only()) {
576 // FIXME: We should do this for Thumb1 as well.
577 setOperationAction(ISD::ADDC, MVT::i32, Custom);
578 setOperationAction(ISD::ADDE, MVT::i32, Custom);
579 setOperationAction(ISD::SUBC, MVT::i32, Custom);
580 setOperationAction(ISD::SUBE, MVT::i32, Custom);
581 }
582
Evan Chenga8e29892007-01-19 07:51:42 +0000583 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000584 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000585 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000586 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000587 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000588 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000589
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000590 // Only ARMv6 has BSWAP.
591 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000592 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000593
Evan Chenga8e29892007-01-19 07:51:42 +0000594 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000595 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000596 // v7M has a hardware divider
597 setOperationAction(ISD::SDIV, MVT::i32, Expand);
598 setOperationAction(ISD::UDIV, MVT::i32, Expand);
599 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000600 setOperationAction(ISD::SREM, MVT::i32, Expand);
601 setOperationAction(ISD::UREM, MVT::i32, Expand);
602 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
603 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000604
Owen Anderson825b72b2009-08-11 20:47:22 +0000605 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
606 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
607 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
608 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000609 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000610
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000611 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000612
Evan Chenga8e29892007-01-19 07:51:42 +0000613 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000614 setOperationAction(ISD::VASTART, MVT::Other, Custom);
615 setOperationAction(ISD::VAARG, MVT::Other, Expand);
616 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
617 setOperationAction(ISD::VAEND, MVT::Other, Expand);
618 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
619 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000620 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000621 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
622 setExceptionPointerRegister(ARM::R0);
623 setExceptionSelectorRegister(ARM::R1);
624
Evan Cheng3a1588a2010-04-15 22:20:34 +0000625 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000626 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
627 // the default expansion.
Eli Friedman4db5aca2011-08-29 18:23:02 +0000628 // FIXME: This should be checking for v6k, not just v6.
Evan Cheng11db0682010-08-11 06:22:01 +0000629 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000630 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000631 // membarrier needs custom lowering; the rest are legal and handled
632 // normally.
633 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000634 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Eli Friedman2bdffe42011-08-31 00:31:29 +0000635 // Custom lowering for 64-bit ops
636 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
637 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
638 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
639 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
640 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
641 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Eli Friedman4d3f3292011-08-31 17:52:22 +0000642 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Eli Friedman26689ac2011-08-03 21:06:02 +0000643 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
644 setInsertFencesForAtomic(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000645 } else {
646 // Set them all for expansion, which will force libcalls.
647 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000648 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000649 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000650 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000651 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000652 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000653 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000654 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000655 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000656 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000657 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000658 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000659 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000660 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedman7cc15662011-09-15 22:18:49 +0000661 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
662 // Unordered/Monotonic case.
663 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
664 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000665 // Since the libcalls include locking, fold in the fences
666 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000667 }
Evan Chenga8e29892007-01-19 07:51:42 +0000668
Evan Cheng416941d2010-11-04 05:19:35 +0000669 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000670
Eli Friedmana2c6f452010-06-26 04:36:50 +0000671 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
672 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000673 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
674 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000675 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000676 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000677
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000678 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
679 !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000680 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
681 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000682 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000683 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
684 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000685
686 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000687 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000688 if (Subtarget->isTargetDarwin()) {
689 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
690 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000691 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000692 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000693
Owen Anderson825b72b2009-08-11 20:47:22 +0000694 setOperationAction(ISD::SETCC, MVT::i32, Expand);
695 setOperationAction(ISD::SETCC, MVT::f32, Expand);
696 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000697 setOperationAction(ISD::SELECT, MVT::i32, Custom);
698 setOperationAction(ISD::SELECT, MVT::f32, Custom);
699 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000700 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
701 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
702 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000703
Owen Anderson825b72b2009-08-11 20:47:22 +0000704 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
705 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
706 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
707 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
708 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000709
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000710 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000711 setOperationAction(ISD::FSIN, MVT::f64, Expand);
712 setOperationAction(ISD::FSIN, MVT::f32, Expand);
713 setOperationAction(ISD::FCOS, MVT::f32, Expand);
714 setOperationAction(ISD::FCOS, MVT::f64, Expand);
715 setOperationAction(ISD::FREM, MVT::f64, Expand);
716 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000717 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
718 !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000719 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
720 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000721 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000722 setOperationAction(ISD::FPOW, MVT::f64, Expand);
723 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000724
Cameron Zwarich33390842011-07-08 21:39:21 +0000725 setOperationAction(ISD::FMA, MVT::f64, Expand);
726 setOperationAction(ISD::FMA, MVT::f32, Expand);
727
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000728 // Various VFP goodness
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000729 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000730 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
731 if (Subtarget->hasVFP2()) {
732 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
733 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
734 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
735 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
736 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000737 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000738 if (!Subtarget->hasFP16()) {
739 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
740 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000741 }
Evan Cheng110cf482008-04-01 01:50:16 +0000742 }
Evan Chenga8e29892007-01-19 07:51:42 +0000743
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000744 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000745 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000746 setTargetDAGCombine(ISD::ADD);
747 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000748 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000749
Owen Anderson080c0922010-11-05 19:27:46 +0000750 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON())
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000751 setTargetDAGCombine(ISD::OR);
Owen Anderson080c0922010-11-05 19:27:46 +0000752 if (Subtarget->hasNEON())
753 setTargetDAGCombine(ISD::AND);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000754
Evan Chenga8e29892007-01-19 07:51:42 +0000755 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000756
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000757 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
758 !Subtarget->hasVFP2())
Evan Chengf7d87ee2010-05-21 00:43:17 +0000759 setSchedulingPreference(Sched::RegPressure);
760 else
761 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000762
Evan Cheng05219282011-01-06 06:52:41 +0000763 //// temporary - rewrite interface to use type
764 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Lang Hames75757f92011-10-26 20:56:52 +0000765 maxStoresPerMemset = 16;
766 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengf6799392010-06-26 01:52:05 +0000767
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000768 // On ARM arguments smaller than 4 bytes are extended, so all arguments
769 // are at least 4 bytes aligned.
770 setMinStackArgumentAlignment(4);
771
Evan Chengfff606d2010-09-24 19:07:23 +0000772 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000773
774 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000775}
776
Andrew Trick32cec0a2011-01-19 02:35:27 +0000777// FIXME: It might make sense to define the representative register class as the
778// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
779// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
780// SPR's representative would be DPR_VFP2. This should work well if register
781// pressure tracking were modified such that a register use would increment the
782// pressure of the register class's representative and all of it's super
783// classes' representatives transitively. We have not implemented this because
784// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000785// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000786// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000787std::pair<const TargetRegisterClass*, uint8_t>
788ARMTargetLowering::findRepresentativeClass(EVT VT) const{
789 const TargetRegisterClass *RRC = 0;
790 uint8_t Cost = 1;
791 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000792 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000793 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000794 // Use DPR as representative register class for all floating point
795 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
796 // the cost is 1 for both f32 and f64.
797 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000798 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000799 RRC = ARM::DPRRegisterClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000800 // When NEON is used for SP, only half of the register file is available
801 // because operations that define both SP and DP results will be constrained
802 // to the VFP2 class (D0-D15). We currently model this constraint prior to
803 // coalescing by double-counting the SP regs. See the FIXME above.
804 if (Subtarget->useNEONForSinglePrecisionFP())
805 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000806 break;
807 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
808 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000809 RRC = ARM::DPRRegisterClass;
810 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000811 break;
812 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000813 RRC = ARM::DPRRegisterClass;
814 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000815 break;
816 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000817 RRC = ARM::DPRRegisterClass;
818 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000819 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000820 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000821 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000822}
823
Evan Chenga8e29892007-01-19 07:51:42 +0000824const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
825 switch (Opcode) {
826 default: return 0;
827 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000828 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000829 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000830 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
831 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000832 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000833 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
834 case ARMISD::tCALL: return "ARMISD::tCALL";
835 case ARMISD::BRCOND: return "ARMISD::BRCOND";
836 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000837 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000838 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
839 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
840 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000841 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000842 case ARMISD::CMPFP: return "ARMISD::CMPFP";
843 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000844 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000845 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
846 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000847
Jim Grosbach3482c802010-01-18 19:58:49 +0000848 case ARMISD::RBIT: return "ARMISD::RBIT";
849
Bob Wilson76a312b2010-03-19 22:51:32 +0000850 case ARMISD::FTOSI: return "ARMISD::FTOSI";
851 case ARMISD::FTOUI: return "ARMISD::FTOUI";
852 case ARMISD::SITOF: return "ARMISD::SITOF";
853 case ARMISD::UITOF: return "ARMISD::UITOF";
854
Evan Chenga8e29892007-01-19 07:51:42 +0000855 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
856 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
857 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000858
Evan Cheng342e3162011-08-30 01:34:54 +0000859 case ARMISD::ADDC: return "ARMISD::ADDC";
860 case ARMISD::ADDE: return "ARMISD::ADDE";
861 case ARMISD::SUBC: return "ARMISD::SUBC";
862 case ARMISD::SUBE: return "ARMISD::SUBE";
863
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000864 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
865 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000866
Evan Chengc5942082009-10-28 06:55:03 +0000867 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
868 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
869
Dale Johannesen51e28e62010-06-03 21:09:53 +0000870 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000871
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000872 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000873
Evan Cheng86198642009-08-07 00:34:42 +0000874 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
875
Jim Grosbach3728e962009-12-10 00:11:09 +0000876 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000877 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000878
Evan Chengdfed19f2010-11-03 06:34:55 +0000879 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
880
Bob Wilson5bafff32009-06-22 23:27:02 +0000881 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000882 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000883 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000884 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
885 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000886 case ARMISD::VCGEU: return "ARMISD::VCGEU";
887 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000888 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
889 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000890 case ARMISD::VCGTU: return "ARMISD::VCGTU";
891 case ARMISD::VTST: return "ARMISD::VTST";
892
893 case ARMISD::VSHL: return "ARMISD::VSHL";
894 case ARMISD::VSHRs: return "ARMISD::VSHRs";
895 case ARMISD::VSHRu: return "ARMISD::VSHRu";
896 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
897 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
898 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
899 case ARMISD::VSHRN: return "ARMISD::VSHRN";
900 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
901 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
902 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
903 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
904 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
905 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
906 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
907 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
908 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
909 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
910 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
911 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
912 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
913 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000914 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000915 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Chengeaa192a2011-11-15 02:12:34 +0000916 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000917 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000918 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000919 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000920 case ARMISD::VREV64: return "ARMISD::VREV64";
921 case ARMISD::VREV32: return "ARMISD::VREV32";
922 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000923 case ARMISD::VZIP: return "ARMISD::VZIP";
924 case ARMISD::VUZP: return "ARMISD::VUZP";
925 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000926 case ARMISD::VTBL1: return "ARMISD::VTBL1";
927 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000928 case ARMISD::VMULLs: return "ARMISD::VMULLs";
929 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000930 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000931 case ARMISD::FMAX: return "ARMISD::FMAX";
932 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000933 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000934 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
935 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000936 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000937 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
938 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
939 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +0000940 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
941 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
942 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
943 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
944 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
945 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
946 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
947 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
948 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
949 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
950 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
951 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
952 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
953 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
954 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
955 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
956 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +0000957 }
958}
959
Duncan Sands28b77e92011-09-06 19:07:46 +0000960EVT ARMTargetLowering::getSetCCResultType(EVT VT) const {
961 if (!VT.isVector()) return getPointerTy();
962 return VT.changeVectorElementTypeToInteger();
963}
964
Evan Cheng06b666c2010-05-15 02:18:07 +0000965/// getRegClassFor - Return the register class that should be used for the
966/// specified value type.
967TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
968 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
969 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
970 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000971 if (Subtarget->hasNEON()) {
972 if (VT == MVT::v4i64)
973 return ARM::QQPRRegisterClass;
974 else if (VT == MVT::v8i64)
975 return ARM::QQQQPRRegisterClass;
976 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000977 return TargetLowering::getRegClassFor(VT);
978}
979
Eric Christopherab695882010-07-21 22:26:11 +0000980// Create a fast isel object.
981FastISel *
982ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
983 return ARM::createFastISel(funcInfo);
984}
985
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000986/// getMaximalGlobalOffset - Returns the maximal possible offset which can
987/// be used for loads / stores from the global.
988unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
989 return (Subtarget->isThumb1Only() ? 127 : 4095);
990}
991
Evan Cheng1cc39842010-05-20 23:26:43 +0000992Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000993 unsigned NumVals = N->getNumValues();
994 if (!NumVals)
995 return Sched::RegPressure;
996
997 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000998 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000999 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +00001000 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +00001001 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman692c1d82011-10-24 17:55:11 +00001002 return Sched::ILP;
Evan Cheng1cc39842010-05-20 23:26:43 +00001003 }
Evan Chengc10f5432010-05-28 23:25:23 +00001004
1005 if (!N->isMachineOpcode())
1006 return Sched::RegPressure;
1007
1008 // Load are scheduled for latency even if there instruction itinerary
1009 // is not available.
1010 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +00001011 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +00001012
Evan Chenge837dea2011-06-28 19:10:37 +00001013 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +00001014 return Sched::RegPressure;
1015 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +00001016 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman692c1d82011-10-24 17:55:11 +00001017 return Sched::ILP;
Evan Chengc10f5432010-05-28 23:25:23 +00001018
Evan Cheng1cc39842010-05-20 23:26:43 +00001019 return Sched::RegPressure;
1020}
1021
Evan Chenga8e29892007-01-19 07:51:42 +00001022//===----------------------------------------------------------------------===//
1023// Lowering Code
1024//===----------------------------------------------------------------------===//
1025
Evan Chenga8e29892007-01-19 07:51:42 +00001026/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1027static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1028 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001029 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +00001030 case ISD::SETNE: return ARMCC::NE;
1031 case ISD::SETEQ: return ARMCC::EQ;
1032 case ISD::SETGT: return ARMCC::GT;
1033 case ISD::SETGE: return ARMCC::GE;
1034 case ISD::SETLT: return ARMCC::LT;
1035 case ISD::SETLE: return ARMCC::LE;
1036 case ISD::SETUGT: return ARMCC::HI;
1037 case ISD::SETUGE: return ARMCC::HS;
1038 case ISD::SETULT: return ARMCC::LO;
1039 case ISD::SETULE: return ARMCC::LS;
1040 }
1041}
1042
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001043/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1044static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001045 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001046 CondCode2 = ARMCC::AL;
1047 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001048 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001049 case ISD::SETEQ:
1050 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1051 case ISD::SETGT:
1052 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1053 case ISD::SETGE:
1054 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1055 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001056 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001057 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1058 case ISD::SETO: CondCode = ARMCC::VC; break;
1059 case ISD::SETUO: CondCode = ARMCC::VS; break;
1060 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1061 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1062 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1063 case ISD::SETLT:
1064 case ISD::SETULT: CondCode = ARMCC::LT; break;
1065 case ISD::SETLE:
1066 case ISD::SETULE: CondCode = ARMCC::LE; break;
1067 case ISD::SETNE:
1068 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1069 }
Evan Chenga8e29892007-01-19 07:51:42 +00001070}
1071
Bob Wilson1f595bb2009-04-17 19:07:39 +00001072//===----------------------------------------------------------------------===//
1073// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001074//===----------------------------------------------------------------------===//
1075
1076#include "ARMGenCallingConv.inc"
1077
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001078/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1079/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001080CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001081 bool Return,
1082 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001083 switch (CC) {
1084 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001085 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001086 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001087 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001088 if (!Subtarget->isAAPCS_ABI())
1089 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1090 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1091 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1092 }
1093 // Fallthrough
1094 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001095 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001096 if (!Subtarget->isAAPCS_ABI())
1097 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1098 else if (Subtarget->hasVFP2() &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001099 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1100 !isVarArg)
Evan Cheng76f920d2010-10-22 18:23:05 +00001101 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1102 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1103 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001104 case CallingConv::ARM_AAPCS_VFP:
Evan Cheng76f920d2010-10-22 18:23:05 +00001105 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001106 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001107 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001108 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001109 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001110 }
1111}
1112
Dan Gohman98ca4f22009-08-05 01:29:28 +00001113/// LowerCallResult - Lower the result values of a call into the
1114/// appropriate copies out of appropriate physical registers.
1115SDValue
1116ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001117 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001118 const SmallVectorImpl<ISD::InputArg> &Ins,
1119 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001120 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001121
Bob Wilson1f595bb2009-04-17 19:07:39 +00001122 // Assign locations to each value returned by this call.
1123 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001124 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1125 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001126 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001127 CCAssignFnForNode(CallConv, /* Return*/ true,
1128 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001129
1130 // Copy all of the result registers out of their specified physreg.
1131 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1132 CCValAssign VA = RVLocs[i];
1133
Bob Wilson80915242009-04-25 00:33:20 +00001134 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001135 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001136 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001137 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001138 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001139 Chain = Lo.getValue(1);
1140 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001141 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001142 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001143 InFlag);
1144 Chain = Hi.getValue(1);
1145 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001146 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001147
Owen Anderson825b72b2009-08-11 20:47:22 +00001148 if (VA.getLocVT() == MVT::v2f64) {
1149 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1150 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1151 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001152
1153 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001154 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001155 Chain = Lo.getValue(1);
1156 InFlag = Lo.getValue(2);
1157 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001158 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001159 Chain = Hi.getValue(1);
1160 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001161 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001162 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1163 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001164 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001165 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001166 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1167 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001168 Chain = Val.getValue(1);
1169 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001170 }
Bob Wilson80915242009-04-25 00:33:20 +00001171
1172 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001173 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001174 case CCValAssign::Full: break;
1175 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001176 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001177 break;
1178 }
1179
Dan Gohman98ca4f22009-08-05 01:29:28 +00001180 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001181 }
1182
Dan Gohman98ca4f22009-08-05 01:29:28 +00001183 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001184}
1185
Bob Wilsondee46d72009-04-17 20:35:10 +00001186/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001187SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001188ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1189 SDValue StackPtr, SDValue Arg,
1190 DebugLoc dl, SelectionDAG &DAG,
1191 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001192 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001193 unsigned LocMemOffset = VA.getLocMemOffset();
1194 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1195 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001196 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001197 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001198 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001199}
1200
Dan Gohman98ca4f22009-08-05 01:29:28 +00001201void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001202 SDValue Chain, SDValue &Arg,
1203 RegsToPassVector &RegsToPass,
1204 CCValAssign &VA, CCValAssign &NextVA,
1205 SDValue &StackPtr,
1206 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001207 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001208
Jim Grosbache5165492009-11-09 00:11:35 +00001209 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001210 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001211 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1212
1213 if (NextVA.isRegLoc())
1214 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1215 else {
1216 assert(NextVA.isMemLoc());
1217 if (StackPtr.getNode() == 0)
1218 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1219
Dan Gohman98ca4f22009-08-05 01:29:28 +00001220 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1221 dl, DAG, NextVA,
1222 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001223 }
1224}
1225
Dan Gohman98ca4f22009-08-05 01:29:28 +00001226/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001227/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1228/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001229SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001230ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001231 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001232 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001233 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001234 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001235 const SmallVectorImpl<ISD::InputArg> &Ins,
1236 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001237 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001238 MachineFunction &MF = DAG.getMachineFunction();
1239 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1240 bool IsSibCall = false;
Bob Wilson6d2f9ce2011-10-07 17:17:49 +00001241 // Disable tail calls if they're not supported.
1242 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Bob Wilson703af3a2010-08-13 22:43:33 +00001243 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001244 if (isTailCall) {
1245 // Check if it's really possible to do a tail call.
1246 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1247 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001248 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001249 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1250 // detected sibcalls.
1251 if (isTailCall) {
1252 ++NumTailCalls;
1253 IsSibCall = true;
1254 }
1255 }
Evan Chenga8e29892007-01-19 07:51:42 +00001256
Bob Wilson1f595bb2009-04-17 19:07:39 +00001257 // Analyze operands of the call, assigning locations to each operand.
1258 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001259 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1260 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001261 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001262 CCAssignFnForNode(CallConv, /* Return*/ false,
1263 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001264
Bob Wilson1f595bb2009-04-17 19:07:39 +00001265 // Get a count of how many bytes are to be pushed on the stack.
1266 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001267
Dale Johannesen51e28e62010-06-03 21:09:53 +00001268 // For tail calls, memory operands are available in our caller's stack.
1269 if (IsSibCall)
1270 NumBytes = 0;
1271
Evan Chenga8e29892007-01-19 07:51:42 +00001272 // Adjust the stack pointer for the new arguments...
1273 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001274 if (!IsSibCall)
1275 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001276
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001277 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001278
Bob Wilson5bafff32009-06-22 23:27:02 +00001279 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001280 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001281
Bob Wilson1f595bb2009-04-17 19:07:39 +00001282 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001283 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001284 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1285 i != e;
1286 ++i, ++realArgIdx) {
1287 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001288 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001289 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001290 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001291
Bob Wilson1f595bb2009-04-17 19:07:39 +00001292 // Promote the value if needed.
1293 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001294 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001295 case CCValAssign::Full: break;
1296 case CCValAssign::SExt:
1297 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1298 break;
1299 case CCValAssign::ZExt:
1300 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1301 break;
1302 case CCValAssign::AExt:
1303 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1304 break;
1305 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001306 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001307 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001308 }
1309
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001310 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001311 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001312 if (VA.getLocVT() == MVT::v2f64) {
1313 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1314 DAG.getConstant(0, MVT::i32));
1315 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1316 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001317
Dan Gohman98ca4f22009-08-05 01:29:28 +00001318 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001319 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1320
1321 VA = ArgLocs[++i]; // skip ahead to next loc
1322 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001323 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001324 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1325 } else {
1326 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001327
Dan Gohman98ca4f22009-08-05 01:29:28 +00001328 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1329 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001330 }
1331 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001332 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001333 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001334 }
1335 } else if (VA.isRegLoc()) {
1336 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001337 } else if (isByVal) {
1338 assert(VA.isMemLoc());
1339 unsigned offset = 0;
1340
1341 // True if this byval aggregate will be split between registers
1342 // and memory.
1343 if (CCInfo.isFirstByValRegValid()) {
1344 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1345 unsigned int i, j;
1346 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1347 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1348 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1349 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1350 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001351 false, false, false, 0);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001352 MemOpChains.push_back(Load.getValue(1));
1353 RegsToPass.push_back(std::make_pair(j, Load));
1354 }
1355 offset = ARM::R4 - CCInfo.getFirstByValReg();
1356 CCInfo.clearFirstByValReg();
1357 }
1358
1359 unsigned LocMemOffset = VA.getLocMemOffset();
1360 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1361 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1362 StkPtrOff);
1363 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1364 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1365 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1366 MVT::i32);
1367 MemOpChains.push_back(DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode,
1368 Flags.getByValAlign(),
1369 /*isVolatile=*/false,
Dan Gohman65fd6562011-11-03 21:49:52 +00001370 /*AlwaysInline=*/false,
Stuart Hastingsc7315872011-04-20 16:47:52 +00001371 MachinePointerInfo(0),
1372 MachinePointerInfo(0)));
1373
1374 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001375 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001376
Dan Gohman98ca4f22009-08-05 01:29:28 +00001377 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1378 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001379 }
Evan Chenga8e29892007-01-19 07:51:42 +00001380 }
1381
1382 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001383 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001384 &MemOpChains[0], MemOpChains.size());
1385
1386 // Build a sequence of copy-to-reg nodes chained together with token chain
1387 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001388 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001389 // Tail call byval lowering might overwrite argument registers so in case of
1390 // tail call optimization the copies to registers are lowered later.
1391 if (!isTailCall)
1392 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1393 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1394 RegsToPass[i].second, InFlag);
1395 InFlag = Chain.getValue(1);
1396 }
Evan Chenga8e29892007-01-19 07:51:42 +00001397
Dale Johannesen51e28e62010-06-03 21:09:53 +00001398 // For tail calls lower the arguments to the 'real' stack slot.
1399 if (isTailCall) {
1400 // Force all the incoming stack arguments to be loaded from the stack
1401 // before any new outgoing arguments are stored to the stack, because the
1402 // outgoing stack slots may alias the incoming argument stack slots, and
1403 // the alias isn't otherwise explicit. This is slightly more conservative
1404 // than necessary, because it means that each store effectively depends
1405 // on every argument instead of just those arguments it would clobber.
1406
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001407 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001408 InFlag = SDValue();
1409 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1410 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1411 RegsToPass[i].second, InFlag);
1412 InFlag = Chain.getValue(1);
1413 }
1414 InFlag =SDValue();
1415 }
1416
Bill Wendling056292f2008-09-16 21:48:12 +00001417 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1418 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1419 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001420 bool isDirect = false;
1421 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001422 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001423 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001424
1425 if (EnableARMLongCalls) {
1426 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1427 && "long-calls with non-static relocation model!");
1428 // Handle a global address or an external symbol. If it's not one of
1429 // those, the target's already in a register, so we don't need to do
1430 // anything extra.
1431 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001432 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001433 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001434 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001435 ARMConstantPoolValue *CPV =
1436 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1437
Jim Grosbache7b52522010-04-14 22:28:31 +00001438 // Get the address of the callee into a register
1439 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1440 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1441 Callee = DAG.getLoad(getPointerTy(), dl,
1442 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001443 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001444 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001445 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1446 const char *Sym = S->getSymbol();
1447
1448 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001449 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001450 ARMConstantPoolValue *CPV =
1451 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1452 ARMPCLabelIndex, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001453 // Get the address of the callee into a register
1454 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1455 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1456 Callee = DAG.getLoad(getPointerTy(), dl,
1457 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001458 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001459 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001460 }
1461 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001462 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001463 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001464 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001465 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001466 getTargetMachine().getRelocationModel() != Reloc::Static;
1467 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001468 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001469 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001470 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001471 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001472 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001473 ARMConstantPoolValue *CPV =
1474 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001475 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001476 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001477 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001478 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001479 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001480 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001481 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001482 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001483 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001484 } else {
1485 // On ELF targets for PIC code, direct calls should go through the PLT
1486 unsigned OpFlags = 0;
1487 if (Subtarget->isTargetELF() &&
1488 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1489 OpFlags = ARMII::MO_PLT;
1490 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1491 }
Bill Wendling056292f2008-09-16 21:48:12 +00001492 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001493 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001494 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001495 getTargetMachine().getRelocationModel() != Reloc::Static;
1496 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001497 // tBX takes a register source operand.
1498 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001499 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001500 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001501 ARMConstantPoolValue *CPV =
1502 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1503 ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001504 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001505 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001506 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001507 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001508 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001509 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001510 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001511 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001512 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001513 } else {
1514 unsigned OpFlags = 0;
1515 // On ELF targets for PIC code, direct calls should go through the PLT
1516 if (Subtarget->isTargetELF() &&
1517 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1518 OpFlags = ARMII::MO_PLT;
1519 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1520 }
Evan Chenga8e29892007-01-19 07:51:42 +00001521 }
1522
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001523 // FIXME: handle tail calls differently.
1524 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001525 if (Subtarget->isThumb()) {
1526 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001527 CallOpc = ARMISD::CALL_NOLINK;
1528 else
1529 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1530 } else {
1531 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001532 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1533 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001534 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001535
Dan Gohman475871a2008-07-27 21:46:04 +00001536 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001537 Ops.push_back(Chain);
1538 Ops.push_back(Callee);
1539
1540 // Add argument registers to the end of the list so that they are known live
1541 // into the call.
1542 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1543 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1544 RegsToPass[i].second.getValueType()));
1545
Gabor Greifba36cb52008-08-28 21:40:38 +00001546 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001547 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001548
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001549 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001550 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001551 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001552
Duncan Sands4bdcb612008-07-02 17:40:58 +00001553 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001554 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001555 InFlag = Chain.getValue(1);
1556
Chris Lattnere563bbc2008-10-11 22:08:30 +00001557 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1558 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001559 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001560 InFlag = Chain.getValue(1);
1561
Bob Wilson1f595bb2009-04-17 19:07:39 +00001562 // Handle result values, copying them out of physregs into vregs that we
1563 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001564 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1565 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001566}
1567
Stuart Hastingsf222e592011-02-28 17:17:53 +00001568/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001569/// on the stack. Remember the next parameter register to allocate,
1570/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001571/// this.
1572void
Stuart Hastingsc7315872011-04-20 16:47:52 +00001573llvm::ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
1574 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1575 assert((State->getCallOrPrologue() == Prologue ||
1576 State->getCallOrPrologue() == Call) &&
1577 "unhandled ParmContext");
1578 if ((!State->isFirstByValRegValid()) &&
1579 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1580 State->setFirstByValReg(reg);
1581 // At a call site, a byval parameter that is split between
1582 // registers and memory needs its size truncated here. In a
1583 // function prologue, such byval parameters are reassembled in
1584 // memory, and are not truncated.
1585 if (State->getCallOrPrologue() == Call) {
1586 unsigned excess = 4 * (ARM::R4 - reg);
1587 assert(size >= excess && "expected larger existing stack allocation");
1588 size -= excess;
1589 }
1590 }
1591 // Confiscate any remaining parameter registers to preclude their
1592 // assignment to subsequent parameters.
1593 while (State->AllocateReg(GPRArgRegs, 4))
1594 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001595}
1596
Dale Johannesen51e28e62010-06-03 21:09:53 +00001597/// MatchingStackOffset - Return true if the given stack call argument is
1598/// already available in the same position (relatively) of the caller's
1599/// incoming argument stack.
1600static
1601bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1602 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1603 const ARMInstrInfo *TII) {
1604 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1605 int FI = INT_MAX;
1606 if (Arg.getOpcode() == ISD::CopyFromReg) {
1607 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001608 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001609 return false;
1610 MachineInstr *Def = MRI->getVRegDef(VR);
1611 if (!Def)
1612 return false;
1613 if (!Flags.isByVal()) {
1614 if (!TII->isLoadFromStackSlot(Def, FI))
1615 return false;
1616 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001617 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001618 }
1619 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1620 if (Flags.isByVal())
1621 // ByVal argument is passed in as a pointer but it's now being
1622 // dereferenced. e.g.
1623 // define @foo(%struct.X* %A) {
1624 // tail call @bar(%struct.X* byval %A)
1625 // }
1626 return false;
1627 SDValue Ptr = Ld->getBasePtr();
1628 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1629 if (!FINode)
1630 return false;
1631 FI = FINode->getIndex();
1632 } else
1633 return false;
1634
1635 assert(FI != INT_MAX);
1636 if (!MFI->isFixedObjectIndex(FI))
1637 return false;
1638 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1639}
1640
1641/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1642/// for tail call optimization. Targets which want to do tail call
1643/// optimization should implement this function.
1644bool
1645ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1646 CallingConv::ID CalleeCC,
1647 bool isVarArg,
1648 bool isCalleeStructRet,
1649 bool isCallerStructRet,
1650 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001651 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001652 const SmallVectorImpl<ISD::InputArg> &Ins,
1653 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001654 const Function *CallerF = DAG.getMachineFunction().getFunction();
1655 CallingConv::ID CallerCC = CallerF->getCallingConv();
1656 bool CCMatch = CallerCC == CalleeCC;
1657
1658 // Look for obvious safe cases to perform tail call optimization that do not
1659 // require ABI changes. This is what gcc calls sibcall.
1660
Jim Grosbach7616b642010-06-16 23:45:49 +00001661 // Do not sibcall optimize vararg calls unless the call site is not passing
1662 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001663 if (isVarArg && !Outs.empty())
1664 return false;
1665
1666 // Also avoid sibcall optimization if either caller or callee uses struct
1667 // return semantics.
1668 if (isCalleeStructRet || isCallerStructRet)
1669 return false;
1670
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001671 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001672 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1673 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1674 // support in the assembler and linker to be used. This would need to be
1675 // fixed to fully support tail calls in Thumb1.
1676 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001677 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1678 // LR. This means if we need to reload LR, it takes an extra instructions,
1679 // which outweighs the value of the tail call; but here we don't know yet
1680 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001681 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001682 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001683
1684 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1685 // but we need to make sure there are enough registers; the only valid
1686 // registers are the 4 used for parameters. We don't currently do this
1687 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001688 if (Subtarget->isThumb1Only())
1689 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001690
Dale Johannesen51e28e62010-06-03 21:09:53 +00001691 // If the calling conventions do not match, then we'd better make sure the
1692 // results are returned in the same way as what the caller expects.
1693 if (!CCMatch) {
1694 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001695 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1696 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001697 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1698
1699 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001700 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1701 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001702 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1703
1704 if (RVLocs1.size() != RVLocs2.size())
1705 return false;
1706 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1707 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1708 return false;
1709 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1710 return false;
1711 if (RVLocs1[i].isRegLoc()) {
1712 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1713 return false;
1714 } else {
1715 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1716 return false;
1717 }
1718 }
1719 }
1720
1721 // If the callee takes no arguments then go on to check the results of the
1722 // call.
1723 if (!Outs.empty()) {
1724 // Check if stack adjustment is needed. For now, do not do this if any
1725 // argument is passed on the stack.
1726 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001727 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1728 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001729 CCInfo.AnalyzeCallOperands(Outs,
1730 CCAssignFnForNode(CalleeCC, false, isVarArg));
1731 if (CCInfo.getNextStackOffset()) {
1732 MachineFunction &MF = DAG.getMachineFunction();
1733
1734 // Check if the arguments are already laid out in the right way as
1735 // the caller's fixed stack objects.
1736 MachineFrameInfo *MFI = MF.getFrameInfo();
1737 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1738 const ARMInstrInfo *TII =
1739 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001740 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1741 i != e;
1742 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001743 CCValAssign &VA = ArgLocs[i];
1744 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001745 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001746 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001747 if (VA.getLocInfo() == CCValAssign::Indirect)
1748 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001749 if (VA.needsCustom()) {
1750 // f64 and vector types are split into multiple registers or
1751 // register/stack-slot combinations. The types will not match
1752 // the registers; give up on memory f64 refs until we figure
1753 // out what to do about this.
1754 if (!VA.isRegLoc())
1755 return false;
1756 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001757 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001758 if (RegVT == MVT::v2f64) {
1759 if (!ArgLocs[++i].isRegLoc())
1760 return false;
1761 if (!ArgLocs[++i].isRegLoc())
1762 return false;
1763 }
1764 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001765 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1766 MFI, MRI, TII))
1767 return false;
1768 }
1769 }
1770 }
1771 }
1772
1773 return true;
1774}
1775
Dan Gohman98ca4f22009-08-05 01:29:28 +00001776SDValue
1777ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001778 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001779 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001780 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001781 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001782
Bob Wilsondee46d72009-04-17 20:35:10 +00001783 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001784 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001785
Bob Wilsondee46d72009-04-17 20:35:10 +00001786 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001787 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1788 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001789
Dan Gohman98ca4f22009-08-05 01:29:28 +00001790 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001791 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1792 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001793
1794 // If this is the first return lowered for this function, add
1795 // the regs to the liveout set for the function.
1796 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1797 for (unsigned i = 0; i != RVLocs.size(); ++i)
1798 if (RVLocs[i].isRegLoc())
1799 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001800 }
1801
Bob Wilson1f595bb2009-04-17 19:07:39 +00001802 SDValue Flag;
1803
1804 // Copy the result values into the output registers.
1805 for (unsigned i = 0, realRVLocIdx = 0;
1806 i != RVLocs.size();
1807 ++i, ++realRVLocIdx) {
1808 CCValAssign &VA = RVLocs[i];
1809 assert(VA.isRegLoc() && "Can only return in registers!");
1810
Dan Gohmanc9403652010-07-07 15:54:55 +00001811 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001812
1813 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001814 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001815 case CCValAssign::Full: break;
1816 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001817 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001818 break;
1819 }
1820
Bob Wilson1f595bb2009-04-17 19:07:39 +00001821 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001822 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001823 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001824 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1825 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001826 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001827 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001828
1829 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1830 Flag = Chain.getValue(1);
1831 VA = RVLocs[++i]; // skip ahead to next loc
1832 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1833 HalfGPRs.getValue(1), Flag);
1834 Flag = Chain.getValue(1);
1835 VA = RVLocs[++i]; // skip ahead to next loc
1836
1837 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001838 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1839 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001840 }
1841 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1842 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001843 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001844 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001845 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001846 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001847 VA = RVLocs[++i]; // skip ahead to next loc
1848 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1849 Flag);
1850 } else
1851 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1852
Bob Wilsondee46d72009-04-17 20:35:10 +00001853 // Guarantee that all emitted copies are
1854 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001855 Flag = Chain.getValue(1);
1856 }
1857
1858 SDValue result;
1859 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001860 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001861 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001862 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001863
1864 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001865}
1866
Evan Cheng3d2125c2010-11-30 23:55:39 +00001867bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1868 if (N->getNumValues() != 1)
1869 return false;
1870 if (!N->hasNUsesOfValue(1, 0))
1871 return false;
1872
1873 unsigned NumCopies = 0;
1874 SDNode* Copies[2];
1875 SDNode *Use = *N->use_begin();
1876 if (Use->getOpcode() == ISD::CopyToReg) {
1877 Copies[NumCopies++] = Use;
1878 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1879 // f64 returned in a pair of GPRs.
1880 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1881 UI != UE; ++UI) {
1882 if (UI->getOpcode() != ISD::CopyToReg)
1883 return false;
1884 Copies[UI.getUse().getResNo()] = *UI;
1885 ++NumCopies;
1886 }
1887 } else if (Use->getOpcode() == ISD::BITCAST) {
1888 // f32 returned in a single GPR.
1889 if (!Use->hasNUsesOfValue(1, 0))
1890 return false;
1891 Use = *Use->use_begin();
1892 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1893 return false;
1894 Copies[NumCopies++] = Use;
1895 } else {
1896 return false;
1897 }
1898
1899 if (NumCopies != 1 && NumCopies != 2)
1900 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001901
1902 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001903 for (unsigned i = 0; i < NumCopies; ++i) {
1904 SDNode *Copy = Copies[i];
1905 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1906 UI != UE; ++UI) {
1907 if (UI->getOpcode() == ISD::CopyToReg) {
1908 SDNode *Use = *UI;
1909 if (Use == Copies[0] || Use == Copies[1])
1910 continue;
1911 return false;
1912 }
1913 if (UI->getOpcode() != ARMISD::RET_FLAG)
1914 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001915 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001916 }
1917 }
1918
Evan Cheng1bf891a2010-12-01 22:59:46 +00001919 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001920}
1921
Evan Cheng485fafc2011-03-21 01:19:09 +00001922bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1923 if (!EnableARMTailCalls)
1924 return false;
1925
1926 if (!CI->isTailCall())
1927 return false;
1928
1929 return !Subtarget->isThumb1Only();
1930}
1931
Bob Wilsonb62d2572009-11-03 00:02:05 +00001932// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1933// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1934// one of the above mentioned nodes. It has to be wrapped because otherwise
1935// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1936// be used to form addressing mode. These wrapped nodes will be selected
1937// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001938static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001939 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001940 // FIXME there is no actual debug info here
1941 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001942 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001943 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001944 if (CP->isMachineConstantPoolEntry())
1945 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1946 CP->getAlignment());
1947 else
1948 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1949 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001950 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001951}
1952
Jim Grosbache1102ca2010-07-19 17:20:38 +00001953unsigned ARMTargetLowering::getJumpTableEncoding() const {
1954 return MachineJumpTableInfo::EK_Inline;
1955}
1956
Dan Gohmand858e902010-04-17 15:26:15 +00001957SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1958 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001959 MachineFunction &MF = DAG.getMachineFunction();
1960 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1961 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001962 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001963 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001964 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001965 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1966 SDValue CPAddr;
1967 if (RelocM == Reloc::Static) {
1968 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1969 } else {
1970 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001971 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001972 ARMConstantPoolValue *CPV =
1973 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
1974 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson907eebd2009-11-02 20:59:23 +00001975 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1976 }
1977 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1978 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001979 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001980 false, false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001981 if (RelocM == Reloc::Static)
1982 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001983 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001984 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001985}
1986
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001987// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001988SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001989ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001990 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001991 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001992 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001993 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001994 MachineFunction &MF = DAG.getMachineFunction();
1995 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001996 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001997 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00001998 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
1999 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002000 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002001 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00002002 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002003 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002004 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002005 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002006
Evan Chenge7e0d622009-11-06 22:24:13 +00002007 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002008 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002009
2010 // call __tls_get_addr.
2011 ArgListTy Args;
2012 ArgListEntry Entry;
2013 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002014 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002015 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002016 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00002017 std::pair<SDValue, SDValue> CallResult =
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002018 LowerCallTo(Chain, (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00002019 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002020 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002021 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002022 return CallResult.first;
2023}
2024
2025// Lower ISD::GlobalTLSAddress using the "initial exec" or
2026// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00002027SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002028ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002029 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002030 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002031 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002032 SDValue Offset;
2033 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002034 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002035 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002036 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002037
Chris Lattner4fb63d02009-07-15 04:12:33 +00002038 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002039 MachineFunction &MF = DAG.getMachineFunction();
2040 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002041 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002042 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002043 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2044 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002045 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2046 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2047 true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002048 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002049 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002050 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002051 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002052 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002053 Chain = Offset.getValue(1);
2054
Evan Chenge7e0d622009-11-06 22:24:13 +00002055 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002056 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002057
Evan Cheng9eda6892009-10-31 03:39:36 +00002058 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002059 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002060 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002061 } else {
2062 // local exec model
Bill Wendling5bb77992011-10-01 08:00:54 +00002063 ARMConstantPoolValue *CPV =
2064 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002065 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002066 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002067 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002068 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002069 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002070 }
2071
2072 // The address of the thread local variable is the add of the thread
2073 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002074 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002075}
2076
Dan Gohman475871a2008-07-27 21:46:04 +00002077SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002078ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002079 // TODO: implement the "local dynamic" model
2080 assert(Subtarget->isTargetELF() &&
2081 "TLS not implemented for non-ELF targets");
2082 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2083 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
2084 // otherwise use the "Local Exec" TLS Model
2085 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
2086 return LowerToTLSGeneralDynamicModel(GA, DAG);
2087 else
2088 return LowerToTLSExecModels(GA, DAG);
2089}
2090
Dan Gohman475871a2008-07-27 21:46:04 +00002091SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002092 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002093 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002094 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002095 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002096 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2097 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002098 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002099 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002100 ARMConstantPoolConstant::Create(GV,
2101 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002102 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002103 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002104 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002105 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002106 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002107 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002108 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002109 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002110 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002111 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002112 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002113 MachinePointerInfo::getGOT(),
2114 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002115 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002116 }
2117
2118 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloy015cca62011-10-26 08:53:19 +00002119 // pair. This is always cheaper.
2120 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002121 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002122 // FIXME: Once remat is capable of dealing with instructions with register
2123 // operands, expand this into two nodes.
2124 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2125 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002126 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002127 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2128 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2129 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2130 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002131 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002132 }
2133}
2134
Dan Gohman475871a2008-07-27 21:46:04 +00002135SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002136 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002137 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002138 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002139 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002140 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002141 MachineFunction &MF = DAG.getMachineFunction();
2142 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2143
Evan Cheng4abce0c2011-05-27 20:11:27 +00002144 // FIXME: Enable this for static codegen when tool issues are fixed.
Evan Chengf31151f2011-10-26 01:17:44 +00002145 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002146 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002147 // FIXME: Once remat is capable of dealing with instructions with register
2148 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002149 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002150 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2151 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2152
Evan Cheng53519f02011-01-21 18:55:51 +00002153 unsigned Wrapper = (RelocM == Reloc::PIC_)
2154 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2155 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002156 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002157 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2158 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002159 MachinePointerInfo::getGOT(),
2160 false, false, false, 0);
Evan Chengfc8475b2011-01-19 02:16:49 +00002161 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002162 }
2163
2164 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002165 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002166 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002167 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002168 } else {
2169 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002170 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2171 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002172 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue,
2173 PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002174 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002175 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002176 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002177
Evan Cheng9eda6892009-10-31 03:39:36 +00002178 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002179 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002180 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002181 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002182
2183 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002184 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002185 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002186 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002187
Evan Cheng63476a82009-09-03 07:04:02 +00002188 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002189 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002190 false, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002191
2192 return Result;
2193}
2194
Dan Gohman475871a2008-07-27 21:46:04 +00002195SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002196 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002197 assert(Subtarget->isTargetELF() &&
2198 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002199 MachineFunction &MF = DAG.getMachineFunction();
2200 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002201 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002202 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002203 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002204 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingfe31e672011-10-01 08:58:29 +00002205 ARMConstantPoolValue *CPV =
2206 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2207 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002208 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002209 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002210 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002211 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002212 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002213 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002214 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002215}
2216
Jim Grosbach0e0da732009-05-12 23:59:14 +00002217SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002218ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2219 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002220 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendlingce370cf2011-10-07 21:25:38 +00002221 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2222 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002223 Op.getOperand(1), Val);
2224}
2225
2226SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002227ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2228 DebugLoc dl = Op.getDebugLoc();
2229 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2230 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2231}
2232
2233SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002234ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002235 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002236 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002237 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002238 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002239 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002240 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002241 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002242 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2243 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002244 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002245 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002246 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002247 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002248 EVT PtrVT = getPointerTy();
2249 DebugLoc dl = Op.getDebugLoc();
2250 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2251 SDValue CPAddr;
2252 unsigned PCAdj = (RelocM != Reloc::PIC_)
2253 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002254 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002255 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2256 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002257 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002258 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002259 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002260 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002261 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002262 false, false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002263
2264 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002265 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002266 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2267 }
2268 return Result;
2269 }
Evan Cheng92e39162011-03-29 23:06:19 +00002270 case Intrinsic::arm_neon_vmulls:
2271 case Intrinsic::arm_neon_vmullu: {
2272 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2273 ? ARMISD::VMULLs : ARMISD::VMULLu;
2274 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2275 Op.getOperand(1), Op.getOperand(2));
2276 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002277 }
2278}
2279
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002280static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002281 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002282 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002283 if (!Subtarget->hasDataBarrier()) {
2284 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2285 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2286 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002287 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002288 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002289 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002290 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002291 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002292
2293 SDValue Op5 = Op.getOperand(5);
2294 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2295 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2296 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2297 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2298
2299 ARM_MB::MemBOpt DMBOpt;
2300 if (isDeviceBarrier)
2301 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2302 else
2303 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2304 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2305 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002306}
2307
Eli Friedman26689ac2011-08-03 21:06:02 +00002308
2309static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2310 const ARMSubtarget *Subtarget) {
2311 // FIXME: handle "fence singlethread" more efficiently.
2312 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002313 if (!Subtarget->hasDataBarrier()) {
2314 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2315 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2316 // here.
2317 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2318 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002319 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002320 DAG.getConstant(0, MVT::i32));
2321 }
2322
Eli Friedman26689ac2011-08-03 21:06:02 +00002323 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002324 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002325}
2326
Evan Chengdfed19f2010-11-03 06:34:55 +00002327static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2328 const ARMSubtarget *Subtarget) {
2329 // ARM pre v5TE and Thumb1 does not have preload instructions.
2330 if (!(Subtarget->isThumb2() ||
2331 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2332 // Just preserve the chain.
2333 return Op.getOperand(0);
2334
2335 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002336 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2337 if (!isRead &&
2338 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2339 // ARMv7 with MP extension has PLDW.
2340 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002341
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002342 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2343 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002344 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002345 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002346 isData = ~isData & 1;
2347 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002348
2349 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002350 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2351 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002352}
2353
Dan Gohman1e93df62010-04-17 14:41:14 +00002354static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2355 MachineFunction &MF = DAG.getMachineFunction();
2356 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2357
Evan Chenga8e29892007-01-19 07:51:42 +00002358 // vastart just stores the address of the VarArgsFrameIndex slot into the
2359 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002360 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002361 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002362 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002363 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002364 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2365 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002366}
2367
Dan Gohman475871a2008-07-27 21:46:04 +00002368SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002369ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2370 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002371 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002372 MachineFunction &MF = DAG.getMachineFunction();
2373 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2374
2375 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002376 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002377 RC = ARM::tGPRRegisterClass;
2378 else
2379 RC = ARM::GPRRegisterClass;
2380
2381 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002382 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002383 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002384
2385 SDValue ArgValue2;
2386 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002387 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002388 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002389
2390 // Create load node to retrieve arguments from the stack.
2391 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002392 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002393 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002394 false, false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002395 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002396 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002397 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002398 }
2399
Jim Grosbache5165492009-11-09 00:11:35 +00002400 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002401}
2402
Stuart Hastingsc7315872011-04-20 16:47:52 +00002403void
2404ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2405 unsigned &VARegSize, unsigned &VARegSaveSize)
2406 const {
2407 unsigned NumGPRs;
2408 if (CCInfo.isFirstByValRegValid())
2409 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2410 else {
2411 unsigned int firstUnalloced;
2412 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2413 sizeof(GPRArgRegs) /
2414 sizeof(GPRArgRegs[0]));
2415 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2416 }
2417
2418 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2419 VARegSize = NumGPRs * 4;
2420 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2421}
2422
2423// The remaining GPRs hold either the beginning of variable-argument
2424// data, or the beginning of an aggregate passed by value (usuall
2425// byval). Either way, we allocate stack slots adjacent to the data
2426// provided by our caller, and store the unallocated registers there.
2427// If this is a variadic function, the va_list pointer will begin with
2428// these values; otherwise, this reassembles a (byval) structure that
2429// was split between registers and memory.
2430void
2431ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2432 DebugLoc dl, SDValue &Chain,
2433 unsigned ArgOffset) const {
2434 MachineFunction &MF = DAG.getMachineFunction();
2435 MachineFrameInfo *MFI = MF.getFrameInfo();
2436 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2437 unsigned firstRegToSaveIndex;
2438 if (CCInfo.isFirstByValRegValid())
2439 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2440 else {
2441 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2442 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2443 }
2444
2445 unsigned VARegSize, VARegSaveSize;
2446 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2447 if (VARegSaveSize) {
2448 // If this function is vararg, store any remaining integer argument regs
2449 // to their spots on the stack so that they may be loaded by deferencing
2450 // the result of va_next.
2451 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002452 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2453 ArgOffset + VARegSaveSize
2454 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002455 false));
2456 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2457 getPointerTy());
2458
2459 SmallVector<SDValue, 4> MemOps;
2460 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
2461 TargetRegisterClass *RC;
2462 if (AFI->isThumb1OnlyFunction())
2463 RC = ARM::tGPRRegisterClass;
2464 else
2465 RC = ARM::GPRRegisterClass;
2466
2467 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2468 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2469 SDValue Store =
2470 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002471 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002472 false, false, 0);
2473 MemOps.push_back(Store);
2474 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2475 DAG.getConstant(4, getPointerTy()));
2476 }
2477 if (!MemOps.empty())
2478 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2479 &MemOps[0], MemOps.size());
2480 } else
2481 // This will point to the next argument passed via stack.
2482 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2483}
2484
Bob Wilson5bafff32009-06-22 23:27:02 +00002485SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002486ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002487 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002488 const SmallVectorImpl<ISD::InputArg>
2489 &Ins,
2490 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002491 SmallVectorImpl<SDValue> &InVals)
2492 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002493 MachineFunction &MF = DAG.getMachineFunction();
2494 MachineFrameInfo *MFI = MF.getFrameInfo();
2495
Bob Wilson1f595bb2009-04-17 19:07:39 +00002496 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2497
2498 // Assign locations to all of the incoming arguments.
2499 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002500 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2501 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002502 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002503 CCAssignFnForNode(CallConv, /* Return*/ false,
2504 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002505
2506 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002507 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002508
Stuart Hastingsf222e592011-02-28 17:17:53 +00002509 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002510 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2511 CCValAssign &VA = ArgLocs[i];
2512
Bob Wilsondee46d72009-04-17 20:35:10 +00002513 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002514 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002515 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002516
Bob Wilson1f595bb2009-04-17 19:07:39 +00002517 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002518 // f64 and vector types are split up into multiple registers or
2519 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002520 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002521 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002522 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002523 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002524 SDValue ArgValue2;
2525 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002526 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002527 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2528 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002529 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002530 false, false, false, 0);
Bob Wilson6a234f02010-04-13 22:03:22 +00002531 } else {
2532 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2533 Chain, DAG, dl);
2534 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002535 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2536 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002537 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002538 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002539 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2540 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002541 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002542
Bob Wilson5bafff32009-06-22 23:27:02 +00002543 } else {
2544 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002545
Owen Anderson825b72b2009-08-11 20:47:22 +00002546 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002547 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002548 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002549 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002550 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002551 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002552 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002553 RC = (AFI->isThumb1OnlyFunction() ?
2554 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002555 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002556 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002557
2558 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002559 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002560 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002561 }
2562
2563 // If this is an 8 or 16-bit value, it is really passed promoted
2564 // to 32 bits. Insert an assert[sz]ext to capture this, then
2565 // truncate to the right size.
2566 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002567 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002568 case CCValAssign::Full: break;
2569 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002570 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002571 break;
2572 case CCValAssign::SExt:
2573 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2574 DAG.getValueType(VA.getValVT()));
2575 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2576 break;
2577 case CCValAssign::ZExt:
2578 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2579 DAG.getValueType(VA.getValVT()));
2580 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2581 break;
2582 }
2583
Dan Gohman98ca4f22009-08-05 01:29:28 +00002584 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002585
2586 } else { // VA.isRegLoc()
2587
2588 // sanity check
2589 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002590 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002591
Stuart Hastingsf222e592011-02-28 17:17:53 +00002592 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002593
Stuart Hastingsf222e592011-02-28 17:17:53 +00002594 // Some Ins[] entries become multiple ArgLoc[] entries.
2595 // Process them only once.
2596 if (index != lastInsIndex)
2597 {
2598 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002599 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002600 // This can be changed with more analysis.
2601 // In case of tail call optimization mark all arguments mutable.
2602 // Since they could be overwritten by lowering of arguments in case of
2603 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002604 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002605 unsigned VARegSize, VARegSaveSize;
2606 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2607 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2608 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002609 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002610 int FI = MFI->CreateFixedObject(Bytes,
2611 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002612 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2613 } else {
2614 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2615 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002616
Stuart Hastingsf222e592011-02-28 17:17:53 +00002617 // Create load nodes to retrieve arguments from the stack.
2618 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2619 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2620 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002621 false, false, false, 0));
Stuart Hastingsf222e592011-02-28 17:17:53 +00002622 }
2623 lastInsIndex = index;
2624 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002625 }
2626 }
2627
2628 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002629 if (isVarArg)
2630 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002631
Dan Gohman98ca4f22009-08-05 01:29:28 +00002632 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002633}
2634
2635/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002636static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002637 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002638 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002639 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002640 // Maybe this has already been legalized into the constant pool?
2641 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002642 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002643 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002644 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002645 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002646 }
2647 }
2648 return false;
2649}
2650
Evan Chenga8e29892007-01-19 07:51:42 +00002651/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2652/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002653SDValue
2654ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002655 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002656 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002657 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002658 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002659 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002660 // Constant does not fit, try adjusting it by one?
2661 switch (CC) {
2662 default: break;
2663 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002664 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002665 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002666 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002667 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002668 }
2669 break;
2670 case ISD::SETULT:
2671 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002672 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002673 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002674 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002675 }
2676 break;
2677 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002678 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002679 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002680 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002681 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002682 }
2683 break;
2684 case ISD::SETULE:
2685 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002686 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002687 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002688 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002689 }
2690 break;
2691 }
2692 }
2693 }
2694
2695 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002696 ARMISD::NodeType CompareType;
2697 switch (CondCode) {
2698 default:
2699 CompareType = ARMISD::CMP;
2700 break;
2701 case ARMCC::EQ:
2702 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002703 // Uses only Z Flag
2704 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002705 break;
2706 }
Evan Cheng218977b2010-07-13 19:27:42 +00002707 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002708 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002709}
2710
2711/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002712SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002713ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002714 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002715 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002716 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002717 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002718 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002719 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2720 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002721}
2722
Bob Wilson79f56c92011-03-08 01:17:20 +00002723/// duplicateCmp - Glue values can have only one use, so this function
2724/// duplicates a comparison node.
2725SDValue
2726ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2727 unsigned Opc = Cmp.getOpcode();
2728 DebugLoc DL = Cmp.getDebugLoc();
2729 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2730 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2731
2732 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2733 Cmp = Cmp.getOperand(0);
2734 Opc = Cmp.getOpcode();
2735 if (Opc == ARMISD::CMPFP)
2736 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2737 else {
2738 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2739 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2740 }
2741 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2742}
2743
Bill Wendlingde2b1512010-08-11 08:43:16 +00002744SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2745 SDValue Cond = Op.getOperand(0);
2746 SDValue SelectTrue = Op.getOperand(1);
2747 SDValue SelectFalse = Op.getOperand(2);
2748 DebugLoc dl = Op.getDebugLoc();
2749
2750 // Convert:
2751 //
2752 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2753 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2754 //
2755 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2756 const ConstantSDNode *CMOVTrue =
2757 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2758 const ConstantSDNode *CMOVFalse =
2759 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2760
2761 if (CMOVTrue && CMOVFalse) {
2762 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2763 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2764
2765 SDValue True;
2766 SDValue False;
2767 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2768 True = SelectTrue;
2769 False = SelectFalse;
2770 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2771 True = SelectFalse;
2772 False = SelectTrue;
2773 }
2774
2775 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002776 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002777 SDValue ARMcc = Cond.getOperand(2);
2778 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002779 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002780 assert(True.getValueType() == VT);
2781 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002782 }
2783 }
2784 }
2785
2786 return DAG.getSelectCC(dl, Cond,
2787 DAG.getConstant(0, Cond.getValueType()),
2788 SelectTrue, SelectFalse, ISD::SETNE);
2789}
2790
Dan Gohmand858e902010-04-17 15:26:15 +00002791SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002792 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002793 SDValue LHS = Op.getOperand(0);
2794 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002795 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002796 SDValue TrueVal = Op.getOperand(2);
2797 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002798 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002799
Owen Anderson825b72b2009-08-11 20:47:22 +00002800 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002801 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002802 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002803 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Jim Grosbachb04546f2011-09-13 20:30:37 +00002804 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002805 }
2806
2807 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002808 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002809
Evan Cheng218977b2010-07-13 19:27:42 +00002810 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2811 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002812 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002813 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002814 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002815 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002816 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002817 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002818 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002819 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002820 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002821 }
2822 return Result;
2823}
2824
Evan Cheng218977b2010-07-13 19:27:42 +00002825/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2826/// to morph to an integer compare sequence.
2827static bool canChangeToInt(SDValue Op, bool &SeenZero,
2828 const ARMSubtarget *Subtarget) {
2829 SDNode *N = Op.getNode();
2830 if (!N->hasOneUse())
2831 // Otherwise it requires moving the value from fp to integer registers.
2832 return false;
2833 if (!N->getNumValues())
2834 return false;
2835 EVT VT = Op.getValueType();
2836 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2837 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2838 // vmrs are very slow, e.g. cortex-a8.
2839 return false;
2840
2841 if (isFloatingPointZero(Op)) {
2842 SeenZero = true;
2843 return true;
2844 }
2845 return ISD::isNormalLoad(N);
2846}
2847
2848static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2849 if (isFloatingPointZero(Op))
2850 return DAG.getConstant(0, MVT::i32);
2851
2852 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2853 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002854 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002855 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002856 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002857
2858 llvm_unreachable("Unknown VFP cmp argument!");
2859}
2860
2861static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2862 SDValue &RetVal1, SDValue &RetVal2) {
2863 if (isFloatingPointZero(Op)) {
2864 RetVal1 = DAG.getConstant(0, MVT::i32);
2865 RetVal2 = DAG.getConstant(0, MVT::i32);
2866 return;
2867 }
2868
2869 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2870 SDValue Ptr = Ld->getBasePtr();
2871 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2872 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002873 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002874 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002875 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002876
2877 EVT PtrType = Ptr.getValueType();
2878 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2879 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2880 PtrType, Ptr, DAG.getConstant(4, PtrType));
2881 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2882 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002883 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002884 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002885 Ld->isInvariant(), NewAlign);
Evan Cheng218977b2010-07-13 19:27:42 +00002886 return;
2887 }
2888
2889 llvm_unreachable("Unknown VFP cmp argument!");
2890}
2891
2892/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2893/// f32 and even f64 comparisons to integer ones.
2894SDValue
2895ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2896 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002897 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002898 SDValue LHS = Op.getOperand(2);
2899 SDValue RHS = Op.getOperand(3);
2900 SDValue Dest = Op.getOperand(4);
2901 DebugLoc dl = Op.getDebugLoc();
2902
2903 bool SeenZero = false;
2904 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2905 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002906 // If one of the operand is zero, it's safe to ignore the NaN case since
2907 // we only care about equality comparisons.
2908 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Bob Wilson1b772f92011-03-08 01:17:16 +00002909 // If unsafe fp math optimization is enabled and there are no other uses of
2910 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00002911 // to an integer comparison.
2912 if (CC == ISD::SETOEQ)
2913 CC = ISD::SETEQ;
2914 else if (CC == ISD::SETUNE)
2915 CC = ISD::SETNE;
2916
2917 SDValue ARMcc;
2918 if (LHS.getValueType() == MVT::f32) {
2919 LHS = bitcastf32Toi32(LHS, DAG);
2920 RHS = bitcastf32Toi32(RHS, DAG);
2921 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2922 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2923 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2924 Chain, Dest, ARMcc, CCR, Cmp);
2925 }
2926
2927 SDValue LHS1, LHS2;
2928 SDValue RHS1, RHS2;
2929 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2930 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2931 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2932 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002933 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002934 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2935 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2936 }
2937
2938 return SDValue();
2939}
2940
2941SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2942 SDValue Chain = Op.getOperand(0);
2943 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2944 SDValue LHS = Op.getOperand(2);
2945 SDValue RHS = Op.getOperand(3);
2946 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002947 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002948
Owen Anderson825b72b2009-08-11 20:47:22 +00002949 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002950 SDValue ARMcc;
2951 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002952 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002953 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002954 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002955 }
2956
Owen Anderson825b72b2009-08-11 20:47:22 +00002957 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002958
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002959 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng218977b2010-07-13 19:27:42 +00002960 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2961 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2962 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2963 if (Result.getNode())
2964 return Result;
2965 }
2966
Evan Chenga8e29892007-01-19 07:51:42 +00002967 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002968 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002969
Evan Cheng218977b2010-07-13 19:27:42 +00002970 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2971 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002972 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002973 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002974 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002975 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002976 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002977 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2978 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002979 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002980 }
2981 return Res;
2982}
2983
Dan Gohmand858e902010-04-17 15:26:15 +00002984SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002985 SDValue Chain = Op.getOperand(0);
2986 SDValue Table = Op.getOperand(1);
2987 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002988 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002989
Owen Andersone50ed302009-08-10 22:56:29 +00002990 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002991 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2992 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002993 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002994 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00002995 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00002996 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
2997 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00002998 if (Subtarget->isThumb2()) {
2999 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3000 // which does another jump to the destination. This also makes it easier
3001 // to translate it to TBB / TBH later.
3002 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00003003 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00003004 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003005 }
Evan Cheng66ac5312009-07-25 00:33:29 +00003006 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00003007 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003008 MachinePointerInfo::getJumpTable(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003009 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003010 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003011 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00003012 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003013 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00003014 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003015 MachinePointerInfo::getJumpTable(),
3016 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003017 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003018 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003019 }
Evan Chenga8e29892007-01-19 07:51:42 +00003020}
3021
Eli Friedman14e809c2011-11-09 23:36:02 +00003022static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
3023 EVT VT = Op.getValueType();
3024 assert(VT.getVectorElementType() == MVT::i32 && "Unexpected custom lowering");
3025
3026 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3027 return Op;
3028 return DAG.UnrollVectorOp(Op.getNode());
3029}
3030
Bob Wilson76a312b2010-03-19 22:51:32 +00003031static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman14e809c2011-11-09 23:36:02 +00003032 EVT VT = Op.getValueType();
3033 if (VT.isVector())
3034 return LowerVectorFP_TO_INT(Op, DAG);
3035
Bob Wilson76a312b2010-03-19 22:51:32 +00003036 DebugLoc dl = Op.getDebugLoc();
3037 unsigned Opc;
3038
3039 switch (Op.getOpcode()) {
3040 default:
3041 assert(0 && "Invalid opcode!");
3042 case ISD::FP_TO_SINT:
3043 Opc = ARMISD::FTOSI;
3044 break;
3045 case ISD::FP_TO_UINT:
3046 Opc = ARMISD::FTOUI;
3047 break;
3048 }
3049 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003050 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003051}
3052
Cameron Zwarich3007d332011-03-29 21:41:55 +00003053static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3054 EVT VT = Op.getValueType();
3055 DebugLoc dl = Op.getDebugLoc();
3056
Eli Friedman14e809c2011-11-09 23:36:02 +00003057 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3058 if (VT.getVectorElementType() == MVT::f32)
3059 return Op;
3060 return DAG.UnrollVectorOp(Op.getNode());
3061 }
3062
Duncan Sands1f6a3292011-08-12 14:54:45 +00003063 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3064 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003065 if (VT != MVT::v4f32)
3066 return DAG.UnrollVectorOp(Op.getNode());
3067
3068 unsigned CastOpc;
3069 unsigned Opc;
3070 switch (Op.getOpcode()) {
3071 default:
3072 assert(0 && "Invalid opcode!");
3073 case ISD::SINT_TO_FP:
3074 CastOpc = ISD::SIGN_EXTEND;
3075 Opc = ISD::SINT_TO_FP;
3076 break;
3077 case ISD::UINT_TO_FP:
3078 CastOpc = ISD::ZERO_EXTEND;
3079 Opc = ISD::UINT_TO_FP;
3080 break;
3081 }
3082
3083 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3084 return DAG.getNode(Opc, dl, VT, Op);
3085}
3086
Bob Wilson76a312b2010-03-19 22:51:32 +00003087static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3088 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003089 if (VT.isVector())
3090 return LowerVectorINT_TO_FP(Op, DAG);
3091
Bob Wilson76a312b2010-03-19 22:51:32 +00003092 DebugLoc dl = Op.getDebugLoc();
3093 unsigned Opc;
3094
3095 switch (Op.getOpcode()) {
3096 default:
3097 assert(0 && "Invalid opcode!");
3098 case ISD::SINT_TO_FP:
3099 Opc = ARMISD::SITOF;
3100 break;
3101 case ISD::UINT_TO_FP:
3102 Opc = ARMISD::UITOF;
3103 break;
3104 }
3105
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003106 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003107 return DAG.getNode(Opc, dl, VT, Op);
3108}
3109
Evan Cheng515fe3a2010-07-08 02:08:50 +00003110SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003111 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003112 SDValue Tmp0 = Op.getOperand(0);
3113 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003114 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003115 EVT VT = Op.getValueType();
3116 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003117 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3118 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3119 bool UseNEON = !InGPR && Subtarget->hasNEON();
3120
3121 if (UseNEON) {
3122 // Use VBSL to copy the sign bit.
3123 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3124 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3125 DAG.getTargetConstant(EncodedVal, MVT::i32));
3126 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3127 if (VT == MVT::f64)
3128 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3129 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3130 DAG.getConstant(32, MVT::i32));
3131 else /*if (VT == MVT::f32)*/
3132 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3133 if (SrcVT == MVT::f32) {
3134 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3135 if (VT == MVT::f64)
3136 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3137 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3138 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003139 } else if (VT == MVT::f32)
3140 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3141 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3142 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003143 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3144 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3145
3146 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3147 MVT::i32);
3148 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3149 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3150 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003151
Evan Chenge573fb32011-02-23 02:24:55 +00003152 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3153 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3154 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003155 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003156 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3157 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3158 DAG.getConstant(0, MVT::i32));
3159 } else {
3160 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3161 }
3162
3163 return Res;
3164 }
Evan Chengc143dd42011-02-11 02:28:55 +00003165
3166 // Bitcast operand 1 to i32.
3167 if (SrcVT == MVT::f64)
3168 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3169 &Tmp1, 1).getValue(1);
3170 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3171
Evan Chenge573fb32011-02-23 02:24:55 +00003172 // Or in the signbit with integer operations.
3173 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3174 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3175 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3176 if (VT == MVT::f32) {
3177 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3178 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3179 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3180 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003181 }
3182
Evan Chenge573fb32011-02-23 02:24:55 +00003183 // f64: Or the high part with signbit and then combine two parts.
3184 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3185 &Tmp0, 1);
3186 SDValue Lo = Tmp0.getValue(0);
3187 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3188 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3189 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003190}
3191
Evan Cheng2457f2c2010-05-22 01:47:14 +00003192SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3193 MachineFunction &MF = DAG.getMachineFunction();
3194 MachineFrameInfo *MFI = MF.getFrameInfo();
3195 MFI->setReturnAddressIsTaken(true);
3196
3197 EVT VT = Op.getValueType();
3198 DebugLoc dl = Op.getDebugLoc();
3199 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3200 if (Depth) {
3201 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3202 SDValue Offset = DAG.getConstant(4, MVT::i32);
3203 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3204 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003205 MachinePointerInfo(), false, false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003206 }
3207
3208 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003209 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003210 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3211}
3212
Dan Gohmand858e902010-04-17 15:26:15 +00003213SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003214 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3215 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003216
Owen Andersone50ed302009-08-10 22:56:29 +00003217 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003218 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3219 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003220 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003221 ? ARM::R7 : ARM::R11;
3222 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3223 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003224 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3225 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003226 false, false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003227 return FrameAddr;
3228}
3229
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003230/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003231/// expand a bit convert where either the source or destination type is i64 to
3232/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3233/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3234/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003235static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003236 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3237 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003238 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003239
Bob Wilson9f3f0612010-04-17 05:30:19 +00003240 // This function is only supposed to be called for i64 types, either as the
3241 // source or destination of the bit convert.
3242 EVT SrcVT = Op.getValueType();
3243 EVT DstVT = N->getValueType(0);
3244 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003245 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003246
Bob Wilson9f3f0612010-04-17 05:30:19 +00003247 // Turn i64->f64 into VMOVDRR.
3248 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003249 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3250 DAG.getConstant(0, MVT::i32));
3251 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3252 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003253 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003254 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003255 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003256
Jim Grosbache5165492009-11-09 00:11:35 +00003257 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003258 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3259 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3260 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3261 // Merge the pieces into a single i64 value.
3262 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3263 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003264
Bob Wilson9f3f0612010-04-17 05:30:19 +00003265 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003266}
3267
Bob Wilson5bafff32009-06-22 23:27:02 +00003268/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003269/// Zero vectors are used to represent vector negation and in those cases
3270/// will be implemented with the NEON VNEG instruction. However, VNEG does
3271/// not support i64 elements, so sometimes the zero vectors will need to be
3272/// explicitly constructed. Regardless, use a canonical VMOV to create the
3273/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003274static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003275 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003276 // The canonical modified immediate encoding of a zero vector is....0!
3277 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3278 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3279 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003280 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003281}
3282
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003283/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3284/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003285SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3286 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003287 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3288 EVT VT = Op.getValueType();
3289 unsigned VTBits = VT.getSizeInBits();
3290 DebugLoc dl = Op.getDebugLoc();
3291 SDValue ShOpLo = Op.getOperand(0);
3292 SDValue ShOpHi = Op.getOperand(1);
3293 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003294 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003295 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003296
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003297 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3298
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003299 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3300 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3301 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3302 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3303 DAG.getConstant(VTBits, MVT::i32));
3304 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3305 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003306 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003307
3308 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3309 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003310 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003311 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003312 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003313 CCR, Cmp);
3314
3315 SDValue Ops[2] = { Lo, Hi };
3316 return DAG.getMergeValues(Ops, 2, dl);
3317}
3318
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003319/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3320/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003321SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3322 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003323 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3324 EVT VT = Op.getValueType();
3325 unsigned VTBits = VT.getSizeInBits();
3326 DebugLoc dl = Op.getDebugLoc();
3327 SDValue ShOpLo = Op.getOperand(0);
3328 SDValue ShOpHi = Op.getOperand(1);
3329 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003330 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003331
3332 assert(Op.getOpcode() == ISD::SHL_PARTS);
3333 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3334 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3335 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3336 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3337 DAG.getConstant(VTBits, MVT::i32));
3338 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3339 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3340
3341 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3342 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3343 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003344 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003345 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003346 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003347 CCR, Cmp);
3348
3349 SDValue Ops[2] = { Lo, Hi };
3350 return DAG.getMergeValues(Ops, 2, dl);
3351}
3352
Jim Grosbach4725ca72010-09-08 03:54:02 +00003353SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003354 SelectionDAG &DAG) const {
3355 // The rounding mode is in bits 23:22 of the FPSCR.
3356 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3357 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3358 // so that the shift + and get folded into a bitfield extract.
3359 DebugLoc dl = Op.getDebugLoc();
3360 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3361 DAG.getConstant(Intrinsic::arm_get_fpscr,
3362 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003363 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003364 DAG.getConstant(1U << 22, MVT::i32));
3365 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3366 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003367 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003368 DAG.getConstant(3, MVT::i32));
3369}
3370
Jim Grosbach3482c802010-01-18 19:58:49 +00003371static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3372 const ARMSubtarget *ST) {
3373 EVT VT = N->getValueType(0);
3374 DebugLoc dl = N->getDebugLoc();
3375
3376 if (!ST->hasV6T2Ops())
3377 return SDValue();
3378
3379 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3380 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3381}
3382
Bob Wilson5bafff32009-06-22 23:27:02 +00003383static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3384 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003385 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003386 DebugLoc dl = N->getDebugLoc();
3387
Bob Wilsond5448bb2010-11-18 21:16:28 +00003388 if (!VT.isVector())
3389 return SDValue();
3390
Bob Wilson5bafff32009-06-22 23:27:02 +00003391 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003392 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003393
Bob Wilsond5448bb2010-11-18 21:16:28 +00003394 // Left shifts translate directly to the vshiftu intrinsic.
3395 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003396 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003397 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3398 N->getOperand(0), N->getOperand(1));
3399
3400 assert((N->getOpcode() == ISD::SRA ||
3401 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3402
3403 // NEON uses the same intrinsics for both left and right shifts. For
3404 // right shifts, the shift amounts are negative, so negate the vector of
3405 // shift amounts.
3406 EVT ShiftVT = N->getOperand(1).getValueType();
3407 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3408 getZeroVector(ShiftVT, DAG, dl),
3409 N->getOperand(1));
3410 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3411 Intrinsic::arm_neon_vshifts :
3412 Intrinsic::arm_neon_vshiftu);
3413 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3414 DAG.getConstant(vshiftInt, MVT::i32),
3415 N->getOperand(0), NegatedCount);
3416}
3417
3418static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3419 const ARMSubtarget *ST) {
3420 EVT VT = N->getValueType(0);
3421 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003422
Eli Friedmance392eb2009-08-22 03:13:10 +00003423 // We can get here for a node like i32 = ISD::SHL i32, i64
3424 if (VT != MVT::i64)
3425 return SDValue();
3426
3427 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003428 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003429
Chris Lattner27a6c732007-11-24 07:07:01 +00003430 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3431 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003432 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003433 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003434
Chris Lattner27a6c732007-11-24 07:07:01 +00003435 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003436 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003437
Chris Lattner27a6c732007-11-24 07:07:01 +00003438 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003439 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003440 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003441 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003442 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003443
Chris Lattner27a6c732007-11-24 07:07:01 +00003444 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3445 // captures the result into a carry flag.
3446 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003447 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003448
Chris Lattner27a6c732007-11-24 07:07:01 +00003449 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003450 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003451
Chris Lattner27a6c732007-11-24 07:07:01 +00003452 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003453 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003454}
3455
Bob Wilson5bafff32009-06-22 23:27:02 +00003456static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3457 SDValue TmpOp0, TmpOp1;
3458 bool Invert = false;
3459 bool Swap = false;
3460 unsigned Opc = 0;
3461
3462 SDValue Op0 = Op.getOperand(0);
3463 SDValue Op1 = Op.getOperand(1);
3464 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003465 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003466 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3467 DebugLoc dl = Op.getDebugLoc();
3468
3469 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3470 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003471 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003472 case ISD::SETUNE:
3473 case ISD::SETNE: Invert = true; // Fallthrough
3474 case ISD::SETOEQ:
3475 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3476 case ISD::SETOLT:
3477 case ISD::SETLT: Swap = true; // Fallthrough
3478 case ISD::SETOGT:
3479 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3480 case ISD::SETOLE:
3481 case ISD::SETLE: Swap = true; // Fallthrough
3482 case ISD::SETOGE:
3483 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3484 case ISD::SETUGE: Swap = true; // Fallthrough
3485 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3486 case ISD::SETUGT: Swap = true; // Fallthrough
3487 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3488 case ISD::SETUEQ: Invert = true; // Fallthrough
3489 case ISD::SETONE:
3490 // Expand this to (OLT | OGT).
3491 TmpOp0 = Op0;
3492 TmpOp1 = Op1;
3493 Opc = ISD::OR;
3494 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3495 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3496 break;
3497 case ISD::SETUO: Invert = true; // Fallthrough
3498 case ISD::SETO:
3499 // Expand this to (OLT | OGE).
3500 TmpOp0 = Op0;
3501 TmpOp1 = Op1;
3502 Opc = ISD::OR;
3503 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3504 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3505 break;
3506 }
3507 } else {
3508 // Integer comparisons.
3509 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003510 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003511 case ISD::SETNE: Invert = true;
3512 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3513 case ISD::SETLT: Swap = true;
3514 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3515 case ISD::SETLE: Swap = true;
3516 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3517 case ISD::SETULT: Swap = true;
3518 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3519 case ISD::SETULE: Swap = true;
3520 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3521 }
3522
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003523 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003524 if (Opc == ARMISD::VCEQ) {
3525
3526 SDValue AndOp;
3527 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3528 AndOp = Op0;
3529 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3530 AndOp = Op1;
3531
3532 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003533 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003534 AndOp = AndOp.getOperand(0);
3535
3536 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3537 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003538 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3539 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003540 Invert = !Invert;
3541 }
3542 }
3543 }
3544
3545 if (Swap)
3546 std::swap(Op0, Op1);
3547
Owen Andersonc24cb352010-11-08 23:21:22 +00003548 // If one of the operands is a constant vector zero, attempt to fold the
3549 // comparison to a specialized compare-against-zero form.
3550 SDValue SingleOp;
3551 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3552 SingleOp = Op0;
3553 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3554 if (Opc == ARMISD::VCGE)
3555 Opc = ARMISD::VCLEZ;
3556 else if (Opc == ARMISD::VCGT)
3557 Opc = ARMISD::VCLTZ;
3558 SingleOp = Op1;
3559 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003560
Owen Andersonc24cb352010-11-08 23:21:22 +00003561 SDValue Result;
3562 if (SingleOp.getNode()) {
3563 switch (Opc) {
3564 case ARMISD::VCEQ:
3565 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3566 case ARMISD::VCGE:
3567 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3568 case ARMISD::VCLEZ:
3569 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3570 case ARMISD::VCGT:
3571 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3572 case ARMISD::VCLTZ:
3573 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3574 default:
3575 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3576 }
3577 } else {
3578 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3579 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003580
3581 if (Invert)
3582 Result = DAG.getNOT(dl, Result, VT);
3583
3584 return Result;
3585}
3586
Bob Wilsond3c42842010-06-14 22:19:57 +00003587/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3588/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003589/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003590static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3591 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003592 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003593 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003594
Bob Wilson827b2102010-06-15 19:05:35 +00003595 // SplatBitSize is set to the smallest size that splats the vector, so a
3596 // zero vector will always have SplatBitSize == 8. However, NEON modified
3597 // immediate instructions others than VMOV do not support the 8-bit encoding
3598 // of a zero vector, and the default encoding of zero is supposed to be the
3599 // 32-bit version.
3600 if (SplatBits == 0)
3601 SplatBitSize = 32;
3602
Bob Wilson5bafff32009-06-22 23:27:02 +00003603 switch (SplatBitSize) {
3604 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003605 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003606 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003607 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003608 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003609 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003610 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003611 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003612 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003613
3614 case 16:
3615 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003616 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003617 if ((SplatBits & ~0xff) == 0) {
3618 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003619 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003620 Imm = SplatBits;
3621 break;
3622 }
3623 if ((SplatBits & ~0xff00) == 0) {
3624 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003625 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003626 Imm = SplatBits >> 8;
3627 break;
3628 }
3629 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003630
3631 case 32:
3632 // NEON's 32-bit VMOV supports splat values where:
3633 // * only one byte is nonzero, or
3634 // * the least significant byte is 0xff and the second byte is nonzero, or
3635 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003636 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003637 if ((SplatBits & ~0xff) == 0) {
3638 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003639 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003640 Imm = SplatBits;
3641 break;
3642 }
3643 if ((SplatBits & ~0xff00) == 0) {
3644 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003645 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003646 Imm = SplatBits >> 8;
3647 break;
3648 }
3649 if ((SplatBits & ~0xff0000) == 0) {
3650 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003651 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003652 Imm = SplatBits >> 16;
3653 break;
3654 }
3655 if ((SplatBits & ~0xff000000) == 0) {
3656 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003657 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003658 Imm = SplatBits >> 24;
3659 break;
3660 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003661
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003662 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3663 if (type == OtherModImm) return SDValue();
3664
Bob Wilson5bafff32009-06-22 23:27:02 +00003665 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003666 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3667 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003668 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003669 Imm = SplatBits >> 8;
3670 SplatBits |= 0xff;
3671 break;
3672 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003673
3674 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003675 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3676 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003677 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003678 Imm = SplatBits >> 16;
3679 SplatBits |= 0xffff;
3680 break;
3681 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003682
3683 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3684 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3685 // VMOV.I32. A (very) minor optimization would be to replicate the value
3686 // and fall through here to test for a valid 64-bit splat. But, then the
3687 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003688 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003689
3690 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003691 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003692 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003693 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003694 uint64_t BitMask = 0xff;
3695 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003696 unsigned ImmMask = 1;
3697 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003698 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003699 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003700 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003701 Imm |= ImmMask;
3702 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003703 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003704 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003705 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003706 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003707 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003708 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003709 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003710 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003711 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003712 break;
3713 }
3714
Bob Wilson1a913ed2010-06-11 21:34:50 +00003715 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003716 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003717 return SDValue();
3718 }
3719
Bob Wilsoncba270d2010-07-13 21:16:48 +00003720 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3721 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003722}
3723
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003724static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3725 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003726 unsigned NumElts = VT.getVectorNumElements();
3727 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003728
3729 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3730 if (M[0] < 0)
3731 return false;
3732
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003733 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003734
3735 // If this is a VEXT shuffle, the immediate value is the index of the first
3736 // element. The other shuffle indices must be the successive elements after
3737 // the first one.
3738 unsigned ExpectedElt = Imm;
3739 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003740 // Increment the expected index. If it wraps around, it may still be
3741 // a VEXT but the source vectors must be swapped.
3742 ExpectedElt += 1;
3743 if (ExpectedElt == NumElts * 2) {
3744 ExpectedElt = 0;
3745 ReverseVEXT = true;
3746 }
3747
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003748 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003749 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003750 return false;
3751 }
3752
3753 // Adjust the index value if the source operands will be swapped.
3754 if (ReverseVEXT)
3755 Imm -= NumElts;
3756
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003757 return true;
3758}
3759
Bob Wilson8bb9e482009-07-26 00:39:34 +00003760/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3761/// instruction with the specified blocksize. (The order of the elements
3762/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003763static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3764 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003765 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3766 "Only possible block sizes for VREV are: 16, 32, 64");
3767
Bob Wilson8bb9e482009-07-26 00:39:34 +00003768 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003769 if (EltSz == 64)
3770 return false;
3771
3772 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003773 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003774 // If the first shuffle index is UNDEF, be optimistic.
3775 if (M[0] < 0)
3776 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003777
3778 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3779 return false;
3780
3781 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003782 if (M[i] < 0) continue; // ignore UNDEF indices
3783 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003784 return false;
3785 }
3786
3787 return true;
3788}
3789
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003790static bool isVTBLMask(const SmallVectorImpl<int> &M, EVT VT) {
3791 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3792 // range, then 0 is placed into the resulting vector. So pretty much any mask
3793 // of 8 elements can work here.
3794 return VT == MVT::v8i8 && M.size() == 8;
3795}
3796
Bob Wilsonc692cb72009-08-21 20:54:19 +00003797static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3798 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003799 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3800 if (EltSz == 64)
3801 return false;
3802
Bob Wilsonc692cb72009-08-21 20:54:19 +00003803 unsigned NumElts = VT.getVectorNumElements();
3804 WhichResult = (M[0] == 0 ? 0 : 1);
3805 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003806 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3807 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003808 return false;
3809 }
3810 return true;
3811}
3812
Bob Wilson324f4f12009-12-03 06:40:55 +00003813/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3814/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3815/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3816static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3817 unsigned &WhichResult) {
3818 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3819 if (EltSz == 64)
3820 return false;
3821
3822 unsigned NumElts = VT.getVectorNumElements();
3823 WhichResult = (M[0] == 0 ? 0 : 1);
3824 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003825 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3826 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003827 return false;
3828 }
3829 return true;
3830}
3831
Bob Wilsonc692cb72009-08-21 20:54:19 +00003832static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3833 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003834 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3835 if (EltSz == 64)
3836 return false;
3837
Bob Wilsonc692cb72009-08-21 20:54:19 +00003838 unsigned NumElts = VT.getVectorNumElements();
3839 WhichResult = (M[0] == 0 ? 0 : 1);
3840 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003841 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003842 if ((unsigned) M[i] != 2 * i + WhichResult)
3843 return false;
3844 }
3845
3846 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003847 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003848 return false;
3849
3850 return true;
3851}
3852
Bob Wilson324f4f12009-12-03 06:40:55 +00003853/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3854/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3855/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3856static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3857 unsigned &WhichResult) {
3858 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3859 if (EltSz == 64)
3860 return false;
3861
3862 unsigned Half = VT.getVectorNumElements() / 2;
3863 WhichResult = (M[0] == 0 ? 0 : 1);
3864 for (unsigned j = 0; j != 2; ++j) {
3865 unsigned Idx = WhichResult;
3866 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003867 int MIdx = M[i + j * Half];
3868 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003869 return false;
3870 Idx += 2;
3871 }
3872 }
3873
3874 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3875 if (VT.is64BitVector() && EltSz == 32)
3876 return false;
3877
3878 return true;
3879}
3880
Bob Wilsonc692cb72009-08-21 20:54:19 +00003881static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3882 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003883 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3884 if (EltSz == 64)
3885 return false;
3886
Bob Wilsonc692cb72009-08-21 20:54:19 +00003887 unsigned NumElts = VT.getVectorNumElements();
3888 WhichResult = (M[0] == 0 ? 0 : 1);
3889 unsigned Idx = WhichResult * NumElts / 2;
3890 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003891 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3892 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003893 return false;
3894 Idx += 1;
3895 }
3896
3897 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003898 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003899 return false;
3900
3901 return true;
3902}
3903
Bob Wilson324f4f12009-12-03 06:40:55 +00003904/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3905/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3906/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3907static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3908 unsigned &WhichResult) {
3909 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3910 if (EltSz == 64)
3911 return false;
3912
3913 unsigned NumElts = VT.getVectorNumElements();
3914 WhichResult = (M[0] == 0 ? 0 : 1);
3915 unsigned Idx = WhichResult * NumElts / 2;
3916 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003917 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3918 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003919 return false;
3920 Idx += 1;
3921 }
3922
3923 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3924 if (VT.is64BitVector() && EltSz == 32)
3925 return false;
3926
3927 return true;
3928}
3929
Dale Johannesenf630c712010-07-29 20:10:08 +00003930// If N is an integer constant that can be moved into a register in one
3931// instruction, return an SDValue of such a constant (will become a MOV
3932// instruction). Otherwise return null.
3933static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3934 const ARMSubtarget *ST, DebugLoc dl) {
3935 uint64_t Val;
3936 if (!isa<ConstantSDNode>(N))
3937 return SDValue();
3938 Val = cast<ConstantSDNode>(N)->getZExtValue();
3939
3940 if (ST->isThumb1Only()) {
3941 if (Val <= 255 || ~Val <= 255)
3942 return DAG.getConstant(Val, MVT::i32);
3943 } else {
3944 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3945 return DAG.getConstant(Val, MVT::i32);
3946 }
3947 return SDValue();
3948}
3949
Bob Wilson5bafff32009-06-22 23:27:02 +00003950// If this is a case we can't handle, return null and let the default
3951// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003952SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3953 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003954 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003955 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003956 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003957
3958 APInt SplatBits, SplatUndef;
3959 unsigned SplatBitSize;
3960 bool HasAnyUndefs;
3961 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003962 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003963 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003964 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003965 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003966 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003967 DAG, VmovVT, VT.is128BitVector(),
3968 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003969 if (Val.getNode()) {
3970 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003971 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003972 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003973
3974 // Try an immediate VMVN.
Eli Friedman8e4d0422011-10-13 22:40:23 +00003975 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003976 Val = isNEONModifiedImm(NegatedImm,
3977 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003978 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003979 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003980 if (Val.getNode()) {
3981 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003982 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003983 }
Evan Chengeaa192a2011-11-15 02:12:34 +00003984
3985 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
3986 if (VT == MVT::v2f32 || VT == MVT::v4f32) {
3987 ConstantFPSDNode *C = cast<ConstantFPSDNode>(Op.getOperand(0));
3988 int ImmVal = ARM_AM::getFP32Imm(C->getValueAPF());
3989 if (ImmVal != -1) {
3990 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
3991 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
3992 }
3993 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003994 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003995 }
3996
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003997 // Scan through the operands to see if only one value is used.
3998 unsigned NumElts = VT.getVectorNumElements();
3999 bool isOnlyLowElement = true;
4000 bool usesOnlyOneValue = true;
4001 bool isConstant = true;
4002 SDValue Value;
4003 for (unsigned i = 0; i < NumElts; ++i) {
4004 SDValue V = Op.getOperand(i);
4005 if (V.getOpcode() == ISD::UNDEF)
4006 continue;
4007 if (i > 0)
4008 isOnlyLowElement = false;
4009 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4010 isConstant = false;
4011
4012 if (!Value.getNode())
4013 Value = V;
4014 else if (V != Value)
4015 usesOnlyOneValue = false;
4016 }
4017
4018 if (!Value.getNode())
4019 return DAG.getUNDEF(VT);
4020
4021 if (isOnlyLowElement)
4022 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4023
Dale Johannesenf630c712010-07-29 20:10:08 +00004024 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4025
Dale Johannesen575cd142010-10-19 20:00:17 +00004026 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4027 // i32 and try again.
4028 if (usesOnlyOneValue && EltSize <= 32) {
4029 if (!isConstant)
4030 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4031 if (VT.getVectorElementType().isFloatingPoint()) {
4032 SmallVector<SDValue, 8> Ops;
4033 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004034 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00004035 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00004036 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
4037 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00004038 Val = LowerBUILD_VECTOR(Val, DAG, ST);
4039 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004040 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004041 }
Dale Johannesen575cd142010-10-19 20:00:17 +00004042 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
4043 if (Val.getNode())
4044 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004045 }
4046
4047 // If all elements are constants and the case above didn't get hit, fall back
4048 // to the default expansion, which will generate a load from the constant
4049 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004050 if (isConstant)
4051 return SDValue();
4052
Bob Wilson11a1dff2011-01-07 21:37:30 +00004053 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4054 if (NumElts >= 4) {
4055 SDValue shuffle = ReconstructShuffle(Op, DAG);
4056 if (shuffle != SDValue())
4057 return shuffle;
4058 }
4059
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004060 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004061 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4062 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004063 if (EltSize >= 32) {
4064 // Do the expansion with floating-point types, since that is what the VFP
4065 // registers are defined to use, and since i64 is not legal.
4066 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4067 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004068 SmallVector<SDValue, 8> Ops;
4069 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004070 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004071 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004072 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004073 }
4074
4075 return SDValue();
4076}
4077
Bob Wilson11a1dff2011-01-07 21:37:30 +00004078// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004079// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004080SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4081 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004082 DebugLoc dl = Op.getDebugLoc();
4083 EVT VT = Op.getValueType();
4084 unsigned NumElts = VT.getVectorNumElements();
4085
4086 SmallVector<SDValue, 2> SourceVecs;
4087 SmallVector<unsigned, 2> MinElts;
4088 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004089
Bob Wilson11a1dff2011-01-07 21:37:30 +00004090 for (unsigned i = 0; i < NumElts; ++i) {
4091 SDValue V = Op.getOperand(i);
4092 if (V.getOpcode() == ISD::UNDEF)
4093 continue;
4094 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4095 // A shuffle can only come from building a vector from various
4096 // elements of other vectors.
4097 return SDValue();
Eli Friedman46995fa2011-10-14 23:58:49 +00004098 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
4099 VT.getVectorElementType()) {
4100 // This code doesn't know how to handle shuffles where the vector
4101 // element types do not match (this happens because type legalization
4102 // promotes the return type of EXTRACT_VECTOR_ELT).
4103 // FIXME: It might be appropriate to extend this code to handle
4104 // mismatched types.
4105 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004106 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004107
Bob Wilson11a1dff2011-01-07 21:37:30 +00004108 // Record this extraction against the appropriate vector if possible...
4109 SDValue SourceVec = V.getOperand(0);
4110 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4111 bool FoundSource = false;
4112 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4113 if (SourceVecs[j] == SourceVec) {
4114 if (MinElts[j] > EltNo)
4115 MinElts[j] = EltNo;
4116 if (MaxElts[j] < EltNo)
4117 MaxElts[j] = EltNo;
4118 FoundSource = true;
4119 break;
4120 }
4121 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004122
Bob Wilson11a1dff2011-01-07 21:37:30 +00004123 // Or record a new source if not...
4124 if (!FoundSource) {
4125 SourceVecs.push_back(SourceVec);
4126 MinElts.push_back(EltNo);
4127 MaxElts.push_back(EltNo);
4128 }
4129 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004130
Bob Wilson11a1dff2011-01-07 21:37:30 +00004131 // Currently only do something sane when at most two source vectors
4132 // involved.
4133 if (SourceVecs.size() > 2)
4134 return SDValue();
4135
4136 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4137 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004138
Bob Wilson11a1dff2011-01-07 21:37:30 +00004139 // This loop extracts the usage patterns of the source vectors
4140 // and prepares appropriate SDValues for a shuffle if possible.
4141 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4142 if (SourceVecs[i].getValueType() == VT) {
4143 // No VEXT necessary
4144 ShuffleSrcs[i] = SourceVecs[i];
4145 VEXTOffsets[i] = 0;
4146 continue;
4147 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4148 // It probably isn't worth padding out a smaller vector just to
4149 // break it down again in a shuffle.
4150 return SDValue();
4151 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004152
Bob Wilson11a1dff2011-01-07 21:37:30 +00004153 // Since only 64-bit and 128-bit vectors are legal on ARM and
4154 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004155 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4156 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004157
Bob Wilson11a1dff2011-01-07 21:37:30 +00004158 if (MaxElts[i] - MinElts[i] >= NumElts) {
4159 // Span too large for a VEXT to cope
4160 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004161 }
4162
Bob Wilson11a1dff2011-01-07 21:37:30 +00004163 if (MinElts[i] >= NumElts) {
4164 // The extraction can just take the second half
4165 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004166 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4167 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004168 DAG.getIntPtrConstant(NumElts));
4169 } else if (MaxElts[i] < NumElts) {
4170 // The extraction can just take the first half
4171 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004172 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4173 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004174 DAG.getIntPtrConstant(0));
4175 } else {
4176 // An actual VEXT is needed
4177 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004178 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4179 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004180 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004181 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4182 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004183 DAG.getIntPtrConstant(NumElts));
4184 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4185 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4186 }
4187 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004188
Bob Wilson11a1dff2011-01-07 21:37:30 +00004189 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004190
Bob Wilson11a1dff2011-01-07 21:37:30 +00004191 for (unsigned i = 0; i < NumElts; ++i) {
4192 SDValue Entry = Op.getOperand(i);
4193 if (Entry.getOpcode() == ISD::UNDEF) {
4194 Mask.push_back(-1);
4195 continue;
4196 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004197
Bob Wilson11a1dff2011-01-07 21:37:30 +00004198 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004199 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4200 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004201 if (ExtractVec == SourceVecs[0]) {
4202 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4203 } else {
4204 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4205 }
4206 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004207
Bob Wilson11a1dff2011-01-07 21:37:30 +00004208 // Final check before we try to produce nonsense...
4209 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004210 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4211 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004212
Bob Wilson11a1dff2011-01-07 21:37:30 +00004213 return SDValue();
4214}
4215
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004216/// isShuffleMaskLegal - Targets can use this to indicate that they only
4217/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4218/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4219/// are assumed to be legal.
4220bool
4221ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4222 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004223 if (VT.getVectorNumElements() == 4 &&
4224 (VT.is128BitVector() || VT.is64BitVector())) {
4225 unsigned PFIndexes[4];
4226 for (unsigned i = 0; i != 4; ++i) {
4227 if (M[i] < 0)
4228 PFIndexes[i] = 8;
4229 else
4230 PFIndexes[i] = M[i];
4231 }
4232
4233 // Compute the index in the perfect shuffle table.
4234 unsigned PFTableIndex =
4235 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4236 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4237 unsigned Cost = (PFEntry >> 30);
4238
4239 if (Cost <= 4)
4240 return true;
4241 }
4242
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004243 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004244 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004245
Bob Wilson53dd2452010-06-07 23:53:38 +00004246 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4247 return (EltSize >= 32 ||
4248 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004249 isVREVMask(M, VT, 64) ||
4250 isVREVMask(M, VT, 32) ||
4251 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004252 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004253 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004254 isVTRNMask(M, VT, WhichResult) ||
4255 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004256 isVZIPMask(M, VT, WhichResult) ||
4257 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4258 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4259 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004260}
4261
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004262/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4263/// the specified operations to build the shuffle.
4264static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4265 SDValue RHS, SelectionDAG &DAG,
4266 DebugLoc dl) {
4267 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4268 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4269 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4270
4271 enum {
4272 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4273 OP_VREV,
4274 OP_VDUP0,
4275 OP_VDUP1,
4276 OP_VDUP2,
4277 OP_VDUP3,
4278 OP_VEXT1,
4279 OP_VEXT2,
4280 OP_VEXT3,
4281 OP_VUZPL, // VUZP, left result
4282 OP_VUZPR, // VUZP, right result
4283 OP_VZIPL, // VZIP, left result
4284 OP_VZIPR, // VZIP, right result
4285 OP_VTRNL, // VTRN, left result
4286 OP_VTRNR // VTRN, right result
4287 };
4288
4289 if (OpNum == OP_COPY) {
4290 if (LHSID == (1*9+2)*9+3) return LHS;
4291 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4292 return RHS;
4293 }
4294
4295 SDValue OpLHS, OpRHS;
4296 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4297 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4298 EVT VT = OpLHS.getValueType();
4299
4300 switch (OpNum) {
4301 default: llvm_unreachable("Unknown shuffle opcode!");
4302 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004303 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004304 if (VT.getVectorElementType() == MVT::i32 ||
4305 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004306 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4307 // vrev <4 x i16> -> VREV32
4308 if (VT.getVectorElementType() == MVT::i16)
4309 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4310 // vrev <4 x i8> -> VREV16
4311 assert(VT.getVectorElementType() == MVT::i8);
4312 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004313 case OP_VDUP0:
4314 case OP_VDUP1:
4315 case OP_VDUP2:
4316 case OP_VDUP3:
4317 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004318 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004319 case OP_VEXT1:
4320 case OP_VEXT2:
4321 case OP_VEXT3:
4322 return DAG.getNode(ARMISD::VEXT, dl, VT,
4323 OpLHS, OpRHS,
4324 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4325 case OP_VUZPL:
4326 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004327 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004328 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4329 case OP_VZIPL:
4330 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004331 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004332 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4333 case OP_VTRNL:
4334 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004335 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4336 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004337 }
4338}
4339
Bill Wendling69a05a72011-03-14 23:02:38 +00004340static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
4341 SmallVectorImpl<int> &ShuffleMask,
4342 SelectionDAG &DAG) {
4343 // Check to see if we can use the VTBL instruction.
4344 SDValue V1 = Op.getOperand(0);
4345 SDValue V2 = Op.getOperand(1);
4346 DebugLoc DL = Op.getDebugLoc();
4347
4348 SmallVector<SDValue, 8> VTBLMask;
4349 for (SmallVectorImpl<int>::iterator
4350 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4351 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4352
4353 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4354 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4355 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4356 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004357
Owen Anderson76706012011-04-05 21:48:57 +00004358 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004359 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4360 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004361}
4362
Bob Wilson5bafff32009-06-22 23:27:02 +00004363static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004364 SDValue V1 = Op.getOperand(0);
4365 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004366 DebugLoc dl = Op.getDebugLoc();
4367 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004368 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004369 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00004370
Bob Wilson28865062009-08-13 02:13:04 +00004371 // Convert shuffles that are directly supported on NEON to target-specific
4372 // DAG nodes, instead of keeping them as shuffles and matching them again
4373 // during code selection. This is more efficient and avoids the possibility
4374 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004375 // FIXME: floating-point vectors should be canonicalized to integer vectors
4376 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004377 SVN->getMask(ShuffleMask);
4378
Bob Wilson53dd2452010-06-07 23:53:38 +00004379 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4380 if (EltSize <= 32) {
4381 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4382 int Lane = SVN->getSplatIndex();
4383 // If this is undef splat, generate it via "just" vdup, if possible.
4384 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004385
Dan Gohman65fd6562011-11-03 21:49:52 +00004386 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson53dd2452010-06-07 23:53:38 +00004387 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4388 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4389 }
Dan Gohman65fd6562011-11-03 21:49:52 +00004390 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
4391 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
4392 // reaches it).
4393 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
4394 !isa<ConstantSDNode>(V1.getOperand(0))) {
4395 bool IsScalarToVector = true;
4396 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
4397 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
4398 IsScalarToVector = false;
4399 break;
4400 }
4401 if (IsScalarToVector)
4402 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4403 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004404 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4405 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004406 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004407
4408 bool ReverseVEXT;
4409 unsigned Imm;
4410 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4411 if (ReverseVEXT)
4412 std::swap(V1, V2);
4413 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4414 DAG.getConstant(Imm, MVT::i32));
4415 }
4416
4417 if (isVREVMask(ShuffleMask, VT, 64))
4418 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4419 if (isVREVMask(ShuffleMask, VT, 32))
4420 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4421 if (isVREVMask(ShuffleMask, VT, 16))
4422 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4423
4424 // Check for Neon shuffles that modify both input vectors in place.
4425 // If both results are used, i.e., if there are two shuffles with the same
4426 // source operands and with masks corresponding to both results of one of
4427 // these operations, DAG memoization will ensure that a single node is
4428 // used for both shuffles.
4429 unsigned WhichResult;
4430 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4431 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4432 V1, V2).getValue(WhichResult);
4433 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4434 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4435 V1, V2).getValue(WhichResult);
4436 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4437 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4438 V1, V2).getValue(WhichResult);
4439
4440 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4441 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4442 V1, V1).getValue(WhichResult);
4443 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4444 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4445 V1, V1).getValue(WhichResult);
4446 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4447 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4448 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004449 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004450
Bob Wilsonc692cb72009-08-21 20:54:19 +00004451 // If the shuffle is not directly supported and it has 4 elements, use
4452 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004453 unsigned NumElts = VT.getVectorNumElements();
4454 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004455 unsigned PFIndexes[4];
4456 for (unsigned i = 0; i != 4; ++i) {
4457 if (ShuffleMask[i] < 0)
4458 PFIndexes[i] = 8;
4459 else
4460 PFIndexes[i] = ShuffleMask[i];
4461 }
4462
4463 // Compute the index in the perfect shuffle table.
4464 unsigned PFTableIndex =
4465 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004466 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4467 unsigned Cost = (PFEntry >> 30);
4468
4469 if (Cost <= 4)
4470 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4471 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004472
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004473 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004474 if (EltSize >= 32) {
4475 // Do the expansion with floating-point types, since that is what the VFP
4476 // registers are defined to use, and since i64 is not legal.
4477 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4478 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004479 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4480 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004481 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004482 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004483 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004484 Ops.push_back(DAG.getUNDEF(EltVT));
4485 else
4486 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4487 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4488 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4489 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004490 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004491 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004492 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004493 }
4494
Bill Wendling69a05a72011-03-14 23:02:38 +00004495 if (VT == MVT::v8i8) {
4496 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4497 if (NewOp.getNode())
4498 return NewOp;
4499 }
4500
Bob Wilson22cac0d2009-08-14 05:16:33 +00004501 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004502}
4503
Eli Friedman5c89cb82011-10-24 23:08:52 +00004504static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
4505 // INSERT_VECTOR_ELT is legal only for immediate indexes.
4506 SDValue Lane = Op.getOperand(2);
4507 if (!isa<ConstantSDNode>(Lane))
4508 return SDValue();
4509
4510 return Op;
4511}
4512
Bob Wilson5bafff32009-06-22 23:27:02 +00004513static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004514 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004515 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004516 if (!isa<ConstantSDNode>(Lane))
4517 return SDValue();
4518
4519 SDValue Vec = Op.getOperand(0);
4520 if (Op.getValueType() == MVT::i32 &&
4521 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4522 DebugLoc dl = Op.getDebugLoc();
4523 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4524 }
4525
4526 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004527}
4528
Bob Wilsona6d65862009-08-03 20:36:38 +00004529static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4530 // The only time a CONCAT_VECTORS operation can have legal types is when
4531 // two 64-bit vectors are concatenated to a 128-bit vector.
4532 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4533 "unexpected CONCAT_VECTORS");
4534 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004535 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004536 SDValue Op0 = Op.getOperand(0);
4537 SDValue Op1 = Op.getOperand(1);
4538 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004539 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004540 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004541 DAG.getIntPtrConstant(0));
4542 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004543 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004544 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004545 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004546 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004547}
4548
Bob Wilson626613d2010-11-23 19:38:38 +00004549/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4550/// element has been zero/sign-extended, depending on the isSigned parameter,
4551/// from an integer type half its size.
4552static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4553 bool isSigned) {
4554 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4555 EVT VT = N->getValueType(0);
4556 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4557 SDNode *BVN = N->getOperand(0).getNode();
4558 if (BVN->getValueType(0) != MVT::v4i32 ||
4559 BVN->getOpcode() != ISD::BUILD_VECTOR)
4560 return false;
4561 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4562 unsigned HiElt = 1 - LoElt;
4563 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4564 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4565 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4566 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4567 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4568 return false;
4569 if (isSigned) {
4570 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4571 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4572 return true;
4573 } else {
4574 if (Hi0->isNullValue() && Hi1->isNullValue())
4575 return true;
4576 }
4577 return false;
4578 }
4579
4580 if (N->getOpcode() != ISD::BUILD_VECTOR)
4581 return false;
4582
4583 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4584 SDNode *Elt = N->getOperand(i).getNode();
4585 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4586 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4587 unsigned HalfSize = EltSize / 2;
4588 if (isSigned) {
Bob Wilson9d45de22011-10-18 18:46:49 +00004589 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004590 return false;
4591 } else {
Bob Wilson9d45de22011-10-18 18:46:49 +00004592 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004593 return false;
4594 }
4595 continue;
4596 }
4597 return false;
4598 }
4599
4600 return true;
4601}
4602
4603/// isSignExtended - Check if a node is a vector value that is sign-extended
4604/// or a constant BUILD_VECTOR with sign-extended elements.
4605static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4606 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4607 return true;
4608 if (isExtendedBUILD_VECTOR(N, DAG, true))
4609 return true;
4610 return false;
4611}
4612
4613/// isZeroExtended - Check if a node is a vector value that is zero-extended
4614/// or a constant BUILD_VECTOR with zero-extended elements.
4615static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4616 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4617 return true;
4618 if (isExtendedBUILD_VECTOR(N, DAG, false))
4619 return true;
4620 return false;
4621}
4622
4623/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4624/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004625static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4626 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4627 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004628 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4629 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4630 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004631 LD->isNonTemporal(), LD->isInvariant(),
4632 LD->getAlignment());
Bob Wilson626613d2010-11-23 19:38:38 +00004633 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4634 // have been legalized as a BITCAST from v4i32.
4635 if (N->getOpcode() == ISD::BITCAST) {
4636 SDNode *BVN = N->getOperand(0).getNode();
4637 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4638 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4639 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4640 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4641 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4642 }
4643 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4644 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4645 EVT VT = N->getValueType(0);
4646 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4647 unsigned NumElts = VT.getVectorNumElements();
4648 MVT TruncVT = MVT::getIntegerVT(EltSize);
4649 SmallVector<SDValue, 8> Ops;
4650 for (unsigned i = 0; i != NumElts; ++i) {
4651 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4652 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004653 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004654 }
4655 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4656 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004657}
4658
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004659static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4660 unsigned Opcode = N->getOpcode();
4661 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4662 SDNode *N0 = N->getOperand(0).getNode();
4663 SDNode *N1 = N->getOperand(1).getNode();
4664 return N0->hasOneUse() && N1->hasOneUse() &&
4665 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4666 }
4667 return false;
4668}
4669
4670static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4671 unsigned Opcode = N->getOpcode();
4672 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4673 SDNode *N0 = N->getOperand(0).getNode();
4674 SDNode *N1 = N->getOperand(1).getNode();
4675 return N0->hasOneUse() && N1->hasOneUse() &&
4676 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4677 }
4678 return false;
4679}
4680
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004681static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4682 // Multiplications are only custom-lowered for 128-bit vectors so that
4683 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4684 EVT VT = Op.getValueType();
4685 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4686 SDNode *N0 = Op.getOperand(0).getNode();
4687 SDNode *N1 = Op.getOperand(1).getNode();
4688 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004689 bool isMLA = false;
4690 bool isN0SExt = isSignExtended(N0, DAG);
4691 bool isN1SExt = isSignExtended(N1, DAG);
4692 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004693 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004694 else {
4695 bool isN0ZExt = isZeroExtended(N0, DAG);
4696 bool isN1ZExt = isZeroExtended(N1, DAG);
4697 if (isN0ZExt && isN1ZExt)
4698 NewOpc = ARMISD::VMULLu;
4699 else if (isN1SExt || isN1ZExt) {
4700 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4701 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4702 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4703 NewOpc = ARMISD::VMULLs;
4704 isMLA = true;
4705 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4706 NewOpc = ARMISD::VMULLu;
4707 isMLA = true;
4708 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4709 std::swap(N0, N1);
4710 NewOpc = ARMISD::VMULLu;
4711 isMLA = true;
4712 }
4713 }
4714
4715 if (!NewOpc) {
4716 if (VT == MVT::v2i64)
4717 // Fall through to expand this. It is not legal.
4718 return SDValue();
4719 else
4720 // Other vector multiplications are legal.
4721 return Op;
4722 }
4723 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004724
4725 // Legalize to a VMULL instruction.
4726 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004727 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004728 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004729 if (!isMLA) {
4730 Op0 = SkipExtension(N0, DAG);
4731 assert(Op0.getValueType().is64BitVector() &&
4732 Op1.getValueType().is64BitVector() &&
4733 "unexpected types for extended operands to VMULL");
4734 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4735 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004736
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004737 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4738 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4739 // vmull q0, d4, d6
4740 // vmlal q0, d5, d6
4741 // is faster than
4742 // vaddl q0, d4, d5
4743 // vmovl q1, d6
4744 // vmul q0, q0, q1
4745 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4746 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4747 EVT Op1VT = Op1.getValueType();
4748 return DAG.getNode(N0->getOpcode(), DL, VT,
4749 DAG.getNode(NewOpc, DL, VT,
4750 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4751 DAG.getNode(NewOpc, DL, VT,
4752 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004753}
4754
Owen Anderson76706012011-04-05 21:48:57 +00004755static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004756LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4757 // Convert to float
4758 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4759 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4760 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4761 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4762 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4763 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4764 // Get reciprocal estimate.
4765 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004766 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004767 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4768 // Because char has a smaller range than uchar, we can actually get away
4769 // without any newton steps. This requires that we use a weird bias
4770 // of 0xb000, however (again, this has been exhaustively tested).
4771 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4772 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4773 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4774 Y = DAG.getConstant(0xb000, MVT::i32);
4775 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4776 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4777 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4778 // Convert back to short.
4779 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4780 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4781 return X;
4782}
4783
Owen Anderson76706012011-04-05 21:48:57 +00004784static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004785LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4786 SDValue N2;
4787 // Convert to float.
4788 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4789 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4790 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4791 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4792 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4793 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004794
Nate Begeman7973f352011-02-11 20:53:29 +00004795 // Use reciprocal estimate and one refinement step.
4796 // float4 recip = vrecpeq_f32(yf);
4797 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004798 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004799 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004800 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004801 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4802 N1, N2);
4803 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4804 // Because short has a smaller range than ushort, we can actually get away
4805 // with only a single newton step. This requires that we use a weird bias
4806 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004807 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004808 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4809 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004810 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004811 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4812 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4813 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4814 // Convert back to integer and return.
4815 // return vmovn_s32(vcvt_s32_f32(result));
4816 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4817 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4818 return N0;
4819}
4820
4821static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4822 EVT VT = Op.getValueType();
4823 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4824 "unexpected type for custom-lowering ISD::SDIV");
4825
4826 DebugLoc dl = Op.getDebugLoc();
4827 SDValue N0 = Op.getOperand(0);
4828 SDValue N1 = Op.getOperand(1);
4829 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004830
Nate Begeman7973f352011-02-11 20:53:29 +00004831 if (VT == MVT::v8i8) {
4832 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4833 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004834
Nate Begeman7973f352011-02-11 20:53:29 +00004835 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4836 DAG.getIntPtrConstant(4));
4837 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004838 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004839 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4840 DAG.getIntPtrConstant(0));
4841 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4842 DAG.getIntPtrConstant(0));
4843
4844 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4845 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4846
4847 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4848 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004849
Nate Begeman7973f352011-02-11 20:53:29 +00004850 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
4851 return N0;
4852 }
4853 return LowerSDIV_v4i16(N0, N1, dl, DAG);
4854}
4855
4856static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
4857 EVT VT = Op.getValueType();
4858 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4859 "unexpected type for custom-lowering ISD::UDIV");
4860
4861 DebugLoc dl = Op.getDebugLoc();
4862 SDValue N0 = Op.getOperand(0);
4863 SDValue N1 = Op.getOperand(1);
4864 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004865
Nate Begeman7973f352011-02-11 20:53:29 +00004866 if (VT == MVT::v8i8) {
4867 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
4868 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004869
Nate Begeman7973f352011-02-11 20:53:29 +00004870 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4871 DAG.getIntPtrConstant(4));
4872 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004873 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004874 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4875 DAG.getIntPtrConstant(0));
4876 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4877 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00004878
Nate Begeman7973f352011-02-11 20:53:29 +00004879 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
4880 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00004881
Nate Begeman7973f352011-02-11 20:53:29 +00004882 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4883 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004884
4885 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00004886 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
4887 N0);
4888 return N0;
4889 }
Owen Anderson76706012011-04-05 21:48:57 +00004890
Nate Begeman7973f352011-02-11 20:53:29 +00004891 // v4i16 sdiv ... Convert to float.
4892 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
4893 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
4894 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
4895 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
4896 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004897 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00004898
4899 // Use reciprocal estimate and two refinement steps.
4900 // float4 recip = vrecpeq_f32(yf);
4901 // recip *= vrecpsq_f32(yf, recip);
4902 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004903 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004904 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00004905 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004906 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004907 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004908 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00004909 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004910 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004911 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004912 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4913 // Simply multiplying by the reciprocal estimate can leave us a few ulps
4914 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
4915 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004916 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00004917 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4918 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4919 N1 = DAG.getConstant(2, MVT::i32);
4920 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4921 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4922 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4923 // Convert back to integer and return.
4924 // return vmovn_u32(vcvt_s32_f32(result));
4925 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4926 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4927 return N0;
4928}
4929
Evan Cheng342e3162011-08-30 01:34:54 +00004930static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
4931 EVT VT = Op.getNode()->getValueType(0);
4932 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
4933
4934 unsigned Opc;
4935 bool ExtraOp = false;
4936 switch (Op.getOpcode()) {
4937 default: assert(0 && "Invalid code");
4938 case ISD::ADDC: Opc = ARMISD::ADDC; break;
4939 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
4940 case ISD::SUBC: Opc = ARMISD::SUBC; break;
4941 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
4942 }
4943
4944 if (!ExtraOp)
4945 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
4946 Op.getOperand(1));
4947 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
4948 Op.getOperand(1), Op.getOperand(2));
4949}
4950
Eli Friedman74bf18c2011-09-15 22:26:18 +00004951static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedman7cc15662011-09-15 22:18:49 +00004952 // Monotonic load/store is legal for all targets
4953 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
4954 return Op;
4955
4956 // Aquire/Release load/store is not legal for targets without a
4957 // dmb or equivalent available.
4958 return SDValue();
4959}
4960
4961
Eli Friedman2bdffe42011-08-31 00:31:29 +00004962static void
Eli Friedman4d3f3292011-08-31 17:52:22 +00004963ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
4964 SelectionDAG &DAG, unsigned NewOp) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00004965 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +00004966 assert (Node->getValueType(0) == MVT::i64 &&
4967 "Only know how to expand i64 atomics");
Eli Friedman2bdffe42011-08-31 00:31:29 +00004968
Eli Friedman4d3f3292011-08-31 17:52:22 +00004969 SmallVector<SDValue, 6> Ops;
4970 Ops.push_back(Node->getOperand(0)); // Chain
4971 Ops.push_back(Node->getOperand(1)); // Ptr
4972 // Low part of Val1
4973 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4974 Node->getOperand(2), DAG.getIntPtrConstant(0)));
4975 // High part of Val1
4976 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4977 Node->getOperand(2), DAG.getIntPtrConstant(1)));
Andrew Trick3af7a672011-09-20 03:06:13 +00004978 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) {
Eli Friedman4d3f3292011-08-31 17:52:22 +00004979 // High part of Val1
4980 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4981 Node->getOperand(3), DAG.getIntPtrConstant(0)));
4982 // High part of Val2
4983 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4984 Node->getOperand(3), DAG.getIntPtrConstant(1)));
4985 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00004986 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
4987 SDValue Result =
Eli Friedman4d3f3292011-08-31 17:52:22 +00004988 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64,
Eli Friedman2bdffe42011-08-31 00:31:29 +00004989 cast<MemSDNode>(Node)->getMemOperand());
Eli Friedman4d3f3292011-08-31 17:52:22 +00004990 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) };
Eli Friedman2bdffe42011-08-31 00:31:29 +00004991 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
4992 Results.push_back(Result.getValue(2));
4993}
4994
Dan Gohmand858e902010-04-17 15:26:15 +00004995SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004996 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004997 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00004998 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00004999 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005000 case ISD::GlobalAddress:
5001 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
5002 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00005003 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00005004 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00005005 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
5006 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005007 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00005008 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00005009 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00005010 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00005011 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00005012 case ISD::SINT_TO_FP:
5013 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
5014 case ISD::FP_TO_SINT:
5015 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005016 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00005017 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00005018 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005019 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00005020 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00005021 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00005022 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
5023 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00005024 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005025 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00005026 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00005027 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00005028 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00005029 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00005030 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00005031 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Duncan Sands28b77e92011-09-06 19:07:46 +00005032 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00005033 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005034 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedman5c89cb82011-10-24 23:08:52 +00005035 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005036 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00005037 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00005038 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005039 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00005040 case ISD::SDIV: return LowerSDIV(Op, DAG);
5041 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00005042 case ISD::ADDC:
5043 case ISD::ADDE:
5044 case ISD::SUBC:
5045 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Eli Friedman7cc15662011-09-15 22:18:49 +00005046 case ISD::ATOMIC_LOAD:
Eli Friedman74bf18c2011-09-15 22:26:18 +00005047 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005048 }
Dan Gohman475871a2008-07-27 21:46:04 +00005049 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00005050}
5051
Duncan Sands1607f052008-12-01 11:39:25 +00005052/// ReplaceNodeResults - Replace the results of node with an illegal result
5053/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00005054void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
5055 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005056 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00005057 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00005058 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00005059 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005060 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00005061 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005062 case ISD::BITCAST:
5063 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005064 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005065 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00005066 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00005067 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005068 break;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005069 case ISD::ATOMIC_LOAD_ADD:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005070 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005071 return;
5072 case ISD::ATOMIC_LOAD_AND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005073 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005074 return;
5075 case ISD::ATOMIC_LOAD_NAND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005076 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005077 return;
5078 case ISD::ATOMIC_LOAD_OR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005079 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005080 return;
5081 case ISD::ATOMIC_LOAD_SUB:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005082 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005083 return;
5084 case ISD::ATOMIC_LOAD_XOR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005085 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005086 return;
5087 case ISD::ATOMIC_SWAP:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005088 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005089 return;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005090 case ISD::ATOMIC_CMP_SWAP:
5091 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG);
5092 return;
Duncan Sands1607f052008-12-01 11:39:25 +00005093 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00005094 if (Res.getNode())
5095 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00005096}
Chris Lattner27a6c732007-11-24 07:07:01 +00005097
Evan Chenga8e29892007-01-19 07:51:42 +00005098//===----------------------------------------------------------------------===//
5099// ARM Scheduler Hooks
5100//===----------------------------------------------------------------------===//
5101
5102MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005103ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
5104 MachineBasicBlock *BB,
5105 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005106 unsigned dest = MI->getOperand(0).getReg();
5107 unsigned ptr = MI->getOperand(1).getReg();
5108 unsigned oldval = MI->getOperand(2).getReg();
5109 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005110 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5111 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005112 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005113
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005114 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5115 unsigned scratch =
Cameron Zwarich141ec632011-05-18 02:29:50 +00005116 MRI.createVirtualRegister(isThumb2 ? ARM::rGPRRegisterClass
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005117 : ARM::GPRRegisterClass);
5118
5119 if (isThumb2) {
Cameron Zwarich141ec632011-05-18 02:29:50 +00005120 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5121 MRI.constrainRegClass(oldval, ARM::rGPRRegisterClass);
5122 MRI.constrainRegClass(newval, ARM::rGPRRegisterClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005123 }
5124
Jim Grosbach5278eb82009-12-11 01:42:04 +00005125 unsigned ldrOpc, strOpc;
5126 switch (Size) {
5127 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005128 case 1:
5129 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00005130 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005131 break;
5132 case 2:
5133 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5134 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5135 break;
5136 case 4:
5137 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5138 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5139 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00005140 }
5141
5142 MachineFunction *MF = BB->getParent();
5143 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5144 MachineFunction::iterator It = BB;
5145 ++It; // insert the new blocks after the current block
5146
5147 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5148 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5149 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5150 MF->insert(It, loop1MBB);
5151 MF->insert(It, loop2MBB);
5152 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005153
5154 // Transfer the remainder of BB and its successor edges to exitMBB.
5155 exitMBB->splice(exitMBB->begin(), BB,
5156 llvm::next(MachineBasicBlock::iterator(MI)),
5157 BB->end());
5158 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005159
5160 // thisMBB:
5161 // ...
5162 // fallthrough --> loop1MBB
5163 BB->addSuccessor(loop1MBB);
5164
5165 // loop1MBB:
5166 // ldrex dest, [ptr]
5167 // cmp dest, oldval
5168 // bne exitMBB
5169 BB = loop1MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005170 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5171 if (ldrOpc == ARM::t2LDREX)
5172 MIB.addImm(0);
5173 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005174 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005175 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005176 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5177 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005178 BB->addSuccessor(loop2MBB);
5179 BB->addSuccessor(exitMBB);
5180
5181 // loop2MBB:
5182 // strex scratch, newval, [ptr]
5183 // cmp scratch, #0
5184 // bne loop1MBB
5185 BB = loop2MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005186 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr);
5187 if (strOpc == ARM::t2STREX)
5188 MIB.addImm(0);
5189 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005190 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005191 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005192 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5193 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005194 BB->addSuccessor(loop1MBB);
5195 BB->addSuccessor(exitMBB);
5196
5197 // exitMBB:
5198 // ...
5199 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005200
Dan Gohman14152b42010-07-06 20:24:04 +00005201 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005202
Jim Grosbach5278eb82009-12-11 01:42:04 +00005203 return BB;
5204}
5205
5206MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005207ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5208 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005209 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5210 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5211
5212 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005213 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005214 MachineFunction::iterator It = BB;
5215 ++It;
5216
5217 unsigned dest = MI->getOperand(0).getReg();
5218 unsigned ptr = MI->getOperand(1).getReg();
5219 unsigned incr = MI->getOperand(2).getReg();
5220 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005221 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005222
5223 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5224 if (isThumb2) {
5225 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5226 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5227 }
5228
Jim Grosbachc3c23542009-12-14 04:22:04 +00005229 unsigned ldrOpc, strOpc;
5230 switch (Size) {
5231 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005232 case 1:
5233 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005234 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005235 break;
5236 case 2:
5237 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5238 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5239 break;
5240 case 4:
5241 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5242 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5243 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005244 }
5245
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005246 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5247 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5248 MF->insert(It, loopMBB);
5249 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005250
5251 // Transfer the remainder of BB and its successor edges to exitMBB.
5252 exitMBB->splice(exitMBB->begin(), BB,
5253 llvm::next(MachineBasicBlock::iterator(MI)),
5254 BB->end());
5255 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005256
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005257 TargetRegisterClass *TRC =
5258 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5259 unsigned scratch = MRI.createVirtualRegister(TRC);
5260 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005261
5262 // thisMBB:
5263 // ...
5264 // fallthrough --> loopMBB
5265 BB->addSuccessor(loopMBB);
5266
5267 // loopMBB:
5268 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005269 // <binop> scratch2, dest, incr
5270 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005271 // cmp scratch, #0
5272 // bne- loopMBB
5273 // fallthrough --> exitMBB
5274 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005275 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5276 if (ldrOpc == ARM::t2LDREX)
5277 MIB.addImm(0);
5278 AddDefaultPred(MIB);
Jim Grosbachc67b5562009-12-15 00:12:35 +00005279 if (BinOpcode) {
5280 // operand order needs to go the other way for NAND
5281 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5282 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5283 addReg(incr).addReg(dest)).addReg(0);
5284 else
5285 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5286 addReg(dest).addReg(incr)).addReg(0);
5287 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005288
Jim Grosbachb6aed502011-09-09 18:37:27 +00005289 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5290 if (strOpc == ARM::t2STREX)
5291 MIB.addImm(0);
5292 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005293 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005294 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005295 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5296 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005297
5298 BB->addSuccessor(loopMBB);
5299 BB->addSuccessor(exitMBB);
5300
5301 // exitMBB:
5302 // ...
5303 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005304
Dan Gohman14152b42010-07-06 20:24:04 +00005305 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005306
Jim Grosbachc3c23542009-12-14 04:22:04 +00005307 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005308}
5309
Jim Grosbachf7da8822011-04-26 19:44:18 +00005310MachineBasicBlock *
5311ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5312 MachineBasicBlock *BB,
5313 unsigned Size,
5314 bool signExtend,
5315 ARMCC::CondCodes Cond) const {
5316 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5317
5318 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5319 MachineFunction *MF = BB->getParent();
5320 MachineFunction::iterator It = BB;
5321 ++It;
5322
5323 unsigned dest = MI->getOperand(0).getReg();
5324 unsigned ptr = MI->getOperand(1).getReg();
5325 unsigned incr = MI->getOperand(2).getReg();
5326 unsigned oldval = dest;
5327 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005328 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005329
5330 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5331 if (isThumb2) {
5332 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5333 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5334 }
5335
Jim Grosbachf7da8822011-04-26 19:44:18 +00005336 unsigned ldrOpc, strOpc, extendOpc;
5337 switch (Size) {
5338 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5339 case 1:
5340 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5341 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005342 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005343 break;
5344 case 2:
5345 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5346 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005347 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005348 break;
5349 case 4:
5350 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5351 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5352 extendOpc = 0;
5353 break;
5354 }
5355
5356 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5357 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5358 MF->insert(It, loopMBB);
5359 MF->insert(It, exitMBB);
5360
5361 // Transfer the remainder of BB and its successor edges to exitMBB.
5362 exitMBB->splice(exitMBB->begin(), BB,
5363 llvm::next(MachineBasicBlock::iterator(MI)),
5364 BB->end());
5365 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5366
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005367 TargetRegisterClass *TRC =
5368 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5369 unsigned scratch = MRI.createVirtualRegister(TRC);
5370 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005371
5372 // thisMBB:
5373 // ...
5374 // fallthrough --> loopMBB
5375 BB->addSuccessor(loopMBB);
5376
5377 // loopMBB:
5378 // ldrex dest, ptr
5379 // (sign extend dest, if required)
5380 // cmp dest, incr
5381 // cmov.cond scratch2, dest, incr
5382 // strex scratch, scratch2, ptr
5383 // cmp scratch, #0
5384 // bne- loopMBB
5385 // fallthrough --> exitMBB
5386 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005387 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5388 if (ldrOpc == ARM::t2LDREX)
5389 MIB.addImm(0);
5390 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005391
5392 // Sign extend the value, if necessary.
5393 if (signExtend && extendOpc) {
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005394 oldval = MRI.createVirtualRegister(ARM::GPRRegisterClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005395 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5396 .addReg(dest)
5397 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005398 }
5399
5400 // Build compare and cmov instructions.
5401 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5402 .addReg(oldval).addReg(incr));
5403 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5404 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5405
Jim Grosbachb6aed502011-09-09 18:37:27 +00005406 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5407 if (strOpc == ARM::t2STREX)
5408 MIB.addImm(0);
5409 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005410 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5411 .addReg(scratch).addImm(0));
5412 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5413 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5414
5415 BB->addSuccessor(loopMBB);
5416 BB->addSuccessor(exitMBB);
5417
5418 // exitMBB:
5419 // ...
5420 BB = exitMBB;
5421
5422 MI->eraseFromParent(); // The instruction is gone now.
5423
5424 return BB;
5425}
5426
Eli Friedman2bdffe42011-08-31 00:31:29 +00005427MachineBasicBlock *
5428ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
5429 unsigned Op1, unsigned Op2,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005430 bool NeedsCarry, bool IsCmpxchg) const {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005431 // This also handles ATOMIC_SWAP, indicated by Op1==0.
5432 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5433
5434 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5435 MachineFunction *MF = BB->getParent();
5436 MachineFunction::iterator It = BB;
5437 ++It;
5438
5439 unsigned destlo = MI->getOperand(0).getReg();
5440 unsigned desthi = MI->getOperand(1).getReg();
5441 unsigned ptr = MI->getOperand(2).getReg();
5442 unsigned vallo = MI->getOperand(3).getReg();
5443 unsigned valhi = MI->getOperand(4).getReg();
5444 DebugLoc dl = MI->getDebugLoc();
5445 bool isThumb2 = Subtarget->isThumb2();
5446
5447 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5448 if (isThumb2) {
5449 MRI.constrainRegClass(destlo, ARM::rGPRRegisterClass);
5450 MRI.constrainRegClass(desthi, ARM::rGPRRegisterClass);
5451 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5452 }
5453
5454 unsigned ldrOpc = isThumb2 ? ARM::t2LDREXD : ARM::LDREXD;
5455 unsigned strOpc = isThumb2 ? ARM::t2STREXD : ARM::STREXD;
5456
5457 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman7df496d2011-09-01 22:27:41 +00005458 MachineBasicBlock *contBB = 0, *cont2BB = 0;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005459 if (IsCmpxchg) {
5460 contBB = MF->CreateMachineBasicBlock(LLVM_BB);
5461 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB);
5462 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005463 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5464 MF->insert(It, loopMBB);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005465 if (IsCmpxchg) {
5466 MF->insert(It, contBB);
5467 MF->insert(It, cont2BB);
5468 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005469 MF->insert(It, exitMBB);
5470
5471 // Transfer the remainder of BB and its successor edges to exitMBB.
5472 exitMBB->splice(exitMBB->begin(), BB,
5473 llvm::next(MachineBasicBlock::iterator(MI)),
5474 BB->end());
5475 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5476
5477 TargetRegisterClass *TRC =
5478 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5479 unsigned storesuccess = MRI.createVirtualRegister(TRC);
5480
5481 // thisMBB:
5482 // ...
5483 // fallthrough --> loopMBB
5484 BB->addSuccessor(loopMBB);
5485
5486 // loopMBB:
5487 // ldrexd r2, r3, ptr
5488 // <binopa> r0, r2, incr
5489 // <binopb> r1, r3, incr
5490 // strexd storesuccess, r0, r1, ptr
5491 // cmp storesuccess, #0
5492 // bne- loopMBB
5493 // fallthrough --> exitMBB
5494 //
5495 // Note that the registers are explicitly specified because there is not any
5496 // way to force the register allocator to allocate a register pair.
5497 //
Andrew Trick3af7a672011-09-20 03:06:13 +00005498 // FIXME: The hardcoded registers are not necessary for Thumb2, but we
Eli Friedman2bdffe42011-08-31 00:31:29 +00005499 // need to properly enforce the restriction that the two output registers
5500 // for ldrexd must be different.
5501 BB = loopMBB;
5502 // Load
5503 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc))
5504 .addReg(ARM::R2, RegState::Define)
5505 .addReg(ARM::R3, RegState::Define).addReg(ptr));
5506 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
5507 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo).addReg(ARM::R2);
5508 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi).addReg(ARM::R3);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005509
5510 if (IsCmpxchg) {
5511 // Add early exit
5512 for (unsigned i = 0; i < 2; i++) {
5513 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr :
5514 ARM::CMPrr))
5515 .addReg(i == 0 ? destlo : desthi)
5516 .addReg(i == 0 ? vallo : valhi));
5517 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5518 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5519 BB->addSuccessor(exitMBB);
5520 BB->addSuccessor(i == 0 ? contBB : cont2BB);
5521 BB = (i == 0 ? contBB : cont2BB);
5522 }
5523
5524 // Copy to physregs for strexd
5525 unsigned setlo = MI->getOperand(5).getReg();
5526 unsigned sethi = MI->getOperand(6).getReg();
5527 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(setlo);
5528 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(sethi);
5529 } else if (Op1) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005530 // Perform binary operation
5531 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), ARM::R0)
5532 .addReg(destlo).addReg(vallo))
5533 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
5534 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), ARM::R1)
5535 .addReg(desthi).addReg(valhi)).addReg(0);
5536 } else {
5537 // Copy to physregs for strexd
5538 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(vallo);
5539 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(valhi);
5540 }
5541
5542 // Store
5543 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), storesuccess)
5544 .addReg(ARM::R0).addReg(ARM::R1).addReg(ptr));
5545 // Cmp+jump
5546 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5547 .addReg(storesuccess).addImm(0));
5548 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5549 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5550
5551 BB->addSuccessor(loopMBB);
5552 BB->addSuccessor(exitMBB);
5553
5554 // exitMBB:
5555 // ...
5556 BB = exitMBB;
5557
5558 MI->eraseFromParent(); // The instruction is gone now.
5559
5560 return BB;
5561}
5562
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005563/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
5564/// registers the function context.
5565void ARMTargetLowering::
5566SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
5567 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005568 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5569 DebugLoc dl = MI->getDebugLoc();
5570 MachineFunction *MF = MBB->getParent();
5571 MachineRegisterInfo *MRI = &MF->getRegInfo();
5572 MachineConstantPool *MCP = MF->getConstantPool();
5573 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5574 const Function *F = MF->getFunction();
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005575
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005576 bool isThumb = Subtarget->isThumb();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005577 bool isThumb2 = Subtarget->isThumb2();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005578
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005579 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005580 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005581 ARMConstantPoolValue *CPV =
5582 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
5583 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
5584
5585 const TargetRegisterClass *TRC =
5586 isThumb ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5587
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005588 // Grab constant pool and fixed stack memory operands.
5589 MachineMemOperand *CPMMO =
5590 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
5591 MachineMemOperand::MOLoad, 4, 4);
5592
5593 MachineMemOperand *FIMMOSt =
5594 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
5595 MachineMemOperand::MOStore, 4, 4);
5596
5597 // Load the address of the dispatch MBB into the jump buffer.
5598 if (isThumb2) {
5599 // Incoming value: jbuf
5600 // ldr.n r5, LCPI1_1
5601 // orr r5, r5, #1
5602 // add r5, pc
5603 // str r5, [$jbuf, #+4] ; &jbuf[1]
5604 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5605 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
5606 .addConstantPoolIndex(CPI)
5607 .addMemOperand(CPMMO));
5608 // Set the low bit because of thumb mode.
5609 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5610 AddDefaultCC(
5611 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
5612 .addReg(NewVReg1, RegState::Kill)
5613 .addImm(0x01)));
5614 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5615 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
5616 .addReg(NewVReg2, RegState::Kill)
5617 .addImm(PCLabelId);
5618 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
5619 .addReg(NewVReg3, RegState::Kill)
5620 .addFrameIndex(FI)
5621 .addImm(36) // &jbuf[1] :: pc
5622 .addMemOperand(FIMMOSt));
5623 } else if (isThumb) {
5624 // Incoming value: jbuf
5625 // ldr.n r1, LCPI1_4
5626 // add r1, pc
5627 // mov r2, #1
5628 // orrs r1, r2
5629 // add r2, $jbuf, #+4 ; &jbuf[1]
5630 // str r1, [r2]
5631 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5632 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
5633 .addConstantPoolIndex(CPI)
5634 .addMemOperand(CPMMO));
5635 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5636 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
5637 .addReg(NewVReg1, RegState::Kill)
5638 .addImm(PCLabelId);
5639 // Set the low bit because of thumb mode.
5640 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5641 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
5642 .addReg(ARM::CPSR, RegState::Define)
5643 .addImm(1));
5644 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5645 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
5646 .addReg(ARM::CPSR, RegState::Define)
5647 .addReg(NewVReg2, RegState::Kill)
5648 .addReg(NewVReg3, RegState::Kill));
5649 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5650 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
5651 .addFrameIndex(FI)
5652 .addImm(36)); // &jbuf[1] :: pc
5653 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
5654 .addReg(NewVReg4, RegState::Kill)
5655 .addReg(NewVReg5, RegState::Kill)
5656 .addImm(0)
5657 .addMemOperand(FIMMOSt));
5658 } else {
5659 // Incoming value: jbuf
5660 // ldr r1, LCPI1_1
5661 // add r1, pc, r1
5662 // str r1, [$jbuf, #+4] ; &jbuf[1]
5663 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5664 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
5665 .addConstantPoolIndex(CPI)
5666 .addImm(0)
5667 .addMemOperand(CPMMO));
5668 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5669 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
5670 .addReg(NewVReg1, RegState::Kill)
5671 .addImm(PCLabelId));
5672 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
5673 .addReg(NewVReg2, RegState::Kill)
5674 .addFrameIndex(FI)
5675 .addImm(36) // &jbuf[1] :: pc
5676 .addMemOperand(FIMMOSt));
5677 }
5678}
5679
5680MachineBasicBlock *ARMTargetLowering::
5681EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
5682 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5683 DebugLoc dl = MI->getDebugLoc();
5684 MachineFunction *MF = MBB->getParent();
5685 MachineRegisterInfo *MRI = &MF->getRegInfo();
5686 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5687 MachineFrameInfo *MFI = MF->getFrameInfo();
5688 int FI = MFI->getFunctionContextIndex();
5689
5690 const TargetRegisterClass *TRC =
5691 Subtarget->isThumb() ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5692
Bill Wendling04f15b42011-10-06 21:29:56 +00005693 // Get a mapping of the call site numbers to all of the landing pads they're
5694 // associated with.
Bill Wendling2a850152011-10-05 00:02:33 +00005695 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
5696 unsigned MaxCSNum = 0;
5697 MachineModuleInfo &MMI = MF->getMMI();
5698 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E; ++BB) {
5699 if (!BB->isLandingPad()) continue;
5700
5701 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
5702 // pad.
5703 for (MachineBasicBlock::iterator
5704 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
5705 if (!II->isEHLabel()) continue;
5706
5707 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendling5cbef192011-10-05 23:28:57 +00005708 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling2a850152011-10-05 00:02:33 +00005709
Bill Wendling5cbef192011-10-05 23:28:57 +00005710 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
5711 for (SmallVectorImpl<unsigned>::iterator
5712 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
5713 CSI != CSE; ++CSI) {
5714 CallSiteNumToLPad[*CSI].push_back(BB);
5715 MaxCSNum = std::max(MaxCSNum, *CSI);
5716 }
Bill Wendling2a850152011-10-05 00:02:33 +00005717 break;
5718 }
5719 }
5720
5721 // Get an ordered list of the machine basic blocks for the jump table.
5722 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling2acf6382011-10-07 23:18:02 +00005723 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling2a850152011-10-05 00:02:33 +00005724 LPadList.reserve(CallSiteNumToLPad.size());
5725 for (unsigned I = 1; I <= MaxCSNum; ++I) {
5726 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
5727 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00005728 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling2a850152011-10-05 00:02:33 +00005729 LPadList.push_back(*II);
Bill Wendling2acf6382011-10-07 23:18:02 +00005730 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
5731 }
Bill Wendling2a850152011-10-05 00:02:33 +00005732 }
5733
Bill Wendling5cbef192011-10-05 23:28:57 +00005734 assert(!LPadList.empty() &&
5735 "No landing pad destinations for the dispatch jump table!");
5736
Bill Wendling04f15b42011-10-06 21:29:56 +00005737 // Create the jump table and associated information.
Bill Wendling2a850152011-10-05 00:02:33 +00005738 MachineJumpTableInfo *JTI =
5739 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
5740 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
5741 unsigned UId = AFI->createJumpTableUId();
5742
Bill Wendling04f15b42011-10-06 21:29:56 +00005743 // Create the MBBs for the dispatch code.
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005744
5745 // Shove the dispatch's address into the return slot in the function context.
5746 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
5747 DispatchBB->setIsLandingPad();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005748
Bill Wendlingbb734682011-10-05 00:39:32 +00005749 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Bill Wendling083a8eb2011-10-06 23:37:36 +00005750 BuildMI(TrapBB, dl, TII->get(Subtarget->isThumb() ? ARM::tTRAP : ARM::TRAP));
Bill Wendlingbb734682011-10-05 00:39:32 +00005751 DispatchBB->addSuccessor(TrapBB);
5752
5753 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
5754 DispatchBB->addSuccessor(DispContBB);
Bill Wendling2a850152011-10-05 00:02:33 +00005755
Bill Wendlinga48ed4f2011-10-17 21:32:56 +00005756 // Insert and MBBs.
Bill Wendling930193c2011-10-06 00:53:33 +00005757 MF->insert(MF->end(), DispatchBB);
5758 MF->insert(MF->end(), DispContBB);
5759 MF->insert(MF->end(), TrapBB);
Bill Wendling930193c2011-10-06 00:53:33 +00005760
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005761 // Insert code into the entry block that creates and registers the function
5762 // context.
5763 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
5764
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005765 MachineMemOperand *FIMMOLd =
Bill Wendling04f15b42011-10-06 21:29:56 +00005766 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendling083a8eb2011-10-06 23:37:36 +00005767 MachineMemOperand::MOLoad |
5768 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling930193c2011-10-06 00:53:33 +00005769
Bob Wilsoneaab6ef2011-11-16 07:11:57 +00005770 BuildMI(DispatchBB, dl, TII->get(ARM::eh_sjlj_dispatchsetup));
5771
Bill Wendling952cb502011-10-18 22:49:07 +00005772 unsigned NumLPads = LPadList.size();
Bill Wendling95ce2e92011-10-06 22:53:00 +00005773 if (Subtarget->isThumb2()) {
5774 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5775 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
5776 .addFrameIndex(FI)
5777 .addImm(4)
5778 .addMemOperand(FIMMOLd));
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005779
Bill Wendling952cb502011-10-18 22:49:07 +00005780 if (NumLPads < 256) {
5781 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
5782 .addReg(NewVReg1)
5783 .addImm(LPadList.size()));
5784 } else {
5785 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5786 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00005787 .addImm(NumLPads & 0xFFFF));
5788
5789 unsigned VReg2 = VReg1;
5790 if ((NumLPads & 0xFFFF0000) != 0) {
5791 VReg2 = MRI->createVirtualRegister(TRC);
5792 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
5793 .addReg(VReg1)
5794 .addImm(NumLPads >> 16));
5795 }
5796
Bill Wendling952cb502011-10-18 22:49:07 +00005797 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
5798 .addReg(NewVReg1)
5799 .addReg(VReg2));
5800 }
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005801
Bill Wendling95ce2e92011-10-06 22:53:00 +00005802 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
5803 .addMBB(TrapBB)
5804 .addImm(ARMCC::HI)
5805 .addReg(ARM::CPSR);
Bill Wendlingbb734682011-10-05 00:39:32 +00005806
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005807 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5808 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005809 .addJumpTableIndex(MJTI)
5810 .addImm(UId));
Bill Wendling2a850152011-10-05 00:02:33 +00005811
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005812 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005813 AddDefaultCC(
5814 AddDefaultPred(
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005815 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
5816 .addReg(NewVReg3, RegState::Kill)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005817 .addReg(NewVReg1)
5818 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
5819
5820 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005821 .addReg(NewVReg4, RegState::Kill)
Bill Wendling2a850152011-10-05 00:02:33 +00005822 .addReg(NewVReg1)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005823 .addJumpTableIndex(MJTI)
5824 .addImm(UId);
5825 } else if (Subtarget->isThumb()) {
Bill Wendling083a8eb2011-10-06 23:37:36 +00005826 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5827 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
5828 .addFrameIndex(FI)
5829 .addImm(1)
5830 .addMemOperand(FIMMOLd));
Bill Wendlingf1083d42011-10-07 22:08:37 +00005831
Bill Wendlinga5871dc2011-10-18 23:11:05 +00005832 if (NumLPads < 256) {
5833 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
5834 .addReg(NewVReg1)
5835 .addImm(NumLPads));
5836 } else {
5837 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling922ad782011-10-19 09:24:02 +00005838 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
5839 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
5840
5841 // MachineConstantPool wants an explicit alignment.
5842 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
5843 if (Align == 0)
5844 Align = getTargetData()->getTypeAllocSize(C->getType());
5845 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendlinga5871dc2011-10-18 23:11:05 +00005846
5847 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5848 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
5849 .addReg(VReg1, RegState::Define)
5850 .addConstantPoolIndex(Idx));
5851 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
5852 .addReg(NewVReg1)
5853 .addReg(VReg1));
5854 }
5855
Bill Wendling083a8eb2011-10-06 23:37:36 +00005856 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
5857 .addMBB(TrapBB)
5858 .addImm(ARMCC::HI)
5859 .addReg(ARM::CPSR);
5860
5861 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5862 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
5863 .addReg(ARM::CPSR, RegState::Define)
5864 .addReg(NewVReg1)
5865 .addImm(2));
5866
5867 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling217f0e92011-10-06 23:41:14 +00005868 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendling083a8eb2011-10-06 23:37:36 +00005869 .addJumpTableIndex(MJTI)
5870 .addImm(UId));
5871
5872 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5873 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
5874 .addReg(ARM::CPSR, RegState::Define)
5875 .addReg(NewVReg2, RegState::Kill)
5876 .addReg(NewVReg3));
5877
5878 MachineMemOperand *JTMMOLd =
5879 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
5880 MachineMemOperand::MOLoad, 4, 4);
5881
5882 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5883 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
5884 .addReg(NewVReg4, RegState::Kill)
5885 .addImm(0)
5886 .addMemOperand(JTMMOLd));
5887
5888 unsigned NewVReg6 = MRI->createVirtualRegister(TRC);
5889 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
5890 .addReg(ARM::CPSR, RegState::Define)
5891 .addReg(NewVReg5, RegState::Kill)
5892 .addReg(NewVReg3));
5893
5894 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
5895 .addReg(NewVReg6, RegState::Kill)
5896 .addJumpTableIndex(MJTI)
5897 .addImm(UId);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005898 } else {
5899 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5900 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
5901 .addFrameIndex(FI)
5902 .addImm(4)
5903 .addMemOperand(FIMMOLd));
Bill Wendling564392b2011-10-18 22:11:18 +00005904
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005905 if (NumLPads < 256) {
5906 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
5907 .addReg(NewVReg1)
5908 .addImm(NumLPads));
Bill Wendling922ad782011-10-19 09:24:02 +00005909 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005910 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5911 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00005912 .addImm(NumLPads & 0xFFFF));
5913
5914 unsigned VReg2 = VReg1;
5915 if ((NumLPads & 0xFFFF0000) != 0) {
5916 VReg2 = MRI->createVirtualRegister(TRC);
5917 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
5918 .addReg(VReg1)
5919 .addImm(NumLPads >> 16));
5920 }
5921
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005922 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
5923 .addReg(NewVReg1)
5924 .addReg(VReg2));
Bill Wendling922ad782011-10-19 09:24:02 +00005925 } else {
5926 MachineConstantPool *ConstantPool = MF->getConstantPool();
5927 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
5928 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
5929
5930 // MachineConstantPool wants an explicit alignment.
5931 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
5932 if (Align == 0)
5933 Align = getTargetData()->getTypeAllocSize(C->getType());
5934 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
5935
5936 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5937 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
5938 .addReg(VReg1, RegState::Define)
Bill Wendling767f8be2011-10-20 20:37:11 +00005939 .addConstantPoolIndex(Idx)
5940 .addImm(0));
Bill Wendling922ad782011-10-19 09:24:02 +00005941 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
5942 .addReg(NewVReg1)
5943 .addReg(VReg1, RegState::Kill));
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005944 }
5945
Bill Wendling95ce2e92011-10-06 22:53:00 +00005946 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
5947 .addMBB(TrapBB)
5948 .addImm(ARMCC::HI)
5949 .addReg(ARM::CPSR);
Bill Wendling2a850152011-10-05 00:02:33 +00005950
Bill Wendling564392b2011-10-18 22:11:18 +00005951 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005952 AddDefaultCC(
Bill Wendling564392b2011-10-18 22:11:18 +00005953 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005954 .addReg(NewVReg1)
5955 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling564392b2011-10-18 22:11:18 +00005956 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5957 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005958 .addJumpTableIndex(MJTI)
5959 .addImm(UId));
5960
5961 MachineMemOperand *JTMMOLd =
5962 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
5963 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling564392b2011-10-18 22:11:18 +00005964 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005965 AddDefaultPred(
Bill Wendling564392b2011-10-18 22:11:18 +00005966 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
5967 .addReg(NewVReg3, RegState::Kill)
5968 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005969 .addImm(0)
5970 .addMemOperand(JTMMOLd));
5971
5972 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
Bill Wendling564392b2011-10-18 22:11:18 +00005973 .addReg(NewVReg5, RegState::Kill)
5974 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005975 .addJumpTableIndex(MJTI)
5976 .addImm(UId);
5977 }
Bill Wendling2a850152011-10-05 00:02:33 +00005978
Bill Wendlingbb734682011-10-05 00:39:32 +00005979 // Add the jump table entries as successors to the MBB.
Bill Wendling2acf6382011-10-07 23:18:02 +00005980 MachineBasicBlock *PrevMBB = 0;
Bill Wendlingbb734682011-10-05 00:39:32 +00005981 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00005982 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
5983 MachineBasicBlock *CurMBB = *I;
5984 if (PrevMBB != CurMBB)
5985 DispContBB->addSuccessor(CurMBB);
5986 PrevMBB = CurMBB;
5987 }
5988
Bill Wendling24bb9252011-10-17 05:25:09 +00005989 // N.B. the order the invoke BBs are processed in doesn't matter here.
Bill Wendling969c9ef2011-10-14 23:34:37 +00005990 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
5991 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
5992 const unsigned *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendlingf7b02072011-10-18 18:30:49 +00005993 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling2acf6382011-10-07 23:18:02 +00005994 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
5995 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
5996 MachineBasicBlock *BB = *I;
Bill Wendling969c9ef2011-10-14 23:34:37 +00005997
5998 // Remove the landing pad successor from the invoke block and replace it
5999 // with the new dispatch block.
Bill Wendlingde39d862011-10-26 07:16:18 +00006000 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6001 BB->succ_end());
6002 while (!Successors.empty()) {
6003 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling2acf6382011-10-07 23:18:02 +00006004 if (SMBB->isLandingPad()) {
6005 BB->removeSuccessor(SMBB);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006006 MBBLPads.push_back(SMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006007 }
6008 }
6009
6010 BB->addSuccessor(DispatchBB);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006011
6012 // Find the invoke call and mark all of the callee-saved registers as
6013 // 'implicit defined' so that they're spilled. This prevents code from
6014 // moving instructions to before the EH block, where they will never be
6015 // executed.
6016 for (MachineBasicBlock::reverse_iterator
6017 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
6018 if (!II->getDesc().isCall()) continue;
6019
6020 DenseMap<unsigned, bool> DefRegs;
6021 for (MachineInstr::mop_iterator
6022 OI = II->operands_begin(), OE = II->operands_end();
6023 OI != OE; ++OI) {
6024 if (!OI->isReg()) continue;
6025 DefRegs[OI->getReg()] = true;
6026 }
6027
6028 MachineInstrBuilder MIB(&*II);
6029
Bill Wendling5d798592011-10-14 23:55:44 +00006030 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006031 unsigned Reg = SavedRegs[i];
6032 if (Subtarget->isThumb2() &&
6033 !ARM::tGPRRegisterClass->contains(Reg) &&
6034 !ARM::hGPRRegisterClass->contains(Reg))
6035 continue;
6036 else if (Subtarget->isThumb1Only() &&
6037 !ARM::tGPRRegisterClass->contains(Reg))
6038 continue;
6039 else if (!Subtarget->isThumb() &&
6040 !ARM::GPRRegisterClass->contains(Reg))
6041 continue;
6042 if (!DefRegs[Reg])
6043 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling5d798592011-10-14 23:55:44 +00006044 }
Bill Wendling969c9ef2011-10-14 23:34:37 +00006045
6046 break;
6047 }
Bill Wendling2acf6382011-10-07 23:18:02 +00006048 }
Bill Wendlingbb734682011-10-05 00:39:32 +00006049
Bill Wendlingf7b02072011-10-18 18:30:49 +00006050 // Mark all former landing pads as non-landing pads. The dispatch is the only
6051 // landing pad now.
6052 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6053 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6054 (*I)->setIsLandingPad(false);
6055
Bill Wendlingbb734682011-10-05 00:39:32 +00006056 // The instruction is gone now.
6057 MI->eraseFromParent();
6058
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006059 return MBB;
6060}
6061
Evan Cheng218977b2010-07-13 19:27:42 +00006062static
6063MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6064 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6065 E = MBB->succ_end(); I != E; ++I)
6066 if (*I != Succ)
6067 return *I;
6068 llvm_unreachable("Expecting a BB with two successors!");
6069}
6070
Jim Grosbache801dc42009-12-12 01:40:06 +00006071MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006072ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00006073 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006074 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00006075 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006076 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00006077 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00006078 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00006079 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00006080 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00006081 }
Jim Grosbachee2c2a42011-09-16 21:55:56 +00006082 // The Thumb2 pre-indexed stores have the same MI operands, they just
6083 // define them differently in the .td files from the isel patterns, so
6084 // they need pseudos.
6085 case ARM::t2STR_preidx:
6086 MI->setDesc(TII->get(ARM::t2STR_PRE));
6087 return BB;
6088 case ARM::t2STRB_preidx:
6089 MI->setDesc(TII->get(ARM::t2STRB_PRE));
6090 return BB;
6091 case ARM::t2STRH_preidx:
6092 MI->setDesc(TII->get(ARM::t2STRH_PRE));
6093 return BB;
6094
Jim Grosbach19dec202011-08-05 20:35:44 +00006095 case ARM::STRi_preidx:
6096 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00006097 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00006098 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
6099 // Decode the offset.
6100 unsigned Offset = MI->getOperand(4).getImm();
6101 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
6102 Offset = ARM_AM::getAM2Offset(Offset);
6103 if (isSub)
6104 Offset = -Offset;
6105
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006106 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00006107 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00006108 .addOperand(MI->getOperand(0)) // Rn_wb
6109 .addOperand(MI->getOperand(1)) // Rt
6110 .addOperand(MI->getOperand(2)) // Rn
6111 .addImm(Offset) // offset (skip GPR==zero_reg)
6112 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006113 .addOperand(MI->getOperand(6))
6114 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00006115 MI->eraseFromParent();
6116 return BB;
6117 }
6118 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00006119 case ARM::STRBr_preidx:
6120 case ARM::STRH_preidx: {
6121 unsigned NewOpc;
6122 switch (MI->getOpcode()) {
6123 default: llvm_unreachable("unexpected opcode!");
6124 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
6125 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
6126 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
6127 }
Jim Grosbach19dec202011-08-05 20:35:44 +00006128 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
6129 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
6130 MIB.addOperand(MI->getOperand(i));
6131 MI->eraseFromParent();
6132 return BB;
6133 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006134 case ARM::ATOMIC_LOAD_ADD_I8:
6135 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6136 case ARM::ATOMIC_LOAD_ADD_I16:
6137 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6138 case ARM::ATOMIC_LOAD_ADD_I32:
6139 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006140
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006141 case ARM::ATOMIC_LOAD_AND_I8:
6142 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6143 case ARM::ATOMIC_LOAD_AND_I16:
6144 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6145 case ARM::ATOMIC_LOAD_AND_I32:
6146 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006147
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006148 case ARM::ATOMIC_LOAD_OR_I8:
6149 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6150 case ARM::ATOMIC_LOAD_OR_I16:
6151 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6152 case ARM::ATOMIC_LOAD_OR_I32:
6153 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006154
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006155 case ARM::ATOMIC_LOAD_XOR_I8:
6156 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6157 case ARM::ATOMIC_LOAD_XOR_I16:
6158 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6159 case ARM::ATOMIC_LOAD_XOR_I32:
6160 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006161
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006162 case ARM::ATOMIC_LOAD_NAND_I8:
6163 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6164 case ARM::ATOMIC_LOAD_NAND_I16:
6165 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6166 case ARM::ATOMIC_LOAD_NAND_I32:
6167 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006168
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006169 case ARM::ATOMIC_LOAD_SUB_I8:
6170 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6171 case ARM::ATOMIC_LOAD_SUB_I16:
6172 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6173 case ARM::ATOMIC_LOAD_SUB_I32:
6174 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006175
Jim Grosbachf7da8822011-04-26 19:44:18 +00006176 case ARM::ATOMIC_LOAD_MIN_I8:
6177 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
6178 case ARM::ATOMIC_LOAD_MIN_I16:
6179 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
6180 case ARM::ATOMIC_LOAD_MIN_I32:
6181 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
6182
6183 case ARM::ATOMIC_LOAD_MAX_I8:
6184 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
6185 case ARM::ATOMIC_LOAD_MAX_I16:
6186 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
6187 case ARM::ATOMIC_LOAD_MAX_I32:
6188 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
6189
6190 case ARM::ATOMIC_LOAD_UMIN_I8:
6191 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
6192 case ARM::ATOMIC_LOAD_UMIN_I16:
6193 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
6194 case ARM::ATOMIC_LOAD_UMIN_I32:
6195 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
6196
6197 case ARM::ATOMIC_LOAD_UMAX_I8:
6198 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
6199 case ARM::ATOMIC_LOAD_UMAX_I16:
6200 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
6201 case ARM::ATOMIC_LOAD_UMAX_I32:
6202 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
6203
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006204 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
6205 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
6206 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00006207
6208 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
6209 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
6210 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006211
Eli Friedman2bdffe42011-08-31 00:31:29 +00006212
6213 case ARM::ATOMADD6432:
6214 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006215 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr,
6216 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006217 case ARM::ATOMSUB6432:
6218 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006219 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6220 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006221 case ARM::ATOMOR6432:
6222 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006223 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006224 case ARM::ATOMXOR6432:
6225 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006226 isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006227 case ARM::ATOMAND6432:
6228 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006229 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006230 case ARM::ATOMSWAP6432:
6231 return EmitAtomicBinary64(MI, BB, 0, 0, false);
Eli Friedman4d3f3292011-08-31 17:52:22 +00006232 case ARM::ATOMCMPXCHG6432:
6233 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
6234 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6235 /*NeedsCarry*/ false, /*IsCmpxchg*/true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006236
Evan Cheng007ea272009-08-12 05:17:19 +00006237 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00006238 // To "insert" a SELECT_CC instruction, we actually have to insert the
6239 // diamond control-flow pattern. The incoming instruction knows the
6240 // destination vreg to set, the condition code register to branch on, the
6241 // true/false values to select between, and a branch opcode to use.
6242 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006243 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00006244 ++It;
6245
6246 // thisMBB:
6247 // ...
6248 // TrueVal = ...
6249 // cmpTY ccX, r1, r2
6250 // bCC copy1MBB
6251 // fallthrough --> copy0MBB
6252 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006253 MachineFunction *F = BB->getParent();
6254 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6255 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00006256 F->insert(It, copy0MBB);
6257 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006258
6259 // Transfer the remainder of BB and its successor edges to sinkMBB.
6260 sinkMBB->splice(sinkMBB->begin(), BB,
6261 llvm::next(MachineBasicBlock::iterator(MI)),
6262 BB->end());
6263 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6264
Dan Gohman258c58c2010-07-06 15:49:48 +00006265 BB->addSuccessor(copy0MBB);
6266 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00006267
Dan Gohman14152b42010-07-06 20:24:04 +00006268 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
6269 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
6270
Evan Chenga8e29892007-01-19 07:51:42 +00006271 // copy0MBB:
6272 // %FalseValue = ...
6273 // # fallthrough to sinkMBB
6274 BB = copy0MBB;
6275
6276 // Update machine-CFG edges
6277 BB->addSuccessor(sinkMBB);
6278
6279 // sinkMBB:
6280 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6281 // ...
6282 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00006283 BuildMI(*BB, BB->begin(), dl,
6284 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00006285 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6286 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6287
Dan Gohman14152b42010-07-06 20:24:04 +00006288 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00006289 return BB;
6290 }
Evan Cheng86198642009-08-07 00:34:42 +00006291
Evan Cheng218977b2010-07-13 19:27:42 +00006292 case ARM::BCCi64:
6293 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00006294 // If there is an unconditional branch to the other successor, remove it.
6295 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00006296
Evan Cheng218977b2010-07-13 19:27:42 +00006297 // Compare both parts that make up the double comparison separately for
6298 // equality.
6299 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
6300
6301 unsigned LHS1 = MI->getOperand(1).getReg();
6302 unsigned LHS2 = MI->getOperand(2).getReg();
6303 if (RHSisZero) {
6304 AddDefaultPred(BuildMI(BB, dl,
6305 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6306 .addReg(LHS1).addImm(0));
6307 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6308 .addReg(LHS2).addImm(0)
6309 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6310 } else {
6311 unsigned RHS1 = MI->getOperand(3).getReg();
6312 unsigned RHS2 = MI->getOperand(4).getReg();
6313 AddDefaultPred(BuildMI(BB, dl,
6314 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6315 .addReg(LHS1).addReg(RHS1));
6316 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6317 .addReg(LHS2).addReg(RHS2)
6318 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6319 }
6320
6321 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
6322 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
6323 if (MI->getOperand(0).getImm() == ARMCC::NE)
6324 std::swap(destMBB, exitMBB);
6325
6326 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6327 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson51f6a7a2011-09-09 21:48:23 +00006328 if (isThumb2)
6329 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
6330 else
6331 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng218977b2010-07-13 19:27:42 +00006332
6333 MI->eraseFromParent(); // The pseudo instruction is gone now.
6334 return BB;
6335 }
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006336
Bill Wendling5bc85282011-10-17 20:37:20 +00006337 case ARM::Int_eh_sjlj_setjmp:
6338 case ARM::Int_eh_sjlj_setjmp_nofp:
6339 case ARM::tInt_eh_sjlj_setjmp:
6340 case ARM::t2Int_eh_sjlj_setjmp:
6341 case ARM::t2Int_eh_sjlj_setjmp_nofp:
6342 EmitSjLjDispatchBlock(MI, BB);
6343 return BB;
6344
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006345 case ARM::ABS:
6346 case ARM::t2ABS: {
6347 // To insert an ABS instruction, we have to insert the
6348 // diamond control-flow pattern. The incoming instruction knows the
6349 // source vreg to test against 0, the destination vreg to set,
6350 // the condition code register to branch on, the
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006351 // true/false values to select between, and a branch opcode to use.
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006352 // It transforms
6353 // V1 = ABS V0
6354 // into
6355 // V2 = MOVS V0
6356 // BCC (branch to SinkBB if V0 >= 0)
6357 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006358 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006359 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6360 MachineFunction::iterator BBI = BB;
6361 ++BBI;
6362 MachineFunction *Fn = BB->getParent();
6363 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6364 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6365 Fn->insert(BBI, RSBBB);
6366 Fn->insert(BBI, SinkBB);
6367
6368 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
6369 unsigned int ABSDstReg = MI->getOperand(0).getReg();
6370 bool isThumb2 = Subtarget->isThumb2();
6371 MachineRegisterInfo &MRI = Fn->getRegInfo();
6372 // In Thumb mode S must not be specified if source register is the SP or
6373 // PC and if destination register is the SP, so restrict register class
6374 unsigned NewMovDstReg = MRI.createVirtualRegister(
6375 isThumb2 ? ARM::rGPRRegisterClass : ARM::GPRRegisterClass);
6376 unsigned NewRsbDstReg = MRI.createVirtualRegister(
6377 isThumb2 ? ARM::rGPRRegisterClass : ARM::GPRRegisterClass);
6378
6379 // Transfer the remainder of BB and its successor edges to sinkMBB.
6380 SinkBB->splice(SinkBB->begin(), BB,
6381 llvm::next(MachineBasicBlock::iterator(MI)),
6382 BB->end());
6383 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
6384
6385 BB->addSuccessor(RSBBB);
6386 BB->addSuccessor(SinkBB);
6387
6388 // fall through to SinkMBB
6389 RSBBB->addSuccessor(SinkBB);
6390
6391 // insert a movs at the end of BB
6392 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVr : ARM::MOVr),
6393 NewMovDstReg)
6394 .addReg(ABSSrcReg, RegState::Kill)
6395 .addImm((unsigned)ARMCC::AL).addReg(0)
6396 .addReg(ARM::CPSR, RegState::Define);
6397
6398 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006399 BuildMI(BB, dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006400 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
6401 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
6402
6403 // insert rsbri in RSBBB
6404 // Note: BCC and rsbri will be converted into predicated rsbmi
6405 // by if-conversion pass
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006406 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006407 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
6408 .addReg(NewMovDstReg, RegState::Kill)
6409 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
6410
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006411 // insert PHI in SinkBB,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006412 // reuse ABSDstReg to not change uses of ABS instruction
6413 BuildMI(*SinkBB, SinkBB->begin(), dl,
6414 TII->get(ARM::PHI), ABSDstReg)
6415 .addReg(NewRsbDstReg).addMBB(RSBBB)
6416 .addReg(NewMovDstReg).addMBB(BB);
6417
6418 // remove ABS instruction
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006419 MI->eraseFromParent();
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006420
6421 // return last added BB
6422 return SinkBB;
6423 }
Evan Chenga8e29892007-01-19 07:51:42 +00006424 }
6425}
6426
Evan Cheng37fefc22011-08-30 19:09:48 +00006427void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
6428 SDNode *Node) const {
Andrew Trick90b7b122011-10-18 19:18:52 +00006429 const MCInstrDesc *MCID = &MI->getDesc();
6430 if (!MCID->hasPostISelHook()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006431 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
6432 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
6433 return;
6434 }
6435
Andrew Trick4815d562011-09-20 03:17:40 +00006436 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
6437 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
6438 // operand is still set to noreg. If needed, set the optional operand's
6439 // register to CPSR, and remove the redundant implicit def.
Andrew Trick3be654f2011-09-21 02:20:46 +00006440 //
Andrew Trick90b7b122011-10-18 19:18:52 +00006441 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick4815d562011-09-20 03:17:40 +00006442
Andrew Trick3be654f2011-09-21 02:20:46 +00006443 // Rename pseudo opcodes.
6444 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
6445 if (NewOpc) {
6446 const ARMBaseInstrInfo *TII =
6447 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick90b7b122011-10-18 19:18:52 +00006448 MCID = &TII->get(NewOpc);
6449
6450 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
6451 "converted opcode should be the same except for cc_out");
6452
6453 MI->setDesc(*MCID);
6454
6455 // Add the optional cc_out operand
6456 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick3be654f2011-09-21 02:20:46 +00006457 }
Andrew Trick90b7b122011-10-18 19:18:52 +00006458 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick4815d562011-09-20 03:17:40 +00006459
6460 // Any ARM instruction that sets the 's' bit should specify an optional
6461 // "cc_out" operand in the last operand position.
Andrew Trick90b7b122011-10-18 19:18:52 +00006462 if (!MCID->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006463 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006464 return;
6465 }
Andrew Trick3be654f2011-09-21 02:20:46 +00006466 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
6467 // since we already have an optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006468 bool definesCPSR = false;
6469 bool deadCPSR = false;
Andrew Trick90b7b122011-10-18 19:18:52 +00006470 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick4815d562011-09-20 03:17:40 +00006471 i != e; ++i) {
6472 const MachineOperand &MO = MI->getOperand(i);
6473 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
6474 definesCPSR = true;
6475 if (MO.isDead())
6476 deadCPSR = true;
6477 MI->RemoveOperand(i);
6478 break;
Evan Cheng37fefc22011-08-30 19:09:48 +00006479 }
6480 }
Andrew Trick4815d562011-09-20 03:17:40 +00006481 if (!definesCPSR) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006482 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006483 return;
6484 }
6485 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick3be654f2011-09-21 02:20:46 +00006486 if (deadCPSR) {
6487 assert(!MI->getOperand(ccOutIdx).getReg() &&
6488 "expect uninitialized optional cc_out operand");
Andrew Trick4815d562011-09-20 03:17:40 +00006489 return;
Andrew Trick3be654f2011-09-21 02:20:46 +00006490 }
Andrew Trick4815d562011-09-20 03:17:40 +00006491
Andrew Trick3be654f2011-09-21 02:20:46 +00006492 // If this instruction was defined with an optional CPSR def and its dag node
6493 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006494 MachineOperand &MO = MI->getOperand(ccOutIdx);
6495 MO.setReg(ARM::CPSR);
6496 MO.setIsDef(true);
Evan Cheng37fefc22011-08-30 19:09:48 +00006497}
6498
Evan Chenga8e29892007-01-19 07:51:42 +00006499//===----------------------------------------------------------------------===//
6500// ARM Optimization Hooks
6501//===----------------------------------------------------------------------===//
6502
Chris Lattnerd1980a52009-03-12 06:52:53 +00006503static
6504SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
6505 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00006506 SelectionDAG &DAG = DCI.DAG;
6507 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00006508 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00006509 unsigned Opc = N->getOpcode();
6510 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
6511 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
6512 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
6513 ISD::CondCode CC = ISD::SETCC_INVALID;
6514
6515 if (isSlctCC) {
6516 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
6517 } else {
6518 SDValue CCOp = Slct.getOperand(0);
6519 if (CCOp.getOpcode() == ISD::SETCC)
6520 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
6521 }
6522
6523 bool DoXform = false;
6524 bool InvCC = false;
6525 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
6526 "Bad input!");
6527
6528 if (LHS.getOpcode() == ISD::Constant &&
6529 cast<ConstantSDNode>(LHS)->isNullValue()) {
6530 DoXform = true;
6531 } else if (CC != ISD::SETCC_INVALID &&
6532 RHS.getOpcode() == ISD::Constant &&
6533 cast<ConstantSDNode>(RHS)->isNullValue()) {
6534 std::swap(LHS, RHS);
6535 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006536 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00006537 Op0.getOperand(0).getValueType();
6538 bool isInt = OpVT.isInteger();
6539 CC = ISD::getSetCCInverse(CC, isInt);
6540
6541 if (!TLI.isCondCodeLegal(CC, OpVT))
6542 return SDValue(); // Inverse operator isn't legal.
6543
6544 DoXform = true;
6545 InvCC = true;
6546 }
6547
6548 if (DoXform) {
6549 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
6550 if (isSlctCC)
6551 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
6552 Slct.getOperand(0), Slct.getOperand(1), CC);
6553 SDValue CCOp = Slct.getOperand(0);
6554 if (InvCC)
6555 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
6556 CCOp.getOperand(0), CCOp.getOperand(1), CC);
6557 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
6558 CCOp, OtherOp, Result);
6559 }
6560 return SDValue();
6561}
6562
Eric Christopherfa6f5912011-06-29 21:10:36 +00006563// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00006564// (only after legalization).
6565static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
6566 TargetLowering::DAGCombinerInfo &DCI,
6567 const ARMSubtarget *Subtarget) {
6568
6569 // Only perform optimization if after legalize, and if NEON is available. We
6570 // also expected both operands to be BUILD_VECTORs.
6571 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
6572 || N0.getOpcode() != ISD::BUILD_VECTOR
6573 || N1.getOpcode() != ISD::BUILD_VECTOR)
6574 return SDValue();
6575
6576 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
6577 EVT VT = N->getValueType(0);
6578 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
6579 return SDValue();
6580
6581 // Check that the vector operands are of the right form.
6582 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
6583 // operands, where N is the size of the formed vector.
6584 // Each EXTRACT_VECTOR should have the same input vector and odd or even
6585 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00006586
6587 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00006588 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00006589 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00006590 SDValue Vec = N0->getOperand(0)->getOperand(0);
6591 SDNode *V = Vec.getNode();
6592 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00006593
Eric Christopherfa6f5912011-06-29 21:10:36 +00006594 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00006595 // check to see if each of their operands are an EXTRACT_VECTOR with
6596 // the same vector and appropriate index.
6597 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
6598 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
6599 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00006600
Tanya Lattner189531f2011-06-14 23:48:48 +00006601 SDValue ExtVec0 = N0->getOperand(i);
6602 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00006603
Tanya Lattner189531f2011-06-14 23:48:48 +00006604 // First operand is the vector, verify its the same.
6605 if (V != ExtVec0->getOperand(0).getNode() ||
6606 V != ExtVec1->getOperand(0).getNode())
6607 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00006608
Tanya Lattner189531f2011-06-14 23:48:48 +00006609 // Second is the constant, verify its correct.
6610 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
6611 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00006612
Tanya Lattner189531f2011-06-14 23:48:48 +00006613 // For the constant, we want to see all the even or all the odd.
6614 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
6615 || C1->getZExtValue() != nextIndex+1)
6616 return SDValue();
6617
6618 // Increment index.
6619 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00006620 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00006621 return SDValue();
6622 }
6623
6624 // Create VPADDL node.
6625 SelectionDAG &DAG = DCI.DAG;
6626 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00006627
6628 // Build operand list.
6629 SmallVector<SDValue, 8> Ops;
6630 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
6631 TLI.getPointerTy()));
6632
6633 // Input is the vector.
6634 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00006635
Tanya Lattner189531f2011-06-14 23:48:48 +00006636 // Get widened type and narrowed type.
6637 MVT widenType;
6638 unsigned numElem = VT.getVectorNumElements();
6639 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
6640 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
6641 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
6642 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
6643 default:
6644 assert(0 && "Invalid vector element type for padd optimization.");
6645 }
6646
6647 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
6648 widenType, &Ops[0], Ops.size());
6649 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
6650}
6651
Bob Wilson3d5792a2010-07-29 20:34:14 +00006652/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
6653/// operands N0 and N1. This is a helper for PerformADDCombine that is
6654/// called with the default operands, and if that fails, with commuted
6655/// operands.
6656static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00006657 TargetLowering::DAGCombinerInfo &DCI,
6658 const ARMSubtarget *Subtarget){
6659
6660 // Attempt to create vpaddl for this add.
6661 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
6662 if (Result.getNode())
6663 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00006664
Chris Lattnerd1980a52009-03-12 06:52:53 +00006665 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
6666 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
6667 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
6668 if (Result.getNode()) return Result;
6669 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00006670 return SDValue();
6671}
6672
Bob Wilson3d5792a2010-07-29 20:34:14 +00006673/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
6674///
6675static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00006676 TargetLowering::DAGCombinerInfo &DCI,
6677 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00006678 SDValue N0 = N->getOperand(0);
6679 SDValue N1 = N->getOperand(1);
6680
6681 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00006682 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00006683 if (Result.getNode())
6684 return Result;
6685
6686 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00006687 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00006688}
6689
Chris Lattnerd1980a52009-03-12 06:52:53 +00006690/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00006691///
Chris Lattnerd1980a52009-03-12 06:52:53 +00006692static SDValue PerformSUBCombine(SDNode *N,
6693 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00006694 SDValue N0 = N->getOperand(0);
6695 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00006696
Chris Lattnerd1980a52009-03-12 06:52:53 +00006697 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
6698 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
6699 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
6700 if (Result.getNode()) return Result;
6701 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00006702
Chris Lattnerd1980a52009-03-12 06:52:53 +00006703 return SDValue();
6704}
6705
Evan Cheng463d3582011-03-31 19:38:48 +00006706/// PerformVMULCombine
6707/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
6708/// special multiplier accumulator forwarding.
6709/// vmul d3, d0, d2
6710/// vmla d3, d1, d2
6711/// is faster than
6712/// vadd d3, d0, d1
6713/// vmul d3, d3, d2
6714static SDValue PerformVMULCombine(SDNode *N,
6715 TargetLowering::DAGCombinerInfo &DCI,
6716 const ARMSubtarget *Subtarget) {
6717 if (!Subtarget->hasVMLxForwarding())
6718 return SDValue();
6719
6720 SelectionDAG &DAG = DCI.DAG;
6721 SDValue N0 = N->getOperand(0);
6722 SDValue N1 = N->getOperand(1);
6723 unsigned Opcode = N0.getOpcode();
6724 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
6725 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00006726 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00006727 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
6728 Opcode != ISD::FADD && Opcode != ISD::FSUB)
6729 return SDValue();
6730 std::swap(N0, N1);
6731 }
6732
6733 EVT VT = N->getValueType(0);
6734 DebugLoc DL = N->getDebugLoc();
6735 SDValue N00 = N0->getOperand(0);
6736 SDValue N01 = N0->getOperand(1);
6737 return DAG.getNode(Opcode, DL, VT,
6738 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
6739 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
6740}
6741
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006742static SDValue PerformMULCombine(SDNode *N,
6743 TargetLowering::DAGCombinerInfo &DCI,
6744 const ARMSubtarget *Subtarget) {
6745 SelectionDAG &DAG = DCI.DAG;
6746
6747 if (Subtarget->isThumb1Only())
6748 return SDValue();
6749
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006750 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
6751 return SDValue();
6752
6753 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00006754 if (VT.is64BitVector() || VT.is128BitVector())
6755 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006756 if (VT != MVT::i32)
6757 return SDValue();
6758
6759 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
6760 if (!C)
6761 return SDValue();
6762
6763 uint64_t MulAmt = C->getZExtValue();
6764 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
6765 ShiftAmt = ShiftAmt & (32 - 1);
6766 SDValue V = N->getOperand(0);
6767 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006768
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006769 SDValue Res;
6770 MulAmt >>= ShiftAmt;
6771 if (isPowerOf2_32(MulAmt - 1)) {
6772 // (mul x, 2^N + 1) => (add (shl x, N), x)
6773 Res = DAG.getNode(ISD::ADD, DL, VT,
6774 V, DAG.getNode(ISD::SHL, DL, VT,
6775 V, DAG.getConstant(Log2_32(MulAmt-1),
6776 MVT::i32)));
6777 } else if (isPowerOf2_32(MulAmt + 1)) {
6778 // (mul x, 2^N - 1) => (sub (shl x, N), x)
6779 Res = DAG.getNode(ISD::SUB, DL, VT,
6780 DAG.getNode(ISD::SHL, DL, VT,
6781 V, DAG.getConstant(Log2_32(MulAmt+1),
6782 MVT::i32)),
6783 V);
6784 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006785 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006786
6787 if (ShiftAmt != 0)
6788 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
6789 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006790
6791 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006792 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006793 return SDValue();
6794}
6795
Owen Anderson080c0922010-11-05 19:27:46 +00006796static SDValue PerformANDCombine(SDNode *N,
6797 TargetLowering::DAGCombinerInfo &DCI) {
Owen Anderson76706012011-04-05 21:48:57 +00006798
Owen Anderson080c0922010-11-05 19:27:46 +00006799 // Attempt to use immediate-form VBIC
6800 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
6801 DebugLoc dl = N->getDebugLoc();
6802 EVT VT = N->getValueType(0);
6803 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006804
Tanya Lattner0433b212011-04-07 15:24:20 +00006805 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
6806 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00006807
Owen Anderson080c0922010-11-05 19:27:46 +00006808 APInt SplatBits, SplatUndef;
6809 unsigned SplatBitSize;
6810 bool HasAnyUndefs;
6811 if (BVN &&
6812 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
6813 if (SplatBitSize <= 64) {
6814 EVT VbicVT;
6815 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
6816 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006817 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00006818 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00006819 if (Val.getNode()) {
6820 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006821 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00006822 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006823 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00006824 }
6825 }
6826 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006827
Owen Anderson080c0922010-11-05 19:27:46 +00006828 return SDValue();
6829}
6830
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006831/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
6832static SDValue PerformORCombine(SDNode *N,
6833 TargetLowering::DAGCombinerInfo &DCI,
6834 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00006835 // Attempt to use immediate-form VORR
6836 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
6837 DebugLoc dl = N->getDebugLoc();
6838 EVT VT = N->getValueType(0);
6839 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006840
Tanya Lattner0433b212011-04-07 15:24:20 +00006841 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
6842 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00006843
Owen Anderson60f48702010-11-03 23:15:26 +00006844 APInt SplatBits, SplatUndef;
6845 unsigned SplatBitSize;
6846 bool HasAnyUndefs;
6847 if (BVN && Subtarget->hasNEON() &&
6848 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
6849 if (SplatBitSize <= 64) {
6850 EVT VorrVT;
6851 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
6852 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00006853 DAG, VorrVT, VT.is128BitVector(),
6854 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00006855 if (Val.getNode()) {
6856 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006857 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00006858 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006859 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00006860 }
6861 }
6862 }
6863
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00006864 SDValue N0 = N->getOperand(0);
6865 if (N0.getOpcode() != ISD::AND)
6866 return SDValue();
6867 SDValue N1 = N->getOperand(1);
6868
6869 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
6870 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
6871 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
6872 APInt SplatUndef;
6873 unsigned SplatBitSize;
6874 bool HasAnyUndefs;
6875
6876 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
6877 APInt SplatBits0;
6878 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
6879 HasAnyUndefs) && !HasAnyUndefs) {
6880 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
6881 APInt SplatBits1;
6882 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
6883 HasAnyUndefs) && !HasAnyUndefs &&
6884 SplatBits0 == ~SplatBits1) {
6885 // Canonicalize the vector type to make instruction selection simpler.
6886 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
6887 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
6888 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00006889 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00006890 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
6891 }
6892 }
6893 }
6894
Jim Grosbach54238562010-07-17 03:30:54 +00006895 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
6896 // reasonable.
6897
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006898 // BFI is only available on V6T2+
6899 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
6900 return SDValue();
6901
Jim Grosbach54238562010-07-17 03:30:54 +00006902 DebugLoc DL = N->getDebugLoc();
6903 // 1) or (and A, mask), val => ARMbfi A, val, mask
6904 // iff (val & mask) == val
6905 //
6906 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
6907 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00006908 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00006909 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00006910 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00006911 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006912
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006913 if (VT != MVT::i32)
6914 return SDValue();
6915
Evan Cheng30fb13f2010-12-13 20:32:54 +00006916 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00006917
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006918 // The value and the mask need to be constants so we can verify this is
6919 // actually a bitfield set. If the mask is 0xffff, we can do better
6920 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00006921 SDValue MaskOp = N0.getOperand(1);
6922 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
6923 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006924 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00006925 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006926 if (Mask == 0xffff)
6927 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006928 SDValue Res;
6929 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00006930 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
6931 if (N1C) {
6932 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00006933 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00006934 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006935
Evan Chenga9688c42010-12-11 04:11:38 +00006936 if (ARM::isBitFieldInvertedMask(Mask)) {
6937 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006938
Evan Cheng30fb13f2010-12-13 20:32:54 +00006939 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00006940 DAG.getConstant(Val, MVT::i32),
6941 DAG.getConstant(Mask, MVT::i32));
6942
6943 // Do not add new nodes to DAG combiner worklist.
6944 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00006945 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00006946 }
Jim Grosbach54238562010-07-17 03:30:54 +00006947 } else if (N1.getOpcode() == ISD::AND) {
6948 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00006949 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
6950 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00006951 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00006952 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006953
Eric Christopher29aeed12011-03-26 01:21:03 +00006954 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
6955 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00006956 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00006957 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00006958 // The pack halfword instruction works better for masks that fit it,
6959 // so use that when it's available.
6960 if (Subtarget->hasT2ExtractPack() &&
6961 (Mask == 0xffff || Mask == 0xffff0000))
6962 return SDValue();
6963 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00006964 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00006965 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00006966 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00006967 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00006968 DAG.getConstant(Mask, MVT::i32));
6969 // Do not add new nodes to DAG combiner worklist.
6970 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00006971 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006972 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00006973 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00006974 // The pack halfword instruction works better for masks that fit it,
6975 // so use that when it's available.
6976 if (Subtarget->hasT2ExtractPack() &&
6977 (Mask2 == 0xffff || Mask2 == 0xffff0000))
6978 return SDValue();
6979 // 2b
6980 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00006981 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00006982 DAG.getConstant(lsb, MVT::i32));
6983 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00006984 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00006985 // Do not add new nodes to DAG combiner worklist.
6986 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00006987 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006988 }
6989 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006990
Evan Cheng30fb13f2010-12-13 20:32:54 +00006991 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
6992 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
6993 ARM::isBitFieldInvertedMask(~Mask)) {
6994 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
6995 // where lsb(mask) == #shamt and masked bits of B are known zero.
6996 SDValue ShAmt = N00.getOperand(1);
6997 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
6998 unsigned LSB = CountTrailingZeros_32(Mask);
6999 if (ShAmtC != LSB)
7000 return SDValue();
7001
7002 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
7003 DAG.getConstant(~Mask, MVT::i32));
7004
7005 // Do not add new nodes to DAG combiner worklist.
7006 DCI.CombineTo(N, Res, false);
7007 }
7008
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007009 return SDValue();
7010}
7011
Evan Chengbf188ae2011-06-15 01:12:31 +00007012/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
7013/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00007014static SDValue PerformBFICombine(SDNode *N,
7015 TargetLowering::DAGCombinerInfo &DCI) {
7016 SDValue N1 = N->getOperand(1);
7017 if (N1.getOpcode() == ISD::AND) {
7018 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7019 if (!N11C)
7020 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007021 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
7022 unsigned LSB = CountTrailingZeros_32(~InvMask);
7023 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
7024 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00007025 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007026 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00007027 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
7028 N->getOperand(0), N1.getOperand(0),
7029 N->getOperand(2));
7030 }
7031 return SDValue();
7032}
7033
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007034/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
7035/// ARMISD::VMOVRRD.
7036static SDValue PerformVMOVRRDCombine(SDNode *N,
7037 TargetLowering::DAGCombinerInfo &DCI) {
7038 // vmovrrd(vmovdrr x, y) -> x,y
7039 SDValue InDouble = N->getOperand(0);
7040 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
7041 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00007042
7043 // vmovrrd(load f64) -> (load i32), (load i32)
7044 SDNode *InNode = InDouble.getNode();
7045 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
7046 InNode->getValueType(0) == MVT::f64 &&
7047 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
7048 !cast<LoadSDNode>(InNode)->isVolatile()) {
7049 // TODO: Should this be done for non-FrameIndex operands?
7050 LoadSDNode *LD = cast<LoadSDNode>(InNode);
7051
7052 SelectionDAG &DAG = DCI.DAG;
7053 DebugLoc DL = LD->getDebugLoc();
7054 SDValue BasePtr = LD->getBasePtr();
7055 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
7056 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007057 LD->isNonTemporal(), LD->isInvariant(),
7058 LD->getAlignment());
Cameron Zwarich4071a712011-04-02 02:40:43 +00007059
7060 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7061 DAG.getConstant(4, MVT::i32));
7062 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
7063 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007064 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich4071a712011-04-02 02:40:43 +00007065 std::min(4U, LD->getAlignment() / 2));
7066
7067 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
7068 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
7069 DCI.RemoveFromWorklist(LD);
7070 DAG.DeleteNode(LD);
7071 return Result;
7072 }
7073
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007074 return SDValue();
7075}
7076
7077/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
7078/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
7079static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
7080 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
7081 SDValue Op0 = N->getOperand(0);
7082 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007083 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007084 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007085 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007086 Op1 = Op1.getOperand(0);
7087 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
7088 Op0.getNode() == Op1.getNode() &&
7089 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007090 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007091 N->getValueType(0), Op0.getOperand(0));
7092 return SDValue();
7093}
7094
Bob Wilson31600902010-12-21 06:43:19 +00007095/// PerformSTORECombine - Target-specific dag combine xforms for
7096/// ISD::STORE.
7097static SDValue PerformSTORECombine(SDNode *N,
7098 TargetLowering::DAGCombinerInfo &DCI) {
7099 // Bitcast an i64 store extracted from a vector to f64.
7100 // Otherwise, the i64 value will be legalized to a pair of i32 values.
7101 StoreSDNode *St = cast<StoreSDNode>(N);
7102 SDValue StVal = St->getValue();
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007103 if (!ISD::isNormalStore(St) || St->isVolatile())
7104 return SDValue();
7105
7106 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
7107 StVal.getNode()->hasOneUse() && !St->isVolatile()) {
7108 SelectionDAG &DAG = DCI.DAG;
7109 DebugLoc DL = St->getDebugLoc();
7110 SDValue BasePtr = St->getBasePtr();
7111 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
7112 StVal.getNode()->getOperand(0), BasePtr,
7113 St->getPointerInfo(), St->isVolatile(),
7114 St->isNonTemporal(), St->getAlignment());
7115
7116 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7117 DAG.getConstant(4, MVT::i32));
7118 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
7119 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
7120 St->isNonTemporal(),
7121 std::min(4U, St->getAlignment() / 2));
7122 }
7123
7124 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00007125 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
7126 return SDValue();
7127
7128 SelectionDAG &DAG = DCI.DAG;
7129 DebugLoc dl = StVal.getDebugLoc();
7130 SDValue IntVec = StVal.getOperand(0);
7131 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7132 IntVec.getValueType().getVectorNumElements());
7133 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
7134 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
7135 Vec, StVal.getOperand(1));
7136 dl = N->getDebugLoc();
7137 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
7138 // Make the DAGCombiner fold the bitcasts.
7139 DCI.AddToWorklist(Vec.getNode());
7140 DCI.AddToWorklist(ExtElt.getNode());
7141 DCI.AddToWorklist(V.getNode());
7142 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
7143 St->getPointerInfo(), St->isVolatile(),
7144 St->isNonTemporal(), St->getAlignment(),
7145 St->getTBAAInfo());
7146}
7147
7148/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
7149/// are normal, non-volatile loads. If so, it is profitable to bitcast an
7150/// i64 vector to have f64 elements, since the value can then be loaded
7151/// directly into a VFP register.
7152static bool hasNormalLoadOperand(SDNode *N) {
7153 unsigned NumElts = N->getValueType(0).getVectorNumElements();
7154 for (unsigned i = 0; i < NumElts; ++i) {
7155 SDNode *Elt = N->getOperand(i).getNode();
7156 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
7157 return true;
7158 }
7159 return false;
7160}
7161
Bob Wilson75f02882010-09-17 22:59:05 +00007162/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
7163/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00007164static SDValue PerformBUILD_VECTORCombine(SDNode *N,
7165 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00007166 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
7167 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
7168 // into a pair of GPRs, which is fine when the value is used as a scalar,
7169 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00007170 SelectionDAG &DAG = DCI.DAG;
7171 if (N->getNumOperands() == 2) {
7172 SDValue RV = PerformVMOVDRRCombine(N, DAG);
7173 if (RV.getNode())
7174 return RV;
7175 }
Bob Wilson75f02882010-09-17 22:59:05 +00007176
Bob Wilson31600902010-12-21 06:43:19 +00007177 // Load i64 elements as f64 values so that type legalization does not split
7178 // them up into i32 values.
7179 EVT VT = N->getValueType(0);
7180 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
7181 return SDValue();
7182 DebugLoc dl = N->getDebugLoc();
7183 SmallVector<SDValue, 8> Ops;
7184 unsigned NumElts = VT.getVectorNumElements();
7185 for (unsigned i = 0; i < NumElts; ++i) {
7186 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
7187 Ops.push_back(V);
7188 // Make the DAGCombiner fold the bitcast.
7189 DCI.AddToWorklist(V.getNode());
7190 }
7191 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
7192 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
7193 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
7194}
7195
7196/// PerformInsertEltCombine - Target-specific dag combine xforms for
7197/// ISD::INSERT_VECTOR_ELT.
7198static SDValue PerformInsertEltCombine(SDNode *N,
7199 TargetLowering::DAGCombinerInfo &DCI) {
7200 // Bitcast an i64 load inserted into a vector to f64.
7201 // Otherwise, the i64 value will be legalized to a pair of i32 values.
7202 EVT VT = N->getValueType(0);
7203 SDNode *Elt = N->getOperand(1).getNode();
7204 if (VT.getVectorElementType() != MVT::i64 ||
7205 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
7206 return SDValue();
7207
7208 SelectionDAG &DAG = DCI.DAG;
7209 DebugLoc dl = N->getDebugLoc();
7210 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7211 VT.getVectorNumElements());
7212 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
7213 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
7214 // Make the DAGCombiner fold the bitcasts.
7215 DCI.AddToWorklist(Vec.getNode());
7216 DCI.AddToWorklist(V.getNode());
7217 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
7218 Vec, V, N->getOperand(2));
7219 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00007220}
7221
Bob Wilsonf20700c2010-10-27 20:38:28 +00007222/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
7223/// ISD::VECTOR_SHUFFLE.
7224static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
7225 // The LLVM shufflevector instruction does not require the shuffle mask
7226 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
7227 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
7228 // operands do not match the mask length, they are extended by concatenating
7229 // them with undef vectors. That is probably the right thing for other
7230 // targets, but for NEON it is better to concatenate two double-register
7231 // size vector operands into a single quad-register size vector. Do that
7232 // transformation here:
7233 // shuffle(concat(v1, undef), concat(v2, undef)) ->
7234 // shuffle(concat(v1, v2), undef)
7235 SDValue Op0 = N->getOperand(0);
7236 SDValue Op1 = N->getOperand(1);
7237 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
7238 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
7239 Op0.getNumOperands() != 2 ||
7240 Op1.getNumOperands() != 2)
7241 return SDValue();
7242 SDValue Concat0Op1 = Op0.getOperand(1);
7243 SDValue Concat1Op1 = Op1.getOperand(1);
7244 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
7245 Concat1Op1.getOpcode() != ISD::UNDEF)
7246 return SDValue();
7247 // Skip the transformation if any of the types are illegal.
7248 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7249 EVT VT = N->getValueType(0);
7250 if (!TLI.isTypeLegal(VT) ||
7251 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
7252 !TLI.isTypeLegal(Concat1Op1.getValueType()))
7253 return SDValue();
7254
7255 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
7256 Op0.getOperand(0), Op1.getOperand(0));
7257 // Translate the shuffle mask.
7258 SmallVector<int, 16> NewMask;
7259 unsigned NumElts = VT.getVectorNumElements();
7260 unsigned HalfElts = NumElts/2;
7261 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
7262 for (unsigned n = 0; n < NumElts; ++n) {
7263 int MaskElt = SVN->getMaskElt(n);
7264 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007265 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00007266 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007267 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00007268 NewElt = HalfElts + MaskElt - NumElts;
7269 NewMask.push_back(NewElt);
7270 }
7271 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
7272 DAG.getUNDEF(VT), NewMask.data());
7273}
7274
Bob Wilson1c3ef902011-02-07 17:43:21 +00007275/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
7276/// NEON load/store intrinsics to merge base address updates.
7277static SDValue CombineBaseUpdate(SDNode *N,
7278 TargetLowering::DAGCombinerInfo &DCI) {
7279 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7280 return SDValue();
7281
7282 SelectionDAG &DAG = DCI.DAG;
7283 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
7284 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
7285 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
7286 SDValue Addr = N->getOperand(AddrOpIdx);
7287
7288 // Search for a use of the address operand that is an increment.
7289 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
7290 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
7291 SDNode *User = *UI;
7292 if (User->getOpcode() != ISD::ADD ||
7293 UI.getUse().getResNo() != Addr.getResNo())
7294 continue;
7295
7296 // Check that the add is independent of the load/store. Otherwise, folding
7297 // it would create a cycle.
7298 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
7299 continue;
7300
7301 // Find the new opcode for the updating load/store.
7302 bool isLoad = true;
7303 bool isLaneOp = false;
7304 unsigned NewOpc = 0;
7305 unsigned NumVecs = 0;
7306 if (isIntrinsic) {
7307 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
7308 switch (IntNo) {
7309 default: assert(0 && "unexpected intrinsic for Neon base update");
7310 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
7311 NumVecs = 1; break;
7312 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
7313 NumVecs = 2; break;
7314 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
7315 NumVecs = 3; break;
7316 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
7317 NumVecs = 4; break;
7318 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
7319 NumVecs = 2; isLaneOp = true; break;
7320 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
7321 NumVecs = 3; isLaneOp = true; break;
7322 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
7323 NumVecs = 4; isLaneOp = true; break;
7324 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
7325 NumVecs = 1; isLoad = false; break;
7326 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
7327 NumVecs = 2; isLoad = false; break;
7328 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
7329 NumVecs = 3; isLoad = false; break;
7330 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
7331 NumVecs = 4; isLoad = false; break;
7332 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
7333 NumVecs = 2; isLoad = false; isLaneOp = true; break;
7334 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
7335 NumVecs = 3; isLoad = false; isLaneOp = true; break;
7336 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
7337 NumVecs = 4; isLoad = false; isLaneOp = true; break;
7338 }
7339 } else {
7340 isLaneOp = true;
7341 switch (N->getOpcode()) {
7342 default: assert(0 && "unexpected opcode for Neon base update");
7343 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
7344 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
7345 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
7346 }
7347 }
7348
7349 // Find the size of memory referenced by the load/store.
7350 EVT VecTy;
7351 if (isLoad)
7352 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00007353 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00007354 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
7355 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
7356 if (isLaneOp)
7357 NumBytes /= VecTy.getVectorNumElements();
7358
7359 // If the increment is a constant, it must match the memory ref size.
7360 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
7361 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
7362 uint64_t IncVal = CInc->getZExtValue();
7363 if (IncVal != NumBytes)
7364 continue;
7365 } else if (NumBytes >= 3 * 16) {
7366 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
7367 // separate instructions that make it harder to use a non-constant update.
7368 continue;
7369 }
7370
7371 // Create the new updating load/store node.
7372 EVT Tys[6];
7373 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
7374 unsigned n;
7375 for (n = 0; n < NumResultVecs; ++n)
7376 Tys[n] = VecTy;
7377 Tys[n++] = MVT::i32;
7378 Tys[n] = MVT::Other;
7379 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
7380 SmallVector<SDValue, 8> Ops;
7381 Ops.push_back(N->getOperand(0)); // incoming chain
7382 Ops.push_back(N->getOperand(AddrOpIdx));
7383 Ops.push_back(Inc);
7384 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
7385 Ops.push_back(N->getOperand(i));
7386 }
7387 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
7388 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
7389 Ops.data(), Ops.size(),
7390 MemInt->getMemoryVT(),
7391 MemInt->getMemOperand());
7392
7393 // Update the uses.
7394 std::vector<SDValue> NewResults;
7395 for (unsigned i = 0; i < NumResultVecs; ++i) {
7396 NewResults.push_back(SDValue(UpdN.getNode(), i));
7397 }
7398 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
7399 DCI.CombineTo(N, NewResults);
7400 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
7401
7402 break;
Owen Anderson76706012011-04-05 21:48:57 +00007403 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00007404 return SDValue();
7405}
7406
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007407/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
7408/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
7409/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
7410/// return true.
7411static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
7412 SelectionDAG &DAG = DCI.DAG;
7413 EVT VT = N->getValueType(0);
7414 // vldN-dup instructions only support 64-bit vectors for N > 1.
7415 if (!VT.is64BitVector())
7416 return false;
7417
7418 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
7419 SDNode *VLD = N->getOperand(0).getNode();
7420 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
7421 return false;
7422 unsigned NumVecs = 0;
7423 unsigned NewOpc = 0;
7424 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
7425 if (IntNo == Intrinsic::arm_neon_vld2lane) {
7426 NumVecs = 2;
7427 NewOpc = ARMISD::VLD2DUP;
7428 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
7429 NumVecs = 3;
7430 NewOpc = ARMISD::VLD3DUP;
7431 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
7432 NumVecs = 4;
7433 NewOpc = ARMISD::VLD4DUP;
7434 } else {
7435 return false;
7436 }
7437
7438 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
7439 // numbers match the load.
7440 unsigned VLDLaneNo =
7441 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
7442 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
7443 UI != UE; ++UI) {
7444 // Ignore uses of the chain result.
7445 if (UI.getUse().getResNo() == NumVecs)
7446 continue;
7447 SDNode *User = *UI;
7448 if (User->getOpcode() != ARMISD::VDUPLANE ||
7449 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
7450 return false;
7451 }
7452
7453 // Create the vldN-dup node.
7454 EVT Tys[5];
7455 unsigned n;
7456 for (n = 0; n < NumVecs; ++n)
7457 Tys[n] = VT;
7458 Tys[n] = MVT::Other;
7459 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
7460 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
7461 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
7462 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
7463 Ops, 2, VLDMemInt->getMemoryVT(),
7464 VLDMemInt->getMemOperand());
7465
7466 // Update the uses.
7467 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
7468 UI != UE; ++UI) {
7469 unsigned ResNo = UI.getUse().getResNo();
7470 // Ignore uses of the chain result.
7471 if (ResNo == NumVecs)
7472 continue;
7473 SDNode *User = *UI;
7474 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
7475 }
7476
7477 // Now the vldN-lane intrinsic is dead except for its chain result.
7478 // Update uses of the chain.
7479 std::vector<SDValue> VLDDupResults;
7480 for (unsigned n = 0; n < NumVecs; ++n)
7481 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
7482 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
7483 DCI.CombineTo(VLD, VLDDupResults);
7484
7485 return true;
7486}
7487
Bob Wilson9e82bf12010-07-14 01:22:12 +00007488/// PerformVDUPLANECombine - Target-specific dag combine xforms for
7489/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007490static SDValue PerformVDUPLANECombine(SDNode *N,
7491 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00007492 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007493
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007494 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
7495 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
7496 if (CombineVLDDUP(N, DCI))
7497 return SDValue(N, 0);
7498
7499 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
7500 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007501 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00007502 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00007503 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00007504 return SDValue();
7505
7506 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
7507 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
7508 // The canonical VMOV for a zero vector uses a 32-bit element size.
7509 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7510 unsigned EltBits;
7511 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
7512 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007513 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007514 if (EltSize > VT.getVectorElementType().getSizeInBits())
7515 return SDValue();
7516
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007517 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007518}
7519
Eric Christopherfa6f5912011-06-29 21:10:36 +00007520// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00007521// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
7522static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
7523{
Chad Rosier118c9a02011-06-28 17:26:57 +00007524 integerPart cN;
7525 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00007526 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
7527 I != E; I++) {
7528 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
7529 if (!C)
7530 return false;
7531
Eric Christopherfa6f5912011-06-29 21:10:36 +00007532 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00007533 APFloat APF = C->getValueAPF();
7534 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
7535 != APFloat::opOK || !isExact)
7536 return false;
7537
7538 c0 = (I == 0) ? cN : c0;
7539 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
7540 return false;
7541 }
7542 C = c0;
7543 return true;
7544}
7545
7546/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
7547/// can replace combinations of VMUL and VCVT (floating-point to integer)
7548/// when the VMUL has a constant operand that is a power of 2.
7549///
7550/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
7551/// vmul.f32 d16, d17, d16
7552/// vcvt.s32.f32 d16, d16
7553/// becomes:
7554/// vcvt.s32.f32 d16, d16, #3
7555static SDValue PerformVCVTCombine(SDNode *N,
7556 TargetLowering::DAGCombinerInfo &DCI,
7557 const ARMSubtarget *Subtarget) {
7558 SelectionDAG &DAG = DCI.DAG;
7559 SDValue Op = N->getOperand(0);
7560
7561 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
7562 Op.getOpcode() != ISD::FMUL)
7563 return SDValue();
7564
7565 uint64_t C;
7566 SDValue N0 = Op->getOperand(0);
7567 SDValue ConstVec = Op->getOperand(1);
7568 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
7569
Eric Christopherfa6f5912011-06-29 21:10:36 +00007570 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00007571 !isConstVecPow2(ConstVec, isSigned, C))
7572 return SDValue();
7573
7574 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
7575 Intrinsic::arm_neon_vcvtfp2fxu;
7576 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7577 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00007578 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00007579 DAG.getConstant(Log2_64(C), MVT::i32));
7580}
7581
7582/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
7583/// can replace combinations of VCVT (integer to floating-point) and VDIV
7584/// when the VDIV has a constant operand that is a power of 2.
7585///
7586/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
7587/// vcvt.f32.s32 d16, d16
7588/// vdiv.f32 d16, d17, d16
7589/// becomes:
7590/// vcvt.f32.s32 d16, d16, #3
7591static SDValue PerformVDIVCombine(SDNode *N,
7592 TargetLowering::DAGCombinerInfo &DCI,
7593 const ARMSubtarget *Subtarget) {
7594 SelectionDAG &DAG = DCI.DAG;
7595 SDValue Op = N->getOperand(0);
7596 unsigned OpOpcode = Op.getNode()->getOpcode();
7597
7598 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
7599 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
7600 return SDValue();
7601
7602 uint64_t C;
7603 SDValue ConstVec = N->getOperand(1);
7604 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
7605
7606 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
7607 !isConstVecPow2(ConstVec, isSigned, C))
7608 return SDValue();
7609
Eric Christopherfa6f5912011-06-29 21:10:36 +00007610 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00007611 Intrinsic::arm_neon_vcvtfxu2fp;
7612 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7613 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00007614 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00007615 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
7616}
7617
7618/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00007619/// operand of a vector shift operation, where all the elements of the
7620/// build_vector must have the same constant integer value.
7621static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
7622 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007623 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00007624 Op = Op.getOperand(0);
7625 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
7626 APInt SplatBits, SplatUndef;
7627 unsigned SplatBitSize;
7628 bool HasAnyUndefs;
7629 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
7630 HasAnyUndefs, ElementBits) ||
7631 SplatBitSize > ElementBits)
7632 return false;
7633 Cnt = SplatBits.getSExtValue();
7634 return true;
7635}
7636
7637/// isVShiftLImm - Check if this is a valid build_vector for the immediate
7638/// operand of a vector shift left operation. That value must be in the range:
7639/// 0 <= Value < ElementBits for a left shift; or
7640/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00007641static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00007642 assert(VT.isVector() && "vector shift count is not a vector type");
7643 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
7644 if (! getVShiftImm(Op, ElementBits, Cnt))
7645 return false;
7646 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
7647}
7648
7649/// isVShiftRImm - Check if this is a valid build_vector for the immediate
7650/// operand of a vector shift right operation. For a shift opcode, the value
7651/// is positive, but for an intrinsic the value count must be negative. The
7652/// absolute value must be in the range:
7653/// 1 <= |Value| <= ElementBits for a right shift; or
7654/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00007655static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00007656 int64_t &Cnt) {
7657 assert(VT.isVector() && "vector shift count is not a vector type");
7658 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
7659 if (! getVShiftImm(Op, ElementBits, Cnt))
7660 return false;
7661 if (isIntrinsic)
7662 Cnt = -Cnt;
7663 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
7664}
7665
7666/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
7667static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
7668 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
7669 switch (IntNo) {
7670 default:
7671 // Don't do anything for most intrinsics.
7672 break;
7673
7674 // Vector shifts: check for immediate versions and lower them.
7675 // Note: This is done during DAG combining instead of DAG legalizing because
7676 // the build_vectors for 64-bit vector element shift counts are generally
7677 // not legal, and it is hard to see their values after they get legalized to
7678 // loads from a constant pool.
7679 case Intrinsic::arm_neon_vshifts:
7680 case Intrinsic::arm_neon_vshiftu:
7681 case Intrinsic::arm_neon_vshiftls:
7682 case Intrinsic::arm_neon_vshiftlu:
7683 case Intrinsic::arm_neon_vshiftn:
7684 case Intrinsic::arm_neon_vrshifts:
7685 case Intrinsic::arm_neon_vrshiftu:
7686 case Intrinsic::arm_neon_vrshiftn:
7687 case Intrinsic::arm_neon_vqshifts:
7688 case Intrinsic::arm_neon_vqshiftu:
7689 case Intrinsic::arm_neon_vqshiftsu:
7690 case Intrinsic::arm_neon_vqshiftns:
7691 case Intrinsic::arm_neon_vqshiftnu:
7692 case Intrinsic::arm_neon_vqshiftnsu:
7693 case Intrinsic::arm_neon_vqrshiftns:
7694 case Intrinsic::arm_neon_vqrshiftnu:
7695 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00007696 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007697 int64_t Cnt;
7698 unsigned VShiftOpc = 0;
7699
7700 switch (IntNo) {
7701 case Intrinsic::arm_neon_vshifts:
7702 case Intrinsic::arm_neon_vshiftu:
7703 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
7704 VShiftOpc = ARMISD::VSHL;
7705 break;
7706 }
7707 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
7708 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
7709 ARMISD::VSHRs : ARMISD::VSHRu);
7710 break;
7711 }
7712 return SDValue();
7713
7714 case Intrinsic::arm_neon_vshiftls:
7715 case Intrinsic::arm_neon_vshiftlu:
7716 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
7717 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007718 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007719
7720 case Intrinsic::arm_neon_vrshifts:
7721 case Intrinsic::arm_neon_vrshiftu:
7722 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
7723 break;
7724 return SDValue();
7725
7726 case Intrinsic::arm_neon_vqshifts:
7727 case Intrinsic::arm_neon_vqshiftu:
7728 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
7729 break;
7730 return SDValue();
7731
7732 case Intrinsic::arm_neon_vqshiftsu:
7733 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
7734 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007735 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007736
7737 case Intrinsic::arm_neon_vshiftn:
7738 case Intrinsic::arm_neon_vrshiftn:
7739 case Intrinsic::arm_neon_vqshiftns:
7740 case Intrinsic::arm_neon_vqshiftnu:
7741 case Intrinsic::arm_neon_vqshiftnsu:
7742 case Intrinsic::arm_neon_vqrshiftns:
7743 case Intrinsic::arm_neon_vqrshiftnu:
7744 case Intrinsic::arm_neon_vqrshiftnsu:
7745 // Narrowing shifts require an immediate right shift.
7746 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
7747 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00007748 llvm_unreachable("invalid shift count for narrowing vector shift "
7749 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007750
7751 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00007752 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00007753 }
7754
7755 switch (IntNo) {
7756 case Intrinsic::arm_neon_vshifts:
7757 case Intrinsic::arm_neon_vshiftu:
7758 // Opcode already set above.
7759 break;
7760 case Intrinsic::arm_neon_vshiftls:
7761 case Intrinsic::arm_neon_vshiftlu:
7762 if (Cnt == VT.getVectorElementType().getSizeInBits())
7763 VShiftOpc = ARMISD::VSHLLi;
7764 else
7765 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
7766 ARMISD::VSHLLs : ARMISD::VSHLLu);
7767 break;
7768 case Intrinsic::arm_neon_vshiftn:
7769 VShiftOpc = ARMISD::VSHRN; break;
7770 case Intrinsic::arm_neon_vrshifts:
7771 VShiftOpc = ARMISD::VRSHRs; break;
7772 case Intrinsic::arm_neon_vrshiftu:
7773 VShiftOpc = ARMISD::VRSHRu; break;
7774 case Intrinsic::arm_neon_vrshiftn:
7775 VShiftOpc = ARMISD::VRSHRN; break;
7776 case Intrinsic::arm_neon_vqshifts:
7777 VShiftOpc = ARMISD::VQSHLs; break;
7778 case Intrinsic::arm_neon_vqshiftu:
7779 VShiftOpc = ARMISD::VQSHLu; break;
7780 case Intrinsic::arm_neon_vqshiftsu:
7781 VShiftOpc = ARMISD::VQSHLsu; break;
7782 case Intrinsic::arm_neon_vqshiftns:
7783 VShiftOpc = ARMISD::VQSHRNs; break;
7784 case Intrinsic::arm_neon_vqshiftnu:
7785 VShiftOpc = ARMISD::VQSHRNu; break;
7786 case Intrinsic::arm_neon_vqshiftnsu:
7787 VShiftOpc = ARMISD::VQSHRNsu; break;
7788 case Intrinsic::arm_neon_vqrshiftns:
7789 VShiftOpc = ARMISD::VQRSHRNs; break;
7790 case Intrinsic::arm_neon_vqrshiftnu:
7791 VShiftOpc = ARMISD::VQRSHRNu; break;
7792 case Intrinsic::arm_neon_vqrshiftnsu:
7793 VShiftOpc = ARMISD::VQRSHRNsu; break;
7794 }
7795
7796 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007797 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007798 }
7799
7800 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00007801 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007802 int64_t Cnt;
7803 unsigned VShiftOpc = 0;
7804
7805 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
7806 VShiftOpc = ARMISD::VSLI;
7807 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
7808 VShiftOpc = ARMISD::VSRI;
7809 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00007810 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007811 }
7812
7813 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
7814 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00007815 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007816 }
7817
7818 case Intrinsic::arm_neon_vqrshifts:
7819 case Intrinsic::arm_neon_vqrshiftu:
7820 // No immediate versions of these to check for.
7821 break;
7822 }
7823
7824 return SDValue();
7825}
7826
7827/// PerformShiftCombine - Checks for immediate versions of vector shifts and
7828/// lowers them. As with the vector shift intrinsics, this is done during DAG
7829/// combining instead of DAG legalizing because the build_vectors for 64-bit
7830/// vector element shift counts are generally not legal, and it is hard to see
7831/// their values after they get legalized to loads from a constant pool.
7832static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
7833 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00007834 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00007835
7836 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00007837 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7838 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00007839 return SDValue();
7840
7841 assert(ST->hasNEON() && "unexpected vector shift");
7842 int64_t Cnt;
7843
7844 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007845 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00007846
7847 case ISD::SHL:
7848 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
7849 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007850 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007851 break;
7852
7853 case ISD::SRA:
7854 case ISD::SRL:
7855 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
7856 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
7857 ARMISD::VSHRs : ARMISD::VSHRu);
7858 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007859 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007860 }
7861 }
7862 return SDValue();
7863}
7864
7865/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
7866/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
7867static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
7868 const ARMSubtarget *ST) {
7869 SDValue N0 = N->getOperand(0);
7870
7871 // Check for sign- and zero-extensions of vector extract operations of 8-
7872 // and 16-bit vector elements. NEON supports these directly. They are
7873 // handled during DAG combining because type legalization will promote them
7874 // to 32-bit types and it is messy to recognize the operations after that.
7875 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
7876 SDValue Vec = N0.getOperand(0);
7877 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00007878 EVT VT = N->getValueType(0);
7879 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007880 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7881
Owen Anderson825b72b2009-08-11 20:47:22 +00007882 if (VT == MVT::i32 &&
7883 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00007884 TLI.isTypeLegal(Vec.getValueType()) &&
7885 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00007886
7887 unsigned Opc = 0;
7888 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007889 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00007890 case ISD::SIGN_EXTEND:
7891 Opc = ARMISD::VGETLANEs;
7892 break;
7893 case ISD::ZERO_EXTEND:
7894 case ISD::ANY_EXTEND:
7895 Opc = ARMISD::VGETLANEu;
7896 break;
7897 }
7898 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
7899 }
7900 }
7901
7902 return SDValue();
7903}
7904
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007905/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
7906/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
7907static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
7908 const ARMSubtarget *ST) {
7909 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00007910 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007911 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
7912 // a NaN; only do the transformation when it matches that behavior.
7913
7914 // For now only do this when using NEON for FP operations; if using VFP, it
7915 // is not obvious that the benefit outweighs the cost of switching to the
7916 // NEON pipeline.
7917 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
7918 N->getValueType(0) != MVT::f32)
7919 return SDValue();
7920
7921 SDValue CondLHS = N->getOperand(0);
7922 SDValue CondRHS = N->getOperand(1);
7923 SDValue LHS = N->getOperand(2);
7924 SDValue RHS = N->getOperand(3);
7925 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
7926
7927 unsigned Opcode = 0;
7928 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00007929 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007930 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00007931 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007932 IsReversed = true ; // x CC y ? y : x
7933 } else {
7934 return SDValue();
7935 }
7936
Bob Wilsone742bb52010-02-24 22:15:53 +00007937 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007938 switch (CC) {
7939 default: break;
7940 case ISD::SETOLT:
7941 case ISD::SETOLE:
7942 case ISD::SETLT:
7943 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007944 case ISD::SETULT:
7945 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00007946 // If LHS is NaN, an ordered comparison will be false and the result will
7947 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
7948 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
7949 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
7950 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
7951 break;
7952 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
7953 // will return -0, so vmin can only be used for unsafe math or if one of
7954 // the operands is known to be nonzero.
7955 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00007956 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00007957 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
7958 break;
7959 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007960 break;
7961
7962 case ISD::SETOGT:
7963 case ISD::SETOGE:
7964 case ISD::SETGT:
7965 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007966 case ISD::SETUGT:
7967 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00007968 // If LHS is NaN, an ordered comparison will be false and the result will
7969 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
7970 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
7971 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
7972 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
7973 break;
7974 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
7975 // will return +0, so vmax can only be used for unsafe math or if one of
7976 // the operands is known to be nonzero.
7977 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00007978 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00007979 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
7980 break;
7981 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007982 break;
7983 }
7984
7985 if (!Opcode)
7986 return SDValue();
7987 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
7988}
7989
Evan Chenge721f5c2011-07-13 00:42:17 +00007990/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
7991SDValue
7992ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
7993 SDValue Cmp = N->getOperand(4);
7994 if (Cmp.getOpcode() != ARMISD::CMPZ)
7995 // Only looking at EQ and NE cases.
7996 return SDValue();
7997
7998 EVT VT = N->getValueType(0);
7999 DebugLoc dl = N->getDebugLoc();
8000 SDValue LHS = Cmp.getOperand(0);
8001 SDValue RHS = Cmp.getOperand(1);
8002 SDValue FalseVal = N->getOperand(0);
8003 SDValue TrueVal = N->getOperand(1);
8004 SDValue ARMcc = N->getOperand(2);
Jim Grosbachb04546f2011-09-13 20:30:37 +00008005 ARMCC::CondCodes CC =
8006 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chenge721f5c2011-07-13 00:42:17 +00008007
8008 // Simplify
8009 // mov r1, r0
8010 // cmp r1, x
8011 // mov r0, y
8012 // moveq r0, x
8013 // to
8014 // cmp r0, x
8015 // movne r0, y
8016 //
8017 // mov r1, r0
8018 // cmp r1, x
8019 // mov r0, x
8020 // movne r0, y
8021 // to
8022 // cmp r0, x
8023 // movne r0, y
8024 /// FIXME: Turn this into a target neutral optimization?
8025 SDValue Res;
Evan Cheng9b88d2d2011-09-28 23:16:31 +00008026 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chenge721f5c2011-07-13 00:42:17 +00008027 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
8028 N->getOperand(3), Cmp);
8029 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
8030 SDValue ARMcc;
8031 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
8032 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
8033 N->getOperand(3), NewCmp);
8034 }
8035
8036 if (Res.getNode()) {
8037 APInt KnownZero, KnownOne;
8038 APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits());
8039 DAG.ComputeMaskedBits(SDValue(N,0), Mask, KnownZero, KnownOne);
8040 // Capture demanded bits information that would be otherwise lost.
8041 if (KnownZero == 0xfffffffe)
8042 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8043 DAG.getValueType(MVT::i1));
8044 else if (KnownZero == 0xffffff00)
8045 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8046 DAG.getValueType(MVT::i8));
8047 else if (KnownZero == 0xffff0000)
8048 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8049 DAG.getValueType(MVT::i16));
8050 }
8051
8052 return Res;
8053}
8054
Dan Gohman475871a2008-07-27 21:46:04 +00008055SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008056 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008057 switch (N->getOpcode()) {
8058 default: break;
Tanya Lattner189531f2011-06-14 23:48:48 +00008059 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008060 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00008061 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008062 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00008063 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00008064 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00008065 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008066 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00008067 case ISD::STORE: return PerformSTORECombine(N, DCI);
8068 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
8069 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00008070 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008071 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00008072 case ISD::FP_TO_SINT:
8073 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
8074 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008075 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00008076 case ISD::SHL:
8077 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008078 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00008079 case ISD::SIGN_EXTEND:
8080 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008081 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
8082 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00008083 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00008084 case ARMISD::VLD2DUP:
8085 case ARMISD::VLD3DUP:
8086 case ARMISD::VLD4DUP:
8087 return CombineBaseUpdate(N, DCI);
8088 case ISD::INTRINSIC_VOID:
8089 case ISD::INTRINSIC_W_CHAIN:
8090 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
8091 case Intrinsic::arm_neon_vld1:
8092 case Intrinsic::arm_neon_vld2:
8093 case Intrinsic::arm_neon_vld3:
8094 case Intrinsic::arm_neon_vld4:
8095 case Intrinsic::arm_neon_vld2lane:
8096 case Intrinsic::arm_neon_vld3lane:
8097 case Intrinsic::arm_neon_vld4lane:
8098 case Intrinsic::arm_neon_vst1:
8099 case Intrinsic::arm_neon_vst2:
8100 case Intrinsic::arm_neon_vst3:
8101 case Intrinsic::arm_neon_vst4:
8102 case Intrinsic::arm_neon_vst2lane:
8103 case Intrinsic::arm_neon_vst3lane:
8104 case Intrinsic::arm_neon_vst4lane:
8105 return CombineBaseUpdate(N, DCI);
8106 default: break;
8107 }
8108 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008109 }
Dan Gohman475871a2008-07-27 21:46:04 +00008110 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008111}
8112
Evan Cheng31959b12011-02-02 01:06:55 +00008113bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
8114 EVT VT) const {
8115 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
8116}
8117
Bill Wendlingaf566342009-08-15 21:21:19 +00008118bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00008119 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00008120 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00008121
8122 switch (VT.getSimpleVT().SimpleTy) {
8123 default:
8124 return false;
8125 case MVT::i8:
8126 case MVT::i16:
8127 case MVT::i32:
8128 return true;
8129 // FIXME: VLD1 etc with standard alignment is legal.
8130 }
8131}
8132
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008133static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
8134 unsigned AlignCheck) {
8135 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
8136 (DstAlign == 0 || DstAlign % AlignCheck == 0));
8137}
8138
8139EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
8140 unsigned DstAlign, unsigned SrcAlign,
Lang Hamesa1e78882011-11-02 23:37:04 +00008141 bool IsZeroVal,
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008142 bool MemcpyStrSrc,
8143 MachineFunction &MF) const {
8144 const Function *F = MF.getFunction();
8145
8146 // See if we can use NEON instructions for this...
Lang Hamesa1e78882011-11-02 23:37:04 +00008147 if (IsZeroVal &&
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008148 !F->hasFnAttr(Attribute::NoImplicitFloat) &&
8149 Subtarget->hasNEON()) {
8150 if (memOpAlign(SrcAlign, DstAlign, 16) && Size >= 16) {
8151 return MVT::v4i32;
8152 } else if (memOpAlign(SrcAlign, DstAlign, 8) && Size >= 8) {
8153 return MVT::v2i32;
8154 }
8155 }
8156
Lang Hames5207bf22011-11-08 18:56:23 +00008157 // Lowering to i32/i16 if the size permits.
8158 if (Size >= 4) {
8159 return MVT::i32;
8160 } else if (Size >= 2) {
8161 return MVT::i16;
8162 }
8163
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008164 // Let the target-independent logic figure it out.
8165 return MVT::Other;
8166}
8167
Evan Chenge6c835f2009-08-14 20:09:37 +00008168static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
8169 if (V < 0)
8170 return false;
8171
8172 unsigned Scale = 1;
8173 switch (VT.getSimpleVT().SimpleTy) {
8174 default: return false;
8175 case MVT::i1:
8176 case MVT::i8:
8177 // Scale == 1;
8178 break;
8179 case MVT::i16:
8180 // Scale == 2;
8181 Scale = 2;
8182 break;
8183 case MVT::i32:
8184 // Scale == 4;
8185 Scale = 4;
8186 break;
8187 }
8188
8189 if ((V & (Scale - 1)) != 0)
8190 return false;
8191 V /= Scale;
8192 return V == (V & ((1LL << 5) - 1));
8193}
8194
8195static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
8196 const ARMSubtarget *Subtarget) {
8197 bool isNeg = false;
8198 if (V < 0) {
8199 isNeg = true;
8200 V = - V;
8201 }
8202
8203 switch (VT.getSimpleVT().SimpleTy) {
8204 default: return false;
8205 case MVT::i1:
8206 case MVT::i8:
8207 case MVT::i16:
8208 case MVT::i32:
8209 // + imm12 or - imm8
8210 if (isNeg)
8211 return V == (V & ((1LL << 8) - 1));
8212 return V == (V & ((1LL << 12) - 1));
8213 case MVT::f32:
8214 case MVT::f64:
8215 // Same as ARM mode. FIXME: NEON?
8216 if (!Subtarget->hasVFP2())
8217 return false;
8218 if ((V & 3) != 0)
8219 return false;
8220 V >>= 2;
8221 return V == (V & ((1LL << 8) - 1));
8222 }
8223}
8224
Evan Chengb01fad62007-03-12 23:30:29 +00008225/// isLegalAddressImmediate - Return true if the integer value can be used
8226/// as the offset of the target addressing mode for load / store of the
8227/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00008228static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00008229 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00008230 if (V == 0)
8231 return true;
8232
Evan Cheng65011532009-03-09 19:15:00 +00008233 if (!VT.isSimple())
8234 return false;
8235
Evan Chenge6c835f2009-08-14 20:09:37 +00008236 if (Subtarget->isThumb1Only())
8237 return isLegalT1AddressImmediate(V, VT);
8238 else if (Subtarget->isThumb2())
8239 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00008240
Evan Chenge6c835f2009-08-14 20:09:37 +00008241 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00008242 if (V < 0)
8243 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00008244 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00008245 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008246 case MVT::i1:
8247 case MVT::i8:
8248 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00008249 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008250 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008251 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00008252 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008253 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008254 case MVT::f32:
8255 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00008256 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00008257 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00008258 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00008259 return false;
8260 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008261 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00008262 }
Evan Chenga8e29892007-01-19 07:51:42 +00008263}
8264
Evan Chenge6c835f2009-08-14 20:09:37 +00008265bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
8266 EVT VT) const {
8267 int Scale = AM.Scale;
8268 if (Scale < 0)
8269 return false;
8270
8271 switch (VT.getSimpleVT().SimpleTy) {
8272 default: return false;
8273 case MVT::i1:
8274 case MVT::i8:
8275 case MVT::i16:
8276 case MVT::i32:
8277 if (Scale == 1)
8278 return true;
8279 // r + r << imm
8280 Scale = Scale & ~1;
8281 return Scale == 2 || Scale == 4 || Scale == 8;
8282 case MVT::i64:
8283 // r + r
8284 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
8285 return true;
8286 return false;
8287 case MVT::isVoid:
8288 // Note, we allow "void" uses (basically, uses that aren't loads or
8289 // stores), because arm allows folding a scale into many arithmetic
8290 // operations. This should be made more precise and revisited later.
8291
8292 // Allow r << imm, but the imm has to be a multiple of two.
8293 if (Scale & 1) return false;
8294 return isPowerOf2_32(Scale);
8295 }
8296}
8297
Chris Lattner37caf8c2007-04-09 23:33:39 +00008298/// isLegalAddressingMode - Return true if the addressing mode represented
8299/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008300bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008301 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008302 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00008303 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00008304 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008305
Chris Lattner37caf8c2007-04-09 23:33:39 +00008306 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008307 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008308 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008309
Chris Lattner37caf8c2007-04-09 23:33:39 +00008310 switch (AM.Scale) {
8311 case 0: // no scale reg, must be "r+i" or "r", or "i".
8312 break;
8313 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00008314 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00008315 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008316 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00008317 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008318 // ARM doesn't support any R+R*scale+imm addr modes.
8319 if (AM.BaseOffs)
8320 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008321
Bob Wilson2c7dab12009-04-08 17:55:28 +00008322 if (!VT.isSimple())
8323 return false;
8324
Evan Chenge6c835f2009-08-14 20:09:37 +00008325 if (Subtarget->isThumb2())
8326 return isLegalT2ScaledAddressingMode(AM, VT);
8327
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008328 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00008329 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00008330 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008331 case MVT::i1:
8332 case MVT::i8:
8333 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008334 if (Scale < 0) Scale = -Scale;
8335 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008336 return true;
8337 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00008338 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008339 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00008340 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00008341 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008342 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008343 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00008344 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008345
Owen Anderson825b72b2009-08-11 20:47:22 +00008346 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00008347 // Note, we allow "void" uses (basically, uses that aren't loads or
8348 // stores), because arm allows folding a scale into many arithmetic
8349 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008350
Chris Lattner37caf8c2007-04-09 23:33:39 +00008351 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00008352 if (Scale & 1) return false;
8353 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00008354 }
8355 break;
Evan Chengb01fad62007-03-12 23:30:29 +00008356 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00008357 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00008358}
8359
Evan Cheng77e47512009-11-11 19:05:52 +00008360/// isLegalICmpImmediate - Return true if the specified immediate is legal
8361/// icmp immediate, that is the target has icmp instructions which can compare
8362/// a register against the immediate without having to materialize the
8363/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00008364bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00008365 if (!Subtarget->isThumb())
8366 return ARM_AM::getSOImmVal(Imm) != -1;
8367 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00008368 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00008369 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00008370}
8371
Dan Gohmancca82142011-05-03 00:46:49 +00008372/// isLegalAddImmediate - Return true if the specified immediate is legal
8373/// add immediate, that is the target has add instructions which can add
8374/// a register with the immediate without having to materialize the
8375/// immediate into a register.
8376bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
8377 return ARM_AM::getSOImmVal(Imm) != -1;
8378}
8379
Owen Andersone50ed302009-08-10 22:56:29 +00008380static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00008381 bool isSEXTLoad, SDValue &Base,
8382 SDValue &Offset, bool &isInc,
8383 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00008384 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
8385 return false;
8386
Owen Anderson825b72b2009-08-11 20:47:22 +00008387 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00008388 // AddressingMode 3
8389 Base = Ptr->getOperand(0);
8390 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008391 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00008392 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008393 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00008394 isInc = false;
8395 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8396 return true;
8397 }
8398 }
8399 isInc = (Ptr->getOpcode() == ISD::ADD);
8400 Offset = Ptr->getOperand(1);
8401 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00008402 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00008403 // AddressingMode 2
8404 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008405 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00008406 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008407 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00008408 isInc = false;
8409 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8410 Base = Ptr->getOperand(0);
8411 return true;
8412 }
8413 }
8414
8415 if (Ptr->getOpcode() == ISD::ADD) {
8416 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00008417 ARM_AM::ShiftOpc ShOpcVal=
8418 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00008419 if (ShOpcVal != ARM_AM::no_shift) {
8420 Base = Ptr->getOperand(1);
8421 Offset = Ptr->getOperand(0);
8422 } else {
8423 Base = Ptr->getOperand(0);
8424 Offset = Ptr->getOperand(1);
8425 }
8426 return true;
8427 }
8428
8429 isInc = (Ptr->getOpcode() == ISD::ADD);
8430 Base = Ptr->getOperand(0);
8431 Offset = Ptr->getOperand(1);
8432 return true;
8433 }
8434
Jim Grosbache5165492009-11-09 00:11:35 +00008435 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00008436 return false;
8437}
8438
Owen Andersone50ed302009-08-10 22:56:29 +00008439static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00008440 bool isSEXTLoad, SDValue &Base,
8441 SDValue &Offset, bool &isInc,
8442 SelectionDAG &DAG) {
8443 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
8444 return false;
8445
8446 Base = Ptr->getOperand(0);
8447 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
8448 int RHSC = (int)RHS->getZExtValue();
8449 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
8450 assert(Ptr->getOpcode() == ISD::ADD);
8451 isInc = false;
8452 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8453 return true;
8454 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
8455 isInc = Ptr->getOpcode() == ISD::ADD;
8456 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
8457 return true;
8458 }
8459 }
8460
8461 return false;
8462}
8463
Evan Chenga8e29892007-01-19 07:51:42 +00008464/// getPreIndexedAddressParts - returns true by value, base pointer and
8465/// offset pointer and addressing mode by reference if the node's address
8466/// can be legally represented as pre-indexed load / store address.
8467bool
Dan Gohman475871a2008-07-27 21:46:04 +00008468ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
8469 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00008470 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00008471 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008472 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00008473 return false;
8474
Owen Andersone50ed302009-08-10 22:56:29 +00008475 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00008476 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00008477 bool isSEXTLoad = false;
8478 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
8479 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008480 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00008481 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
8482 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
8483 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008484 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00008485 } else
8486 return false;
8487
8488 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00008489 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00008490 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00008491 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
8492 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00008493 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00008494 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00008495 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00008496 if (!isLegal)
8497 return false;
8498
8499 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
8500 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00008501}
8502
8503/// getPostIndexedAddressParts - returns true by value, base pointer and
8504/// offset pointer and addressing mode by reference if this node can be
8505/// combined with a load / store to form a post-indexed load / store.
8506bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00008507 SDValue &Base,
8508 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00008509 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00008510 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008511 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00008512 return false;
8513
Owen Andersone50ed302009-08-10 22:56:29 +00008514 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00008515 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00008516 bool isSEXTLoad = false;
8517 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008518 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00008519 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00008520 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
8521 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008522 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00008523 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00008524 } else
8525 return false;
8526
8527 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00008528 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00008529 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00008530 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00008531 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00008532 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00008533 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
8534 isInc, DAG);
8535 if (!isLegal)
8536 return false;
8537
Evan Cheng28dad2a2010-05-18 21:31:17 +00008538 if (Ptr != Base) {
8539 // Swap base ptr and offset to catch more post-index load / store when
8540 // it's legal. In Thumb2 mode, offset must be an immediate.
8541 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
8542 !Subtarget->isThumb2())
8543 std::swap(Base, Offset);
8544
8545 // Post-indexed load / store update the base pointer.
8546 if (Ptr != Base)
8547 return false;
8548 }
8549
Evan Chenge88d5ce2009-07-02 07:28:31 +00008550 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
8551 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00008552}
8553
Dan Gohman475871a2008-07-27 21:46:04 +00008554void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00008555 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008556 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008557 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008558 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00008559 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008560 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00008561 switch (Op.getOpcode()) {
8562 default: break;
8563 case ARMISD::CMOV: {
8564 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00008565 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00008566 if (KnownZero == 0 && KnownOne == 0) return;
8567
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008568 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00008569 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
8570 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00008571 KnownZero &= KnownZeroRHS;
8572 KnownOne &= KnownOneRHS;
8573 return;
8574 }
8575 }
8576}
8577
8578//===----------------------------------------------------------------------===//
8579// ARM Inline Assembly Support
8580//===----------------------------------------------------------------------===//
8581
Evan Cheng55d42002011-01-08 01:24:27 +00008582bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
8583 // Looking for "rev" which is V6+.
8584 if (!Subtarget->hasV6Ops())
8585 return false;
8586
8587 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
8588 std::string AsmStr = IA->getAsmString();
8589 SmallVector<StringRef, 4> AsmPieces;
8590 SplitString(AsmStr, AsmPieces, ";\n");
8591
8592 switch (AsmPieces.size()) {
8593 default: return false;
8594 case 1:
8595 AsmStr = AsmPieces[0];
8596 AsmPieces.clear();
8597 SplitString(AsmStr, AsmPieces, " \t,");
8598
8599 // rev $0, $1
8600 if (AsmPieces.size() == 3 &&
8601 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
8602 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008603 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00008604 if (Ty && Ty->getBitWidth() == 32)
8605 return IntrinsicLowering::LowerToByteSwap(CI);
8606 }
8607 break;
8608 }
8609
8610 return false;
8611}
8612
Evan Chenga8e29892007-01-19 07:51:42 +00008613/// getConstraintType - Given a constraint letter, return the type of
8614/// constraint it is for this target.
8615ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00008616ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
8617 if (Constraint.size() == 1) {
8618 switch (Constraint[0]) {
8619 default: break;
8620 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008621 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00008622 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00008623 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008624 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00008625 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00008626 // An address with a single base register. Due to the way we
8627 // currently handle addresses it is the same as an 'r' memory constraint.
8628 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00008629 }
Eric Christopher1312ca82011-06-21 22:10:57 +00008630 } else if (Constraint.size() == 2) {
8631 switch (Constraint[0]) {
8632 default: break;
8633 // All 'U+' constraints are addresses.
8634 case 'U': return C_Memory;
8635 }
Evan Chenga8e29892007-01-19 07:51:42 +00008636 }
Chris Lattner4234f572007-03-25 02:14:49 +00008637 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00008638}
8639
John Thompson44ab89e2010-10-29 17:29:13 +00008640/// Examine constraint type and operand type and determine a weight value.
8641/// This object must already have been set up with the operand type
8642/// and the current alternative constraint selected.
8643TargetLowering::ConstraintWeight
8644ARMTargetLowering::getSingleConstraintMatchWeight(
8645 AsmOperandInfo &info, const char *constraint) const {
8646 ConstraintWeight weight = CW_Invalid;
8647 Value *CallOperandVal = info.CallOperandVal;
8648 // If we don't have a value, we can't do a match,
8649 // but allow it at the lowest weight.
8650 if (CallOperandVal == NULL)
8651 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008652 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00008653 // Look at the constraint type.
8654 switch (*constraint) {
8655 default:
8656 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
8657 break;
8658 case 'l':
8659 if (type->isIntegerTy()) {
8660 if (Subtarget->isThumb())
8661 weight = CW_SpecificReg;
8662 else
8663 weight = CW_Register;
8664 }
8665 break;
8666 case 'w':
8667 if (type->isFloatingPointTy())
8668 weight = CW_Register;
8669 break;
8670 }
8671 return weight;
8672}
8673
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008674typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
8675RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00008676ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00008677 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00008678 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00008679 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00008680 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00008681 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00008682 if (Subtarget->isThumb())
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008683 return RCPair(0U, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +00008684 else
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008685 return RCPair(0U, ARM::GPRRegisterClass);
Eric Christopher73744df2011-06-30 23:23:01 +00008686 case 'h': // High regs or no regs.
8687 if (Subtarget->isThumb())
Andrew Trick3af7a672011-09-20 03:06:13 +00008688 return RCPair(0U, ARM::hGPRRegisterClass);
Eric Christopher1070f822011-07-01 00:19:27 +00008689 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008690 case 'r':
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008691 return RCPair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008692 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00008693 if (VT == MVT::f32)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008694 return RCPair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00008695 if (VT.getSizeInBits() == 64)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008696 return RCPair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00008697 if (VT.getSizeInBits() == 128)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008698 return RCPair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008699 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00008700 case 'x':
8701 if (VT == MVT::f32)
Andrew Trick3af7a672011-09-20 03:06:13 +00008702 return RCPair(0U, ARM::SPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008703 if (VT.getSizeInBits() == 64)
Andrew Trick3af7a672011-09-20 03:06:13 +00008704 return RCPair(0U, ARM::DPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008705 if (VT.getSizeInBits() == 128)
Andrew Trick3af7a672011-09-20 03:06:13 +00008706 return RCPair(0U, ARM::QPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008707 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008708 case 't':
8709 if (VT == MVT::f32)
Andrew Trick3af7a672011-09-20 03:06:13 +00008710 return RCPair(0U, ARM::SPRRegisterClass);
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008711 break;
Evan Chenga8e29892007-01-19 07:51:42 +00008712 }
8713 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00008714 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00008715 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00008716
Evan Chenga8e29892007-01-19 07:51:42 +00008717 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
8718}
8719
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008720/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
8721/// vector. If it is invalid, don't add anything to Ops.
8722void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00008723 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008724 std::vector<SDValue>&Ops,
8725 SelectionDAG &DAG) const {
8726 SDValue Result(0, 0);
8727
Eric Christopher100c8332011-06-02 23:16:42 +00008728 // Currently only support length 1 constraints.
8729 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00008730
Eric Christopher100c8332011-06-02 23:16:42 +00008731 char ConstraintLetter = Constraint[0];
8732 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008733 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00008734 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008735 case 'I': case 'J': case 'K': case 'L':
8736 case 'M': case 'N': case 'O':
8737 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
8738 if (!C)
8739 return;
8740
8741 int64_t CVal64 = C->getSExtValue();
8742 int CVal = (int) CVal64;
8743 // None of these constraints allow values larger than 32 bits. Check
8744 // that the value fits in an int.
8745 if (CVal != CVal64)
8746 return;
8747
Eric Christopher100c8332011-06-02 23:16:42 +00008748 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00008749 case 'j':
Andrew Trick3af7a672011-09-20 03:06:13 +00008750 // Constant suitable for movw, must be between 0 and
8751 // 65535.
8752 if (Subtarget->hasV6T2Ops())
8753 if (CVal >= 0 && CVal <= 65535)
8754 break;
8755 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008756 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008757 if (Subtarget->isThumb1Only()) {
8758 // This must be a constant between 0 and 255, for ADD
8759 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008760 if (CVal >= 0 && CVal <= 255)
8761 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008762 } else if (Subtarget->isThumb2()) {
8763 // A constant that can be used as an immediate value in a
8764 // data-processing instruction.
8765 if (ARM_AM::getT2SOImmVal(CVal) != -1)
8766 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008767 } else {
8768 // A constant that can be used as an immediate value in a
8769 // data-processing instruction.
8770 if (ARM_AM::getSOImmVal(CVal) != -1)
8771 break;
8772 }
8773 return;
8774
8775 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008776 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008777 // This must be a constant between -255 and -1, for negated ADD
8778 // immediates. This can be used in GCC with an "n" modifier that
8779 // prints the negated value, for use with SUB instructions. It is
8780 // not useful otherwise but is implemented for compatibility.
8781 if (CVal >= -255 && CVal <= -1)
8782 break;
8783 } else {
8784 // This must be a constant between -4095 and 4095. It is not clear
8785 // what this constraint is intended for. Implemented for
8786 // compatibility with GCC.
8787 if (CVal >= -4095 && CVal <= 4095)
8788 break;
8789 }
8790 return;
8791
8792 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008793 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008794 // A 32-bit value where only one byte has a nonzero value. Exclude
8795 // zero to match GCC. This constraint is used by GCC internally for
8796 // constants that can be loaded with a move/shift combination.
8797 // It is not useful otherwise but is implemented for compatibility.
8798 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
8799 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008800 } else if (Subtarget->isThumb2()) {
8801 // A constant whose bitwise inverse can be used as an immediate
8802 // value in a data-processing instruction. This can be used in GCC
8803 // with a "B" modifier that prints the inverted value, for use with
8804 // BIC and MVN instructions. It is not useful otherwise but is
8805 // implemented for compatibility.
8806 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
8807 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008808 } else {
8809 // A constant whose bitwise inverse can be used as an immediate
8810 // value in a data-processing instruction. This can be used in GCC
8811 // with a "B" modifier that prints the inverted value, for use with
8812 // BIC and MVN instructions. It is not useful otherwise but is
8813 // implemented for compatibility.
8814 if (ARM_AM::getSOImmVal(~CVal) != -1)
8815 break;
8816 }
8817 return;
8818
8819 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008820 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008821 // This must be a constant between -7 and 7,
8822 // for 3-operand ADD/SUB immediate instructions.
8823 if (CVal >= -7 && CVal < 7)
8824 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008825 } else if (Subtarget->isThumb2()) {
8826 // A constant whose negation can be used as an immediate value in a
8827 // data-processing instruction. This can be used in GCC with an "n"
8828 // modifier that prints the negated value, for use with SUB
8829 // instructions. It is not useful otherwise but is implemented for
8830 // compatibility.
8831 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
8832 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008833 } else {
8834 // A constant whose negation can be used as an immediate value in a
8835 // data-processing instruction. This can be used in GCC with an "n"
8836 // modifier that prints the negated value, for use with SUB
8837 // instructions. It is not useful otherwise but is implemented for
8838 // compatibility.
8839 if (ARM_AM::getSOImmVal(-CVal) != -1)
8840 break;
8841 }
8842 return;
8843
8844 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008845 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008846 // This must be a multiple of 4 between 0 and 1020, for
8847 // ADD sp + immediate.
8848 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
8849 break;
8850 } else {
8851 // A power of two or a constant between 0 and 32. This is used in
8852 // GCC for the shift amount on shifted register operands, but it is
8853 // useful in general for any shift amounts.
8854 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
8855 break;
8856 }
8857 return;
8858
8859 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008860 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008861 // This must be a constant between 0 and 31, for shift amounts.
8862 if (CVal >= 0 && CVal <= 31)
8863 break;
8864 }
8865 return;
8866
8867 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008868 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008869 // This must be a multiple of 4 between -508 and 508, for
8870 // ADD/SUB sp = sp + immediate.
8871 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
8872 break;
8873 }
8874 return;
8875 }
8876 Result = DAG.getTargetConstant(CVal, Op.getValueType());
8877 break;
8878 }
8879
8880 if (Result.getNode()) {
8881 Ops.push_back(Result);
8882 return;
8883 }
Dale Johannesen1784d162010-06-25 21:55:36 +00008884 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008885}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00008886
8887bool
8888ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
8889 // The ARM target isn't yet aware of offsets.
8890 return false;
8891}
Evan Cheng39382422009-10-28 01:44:26 +00008892
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008893bool ARM::isBitFieldInvertedMask(unsigned v) {
8894 if (v == 0xffffffff)
8895 return 0;
8896 // there can be 1's on either or both "outsides", all the "inside"
8897 // bits must be 0's
8898 unsigned int lsb = 0, msb = 31;
8899 while (v & (1 << msb)) --msb;
8900 while (v & (1 << lsb)) ++lsb;
8901 for (unsigned int i = lsb; i <= msb; ++i) {
8902 if (v & (1 << i))
8903 return 0;
8904 }
8905 return 1;
8906}
8907
Evan Cheng39382422009-10-28 01:44:26 +00008908/// isFPImmLegal - Returns true if the target can instruction select the
8909/// specified FP immediate natively. If false, the legalizer will
8910/// materialize the FP immediate as a load from a constant pool.
8911bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
8912 if (!Subtarget->hasVFP3())
8913 return false;
8914 if (VT == MVT::f32)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00008915 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00008916 if (VT == MVT::f64)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00008917 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00008918 return false;
8919}
Bob Wilson65ffec42010-09-21 17:56:22 +00008920
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008921/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00008922/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
8923/// specified in the intrinsic calls.
8924bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
8925 const CallInst &I,
8926 unsigned Intrinsic) const {
8927 switch (Intrinsic) {
8928 case Intrinsic::arm_neon_vld1:
8929 case Intrinsic::arm_neon_vld2:
8930 case Intrinsic::arm_neon_vld3:
8931 case Intrinsic::arm_neon_vld4:
8932 case Intrinsic::arm_neon_vld2lane:
8933 case Intrinsic::arm_neon_vld3lane:
8934 case Intrinsic::arm_neon_vld4lane: {
8935 Info.opc = ISD::INTRINSIC_W_CHAIN;
8936 // Conservatively set memVT to the entire set of vectors loaded.
8937 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
8938 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
8939 Info.ptrVal = I.getArgOperand(0);
8940 Info.offset = 0;
8941 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
8942 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
8943 Info.vol = false; // volatile loads with NEON intrinsics not supported
8944 Info.readMem = true;
8945 Info.writeMem = false;
8946 return true;
8947 }
8948 case Intrinsic::arm_neon_vst1:
8949 case Intrinsic::arm_neon_vst2:
8950 case Intrinsic::arm_neon_vst3:
8951 case Intrinsic::arm_neon_vst4:
8952 case Intrinsic::arm_neon_vst2lane:
8953 case Intrinsic::arm_neon_vst3lane:
8954 case Intrinsic::arm_neon_vst4lane: {
8955 Info.opc = ISD::INTRINSIC_VOID;
8956 // Conservatively set memVT to the entire set of vectors stored.
8957 unsigned NumElts = 0;
8958 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008959 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +00008960 if (!ArgTy->isVectorTy())
8961 break;
8962 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
8963 }
8964 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
8965 Info.ptrVal = I.getArgOperand(0);
8966 Info.offset = 0;
8967 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
8968 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
8969 Info.vol = false; // volatile stores with NEON intrinsics not supported
8970 Info.readMem = false;
8971 Info.writeMem = true;
8972 return true;
8973 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00008974 case Intrinsic::arm_strexd: {
8975 Info.opc = ISD::INTRINSIC_W_CHAIN;
8976 Info.memVT = MVT::i64;
8977 Info.ptrVal = I.getArgOperand(2);
8978 Info.offset = 0;
8979 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00008980 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00008981 Info.readMem = false;
8982 Info.writeMem = true;
8983 return true;
8984 }
8985 case Intrinsic::arm_ldrexd: {
8986 Info.opc = ISD::INTRINSIC_W_CHAIN;
8987 Info.memVT = MVT::i64;
8988 Info.ptrVal = I.getArgOperand(0);
8989 Info.offset = 0;
8990 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00008991 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00008992 Info.readMem = true;
8993 Info.writeMem = false;
8994 return true;
8995 }
Bob Wilson65ffec42010-09-21 17:56:22 +00008996 default:
8997 break;
8998 }
8999
9000 return false;
9001}