blob: de10afabf3fcddeb8a53dccfbba5d08408e8b6cc [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 Cheng55d42002011-01-08 01:24:27 +000046#include "llvm/ADT/StringExtras.h"
Dale Johannesen51e28e62010-06-03 21:09:53 +000047#include "llvm/ADT/Statistic.h"
Jim Grosbache7b52522010-04-14 22:28:31 +000048#include "llvm/Support/CommandLine.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000049#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000050#include "llvm/Support/MathExtras.h"
Jim Grosbache801dc42009-12-12 01:40:06 +000051#include "llvm/Support/raw_ostream.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000052#include <sstream>
Evan Chenga8e29892007-01-19 07:51:42 +000053using namespace llvm;
54
Dale Johannesen51e28e62010-06-03 21:09:53 +000055STATISTIC(NumTailCalls, "Number of tail calls");
Evan Chengfc8475b2011-01-19 02:16:49 +000056STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Dale Johannesen51e28e62010-06-03 21:09:53 +000057
Bob Wilson703af3a2010-08-13 22:43:33 +000058// This option should go away when tail calls fully work.
59static cl::opt<bool>
60EnableARMTailCalls("arm-tail-calls", cl::Hidden,
61 cl::desc("Generate tail calls (TEMPORARY OPTION)."),
62 cl::init(false));
63
Eric Christopher836c6242010-12-15 23:47:29 +000064cl::opt<bool>
Jim Grosbache7b52522010-04-14 22:28:31 +000065EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng515fe3a2010-07-08 02:08:50 +000066 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbache7b52522010-04-14 22:28:31 +000067 cl::init(false));
68
Evan Cheng46df4eb2010-06-16 07:35:02 +000069static cl::opt<bool>
70ARMInterworking("arm-interworking", cl::Hidden,
71 cl::desc("Enable / disable ARM interworking (for debugging only)"),
72 cl::init(true));
73
Benjamin Kramer0861f572011-11-26 23:01:57 +000074namespace {
Cameron Zwaricha86686e2011-06-10 20:59:24 +000075 class ARMCCState : public CCState {
76 public:
77 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
78 const TargetMachine &TM, SmallVector<CCValAssign, 16> &locs,
79 LLVMContext &C, ParmContext PC)
80 : CCState(CC, isVarArg, MF, TM, locs, C) {
81 assert(((PC == Call) || (PC == Prologue)) &&
82 "ARMCCState users must specify whether their context is call"
83 "or prologue generation.");
84 CallOrPrologue = PC;
85 }
86 };
87}
88
Stuart Hastingsc7315872011-04-20 16:47:52 +000089// The APCS parameter registers.
90static const unsigned GPRArgRegs[] = {
91 ARM::R0, ARM::R1, ARM::R2, ARM::R3
92};
93
Owen Andersone50ed302009-08-10 22:56:29 +000094void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
95 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000096 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000097 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000098 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
99 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000100
Owen Anderson70671842009-08-10 20:18:46 +0000101 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000102 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000103 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000104 }
105
Owen Andersone50ed302009-08-10 22:56:29 +0000106 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000107 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Duncan Sands28b77e92011-09-06 19:07:46 +0000108 setOperationAction(ISD::SETCC, VT.getSimpleVT(), Custom);
Eli Friedman5c89cb82011-10-24 23:08:52 +0000109 setOperationAction(ISD::INSERT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson3468c2e2010-11-03 16:24:50 +0000110 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Eli Friedman14e809c2011-11-09 23:36:02 +0000111 if (ElemTy == MVT::i32) {
112 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Custom);
113 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Custom);
114 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Custom);
115 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Custom);
116 } else {
Bob Wilson0696fdf2009-09-16 20:20:44 +0000117 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
118 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
119 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
120 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
121 }
Owen Anderson70671842009-08-10 20:18:46 +0000122 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
123 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Bob Wilson07f6e802010-06-16 21:34:01 +0000124 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
Bob Wilson5e8b8332011-01-07 04:59:04 +0000125 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Legal);
Bob Wilsond0910c42010-04-06 22:02:24 +0000126 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
127 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Eli Friedman15f58c52011-11-11 03:16:38 +0000128 setOperationAction(ISD::SIGN_EXTEND_INREG, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000129 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000130 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
131 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
132 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +0000133 }
134
135 // Promote all bit-wise operations.
136 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000137 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000138 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
139 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000140 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000141 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000142 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000143 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000144 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000145 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000146 }
Bob Wilson16330762009-09-16 00:17:28 +0000147
148 // Neon does not support vector divide/remainder operations.
149 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
150 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
151 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
152 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
153 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
154 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000155}
156
Owen Andersone50ed302009-08-10 22:56:29 +0000157void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000158 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000159 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000160}
161
Owen Andersone50ed302009-08-10 22:56:29 +0000162void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000163 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000164 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000165}
166
Chris Lattnerf0144122009-07-28 03:13:23 +0000167static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
168 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000169 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000170
Chris Lattner80ec2792009-08-02 00:34:36 +0000171 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000172}
173
Evan Chenga8e29892007-01-19 07:51:42 +0000174ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000175 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000176 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng31446872010-07-23 22:39:59 +0000177 RegInfo = TM.getRegisterInfo();
Evan Cheng3ef1c872010-09-10 01:29:16 +0000178 Itins = TM.getInstrItineraryData();
Evan Chenga8e29892007-01-19 07:51:42 +0000179
Duncan Sands28b77e92011-09-06 19:07:46 +0000180 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
181
Evan Chengb1df8f22007-04-27 08:15:43 +0000182 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000183 // Uses VFP for Thumb libfuncs if available.
184 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
185 // Single-precision floating-point arithmetic.
186 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
187 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
188 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
189 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000190
Evan Chengb1df8f22007-04-27 08:15:43 +0000191 // Double-precision floating-point arithmetic.
192 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
193 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
194 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
195 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000196
Evan Chengb1df8f22007-04-27 08:15:43 +0000197 // Single-precision comparisons.
198 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
199 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
200 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
201 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
202 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
203 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
204 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
205 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000206
Evan Chengb1df8f22007-04-27 08:15:43 +0000207 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
208 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
209 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
210 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
211 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
212 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
213 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
214 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000215
Evan Chengb1df8f22007-04-27 08:15:43 +0000216 // Double-precision comparisons.
217 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
218 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
219 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
220 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
221 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
222 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
223 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
224 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000225
Evan Chengb1df8f22007-04-27 08:15:43 +0000226 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
227 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
228 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
229 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
230 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
231 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
232 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
233 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000234
Evan Chengb1df8f22007-04-27 08:15:43 +0000235 // Floating-point to integer conversions.
236 // i64 conversions are done via library routines even when generating VFP
237 // instructions, so use the same ones.
238 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
239 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
240 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
241 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000242
Evan Chengb1df8f22007-04-27 08:15:43 +0000243 // Conversions between floating types.
244 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
245 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
246
247 // Integer to floating-point conversions.
248 // i64 conversions are done via library routines even when generating VFP
249 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000250 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
251 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000252 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
253 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
254 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
255 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
256 }
Evan Chenga8e29892007-01-19 07:51:42 +0000257 }
258
Bob Wilson2f954612009-05-22 17:38:41 +0000259 // These libcalls are not available in 32-bit.
260 setLibcallName(RTLIB::SHL_I128, 0);
261 setLibcallName(RTLIB::SRL_I128, 0);
262 setLibcallName(RTLIB::SRA_I128, 0);
263
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000264 if (Subtarget->isAAPCS_ABI()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000265 // Double-precision floating-point arithmetic helper functions
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000266 // RTABI chapter 4.1.2, Table 2
267 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd");
268 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv");
269 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul");
270 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub");
271 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS);
272 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS);
273 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS);
274 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS);
275
276 // Double-precision floating-point comparison helper functions
277 // RTABI chapter 4.1.2, Table 3
278 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq");
279 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
280 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq");
281 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ);
282 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt");
283 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
284 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple");
285 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
286 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge");
287 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
288 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt");
289 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
290 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun");
291 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
292 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun");
293 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
294 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS);
295 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS);
296 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS);
297 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS);
298 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS);
299 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS);
300 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS);
301 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS);
302
303 // Single-precision floating-point arithmetic helper functions
304 // RTABI chapter 4.1.2, Table 4
305 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd");
306 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv");
307 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul");
308 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub");
309 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS);
310 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS);
311 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS);
312 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS);
313
314 // Single-precision floating-point comparison helper functions
315 // RTABI chapter 4.1.2, Table 5
316 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq");
317 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
318 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq");
319 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ);
320 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt");
321 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
322 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple");
323 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
324 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge");
325 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
326 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt");
327 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
328 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun");
329 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
330 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun");
331 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
332 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS);
333 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS);
334 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS);
335 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS);
336 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS);
337 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS);
338 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS);
339 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS);
340
341 // Floating-point to integer conversions.
342 // RTABI chapter 4.1.2, Table 6
343 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz");
344 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz");
345 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz");
346 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz");
347 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz");
348 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz");
349 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz");
350 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz");
351 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS);
352 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS);
353 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS);
354 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS);
355 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS);
356 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS);
357 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS);
358 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS);
359
360 // Conversions between floating types.
361 // RTABI chapter 4.1.2, Table 7
362 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f");
363 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d");
364 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000365 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000366
367 // Integer to floating-point conversions.
368 // RTABI chapter 4.1.2, Table 8
369 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d");
370 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d");
371 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d");
372 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d");
373 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f");
374 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f");
375 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f");
376 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f");
377 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
378 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
379 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
380 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
381 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
382 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
383 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
384 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
385
386 // Long long helper functions
387 // RTABI chapter 4.2, Table 9
388 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000389 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
390 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
391 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
392 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
393 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
394 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
395 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
396 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
397 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
398
399 // Integer division functions
400 // RTABI chapter 4.3.1
401 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
402 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
403 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000404 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000405 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
406 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
407 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000408 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000409 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
410 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
411 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000412 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000413 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);
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000416 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
Renato Golin1ec11fb2011-05-22 21:41:23 +0000417
418 // Memory operations
419 // RTABI chapter 4.3.4
420 setLibcallName(RTLIB::MEMCPY, "__aeabi_memcpy");
421 setLibcallName(RTLIB::MEMMOVE, "__aeabi_memmove");
422 setLibcallName(RTLIB::MEMSET, "__aeabi_memset");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000423 setLibcallCallingConv(RTLIB::MEMCPY, CallingConv::ARM_AAPCS);
424 setLibcallCallingConv(RTLIB::MEMMOVE, CallingConv::ARM_AAPCS);
425 setLibcallCallingConv(RTLIB::MEMSET, CallingConv::ARM_AAPCS);
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000426 }
427
Bob Wilson2fef4572011-10-07 16:59:21 +0000428 // Use divmod compiler-rt calls for iOS 5.0 and later.
429 if (Subtarget->getTargetTriple().getOS() == Triple::IOS &&
430 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
431 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
432 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
433 }
434
David Goodwinf1daf7d2009-07-08 23:10:31 +0000435 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000436 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000437 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000438 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000439 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
440 !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000441 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000442 if (!Subtarget->isFPOnlySP())
443 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000444
Owen Anderson825b72b2009-08-11 20:47:22 +0000445 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000446 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000447
Eli Friedman9f1f26a2011-11-08 01:43:53 +0000448 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
449 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
450 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
451 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
452 setTruncStoreAction((MVT::SimpleValueType)VT,
453 (MVT::SimpleValueType)InnerVT, Expand);
454 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
455 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
456 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
457 }
458
Bob Wilson5bafff32009-06-22 23:27:02 +0000459 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000460 addDRTypeForNEON(MVT::v2f32);
461 addDRTypeForNEON(MVT::v8i8);
462 addDRTypeForNEON(MVT::v4i16);
463 addDRTypeForNEON(MVT::v2i32);
464 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000465
Owen Anderson825b72b2009-08-11 20:47:22 +0000466 addQRTypeForNEON(MVT::v4f32);
467 addQRTypeForNEON(MVT::v2f64);
468 addQRTypeForNEON(MVT::v16i8);
469 addQRTypeForNEON(MVT::v8i16);
470 addQRTypeForNEON(MVT::v4i32);
471 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000472
Bob Wilson74dc72e2009-09-15 23:55:57 +0000473 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
474 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000475 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
476 // supported for v4f32.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000477 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
478 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
479 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000480 // FIXME: Code duplication: FDIV and FREM are expanded always, see
481 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000482 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
483 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000484 // FIXME: Create unittest.
485 // In another words, find a way when "copysign" appears in DAG with vector
486 // operands.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000487 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000488 // FIXME: Code duplication: SETCC has custom operation action, see
489 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sands28b77e92011-09-06 19:07:46 +0000490 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000491 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000492 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
493 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
494 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
495 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
496 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
497 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
498 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
499 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
500 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
501 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
502 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
503 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000504 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000505 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
506 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
507 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
508 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
509 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000510
511 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
512 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
513 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
514 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
515 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
516 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
517 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
518 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
519 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
520 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Bob Wilson74dc72e2009-09-15 23:55:57 +0000521
Bob Wilson642b3292009-09-16 00:32:15 +0000522 // Neon does not support some operations on v1i64 and v2i64 types.
523 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000524 // Custom handling for some quad-vector types to detect VMULL.
525 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
526 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
527 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begeman7973f352011-02-11 20:53:29 +0000528 // Custom handling for some vector types to avoid expensive expansions
529 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
530 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
531 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
532 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000533 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
534 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000535 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
536 // a destination type that is wider than the source.
537 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
538 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000539
Bob Wilson1c3ef902011-02-07 17:43:21 +0000540 setTargetDAGCombine(ISD::INTRINSIC_VOID);
541 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000542 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
543 setTargetDAGCombine(ISD::SHL);
544 setTargetDAGCombine(ISD::SRL);
545 setTargetDAGCombine(ISD::SRA);
546 setTargetDAGCombine(ISD::SIGN_EXTEND);
547 setTargetDAGCombine(ISD::ZERO_EXTEND);
548 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000549 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000550 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000551 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000552 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
553 setTargetDAGCombine(ISD::STORE);
Chad Rosieref01edf2011-06-24 19:23:04 +0000554 setTargetDAGCombine(ISD::FP_TO_SINT);
555 setTargetDAGCombine(ISD::FP_TO_UINT);
556 setTargetDAGCombine(ISD::FDIV);
Nadav Rotem004a24b2011-10-15 20:03:12 +0000557
558 setLoadExtAction(ISD::EXTLOAD, MVT::v4i8, Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000559 }
560
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000561 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000562
563 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000564 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000565
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000566 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000567 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000568
Evan Chenga8e29892007-01-19 07:51:42 +0000569 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000570 if (!Subtarget->isThumb1Only()) {
571 for (unsigned im = (unsigned)ISD::PRE_INC;
572 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000573 setIndexedLoadAction(im, MVT::i1, Legal);
574 setIndexedLoadAction(im, MVT::i8, Legal);
575 setIndexedLoadAction(im, MVT::i16, Legal);
576 setIndexedLoadAction(im, MVT::i32, Legal);
577 setIndexedStoreAction(im, MVT::i1, Legal);
578 setIndexedStoreAction(im, MVT::i8, Legal);
579 setIndexedStoreAction(im, MVT::i16, Legal);
580 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000581 }
Evan Chenga8e29892007-01-19 07:51:42 +0000582 }
583
584 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000585 setOperationAction(ISD::MUL, MVT::i64, Expand);
586 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000587 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000588 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
589 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000590 }
Jim Grosbacha7603982011-07-01 21:12:19 +0000591 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
592 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopher2cc40132011-04-19 18:49:19 +0000593 setOperationAction(ISD::MULHS, MVT::i32, Expand);
594
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000595 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000596 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000597 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000598 setOperationAction(ISD::SRL, MVT::i64, Custom);
599 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000600
Evan Cheng342e3162011-08-30 01:34:54 +0000601 if (!Subtarget->isThumb1Only()) {
602 // FIXME: We should do this for Thumb1 as well.
603 setOperationAction(ISD::ADDC, MVT::i32, Custom);
604 setOperationAction(ISD::ADDE, MVT::i32, Custom);
605 setOperationAction(ISD::SUBC, MVT::i32, Custom);
606 setOperationAction(ISD::SUBE, MVT::i32, Custom);
607 }
608
Evan Chenga8e29892007-01-19 07:51:42 +0000609 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000610 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000611 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000612 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000613 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000614 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000615
Chandler Carruth63974b22011-12-13 01:56:10 +0000616 // These just redirect to CTTZ and CTLZ on ARM.
617 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
618 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
619
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000620 // Only ARMv6 has BSWAP.
621 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000622 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000623
Evan Chenga8e29892007-01-19 07:51:42 +0000624 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000625 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000626 // v7M has a hardware divider
627 setOperationAction(ISD::SDIV, MVT::i32, Expand);
628 setOperationAction(ISD::UDIV, MVT::i32, Expand);
629 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000630 setOperationAction(ISD::SREM, MVT::i32, Expand);
631 setOperationAction(ISD::UREM, MVT::i32, Expand);
632 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
633 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000634
Owen Anderson825b72b2009-08-11 20:47:22 +0000635 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
636 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
637 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
638 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000639 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000640
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000641 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000642
Evan Chenga8e29892007-01-19 07:51:42 +0000643 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000644 setOperationAction(ISD::VASTART, MVT::Other, Custom);
645 setOperationAction(ISD::VAARG, MVT::Other, Expand);
646 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
647 setOperationAction(ISD::VAEND, MVT::Other, Expand);
648 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
649 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendlingbdf9db62012-02-13 23:47:16 +0000650
651 if (!Subtarget->isTargetDarwin()) {
652 // Non-Darwin platforms may return values in these registers via the
653 // personality function.
654 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
655 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
656 setExceptionPointerRegister(ARM::R0);
657 setExceptionSelectorRegister(ARM::R1);
658 }
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000659
Evan Cheng3a1588a2010-04-15 22:20:34 +0000660 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000661 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
662 // the default expansion.
Eli Friedman4db5aca2011-08-29 18:23:02 +0000663 // FIXME: This should be checking for v6k, not just v6.
Evan Cheng11db0682010-08-11 06:22:01 +0000664 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000665 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000666 // membarrier needs custom lowering; the rest are legal and handled
667 // normally.
668 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000669 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Eli Friedman2bdffe42011-08-31 00:31:29 +0000670 // Custom lowering for 64-bit ops
671 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
672 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
673 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
674 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
675 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
676 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Eli Friedman4d3f3292011-08-31 17:52:22 +0000677 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Eli Friedman26689ac2011-08-03 21:06:02 +0000678 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
679 setInsertFencesForAtomic(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000680 } else {
681 // Set them all for expansion, which will force libcalls.
682 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000683 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000684 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000685 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000686 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000687 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000688 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000689 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000690 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000691 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000692 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000693 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000694 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000695 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedman7cc15662011-09-15 22:18:49 +0000696 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
697 // Unordered/Monotonic case.
698 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
699 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000700 // Since the libcalls include locking, fold in the fences
701 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000702 }
Evan Chenga8e29892007-01-19 07:51:42 +0000703
Evan Cheng416941d2010-11-04 05:19:35 +0000704 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000705
Eli Friedmana2c6f452010-06-26 04:36:50 +0000706 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
707 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000708 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
709 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000710 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000711 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000712
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000713 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
714 !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000715 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
716 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000717 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000718 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
719 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000720
721 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000722 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000723 if (Subtarget->isTargetDarwin()) {
724 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
725 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000726 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000727 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000728
Owen Anderson825b72b2009-08-11 20:47:22 +0000729 setOperationAction(ISD::SETCC, MVT::i32, Expand);
730 setOperationAction(ISD::SETCC, MVT::f32, Expand);
731 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000732 setOperationAction(ISD::SELECT, MVT::i32, Custom);
733 setOperationAction(ISD::SELECT, MVT::f32, Custom);
734 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000735 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
736 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
737 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000738
Owen Anderson825b72b2009-08-11 20:47:22 +0000739 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
740 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
741 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
742 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
743 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000744
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000745 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000746 setOperationAction(ISD::FSIN, MVT::f64, Expand);
747 setOperationAction(ISD::FSIN, MVT::f32, Expand);
748 setOperationAction(ISD::FCOS, MVT::f32, Expand);
749 setOperationAction(ISD::FCOS, MVT::f64, Expand);
750 setOperationAction(ISD::FREM, MVT::f64, Expand);
751 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000752 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
753 !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000754 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
755 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000756 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000757 setOperationAction(ISD::FPOW, MVT::f64, Expand);
758 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000759
Cameron Zwarich33390842011-07-08 21:39:21 +0000760 setOperationAction(ISD::FMA, MVT::f64, Expand);
761 setOperationAction(ISD::FMA, MVT::f32, Expand);
762
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000763 // Various VFP goodness
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000764 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000765 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
766 if (Subtarget->hasVFP2()) {
767 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
768 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
769 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
770 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
771 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000772 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000773 if (!Subtarget->hasFP16()) {
774 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
775 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000776 }
Evan Cheng110cf482008-04-01 01:50:16 +0000777 }
Evan Chenga8e29892007-01-19 07:51:42 +0000778
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000779 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000780 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000781 setTargetDAGCombine(ISD::ADD);
782 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000783 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000784
Owen Anderson080c0922010-11-05 19:27:46 +0000785 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON())
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000786 setTargetDAGCombine(ISD::OR);
Owen Anderson080c0922010-11-05 19:27:46 +0000787 if (Subtarget->hasNEON())
788 setTargetDAGCombine(ISD::AND);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000789
Evan Chenga8e29892007-01-19 07:51:42 +0000790 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000791
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000792 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
793 !Subtarget->hasVFP2())
Evan Chengf7d87ee2010-05-21 00:43:17 +0000794 setSchedulingPreference(Sched::RegPressure);
795 else
796 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000797
Evan Cheng05219282011-01-06 06:52:41 +0000798 //// temporary - rewrite interface to use type
799 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Lang Hames75757f92011-10-26 20:56:52 +0000800 maxStoresPerMemset = 16;
801 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengf6799392010-06-26 01:52:05 +0000802
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000803 // On ARM arguments smaller than 4 bytes are extended, so all arguments
804 // are at least 4 bytes aligned.
805 setMinStackArgumentAlignment(4);
806
Evan Chengfff606d2010-09-24 19:07:23 +0000807 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000808
809 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000810}
811
Andrew Trick32cec0a2011-01-19 02:35:27 +0000812// FIXME: It might make sense to define the representative register class as the
813// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
814// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
815// SPR's representative would be DPR_VFP2. This should work well if register
816// pressure tracking were modified such that a register use would increment the
817// pressure of the register class's representative and all of it's super
818// classes' representatives transitively. We have not implemented this because
819// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000820// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000821// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000822std::pair<const TargetRegisterClass*, uint8_t>
823ARMTargetLowering::findRepresentativeClass(EVT VT) const{
824 const TargetRegisterClass *RRC = 0;
825 uint8_t Cost = 1;
826 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000827 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000828 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000829 // Use DPR as representative register class for all floating point
830 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
831 // the cost is 1 for both f32 and f64.
832 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000833 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000834 RRC = ARM::DPRRegisterClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000835 // When NEON is used for SP, only half of the register file is available
836 // because operations that define both SP and DP results will be constrained
837 // to the VFP2 class (D0-D15). We currently model this constraint prior to
838 // coalescing by double-counting the SP regs. See the FIXME above.
839 if (Subtarget->useNEONForSinglePrecisionFP())
840 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000841 break;
842 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
843 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000844 RRC = ARM::DPRRegisterClass;
845 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000846 break;
847 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000848 RRC = ARM::DPRRegisterClass;
849 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000850 break;
851 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000852 RRC = ARM::DPRRegisterClass;
853 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000854 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000855 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000856 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000857}
858
Evan Chenga8e29892007-01-19 07:51:42 +0000859const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
860 switch (Opcode) {
861 default: return 0;
862 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000863 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000864 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000865 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
866 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000867 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000868 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
869 case ARMISD::tCALL: return "ARMISD::tCALL";
870 case ARMISD::BRCOND: return "ARMISD::BRCOND";
871 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000872 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000873 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
874 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
875 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000876 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000877 case ARMISD::CMPFP: return "ARMISD::CMPFP";
878 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000879 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000880 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
881 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000882
Jim Grosbach3482c802010-01-18 19:58:49 +0000883 case ARMISD::RBIT: return "ARMISD::RBIT";
884
Bob Wilson76a312b2010-03-19 22:51:32 +0000885 case ARMISD::FTOSI: return "ARMISD::FTOSI";
886 case ARMISD::FTOUI: return "ARMISD::FTOUI";
887 case ARMISD::SITOF: return "ARMISD::SITOF";
888 case ARMISD::UITOF: return "ARMISD::UITOF";
889
Evan Chenga8e29892007-01-19 07:51:42 +0000890 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
891 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
892 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000893
Evan Cheng342e3162011-08-30 01:34:54 +0000894 case ARMISD::ADDC: return "ARMISD::ADDC";
895 case ARMISD::ADDE: return "ARMISD::ADDE";
896 case ARMISD::SUBC: return "ARMISD::SUBC";
897 case ARMISD::SUBE: return "ARMISD::SUBE";
898
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000899 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
900 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000901
Evan Chengc5942082009-10-28 06:55:03 +0000902 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
903 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
904
Dale Johannesen51e28e62010-06-03 21:09:53 +0000905 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000906
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000907 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000908
Evan Cheng86198642009-08-07 00:34:42 +0000909 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
910
Jim Grosbach3728e962009-12-10 00:11:09 +0000911 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000912 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000913
Evan Chengdfed19f2010-11-03 06:34:55 +0000914 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
915
Bob Wilson5bafff32009-06-22 23:27:02 +0000916 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000917 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000918 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000919 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
920 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000921 case ARMISD::VCGEU: return "ARMISD::VCGEU";
922 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000923 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
924 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000925 case ARMISD::VCGTU: return "ARMISD::VCGTU";
926 case ARMISD::VTST: return "ARMISD::VTST";
927
928 case ARMISD::VSHL: return "ARMISD::VSHL";
929 case ARMISD::VSHRs: return "ARMISD::VSHRs";
930 case ARMISD::VSHRu: return "ARMISD::VSHRu";
931 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
932 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
933 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
934 case ARMISD::VSHRN: return "ARMISD::VSHRN";
935 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
936 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
937 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
938 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
939 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
940 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
941 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
942 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
943 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
944 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
945 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
946 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
947 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
948 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000949 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000950 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Chengeaa192a2011-11-15 02:12:34 +0000951 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000952 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000953 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000954 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000955 case ARMISD::VREV64: return "ARMISD::VREV64";
956 case ARMISD::VREV32: return "ARMISD::VREV32";
957 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000958 case ARMISD::VZIP: return "ARMISD::VZIP";
959 case ARMISD::VUZP: return "ARMISD::VUZP";
960 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000961 case ARMISD::VTBL1: return "ARMISD::VTBL1";
962 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000963 case ARMISD::VMULLs: return "ARMISD::VMULLs";
964 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000965 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000966 case ARMISD::FMAX: return "ARMISD::FMAX";
967 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000968 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000969 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
970 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000971 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000972 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
973 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
974 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +0000975 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
976 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
977 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
978 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
979 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
980 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
981 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
982 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
983 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
984 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
985 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
986 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
987 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
988 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
989 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
990 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
991 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +0000992 }
993}
994
Duncan Sands28b77e92011-09-06 19:07:46 +0000995EVT ARMTargetLowering::getSetCCResultType(EVT VT) const {
996 if (!VT.isVector()) return getPointerTy();
997 return VT.changeVectorElementTypeToInteger();
998}
999
Evan Cheng06b666c2010-05-15 02:18:07 +00001000/// getRegClassFor - Return the register class that should be used for the
1001/// specified value type.
1002TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
1003 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1004 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1005 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +00001006 if (Subtarget->hasNEON()) {
1007 if (VT == MVT::v4i64)
1008 return ARM::QQPRRegisterClass;
1009 else if (VT == MVT::v8i64)
1010 return ARM::QQQQPRRegisterClass;
1011 }
Evan Cheng06b666c2010-05-15 02:18:07 +00001012 return TargetLowering::getRegClassFor(VT);
1013}
1014
Eric Christopherab695882010-07-21 22:26:11 +00001015// Create a fast isel object.
1016FastISel *
1017ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
1018 return ARM::createFastISel(funcInfo);
1019}
1020
Anton Korobeynikovcec36f42010-07-24 21:52:08 +00001021/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1022/// be used for loads / stores from the global.
1023unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1024 return (Subtarget->isThumb1Only() ? 127 : 4095);
1025}
1026
Evan Cheng1cc39842010-05-20 23:26:43 +00001027Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +00001028 unsigned NumVals = N->getNumValues();
1029 if (!NumVals)
1030 return Sched::RegPressure;
1031
1032 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +00001033 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001034 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +00001035 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +00001036 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman692c1d82011-10-24 17:55:11 +00001037 return Sched::ILP;
Evan Cheng1cc39842010-05-20 23:26:43 +00001038 }
Evan Chengc10f5432010-05-28 23:25:23 +00001039
1040 if (!N->isMachineOpcode())
1041 return Sched::RegPressure;
1042
1043 // Load are scheduled for latency even if there instruction itinerary
1044 // is not available.
1045 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +00001046 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +00001047
Evan Chenge837dea2011-06-28 19:10:37 +00001048 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +00001049 return Sched::RegPressure;
1050 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +00001051 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman692c1d82011-10-24 17:55:11 +00001052 return Sched::ILP;
Evan Chengc10f5432010-05-28 23:25:23 +00001053
Evan Cheng1cc39842010-05-20 23:26:43 +00001054 return Sched::RegPressure;
1055}
1056
Evan Chenga8e29892007-01-19 07:51:42 +00001057//===----------------------------------------------------------------------===//
1058// Lowering Code
1059//===----------------------------------------------------------------------===//
1060
Evan Chenga8e29892007-01-19 07:51:42 +00001061/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1062static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1063 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001064 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +00001065 case ISD::SETNE: return ARMCC::NE;
1066 case ISD::SETEQ: return ARMCC::EQ;
1067 case ISD::SETGT: return ARMCC::GT;
1068 case ISD::SETGE: return ARMCC::GE;
1069 case ISD::SETLT: return ARMCC::LT;
1070 case ISD::SETLE: return ARMCC::LE;
1071 case ISD::SETUGT: return ARMCC::HI;
1072 case ISD::SETUGE: return ARMCC::HS;
1073 case ISD::SETULT: return ARMCC::LO;
1074 case ISD::SETULE: return ARMCC::LS;
1075 }
1076}
1077
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001078/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1079static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001080 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001081 CondCode2 = ARMCC::AL;
1082 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001083 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001084 case ISD::SETEQ:
1085 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1086 case ISD::SETGT:
1087 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1088 case ISD::SETGE:
1089 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1090 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001091 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001092 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1093 case ISD::SETO: CondCode = ARMCC::VC; break;
1094 case ISD::SETUO: CondCode = ARMCC::VS; break;
1095 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1096 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1097 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1098 case ISD::SETLT:
1099 case ISD::SETULT: CondCode = ARMCC::LT; break;
1100 case ISD::SETLE:
1101 case ISD::SETULE: CondCode = ARMCC::LE; break;
1102 case ISD::SETNE:
1103 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1104 }
Evan Chenga8e29892007-01-19 07:51:42 +00001105}
1106
Bob Wilson1f595bb2009-04-17 19:07:39 +00001107//===----------------------------------------------------------------------===//
1108// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001109//===----------------------------------------------------------------------===//
1110
1111#include "ARMGenCallingConv.inc"
1112
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001113/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1114/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001115CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001116 bool Return,
1117 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001118 switch (CC) {
1119 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001120 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001121 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001122 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001123 if (!Subtarget->isAAPCS_ABI())
1124 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1125 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1126 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1127 }
1128 // Fallthrough
1129 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001130 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001131 if (!Subtarget->isAAPCS_ABI())
1132 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1133 else if (Subtarget->hasVFP2() &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001134 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1135 !isVarArg)
Evan Cheng76f920d2010-10-22 18:23:05 +00001136 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1137 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1138 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001139 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikovf349cb82012-01-29 09:06:09 +00001140 if (!isVarArg)
1141 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1142 // Fallthrough
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001143 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001144 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001145 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001146 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001147 }
1148}
1149
Dan Gohman98ca4f22009-08-05 01:29:28 +00001150/// LowerCallResult - Lower the result values of a call into the
1151/// appropriate copies out of appropriate physical registers.
1152SDValue
1153ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001154 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001155 const SmallVectorImpl<ISD::InputArg> &Ins,
1156 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001157 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001158
Bob Wilson1f595bb2009-04-17 19:07:39 +00001159 // Assign locations to each value returned by this call.
1160 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001161 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1162 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001163 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001164 CCAssignFnForNode(CallConv, /* Return*/ true,
1165 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001166
1167 // Copy all of the result registers out of their specified physreg.
1168 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1169 CCValAssign VA = RVLocs[i];
1170
Bob Wilson80915242009-04-25 00:33:20 +00001171 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001172 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001173 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001174 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001175 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001176 Chain = Lo.getValue(1);
1177 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001178 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001179 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001180 InFlag);
1181 Chain = Hi.getValue(1);
1182 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001183 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001184
Owen Anderson825b72b2009-08-11 20:47:22 +00001185 if (VA.getLocVT() == MVT::v2f64) {
1186 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1187 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1188 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001189
1190 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001191 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001192 Chain = Lo.getValue(1);
1193 InFlag = Lo.getValue(2);
1194 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001195 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001196 Chain = Hi.getValue(1);
1197 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001198 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001199 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1200 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001201 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001202 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001203 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1204 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001205 Chain = Val.getValue(1);
1206 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001207 }
Bob Wilson80915242009-04-25 00:33:20 +00001208
1209 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001210 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001211 case CCValAssign::Full: break;
1212 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001213 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001214 break;
1215 }
1216
Dan Gohman98ca4f22009-08-05 01:29:28 +00001217 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001218 }
1219
Dan Gohman98ca4f22009-08-05 01:29:28 +00001220 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001221}
1222
Bob Wilsondee46d72009-04-17 20:35:10 +00001223/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001224SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001225ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1226 SDValue StackPtr, SDValue Arg,
1227 DebugLoc dl, SelectionDAG &DAG,
1228 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001229 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001230 unsigned LocMemOffset = VA.getLocMemOffset();
1231 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1232 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001233 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001234 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001235 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001236}
1237
Dan Gohman98ca4f22009-08-05 01:29:28 +00001238void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001239 SDValue Chain, SDValue &Arg,
1240 RegsToPassVector &RegsToPass,
1241 CCValAssign &VA, CCValAssign &NextVA,
1242 SDValue &StackPtr,
1243 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001244 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001245
Jim Grosbache5165492009-11-09 00:11:35 +00001246 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001247 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001248 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1249
1250 if (NextVA.isRegLoc())
1251 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1252 else {
1253 assert(NextVA.isMemLoc());
1254 if (StackPtr.getNode() == 0)
1255 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1256
Dan Gohman98ca4f22009-08-05 01:29:28 +00001257 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1258 dl, DAG, NextVA,
1259 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001260 }
1261}
1262
Dan Gohman98ca4f22009-08-05 01:29:28 +00001263/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001264/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1265/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001266SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001267ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001268 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001269 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001270 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001271 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001272 const SmallVectorImpl<ISD::InputArg> &Ins,
1273 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001274 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001275 MachineFunction &MF = DAG.getMachineFunction();
1276 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1277 bool IsSibCall = false;
Bob Wilson6d2f9ce2011-10-07 17:17:49 +00001278 // Disable tail calls if they're not supported.
1279 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Bob Wilson703af3a2010-08-13 22:43:33 +00001280 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001281 if (isTailCall) {
1282 // Check if it's really possible to do a tail call.
1283 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1284 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001285 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001286 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1287 // detected sibcalls.
1288 if (isTailCall) {
1289 ++NumTailCalls;
1290 IsSibCall = true;
1291 }
1292 }
Evan Chenga8e29892007-01-19 07:51:42 +00001293
Bob Wilson1f595bb2009-04-17 19:07:39 +00001294 // Analyze operands of the call, assigning locations to each operand.
1295 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001296 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1297 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001298 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001299 CCAssignFnForNode(CallConv, /* Return*/ false,
1300 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001301
Bob Wilson1f595bb2009-04-17 19:07:39 +00001302 // Get a count of how many bytes are to be pushed on the stack.
1303 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001304
Dale Johannesen51e28e62010-06-03 21:09:53 +00001305 // For tail calls, memory operands are available in our caller's stack.
1306 if (IsSibCall)
1307 NumBytes = 0;
1308
Evan Chenga8e29892007-01-19 07:51:42 +00001309 // Adjust the stack pointer for the new arguments...
1310 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001311 if (!IsSibCall)
1312 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001313
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001314 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001315
Bob Wilson5bafff32009-06-22 23:27:02 +00001316 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001317 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001318
Bob Wilson1f595bb2009-04-17 19:07:39 +00001319 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001320 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001321 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1322 i != e;
1323 ++i, ++realArgIdx) {
1324 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001325 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001326 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001327 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001328
Bob Wilson1f595bb2009-04-17 19:07:39 +00001329 // Promote the value if needed.
1330 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001331 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001332 case CCValAssign::Full: break;
1333 case CCValAssign::SExt:
1334 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1335 break;
1336 case CCValAssign::ZExt:
1337 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1338 break;
1339 case CCValAssign::AExt:
1340 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1341 break;
1342 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001343 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001344 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001345 }
1346
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001347 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001348 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001349 if (VA.getLocVT() == MVT::v2f64) {
1350 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1351 DAG.getConstant(0, MVT::i32));
1352 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1353 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001354
Dan Gohman98ca4f22009-08-05 01:29:28 +00001355 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001356 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1357
1358 VA = ArgLocs[++i]; // skip ahead to next loc
1359 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001360 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001361 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1362 } else {
1363 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001364
Dan Gohman98ca4f22009-08-05 01:29:28 +00001365 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1366 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001367 }
1368 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001369 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001370 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001371 }
1372 } else if (VA.isRegLoc()) {
1373 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001374 } else if (isByVal) {
1375 assert(VA.isMemLoc());
1376 unsigned offset = 0;
1377
1378 // True if this byval aggregate will be split between registers
1379 // and memory.
1380 if (CCInfo.isFirstByValRegValid()) {
1381 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1382 unsigned int i, j;
1383 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1384 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1385 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1386 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1387 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001388 false, false, false, 0);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001389 MemOpChains.push_back(Load.getValue(1));
1390 RegsToPass.push_back(std::make_pair(j, Load));
1391 }
1392 offset = ARM::R4 - CCInfo.getFirstByValReg();
1393 CCInfo.clearFirstByValReg();
1394 }
1395
1396 unsigned LocMemOffset = VA.getLocMemOffset();
1397 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1398 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1399 StkPtrOff);
1400 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1401 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1402 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1403 MVT::i32);
1404 MemOpChains.push_back(DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode,
1405 Flags.getByValAlign(),
1406 /*isVolatile=*/false,
Dan Gohman65fd6562011-11-03 21:49:52 +00001407 /*AlwaysInline=*/false,
Stuart Hastingsc7315872011-04-20 16:47:52 +00001408 MachinePointerInfo(0),
1409 MachinePointerInfo(0)));
1410
1411 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001412 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001413
Dan Gohman98ca4f22009-08-05 01:29:28 +00001414 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1415 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001416 }
Evan Chenga8e29892007-01-19 07:51:42 +00001417 }
1418
1419 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001420 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001421 &MemOpChains[0], MemOpChains.size());
1422
1423 // Build a sequence of copy-to-reg nodes chained together with token chain
1424 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001425 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001426 // Tail call byval lowering might overwrite argument registers so in case of
1427 // tail call optimization the copies to registers are lowered later.
1428 if (!isTailCall)
1429 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1430 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1431 RegsToPass[i].second, InFlag);
1432 InFlag = Chain.getValue(1);
1433 }
Evan Chenga8e29892007-01-19 07:51:42 +00001434
Dale Johannesen51e28e62010-06-03 21:09:53 +00001435 // For tail calls lower the arguments to the 'real' stack slot.
1436 if (isTailCall) {
1437 // Force all the incoming stack arguments to be loaded from the stack
1438 // before any new outgoing arguments are stored to the stack, because the
1439 // outgoing stack slots may alias the incoming argument stack slots, and
1440 // the alias isn't otherwise explicit. This is slightly more conservative
1441 // than necessary, because it means that each store effectively depends
1442 // on every argument instead of just those arguments it would clobber.
1443
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001444 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001445 InFlag = SDValue();
1446 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1447 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1448 RegsToPass[i].second, InFlag);
1449 InFlag = Chain.getValue(1);
1450 }
1451 InFlag =SDValue();
1452 }
1453
Bill Wendling056292f2008-09-16 21:48:12 +00001454 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1455 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1456 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001457 bool isDirect = false;
1458 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001459 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001460 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001461
1462 if (EnableARMLongCalls) {
1463 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1464 && "long-calls with non-static relocation model!");
1465 // Handle a global address or an external symbol. If it's not one of
1466 // those, the target's already in a register, so we don't need to do
1467 // anything extra.
1468 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001469 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001470 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001471 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001472 ARMConstantPoolValue *CPV =
1473 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1474
Jim Grosbache7b52522010-04-14 22:28:31 +00001475 // Get the address of the callee into a register
1476 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1477 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1478 Callee = DAG.getLoad(getPointerTy(), dl,
1479 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001480 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001481 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001482 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1483 const char *Sym = S->getSymbol();
1484
1485 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001486 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001487 ARMConstantPoolValue *CPV =
1488 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1489 ARMPCLabelIndex, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001490 // Get the address of the callee into a register
1491 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1492 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1493 Callee = DAG.getLoad(getPointerTy(), dl,
1494 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001495 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001496 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001497 }
1498 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001499 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001500 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001501 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001502 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001503 getTargetMachine().getRelocationModel() != Reloc::Static;
1504 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001505 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001506 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001507 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001508 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001509 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001510 ARMConstantPoolValue *CPV =
1511 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001512 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001513 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001514 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001515 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001516 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001517 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001518 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001519 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001520 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001521 } else {
1522 // On ELF targets for PIC code, direct calls should go through the PLT
1523 unsigned OpFlags = 0;
1524 if (Subtarget->isTargetELF() &&
1525 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1526 OpFlags = ARMII::MO_PLT;
1527 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1528 }
Bill Wendling056292f2008-09-16 21:48:12 +00001529 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001530 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001531 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001532 getTargetMachine().getRelocationModel() != Reloc::Static;
1533 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001534 // tBX takes a register source operand.
1535 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001536 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001537 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001538 ARMConstantPoolValue *CPV =
1539 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1540 ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001541 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001542 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001543 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001544 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001545 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001546 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001547 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001548 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001549 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001550 } else {
1551 unsigned OpFlags = 0;
1552 // On ELF targets for PIC code, direct calls should go through the PLT
1553 if (Subtarget->isTargetELF() &&
1554 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1555 OpFlags = ARMII::MO_PLT;
1556 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1557 }
Evan Chenga8e29892007-01-19 07:51:42 +00001558 }
1559
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001560 // FIXME: handle tail calls differently.
1561 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001562 if (Subtarget->isThumb()) {
1563 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001564 CallOpc = ARMISD::CALL_NOLINK;
1565 else
1566 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1567 } else {
1568 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001569 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1570 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001571 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001572
Dan Gohman475871a2008-07-27 21:46:04 +00001573 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001574 Ops.push_back(Chain);
1575 Ops.push_back(Callee);
1576
1577 // Add argument registers to the end of the list so that they are known live
1578 // into the call.
1579 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1580 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1581 RegsToPass[i].second.getValueType()));
1582
Gabor Greifba36cb52008-08-28 21:40:38 +00001583 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001584 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001585
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001586 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001587 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001588 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001589
Duncan Sands4bdcb612008-07-02 17:40:58 +00001590 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001591 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001592 InFlag = Chain.getValue(1);
1593
Chris Lattnere563bbc2008-10-11 22:08:30 +00001594 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1595 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001596 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001597 InFlag = Chain.getValue(1);
1598
Bob Wilson1f595bb2009-04-17 19:07:39 +00001599 // Handle result values, copying them out of physregs into vregs that we
1600 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001601 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1602 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001603}
1604
Stuart Hastingsf222e592011-02-28 17:17:53 +00001605/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001606/// on the stack. Remember the next parameter register to allocate,
1607/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001608/// this.
1609void
Stuart Hastingsc7315872011-04-20 16:47:52 +00001610llvm::ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
1611 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1612 assert((State->getCallOrPrologue() == Prologue ||
1613 State->getCallOrPrologue() == Call) &&
1614 "unhandled ParmContext");
1615 if ((!State->isFirstByValRegValid()) &&
1616 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1617 State->setFirstByValReg(reg);
1618 // At a call site, a byval parameter that is split between
1619 // registers and memory needs its size truncated here. In a
1620 // function prologue, such byval parameters are reassembled in
1621 // memory, and are not truncated.
1622 if (State->getCallOrPrologue() == Call) {
1623 unsigned excess = 4 * (ARM::R4 - reg);
1624 assert(size >= excess && "expected larger existing stack allocation");
1625 size -= excess;
1626 }
1627 }
1628 // Confiscate any remaining parameter registers to preclude their
1629 // assignment to subsequent parameters.
1630 while (State->AllocateReg(GPRArgRegs, 4))
1631 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001632}
1633
Dale Johannesen51e28e62010-06-03 21:09:53 +00001634/// MatchingStackOffset - Return true if the given stack call argument is
1635/// already available in the same position (relatively) of the caller's
1636/// incoming argument stack.
1637static
1638bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1639 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1640 const ARMInstrInfo *TII) {
1641 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1642 int FI = INT_MAX;
1643 if (Arg.getOpcode() == ISD::CopyFromReg) {
1644 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001645 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001646 return false;
1647 MachineInstr *Def = MRI->getVRegDef(VR);
1648 if (!Def)
1649 return false;
1650 if (!Flags.isByVal()) {
1651 if (!TII->isLoadFromStackSlot(Def, FI))
1652 return false;
1653 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001654 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001655 }
1656 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1657 if (Flags.isByVal())
1658 // ByVal argument is passed in as a pointer but it's now being
1659 // dereferenced. e.g.
1660 // define @foo(%struct.X* %A) {
1661 // tail call @bar(%struct.X* byval %A)
1662 // }
1663 return false;
1664 SDValue Ptr = Ld->getBasePtr();
1665 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1666 if (!FINode)
1667 return false;
1668 FI = FINode->getIndex();
1669 } else
1670 return false;
1671
1672 assert(FI != INT_MAX);
1673 if (!MFI->isFixedObjectIndex(FI))
1674 return false;
1675 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1676}
1677
1678/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1679/// for tail call optimization. Targets which want to do tail call
1680/// optimization should implement this function.
1681bool
1682ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1683 CallingConv::ID CalleeCC,
1684 bool isVarArg,
1685 bool isCalleeStructRet,
1686 bool isCallerStructRet,
1687 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001688 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001689 const SmallVectorImpl<ISD::InputArg> &Ins,
1690 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001691 const Function *CallerF = DAG.getMachineFunction().getFunction();
1692 CallingConv::ID CallerCC = CallerF->getCallingConv();
1693 bool CCMatch = CallerCC == CalleeCC;
1694
1695 // Look for obvious safe cases to perform tail call optimization that do not
1696 // require ABI changes. This is what gcc calls sibcall.
1697
Jim Grosbach7616b642010-06-16 23:45:49 +00001698 // Do not sibcall optimize vararg calls unless the call site is not passing
1699 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001700 if (isVarArg && !Outs.empty())
1701 return false;
1702
1703 // Also avoid sibcall optimization if either caller or callee uses struct
1704 // return semantics.
1705 if (isCalleeStructRet || isCallerStructRet)
1706 return false;
1707
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001708 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001709 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1710 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1711 // support in the assembler and linker to be used. This would need to be
1712 // fixed to fully support tail calls in Thumb1.
1713 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001714 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1715 // LR. This means if we need to reload LR, it takes an extra instructions,
1716 // which outweighs the value of the tail call; but here we don't know yet
1717 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001718 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001719 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001720
1721 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1722 // but we need to make sure there are enough registers; the only valid
1723 // registers are the 4 used for parameters. We don't currently do this
1724 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001725 if (Subtarget->isThumb1Only())
1726 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001727
Dale Johannesen51e28e62010-06-03 21:09:53 +00001728 // If the calling conventions do not match, then we'd better make sure the
1729 // results are returned in the same way as what the caller expects.
1730 if (!CCMatch) {
1731 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001732 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1733 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001734 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1735
1736 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001737 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1738 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001739 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1740
1741 if (RVLocs1.size() != RVLocs2.size())
1742 return false;
1743 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1744 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1745 return false;
1746 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1747 return false;
1748 if (RVLocs1[i].isRegLoc()) {
1749 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1750 return false;
1751 } else {
1752 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1753 return false;
1754 }
1755 }
1756 }
1757
1758 // If the callee takes no arguments then go on to check the results of the
1759 // call.
1760 if (!Outs.empty()) {
1761 // Check if stack adjustment is needed. For now, do not do this if any
1762 // argument is passed on the stack.
1763 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001764 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1765 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001766 CCInfo.AnalyzeCallOperands(Outs,
1767 CCAssignFnForNode(CalleeCC, false, isVarArg));
1768 if (CCInfo.getNextStackOffset()) {
1769 MachineFunction &MF = DAG.getMachineFunction();
1770
1771 // Check if the arguments are already laid out in the right way as
1772 // the caller's fixed stack objects.
1773 MachineFrameInfo *MFI = MF.getFrameInfo();
1774 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1775 const ARMInstrInfo *TII =
1776 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001777 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1778 i != e;
1779 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001780 CCValAssign &VA = ArgLocs[i];
1781 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001782 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001783 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001784 if (VA.getLocInfo() == CCValAssign::Indirect)
1785 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001786 if (VA.needsCustom()) {
1787 // f64 and vector types are split into multiple registers or
1788 // register/stack-slot combinations. The types will not match
1789 // the registers; give up on memory f64 refs until we figure
1790 // out what to do about this.
1791 if (!VA.isRegLoc())
1792 return false;
1793 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001794 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001795 if (RegVT == MVT::v2f64) {
1796 if (!ArgLocs[++i].isRegLoc())
1797 return false;
1798 if (!ArgLocs[++i].isRegLoc())
1799 return false;
1800 }
1801 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001802 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1803 MFI, MRI, TII))
1804 return false;
1805 }
1806 }
1807 }
1808 }
1809
1810 return true;
1811}
1812
Dan Gohman98ca4f22009-08-05 01:29:28 +00001813SDValue
1814ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001815 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001816 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001817 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001818 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001819
Bob Wilsondee46d72009-04-17 20:35:10 +00001820 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001821 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001822
Bob Wilsondee46d72009-04-17 20:35:10 +00001823 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001824 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1825 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001826
Dan Gohman98ca4f22009-08-05 01:29:28 +00001827 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001828 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1829 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001830
1831 // If this is the first return lowered for this function, add
1832 // the regs to the liveout set for the function.
1833 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1834 for (unsigned i = 0; i != RVLocs.size(); ++i)
1835 if (RVLocs[i].isRegLoc())
1836 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001837 }
1838
Bob Wilson1f595bb2009-04-17 19:07:39 +00001839 SDValue Flag;
1840
1841 // Copy the result values into the output registers.
1842 for (unsigned i = 0, realRVLocIdx = 0;
1843 i != RVLocs.size();
1844 ++i, ++realRVLocIdx) {
1845 CCValAssign &VA = RVLocs[i];
1846 assert(VA.isRegLoc() && "Can only return in registers!");
1847
Dan Gohmanc9403652010-07-07 15:54:55 +00001848 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001849
1850 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001851 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001852 case CCValAssign::Full: break;
1853 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001854 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001855 break;
1856 }
1857
Bob Wilson1f595bb2009-04-17 19:07:39 +00001858 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001859 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001860 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001861 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1862 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001863 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001864 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001865
1866 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1867 Flag = Chain.getValue(1);
1868 VA = RVLocs[++i]; // skip ahead to next loc
1869 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1870 HalfGPRs.getValue(1), Flag);
1871 Flag = Chain.getValue(1);
1872 VA = RVLocs[++i]; // skip ahead to next loc
1873
1874 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001875 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1876 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001877 }
1878 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1879 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001880 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001881 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001882 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001883 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001884 VA = RVLocs[++i]; // skip ahead to next loc
1885 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1886 Flag);
1887 } else
1888 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1889
Bob Wilsondee46d72009-04-17 20:35:10 +00001890 // Guarantee that all emitted copies are
1891 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001892 Flag = Chain.getValue(1);
1893 }
1894
1895 SDValue result;
1896 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001897 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001898 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001899 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001900
1901 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001902}
1903
Evan Cheng3d2125c2010-11-30 23:55:39 +00001904bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1905 if (N->getNumValues() != 1)
1906 return false;
1907 if (!N->hasNUsesOfValue(1, 0))
1908 return false;
1909
1910 unsigned NumCopies = 0;
Jason W Kim1de886c2012-02-10 16:07:59 +00001911 SDNode* Copies[2] = { 0, 0 };
Evan Cheng3d2125c2010-11-30 23:55:39 +00001912 SDNode *Use = *N->use_begin();
1913 if (Use->getOpcode() == ISD::CopyToReg) {
1914 Copies[NumCopies++] = Use;
1915 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1916 // f64 returned in a pair of GPRs.
1917 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1918 UI != UE; ++UI) {
1919 if (UI->getOpcode() != ISD::CopyToReg)
1920 return false;
1921 Copies[UI.getUse().getResNo()] = *UI;
1922 ++NumCopies;
1923 }
1924 } else if (Use->getOpcode() == ISD::BITCAST) {
1925 // f32 returned in a single GPR.
1926 if (!Use->hasNUsesOfValue(1, 0))
1927 return false;
1928 Use = *Use->use_begin();
1929 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1930 return false;
1931 Copies[NumCopies++] = Use;
1932 } else {
1933 return false;
1934 }
1935
1936 if (NumCopies != 1 && NumCopies != 2)
1937 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001938
1939 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001940 for (unsigned i = 0; i < NumCopies; ++i) {
1941 SDNode *Copy = Copies[i];
1942 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1943 UI != UE; ++UI) {
1944 if (UI->getOpcode() == ISD::CopyToReg) {
1945 SDNode *Use = *UI;
Jason W Kim1de886c2012-02-10 16:07:59 +00001946 if (Use == Copies[0] || ((NumCopies == 2) && (Use == Copies[1])))
Evan Cheng3d2125c2010-11-30 23:55:39 +00001947 continue;
1948 return false;
1949 }
1950 if (UI->getOpcode() != ARMISD::RET_FLAG)
1951 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001952 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001953 }
1954 }
1955
Evan Cheng1bf891a2010-12-01 22:59:46 +00001956 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001957}
1958
Evan Cheng485fafc2011-03-21 01:19:09 +00001959bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1960 if (!EnableARMTailCalls)
1961 return false;
1962
1963 if (!CI->isTailCall())
1964 return false;
1965
1966 return !Subtarget->isThumb1Only();
1967}
1968
Bob Wilsonb62d2572009-11-03 00:02:05 +00001969// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1970// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1971// one of the above mentioned nodes. It has to be wrapped because otherwise
1972// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1973// be used to form addressing mode. These wrapped nodes will be selected
1974// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001975static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001976 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001977 // FIXME there is no actual debug info here
1978 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001979 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001980 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001981 if (CP->isMachineConstantPoolEntry())
1982 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1983 CP->getAlignment());
1984 else
1985 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1986 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001987 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001988}
1989
Jim Grosbache1102ca2010-07-19 17:20:38 +00001990unsigned ARMTargetLowering::getJumpTableEncoding() const {
1991 return MachineJumpTableInfo::EK_Inline;
1992}
1993
Dan Gohmand858e902010-04-17 15:26:15 +00001994SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1995 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001996 MachineFunction &MF = DAG.getMachineFunction();
1997 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1998 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001999 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00002000 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00002001 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00002002 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2003 SDValue CPAddr;
2004 if (RelocM == Reloc::Static) {
2005 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2006 } else {
2007 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002008 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00002009 ARMConstantPoolValue *CPV =
2010 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2011 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson907eebd2009-11-02 20:59:23 +00002012 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2013 }
2014 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2015 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002016 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002017 false, false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00002018 if (RelocM == Reloc::Static)
2019 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00002020 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00002021 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00002022}
2023
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002024// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00002025SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002026ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002027 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002028 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002029 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002030 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00002031 MachineFunction &MF = DAG.getMachineFunction();
2032 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002033 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002034 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002035 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2036 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002037 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002038 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00002039 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002040 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002041 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002042 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002043
Evan Chenge7e0d622009-11-06 22:24:13 +00002044 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002045 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002046
2047 // call __tls_get_addr.
2048 ArgListTy Args;
2049 ArgListEntry Entry;
2050 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002051 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002052 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002053 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00002054 std::pair<SDValue, SDValue> CallResult =
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002055 LowerCallTo(Chain, (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00002056 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002057 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002058 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002059 return CallResult.first;
2060}
2061
2062// Lower ISD::GlobalTLSAddress using the "initial exec" or
2063// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00002064SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002065ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002066 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002067 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002068 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002069 SDValue Offset;
2070 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002071 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002072 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002073 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002074
Chris Lattner4fb63d02009-07-15 04:12:33 +00002075 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002076 MachineFunction &MF = DAG.getMachineFunction();
2077 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002078 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002079 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002080 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2081 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002082 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2083 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2084 true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002085 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002086 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002087 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002088 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002089 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002090 Chain = Offset.getValue(1);
2091
Evan Chenge7e0d622009-11-06 22:24:13 +00002092 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002093 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002094
Evan Cheng9eda6892009-10-31 03:39:36 +00002095 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002096 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002097 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002098 } else {
2099 // local exec model
Bill Wendling5bb77992011-10-01 08:00:54 +00002100 ARMConstantPoolValue *CPV =
2101 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002102 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002103 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002104 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002105 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002106 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002107 }
2108
2109 // The address of the thread local variable is the add of the thread
2110 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002111 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002112}
2113
Dan Gohman475871a2008-07-27 21:46:04 +00002114SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002115ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002116 // TODO: implement the "local dynamic" model
2117 assert(Subtarget->isTargetELF() &&
2118 "TLS not implemented for non-ELF targets");
2119 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2120 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
2121 // otherwise use the "Local Exec" TLS Model
2122 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
2123 return LowerToTLSGeneralDynamicModel(GA, DAG);
2124 else
2125 return LowerToTLSExecModels(GA, DAG);
2126}
2127
Dan Gohman475871a2008-07-27 21:46:04 +00002128SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002129 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002130 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002131 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002132 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002133 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2134 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002135 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002136 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002137 ARMConstantPoolConstant::Create(GV,
2138 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002139 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002140 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002141 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002142 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002143 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002144 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002145 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002146 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002147 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002148 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002149 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002150 MachinePointerInfo::getGOT(),
2151 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002152 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002153 }
2154
2155 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloy015cca62011-10-26 08:53:19 +00002156 // pair. This is always cheaper.
2157 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002158 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002159 // FIXME: Once remat is capable of dealing with instructions with register
2160 // operands, expand this into two nodes.
2161 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2162 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002163 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002164 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2165 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2166 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2167 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002168 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002169 }
2170}
2171
Dan Gohman475871a2008-07-27 21:46:04 +00002172SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002173 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002174 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002175 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002176 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002177 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002178 MachineFunction &MF = DAG.getMachineFunction();
2179 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2180
Jakob Stoklund Olesen8f37a242012-01-07 20:49:15 +00002181 // FIXME: Enable this for static codegen when tool issues are fixed. Also
2182 // update ARMFastISel::ARMMaterializeGV.
Evan Chengf31151f2011-10-26 01:17:44 +00002183 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002184 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002185 // FIXME: Once remat is capable of dealing with instructions with register
2186 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002187 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002188 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2189 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2190
Evan Cheng53519f02011-01-21 18:55:51 +00002191 unsigned Wrapper = (RelocM == Reloc::PIC_)
2192 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2193 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002194 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002195 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2196 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002197 MachinePointerInfo::getGOT(),
2198 false, false, false, 0);
Evan Chengfc8475b2011-01-19 02:16:49 +00002199 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002200 }
2201
2202 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002203 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002204 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002205 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002206 } else {
2207 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002208 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2209 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002210 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue,
2211 PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002212 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002213 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002214 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002215
Evan Cheng9eda6892009-10-31 03:39:36 +00002216 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002217 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002218 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002219 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002220
2221 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002222 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002223 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002224 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002225
Evan Cheng63476a82009-09-03 07:04:02 +00002226 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002227 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002228 false, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002229
2230 return Result;
2231}
2232
Dan Gohman475871a2008-07-27 21:46:04 +00002233SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002234 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002235 assert(Subtarget->isTargetELF() &&
2236 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002237 MachineFunction &MF = DAG.getMachineFunction();
2238 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002239 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002240 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002241 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002242 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingfe31e672011-10-01 08:58:29 +00002243 ARMConstantPoolValue *CPV =
2244 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2245 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002246 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002247 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002248 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002249 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002250 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002251 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002252 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002253}
2254
Jim Grosbach0e0da732009-05-12 23:59:14 +00002255SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002256ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2257 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002258 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendlingce370cf2011-10-07 21:25:38 +00002259 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2260 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002261 Op.getOperand(1), Val);
2262}
2263
2264SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002265ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2266 DebugLoc dl = Op.getDebugLoc();
2267 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2268 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2269}
2270
2271SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002272ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002273 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002274 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002275 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002276 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002277 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002278 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002279 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002280 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2281 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002282 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002283 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002284 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002285 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002286 EVT PtrVT = getPointerTy();
2287 DebugLoc dl = Op.getDebugLoc();
2288 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2289 SDValue CPAddr;
2290 unsigned PCAdj = (RelocM != Reloc::PIC_)
2291 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002292 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002293 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2294 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002295 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002296 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002297 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002298 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002299 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002300 false, false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002301
2302 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002303 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002304 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2305 }
2306 return Result;
2307 }
Evan Cheng92e39162011-03-29 23:06:19 +00002308 case Intrinsic::arm_neon_vmulls:
2309 case Intrinsic::arm_neon_vmullu: {
2310 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2311 ? ARMISD::VMULLs : ARMISD::VMULLu;
2312 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2313 Op.getOperand(1), Op.getOperand(2));
2314 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002315 }
2316}
2317
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002318static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002319 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002320 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002321 if (!Subtarget->hasDataBarrier()) {
2322 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2323 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2324 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002325 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002326 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002327 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002328 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002329 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002330
2331 SDValue Op5 = Op.getOperand(5);
2332 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2333 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2334 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2335 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2336
2337 ARM_MB::MemBOpt DMBOpt;
2338 if (isDeviceBarrier)
2339 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2340 else
2341 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2342 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2343 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002344}
2345
Eli Friedman26689ac2011-08-03 21:06:02 +00002346
2347static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2348 const ARMSubtarget *Subtarget) {
2349 // FIXME: handle "fence singlethread" more efficiently.
2350 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002351 if (!Subtarget->hasDataBarrier()) {
2352 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2353 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2354 // here.
2355 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2356 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002357 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002358 DAG.getConstant(0, MVT::i32));
2359 }
2360
Eli Friedman26689ac2011-08-03 21:06:02 +00002361 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002362 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002363}
2364
Evan Chengdfed19f2010-11-03 06:34:55 +00002365static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2366 const ARMSubtarget *Subtarget) {
2367 // ARM pre v5TE and Thumb1 does not have preload instructions.
2368 if (!(Subtarget->isThumb2() ||
2369 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2370 // Just preserve the chain.
2371 return Op.getOperand(0);
2372
2373 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002374 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2375 if (!isRead &&
2376 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2377 // ARMv7 with MP extension has PLDW.
2378 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002379
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002380 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2381 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002382 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002383 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002384 isData = ~isData & 1;
2385 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002386
2387 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002388 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2389 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002390}
2391
Dan Gohman1e93df62010-04-17 14:41:14 +00002392static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2393 MachineFunction &MF = DAG.getMachineFunction();
2394 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2395
Evan Chenga8e29892007-01-19 07:51:42 +00002396 // vastart just stores the address of the VarArgsFrameIndex slot into the
2397 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002398 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002399 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002400 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002401 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002402 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2403 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002404}
2405
Dan Gohman475871a2008-07-27 21:46:04 +00002406SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002407ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2408 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002409 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002410 MachineFunction &MF = DAG.getMachineFunction();
2411 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2412
2413 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002414 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002415 RC = ARM::tGPRRegisterClass;
2416 else
2417 RC = ARM::GPRRegisterClass;
2418
2419 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002420 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002421 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002422
2423 SDValue ArgValue2;
2424 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002425 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002426 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002427
2428 // Create load node to retrieve arguments from the stack.
2429 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002430 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002431 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002432 false, false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002433 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002434 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002435 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002436 }
2437
Jim Grosbache5165492009-11-09 00:11:35 +00002438 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002439}
2440
Stuart Hastingsc7315872011-04-20 16:47:52 +00002441void
2442ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2443 unsigned &VARegSize, unsigned &VARegSaveSize)
2444 const {
2445 unsigned NumGPRs;
2446 if (CCInfo.isFirstByValRegValid())
2447 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2448 else {
2449 unsigned int firstUnalloced;
2450 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2451 sizeof(GPRArgRegs) /
2452 sizeof(GPRArgRegs[0]));
2453 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2454 }
2455
2456 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2457 VARegSize = NumGPRs * 4;
2458 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2459}
2460
2461// The remaining GPRs hold either the beginning of variable-argument
2462// data, or the beginning of an aggregate passed by value (usuall
2463// byval). Either way, we allocate stack slots adjacent to the data
2464// provided by our caller, and store the unallocated registers there.
2465// If this is a variadic function, the va_list pointer will begin with
2466// these values; otherwise, this reassembles a (byval) structure that
2467// was split between registers and memory.
2468void
2469ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2470 DebugLoc dl, SDValue &Chain,
2471 unsigned ArgOffset) const {
2472 MachineFunction &MF = DAG.getMachineFunction();
2473 MachineFrameInfo *MFI = MF.getFrameInfo();
2474 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2475 unsigned firstRegToSaveIndex;
2476 if (CCInfo.isFirstByValRegValid())
2477 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2478 else {
2479 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2480 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2481 }
2482
2483 unsigned VARegSize, VARegSaveSize;
2484 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2485 if (VARegSaveSize) {
2486 // If this function is vararg, store any remaining integer argument regs
2487 // to their spots on the stack so that they may be loaded by deferencing
2488 // the result of va_next.
2489 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002490 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2491 ArgOffset + VARegSaveSize
2492 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002493 false));
2494 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2495 getPointerTy());
2496
2497 SmallVector<SDValue, 4> MemOps;
2498 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
2499 TargetRegisterClass *RC;
2500 if (AFI->isThumb1OnlyFunction())
2501 RC = ARM::tGPRRegisterClass;
2502 else
2503 RC = ARM::GPRRegisterClass;
2504
2505 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2506 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2507 SDValue Store =
2508 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002509 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002510 false, false, 0);
2511 MemOps.push_back(Store);
2512 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2513 DAG.getConstant(4, getPointerTy()));
2514 }
2515 if (!MemOps.empty())
2516 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2517 &MemOps[0], MemOps.size());
2518 } else
2519 // This will point to the next argument passed via stack.
2520 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2521}
2522
Bob Wilson5bafff32009-06-22 23:27:02 +00002523SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002524ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002525 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002526 const SmallVectorImpl<ISD::InputArg>
2527 &Ins,
2528 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002529 SmallVectorImpl<SDValue> &InVals)
2530 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002531 MachineFunction &MF = DAG.getMachineFunction();
2532 MachineFrameInfo *MFI = MF.getFrameInfo();
2533
Bob Wilson1f595bb2009-04-17 19:07:39 +00002534 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2535
2536 // Assign locations to all of the incoming arguments.
2537 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002538 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2539 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002540 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002541 CCAssignFnForNode(CallConv, /* Return*/ false,
2542 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002543
2544 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002545 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002546
Stuart Hastingsf222e592011-02-28 17:17:53 +00002547 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002548 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2549 CCValAssign &VA = ArgLocs[i];
2550
Bob Wilsondee46d72009-04-17 20:35:10 +00002551 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002552 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002553 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002554
Bob Wilson1f595bb2009-04-17 19:07:39 +00002555 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002556 // f64 and vector types are split up into multiple registers or
2557 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002558 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002559 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002560 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002561 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002562 SDValue ArgValue2;
2563 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002564 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002565 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2566 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002567 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002568 false, false, false, 0);
Bob Wilson6a234f02010-04-13 22:03:22 +00002569 } else {
2570 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2571 Chain, DAG, dl);
2572 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002573 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2574 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002575 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002576 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002577 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2578 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002579 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002580
Bob Wilson5bafff32009-06-22 23:27:02 +00002581 } else {
2582 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002583
Owen Anderson825b72b2009-08-11 20:47:22 +00002584 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002585 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002586 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002587 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002588 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002589 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002590 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002591 RC = (AFI->isThumb1OnlyFunction() ?
2592 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002593 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002594 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002595
2596 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002597 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002598 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002599 }
2600
2601 // If this is an 8 or 16-bit value, it is really passed promoted
2602 // to 32 bits. Insert an assert[sz]ext to capture this, then
2603 // truncate to the right size.
2604 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002605 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002606 case CCValAssign::Full: break;
2607 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002608 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002609 break;
2610 case CCValAssign::SExt:
2611 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2612 DAG.getValueType(VA.getValVT()));
2613 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2614 break;
2615 case CCValAssign::ZExt:
2616 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2617 DAG.getValueType(VA.getValVT()));
2618 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2619 break;
2620 }
2621
Dan Gohman98ca4f22009-08-05 01:29:28 +00002622 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002623
2624 } else { // VA.isRegLoc()
2625
2626 // sanity check
2627 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002628 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002629
Stuart Hastingsf222e592011-02-28 17:17:53 +00002630 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002631
Stuart Hastingsf222e592011-02-28 17:17:53 +00002632 // Some Ins[] entries become multiple ArgLoc[] entries.
2633 // Process them only once.
2634 if (index != lastInsIndex)
2635 {
2636 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002637 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002638 // This can be changed with more analysis.
2639 // In case of tail call optimization mark all arguments mutable.
2640 // Since they could be overwritten by lowering of arguments in case of
2641 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002642 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002643 unsigned VARegSize, VARegSaveSize;
2644 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2645 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2646 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002647 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002648 int FI = MFI->CreateFixedObject(Bytes,
2649 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002650 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2651 } else {
2652 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2653 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002654
Stuart Hastingsf222e592011-02-28 17:17:53 +00002655 // Create load nodes to retrieve arguments from the stack.
2656 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2657 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2658 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002659 false, false, false, 0));
Stuart Hastingsf222e592011-02-28 17:17:53 +00002660 }
2661 lastInsIndex = index;
2662 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002663 }
2664 }
2665
2666 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002667 if (isVarArg)
2668 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002669
Dan Gohman98ca4f22009-08-05 01:29:28 +00002670 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002671}
2672
2673/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002674static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002675 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002676 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002677 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002678 // Maybe this has already been legalized into the constant pool?
2679 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002680 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002681 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002682 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002683 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002684 }
2685 }
2686 return false;
2687}
2688
Evan Chenga8e29892007-01-19 07:51:42 +00002689/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2690/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002691SDValue
2692ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002693 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002694 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002695 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002696 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002697 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002698 // Constant does not fit, try adjusting it by one?
2699 switch (CC) {
2700 default: break;
2701 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002702 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002703 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002704 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002705 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002706 }
2707 break;
2708 case ISD::SETULT:
2709 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002710 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002711 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002712 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002713 }
2714 break;
2715 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002716 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002717 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002718 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002719 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002720 }
2721 break;
2722 case ISD::SETULE:
2723 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002724 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002725 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002726 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002727 }
2728 break;
2729 }
2730 }
2731 }
2732
2733 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002734 ARMISD::NodeType CompareType;
2735 switch (CondCode) {
2736 default:
2737 CompareType = ARMISD::CMP;
2738 break;
2739 case ARMCC::EQ:
2740 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002741 // Uses only Z Flag
2742 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002743 break;
2744 }
Evan Cheng218977b2010-07-13 19:27:42 +00002745 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002746 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002747}
2748
2749/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002750SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002751ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002752 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002753 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002754 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002755 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002756 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002757 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2758 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002759}
2760
Bob Wilson79f56c92011-03-08 01:17:20 +00002761/// duplicateCmp - Glue values can have only one use, so this function
2762/// duplicates a comparison node.
2763SDValue
2764ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2765 unsigned Opc = Cmp.getOpcode();
2766 DebugLoc DL = Cmp.getDebugLoc();
2767 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2768 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2769
2770 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2771 Cmp = Cmp.getOperand(0);
2772 Opc = Cmp.getOpcode();
2773 if (Opc == ARMISD::CMPFP)
2774 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2775 else {
2776 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2777 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2778 }
2779 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2780}
2781
Bill Wendlingde2b1512010-08-11 08:43:16 +00002782SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2783 SDValue Cond = Op.getOperand(0);
2784 SDValue SelectTrue = Op.getOperand(1);
2785 SDValue SelectFalse = Op.getOperand(2);
2786 DebugLoc dl = Op.getDebugLoc();
2787
2788 // Convert:
2789 //
2790 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2791 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2792 //
2793 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2794 const ConstantSDNode *CMOVTrue =
2795 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2796 const ConstantSDNode *CMOVFalse =
2797 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2798
2799 if (CMOVTrue && CMOVFalse) {
2800 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2801 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2802
2803 SDValue True;
2804 SDValue False;
2805 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2806 True = SelectTrue;
2807 False = SelectFalse;
2808 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2809 True = SelectFalse;
2810 False = SelectTrue;
2811 }
2812
2813 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002814 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002815 SDValue ARMcc = Cond.getOperand(2);
2816 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002817 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002818 assert(True.getValueType() == VT);
2819 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002820 }
2821 }
2822 }
2823
2824 return DAG.getSelectCC(dl, Cond,
2825 DAG.getConstant(0, Cond.getValueType()),
2826 SelectTrue, SelectFalse, ISD::SETNE);
2827}
2828
Dan Gohmand858e902010-04-17 15:26:15 +00002829SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002830 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002831 SDValue LHS = Op.getOperand(0);
2832 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002833 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002834 SDValue TrueVal = Op.getOperand(2);
2835 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002836 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002837
Owen Anderson825b72b2009-08-11 20:47:22 +00002838 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002839 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002840 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002841 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Jim Grosbachb04546f2011-09-13 20:30:37 +00002842 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002843 }
2844
2845 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002846 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002847
Evan Cheng218977b2010-07-13 19:27:42 +00002848 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2849 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002850 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002851 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002852 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002853 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002854 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002855 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002856 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002857 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002858 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002859 }
2860 return Result;
2861}
2862
Evan Cheng218977b2010-07-13 19:27:42 +00002863/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2864/// to morph to an integer compare sequence.
2865static bool canChangeToInt(SDValue Op, bool &SeenZero,
2866 const ARMSubtarget *Subtarget) {
2867 SDNode *N = Op.getNode();
2868 if (!N->hasOneUse())
2869 // Otherwise it requires moving the value from fp to integer registers.
2870 return false;
2871 if (!N->getNumValues())
2872 return false;
2873 EVT VT = Op.getValueType();
2874 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2875 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2876 // vmrs are very slow, e.g. cortex-a8.
2877 return false;
2878
2879 if (isFloatingPointZero(Op)) {
2880 SeenZero = true;
2881 return true;
2882 }
2883 return ISD::isNormalLoad(N);
2884}
2885
2886static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2887 if (isFloatingPointZero(Op))
2888 return DAG.getConstant(0, MVT::i32);
2889
2890 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2891 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002892 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002893 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002894 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002895
2896 llvm_unreachable("Unknown VFP cmp argument!");
2897}
2898
2899static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2900 SDValue &RetVal1, SDValue &RetVal2) {
2901 if (isFloatingPointZero(Op)) {
2902 RetVal1 = DAG.getConstant(0, MVT::i32);
2903 RetVal2 = DAG.getConstant(0, MVT::i32);
2904 return;
2905 }
2906
2907 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2908 SDValue Ptr = Ld->getBasePtr();
2909 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2910 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002911 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002912 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002913 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002914
2915 EVT PtrType = Ptr.getValueType();
2916 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2917 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2918 PtrType, Ptr, DAG.getConstant(4, PtrType));
2919 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2920 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002921 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002922 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002923 Ld->isInvariant(), NewAlign);
Evan Cheng218977b2010-07-13 19:27:42 +00002924 return;
2925 }
2926
2927 llvm_unreachable("Unknown VFP cmp argument!");
2928}
2929
2930/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2931/// f32 and even f64 comparisons to integer ones.
2932SDValue
2933ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2934 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002935 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002936 SDValue LHS = Op.getOperand(2);
2937 SDValue RHS = Op.getOperand(3);
2938 SDValue Dest = Op.getOperand(4);
2939 DebugLoc dl = Op.getDebugLoc();
2940
2941 bool SeenZero = false;
2942 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2943 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002944 // If one of the operand is zero, it's safe to ignore the NaN case since
2945 // we only care about equality comparisons.
2946 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Bob Wilson1b772f92011-03-08 01:17:16 +00002947 // If unsafe fp math optimization is enabled and there are no other uses of
2948 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00002949 // to an integer comparison.
2950 if (CC == ISD::SETOEQ)
2951 CC = ISD::SETEQ;
2952 else if (CC == ISD::SETUNE)
2953 CC = ISD::SETNE;
2954
2955 SDValue ARMcc;
2956 if (LHS.getValueType() == MVT::f32) {
2957 LHS = bitcastf32Toi32(LHS, DAG);
2958 RHS = bitcastf32Toi32(RHS, DAG);
2959 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2960 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2961 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2962 Chain, Dest, ARMcc, CCR, Cmp);
2963 }
2964
2965 SDValue LHS1, LHS2;
2966 SDValue RHS1, RHS2;
2967 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2968 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2969 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2970 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002971 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002972 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2973 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2974 }
2975
2976 return SDValue();
2977}
2978
2979SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2980 SDValue Chain = Op.getOperand(0);
2981 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2982 SDValue LHS = Op.getOperand(2);
2983 SDValue RHS = Op.getOperand(3);
2984 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002985 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002986
Owen Anderson825b72b2009-08-11 20:47:22 +00002987 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002988 SDValue ARMcc;
2989 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002990 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002991 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002992 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002993 }
2994
Owen Anderson825b72b2009-08-11 20:47:22 +00002995 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002996
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002997 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng218977b2010-07-13 19:27:42 +00002998 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2999 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3000 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3001 if (Result.getNode())
3002 return Result;
3003 }
3004
Evan Chenga8e29892007-01-19 07:51:42 +00003005 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00003006 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003007
Evan Cheng218977b2010-07-13 19:27:42 +00003008 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3009 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003010 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003011 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003012 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00003013 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003014 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00003015 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3016 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00003017 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003018 }
3019 return Res;
3020}
3021
Dan Gohmand858e902010-04-17 15:26:15 +00003022SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00003023 SDValue Chain = Op.getOperand(0);
3024 SDValue Table = Op.getOperand(1);
3025 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003026 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003027
Owen Andersone50ed302009-08-10 22:56:29 +00003028 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00003029 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3030 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00003031 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00003032 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00003033 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00003034 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3035 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00003036 if (Subtarget->isThumb2()) {
3037 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3038 // which does another jump to the destination. This also makes it easier
3039 // to translate it to TBB / TBH later.
3040 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00003041 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00003042 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003043 }
Evan Cheng66ac5312009-07-25 00:33:29 +00003044 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00003045 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003046 MachinePointerInfo::getJumpTable(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003047 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003048 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003049 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00003050 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003051 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00003052 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003053 MachinePointerInfo::getJumpTable(),
3054 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003055 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003056 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003057 }
Evan Chenga8e29892007-01-19 07:51:42 +00003058}
3059
Eli Friedman14e809c2011-11-09 23:36:02 +00003060static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
David Blaikie810d6d32012-01-16 05:17:39 +00003061 assert(Op.getValueType().getVectorElementType() == MVT::i32
3062 && "Unexpected custom lowering");
Eli Friedman14e809c2011-11-09 23:36:02 +00003063
3064 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3065 return Op;
3066 return DAG.UnrollVectorOp(Op.getNode());
3067}
3068
Bob Wilson76a312b2010-03-19 22:51:32 +00003069static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman14e809c2011-11-09 23:36:02 +00003070 EVT VT = Op.getValueType();
3071 if (VT.isVector())
3072 return LowerVectorFP_TO_INT(Op, DAG);
3073
Bob Wilson76a312b2010-03-19 22:51:32 +00003074 DebugLoc dl = Op.getDebugLoc();
3075 unsigned Opc;
3076
3077 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003078 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003079 case ISD::FP_TO_SINT:
3080 Opc = ARMISD::FTOSI;
3081 break;
3082 case ISD::FP_TO_UINT:
3083 Opc = ARMISD::FTOUI;
3084 break;
3085 }
3086 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003087 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003088}
3089
Cameron Zwarich3007d332011-03-29 21:41:55 +00003090static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3091 EVT VT = Op.getValueType();
3092 DebugLoc dl = Op.getDebugLoc();
3093
Eli Friedman14e809c2011-11-09 23:36:02 +00003094 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3095 if (VT.getVectorElementType() == MVT::f32)
3096 return Op;
3097 return DAG.UnrollVectorOp(Op.getNode());
3098 }
3099
Duncan Sands1f6a3292011-08-12 14:54:45 +00003100 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3101 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003102 if (VT != MVT::v4f32)
3103 return DAG.UnrollVectorOp(Op.getNode());
3104
3105 unsigned CastOpc;
3106 unsigned Opc;
3107 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003108 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003109 case ISD::SINT_TO_FP:
3110 CastOpc = ISD::SIGN_EXTEND;
3111 Opc = ISD::SINT_TO_FP;
3112 break;
3113 case ISD::UINT_TO_FP:
3114 CastOpc = ISD::ZERO_EXTEND;
3115 Opc = ISD::UINT_TO_FP;
3116 break;
3117 }
3118
3119 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3120 return DAG.getNode(Opc, dl, VT, Op);
3121}
3122
Bob Wilson76a312b2010-03-19 22:51:32 +00003123static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3124 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003125 if (VT.isVector())
3126 return LowerVectorINT_TO_FP(Op, DAG);
3127
Bob Wilson76a312b2010-03-19 22:51:32 +00003128 DebugLoc dl = Op.getDebugLoc();
3129 unsigned Opc;
3130
3131 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003132 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003133 case ISD::SINT_TO_FP:
3134 Opc = ARMISD::SITOF;
3135 break;
3136 case ISD::UINT_TO_FP:
3137 Opc = ARMISD::UITOF;
3138 break;
3139 }
3140
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003141 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003142 return DAG.getNode(Opc, dl, VT, Op);
3143}
3144
Evan Cheng515fe3a2010-07-08 02:08:50 +00003145SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003146 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003147 SDValue Tmp0 = Op.getOperand(0);
3148 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003149 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003150 EVT VT = Op.getValueType();
3151 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003152 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3153 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3154 bool UseNEON = !InGPR && Subtarget->hasNEON();
3155
3156 if (UseNEON) {
3157 // Use VBSL to copy the sign bit.
3158 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3159 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3160 DAG.getTargetConstant(EncodedVal, MVT::i32));
3161 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3162 if (VT == MVT::f64)
3163 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3164 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3165 DAG.getConstant(32, MVT::i32));
3166 else /*if (VT == MVT::f32)*/
3167 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3168 if (SrcVT == MVT::f32) {
3169 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3170 if (VT == MVT::f64)
3171 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3172 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3173 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003174 } else if (VT == MVT::f32)
3175 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3176 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3177 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003178 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3179 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3180
3181 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3182 MVT::i32);
3183 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3184 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3185 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003186
Evan Chenge573fb32011-02-23 02:24:55 +00003187 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3188 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3189 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003190 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003191 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3192 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3193 DAG.getConstant(0, MVT::i32));
3194 } else {
3195 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3196 }
3197
3198 return Res;
3199 }
Evan Chengc143dd42011-02-11 02:28:55 +00003200
3201 // Bitcast operand 1 to i32.
3202 if (SrcVT == MVT::f64)
3203 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3204 &Tmp1, 1).getValue(1);
3205 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3206
Evan Chenge573fb32011-02-23 02:24:55 +00003207 // Or in the signbit with integer operations.
3208 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3209 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3210 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3211 if (VT == MVT::f32) {
3212 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3213 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3214 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3215 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003216 }
3217
Evan Chenge573fb32011-02-23 02:24:55 +00003218 // f64: Or the high part with signbit and then combine two parts.
3219 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3220 &Tmp0, 1);
3221 SDValue Lo = Tmp0.getValue(0);
3222 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3223 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3224 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003225}
3226
Evan Cheng2457f2c2010-05-22 01:47:14 +00003227SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3228 MachineFunction &MF = DAG.getMachineFunction();
3229 MachineFrameInfo *MFI = MF.getFrameInfo();
3230 MFI->setReturnAddressIsTaken(true);
3231
3232 EVT VT = Op.getValueType();
3233 DebugLoc dl = Op.getDebugLoc();
3234 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3235 if (Depth) {
3236 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3237 SDValue Offset = DAG.getConstant(4, MVT::i32);
3238 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3239 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003240 MachinePointerInfo(), false, false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003241 }
3242
3243 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003244 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003245 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3246}
3247
Dan Gohmand858e902010-04-17 15:26:15 +00003248SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003249 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3250 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003251
Owen Andersone50ed302009-08-10 22:56:29 +00003252 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003253 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3254 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003255 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003256 ? ARM::R7 : ARM::R11;
3257 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3258 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003259 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3260 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003261 false, false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003262 return FrameAddr;
3263}
3264
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003265/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003266/// expand a bit convert where either the source or destination type is i64 to
3267/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3268/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3269/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003270static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003271 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3272 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003273 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003274
Bob Wilson9f3f0612010-04-17 05:30:19 +00003275 // This function is only supposed to be called for i64 types, either as the
3276 // source or destination of the bit convert.
3277 EVT SrcVT = Op.getValueType();
3278 EVT DstVT = N->getValueType(0);
3279 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003280 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003281
Bob Wilson9f3f0612010-04-17 05:30:19 +00003282 // Turn i64->f64 into VMOVDRR.
3283 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003284 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3285 DAG.getConstant(0, MVT::i32));
3286 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3287 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003288 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003289 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003290 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003291
Jim Grosbache5165492009-11-09 00:11:35 +00003292 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003293 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3294 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3295 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3296 // Merge the pieces into a single i64 value.
3297 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3298 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003299
Bob Wilson9f3f0612010-04-17 05:30:19 +00003300 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003301}
3302
Bob Wilson5bafff32009-06-22 23:27:02 +00003303/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003304/// Zero vectors are used to represent vector negation and in those cases
3305/// will be implemented with the NEON VNEG instruction. However, VNEG does
3306/// not support i64 elements, so sometimes the zero vectors will need to be
3307/// explicitly constructed. Regardless, use a canonical VMOV to create the
3308/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003309static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003310 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003311 // The canonical modified immediate encoding of a zero vector is....0!
3312 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3313 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3314 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003315 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003316}
3317
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003318/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3319/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003320SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3321 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003322 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3323 EVT VT = Op.getValueType();
3324 unsigned VTBits = VT.getSizeInBits();
3325 DebugLoc dl = Op.getDebugLoc();
3326 SDValue ShOpLo = Op.getOperand(0);
3327 SDValue ShOpHi = Op.getOperand(1);
3328 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003329 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003330 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003331
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003332 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3333
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003334 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3335 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3336 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3337 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3338 DAG.getConstant(VTBits, MVT::i32));
3339 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3340 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003341 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003342
3343 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3344 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003345 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003346 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003347 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003348 CCR, Cmp);
3349
3350 SDValue Ops[2] = { Lo, Hi };
3351 return DAG.getMergeValues(Ops, 2, dl);
3352}
3353
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003354/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3355/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003356SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3357 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003358 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3359 EVT VT = Op.getValueType();
3360 unsigned VTBits = VT.getSizeInBits();
3361 DebugLoc dl = Op.getDebugLoc();
3362 SDValue ShOpLo = Op.getOperand(0);
3363 SDValue ShOpHi = Op.getOperand(1);
3364 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003365 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003366
3367 assert(Op.getOpcode() == ISD::SHL_PARTS);
3368 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3369 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3370 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3371 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3372 DAG.getConstant(VTBits, MVT::i32));
3373 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3374 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3375
3376 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3377 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3378 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003379 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003380 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003381 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003382 CCR, Cmp);
3383
3384 SDValue Ops[2] = { Lo, Hi };
3385 return DAG.getMergeValues(Ops, 2, dl);
3386}
3387
Jim Grosbach4725ca72010-09-08 03:54:02 +00003388SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003389 SelectionDAG &DAG) const {
3390 // The rounding mode is in bits 23:22 of the FPSCR.
3391 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3392 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3393 // so that the shift + and get folded into a bitfield extract.
3394 DebugLoc dl = Op.getDebugLoc();
3395 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3396 DAG.getConstant(Intrinsic::arm_get_fpscr,
3397 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003398 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003399 DAG.getConstant(1U << 22, MVT::i32));
3400 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3401 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003402 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003403 DAG.getConstant(3, MVT::i32));
3404}
3405
Jim Grosbach3482c802010-01-18 19:58:49 +00003406static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3407 const ARMSubtarget *ST) {
3408 EVT VT = N->getValueType(0);
3409 DebugLoc dl = N->getDebugLoc();
3410
3411 if (!ST->hasV6T2Ops())
3412 return SDValue();
3413
3414 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3415 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3416}
3417
Bob Wilson5bafff32009-06-22 23:27:02 +00003418static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3419 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003420 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003421 DebugLoc dl = N->getDebugLoc();
3422
Bob Wilsond5448bb2010-11-18 21:16:28 +00003423 if (!VT.isVector())
3424 return SDValue();
3425
Bob Wilson5bafff32009-06-22 23:27:02 +00003426 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003427 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003428
Bob Wilsond5448bb2010-11-18 21:16:28 +00003429 // Left shifts translate directly to the vshiftu intrinsic.
3430 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003431 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003432 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3433 N->getOperand(0), N->getOperand(1));
3434
3435 assert((N->getOpcode() == ISD::SRA ||
3436 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3437
3438 // NEON uses the same intrinsics for both left and right shifts. For
3439 // right shifts, the shift amounts are negative, so negate the vector of
3440 // shift amounts.
3441 EVT ShiftVT = N->getOperand(1).getValueType();
3442 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3443 getZeroVector(ShiftVT, DAG, dl),
3444 N->getOperand(1));
3445 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3446 Intrinsic::arm_neon_vshifts :
3447 Intrinsic::arm_neon_vshiftu);
3448 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3449 DAG.getConstant(vshiftInt, MVT::i32),
3450 N->getOperand(0), NegatedCount);
3451}
3452
3453static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3454 const ARMSubtarget *ST) {
3455 EVT VT = N->getValueType(0);
3456 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003457
Eli Friedmance392eb2009-08-22 03:13:10 +00003458 // We can get here for a node like i32 = ISD::SHL i32, i64
3459 if (VT != MVT::i64)
3460 return SDValue();
3461
3462 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003463 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003464
Chris Lattner27a6c732007-11-24 07:07:01 +00003465 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3466 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003467 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003468 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003469
Chris Lattner27a6c732007-11-24 07:07:01 +00003470 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003471 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003472
Chris Lattner27a6c732007-11-24 07:07:01 +00003473 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003474 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003475 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003476 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003477 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003478
Chris Lattner27a6c732007-11-24 07:07:01 +00003479 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3480 // captures the result into a carry flag.
3481 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003482 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003483
Chris Lattner27a6c732007-11-24 07:07:01 +00003484 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003485 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003486
Chris Lattner27a6c732007-11-24 07:07:01 +00003487 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003488 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003489}
3490
Bob Wilson5bafff32009-06-22 23:27:02 +00003491static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3492 SDValue TmpOp0, TmpOp1;
3493 bool Invert = false;
3494 bool Swap = false;
3495 unsigned Opc = 0;
3496
3497 SDValue Op0 = Op.getOperand(0);
3498 SDValue Op1 = Op.getOperand(1);
3499 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003500 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003501 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3502 DebugLoc dl = Op.getDebugLoc();
3503
3504 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3505 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003506 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003507 case ISD::SETUNE:
3508 case ISD::SETNE: Invert = true; // Fallthrough
3509 case ISD::SETOEQ:
3510 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3511 case ISD::SETOLT:
3512 case ISD::SETLT: Swap = true; // Fallthrough
3513 case ISD::SETOGT:
3514 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3515 case ISD::SETOLE:
3516 case ISD::SETLE: Swap = true; // Fallthrough
3517 case ISD::SETOGE:
3518 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3519 case ISD::SETUGE: Swap = true; // Fallthrough
3520 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3521 case ISD::SETUGT: Swap = true; // Fallthrough
3522 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3523 case ISD::SETUEQ: Invert = true; // Fallthrough
3524 case ISD::SETONE:
3525 // Expand this to (OLT | OGT).
3526 TmpOp0 = Op0;
3527 TmpOp1 = Op1;
3528 Opc = ISD::OR;
3529 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3530 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3531 break;
3532 case ISD::SETUO: Invert = true; // Fallthrough
3533 case ISD::SETO:
3534 // Expand this to (OLT | OGE).
3535 TmpOp0 = Op0;
3536 TmpOp1 = Op1;
3537 Opc = ISD::OR;
3538 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3539 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3540 break;
3541 }
3542 } else {
3543 // Integer comparisons.
3544 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003545 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003546 case ISD::SETNE: Invert = true;
3547 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3548 case ISD::SETLT: Swap = true;
3549 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3550 case ISD::SETLE: Swap = true;
3551 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3552 case ISD::SETULT: Swap = true;
3553 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3554 case ISD::SETULE: Swap = true;
3555 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3556 }
3557
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003558 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003559 if (Opc == ARMISD::VCEQ) {
3560
3561 SDValue AndOp;
3562 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3563 AndOp = Op0;
3564 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3565 AndOp = Op1;
3566
3567 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003568 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003569 AndOp = AndOp.getOperand(0);
3570
3571 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3572 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003573 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3574 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003575 Invert = !Invert;
3576 }
3577 }
3578 }
3579
3580 if (Swap)
3581 std::swap(Op0, Op1);
3582
Owen Andersonc24cb352010-11-08 23:21:22 +00003583 // If one of the operands is a constant vector zero, attempt to fold the
3584 // comparison to a specialized compare-against-zero form.
3585 SDValue SingleOp;
3586 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3587 SingleOp = Op0;
3588 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3589 if (Opc == ARMISD::VCGE)
3590 Opc = ARMISD::VCLEZ;
3591 else if (Opc == ARMISD::VCGT)
3592 Opc = ARMISD::VCLTZ;
3593 SingleOp = Op1;
3594 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003595
Owen Andersonc24cb352010-11-08 23:21:22 +00003596 SDValue Result;
3597 if (SingleOp.getNode()) {
3598 switch (Opc) {
3599 case ARMISD::VCEQ:
3600 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3601 case ARMISD::VCGE:
3602 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3603 case ARMISD::VCLEZ:
3604 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3605 case ARMISD::VCGT:
3606 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3607 case ARMISD::VCLTZ:
3608 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3609 default:
3610 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3611 }
3612 } else {
3613 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3614 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003615
3616 if (Invert)
3617 Result = DAG.getNOT(dl, Result, VT);
3618
3619 return Result;
3620}
3621
Bob Wilsond3c42842010-06-14 22:19:57 +00003622/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3623/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003624/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003625static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3626 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003627 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003628 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003629
Bob Wilson827b2102010-06-15 19:05:35 +00003630 // SplatBitSize is set to the smallest size that splats the vector, so a
3631 // zero vector will always have SplatBitSize == 8. However, NEON modified
3632 // immediate instructions others than VMOV do not support the 8-bit encoding
3633 // of a zero vector, and the default encoding of zero is supposed to be the
3634 // 32-bit version.
3635 if (SplatBits == 0)
3636 SplatBitSize = 32;
3637
Bob Wilson5bafff32009-06-22 23:27:02 +00003638 switch (SplatBitSize) {
3639 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003640 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003641 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003642 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003643 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003644 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003645 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003646 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003647 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003648
3649 case 16:
3650 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003651 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003652 if ((SplatBits & ~0xff) == 0) {
3653 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003654 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003655 Imm = SplatBits;
3656 break;
3657 }
3658 if ((SplatBits & ~0xff00) == 0) {
3659 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003660 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003661 Imm = SplatBits >> 8;
3662 break;
3663 }
3664 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003665
3666 case 32:
3667 // NEON's 32-bit VMOV supports splat values where:
3668 // * only one byte is nonzero, or
3669 // * the least significant byte is 0xff and the second byte is nonzero, or
3670 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003671 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003672 if ((SplatBits & ~0xff) == 0) {
3673 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003674 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003675 Imm = SplatBits;
3676 break;
3677 }
3678 if ((SplatBits & ~0xff00) == 0) {
3679 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003680 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003681 Imm = SplatBits >> 8;
3682 break;
3683 }
3684 if ((SplatBits & ~0xff0000) == 0) {
3685 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003686 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003687 Imm = SplatBits >> 16;
3688 break;
3689 }
3690 if ((SplatBits & ~0xff000000) == 0) {
3691 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003692 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003693 Imm = SplatBits >> 24;
3694 break;
3695 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003696
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003697 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3698 if (type == OtherModImm) return SDValue();
3699
Bob Wilson5bafff32009-06-22 23:27:02 +00003700 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003701 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3702 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003703 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003704 Imm = SplatBits >> 8;
3705 SplatBits |= 0xff;
3706 break;
3707 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003708
3709 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003710 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3711 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003712 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003713 Imm = SplatBits >> 16;
3714 SplatBits |= 0xffff;
3715 break;
3716 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003717
3718 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3719 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3720 // VMOV.I32. A (very) minor optimization would be to replicate the value
3721 // and fall through here to test for a valid 64-bit splat. But, then the
3722 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003723 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003724
3725 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003726 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003727 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003728 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003729 uint64_t BitMask = 0xff;
3730 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003731 unsigned ImmMask = 1;
3732 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003733 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003734 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003735 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003736 Imm |= ImmMask;
3737 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003738 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003739 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003740 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003741 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003742 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003743 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003744 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003745 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003746 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003747 break;
3748 }
3749
Bob Wilson1a913ed2010-06-11 21:34:50 +00003750 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003751 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003752 }
3753
Bob Wilsoncba270d2010-07-13 21:16:48 +00003754 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3755 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003756}
3757
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003758static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003759 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003760 unsigned NumElts = VT.getVectorNumElements();
3761 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003762
3763 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3764 if (M[0] < 0)
3765 return false;
3766
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003767 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003768
3769 // If this is a VEXT shuffle, the immediate value is the index of the first
3770 // element. The other shuffle indices must be the successive elements after
3771 // the first one.
3772 unsigned ExpectedElt = Imm;
3773 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003774 // Increment the expected index. If it wraps around, it may still be
3775 // a VEXT but the source vectors must be swapped.
3776 ExpectedElt += 1;
3777 if (ExpectedElt == NumElts * 2) {
3778 ExpectedElt = 0;
3779 ReverseVEXT = true;
3780 }
3781
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003782 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003783 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003784 return false;
3785 }
3786
3787 // Adjust the index value if the source operands will be swapped.
3788 if (ReverseVEXT)
3789 Imm -= NumElts;
3790
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003791 return true;
3792}
3793
Bob Wilson8bb9e482009-07-26 00:39:34 +00003794/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3795/// instruction with the specified blocksize. (The order of the elements
3796/// within each block of the vector is reversed.)
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003797static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003798 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3799 "Only possible block sizes for VREV are: 16, 32, 64");
3800
Bob Wilson8bb9e482009-07-26 00:39:34 +00003801 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003802 if (EltSz == 64)
3803 return false;
3804
3805 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003806 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003807 // If the first shuffle index is UNDEF, be optimistic.
3808 if (M[0] < 0)
3809 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003810
3811 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3812 return false;
3813
3814 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003815 if (M[i] < 0) continue; // ignore UNDEF indices
3816 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003817 return false;
3818 }
3819
3820 return true;
3821}
3822
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003823static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003824 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3825 // range, then 0 is placed into the resulting vector. So pretty much any mask
3826 // of 8 elements can work here.
3827 return VT == MVT::v8i8 && M.size() == 8;
3828}
3829
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003830static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003831 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3832 if (EltSz == 64)
3833 return false;
3834
Bob Wilsonc692cb72009-08-21 20:54:19 +00003835 unsigned NumElts = VT.getVectorNumElements();
3836 WhichResult = (M[0] == 0 ? 0 : 1);
3837 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003838 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3839 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003840 return false;
3841 }
3842 return true;
3843}
3844
Bob Wilson324f4f12009-12-03 06:40:55 +00003845/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3846/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3847/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003848static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00003849 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3850 if (EltSz == 64)
3851 return false;
3852
3853 unsigned NumElts = VT.getVectorNumElements();
3854 WhichResult = (M[0] == 0 ? 0 : 1);
3855 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003856 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3857 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003858 return false;
3859 }
3860 return true;
3861}
3862
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003863static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003864 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3865 if (EltSz == 64)
3866 return false;
3867
Bob Wilsonc692cb72009-08-21 20:54:19 +00003868 unsigned NumElts = VT.getVectorNumElements();
3869 WhichResult = (M[0] == 0 ? 0 : 1);
3870 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003871 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003872 if ((unsigned) M[i] != 2 * i + WhichResult)
3873 return false;
3874 }
3875
3876 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003877 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003878 return false;
3879
3880 return true;
3881}
3882
Bob Wilson324f4f12009-12-03 06:40:55 +00003883/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3884/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3885/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003886static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00003887 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3888 if (EltSz == 64)
3889 return false;
3890
3891 unsigned Half = VT.getVectorNumElements() / 2;
3892 WhichResult = (M[0] == 0 ? 0 : 1);
3893 for (unsigned j = 0; j != 2; ++j) {
3894 unsigned Idx = WhichResult;
3895 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003896 int MIdx = M[i + j * Half];
3897 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003898 return false;
3899 Idx += 2;
3900 }
3901 }
3902
3903 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3904 if (VT.is64BitVector() && EltSz == 32)
3905 return false;
3906
3907 return true;
3908}
3909
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003910static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003911 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3912 if (EltSz == 64)
3913 return false;
3914
Bob Wilsonc692cb72009-08-21 20:54:19 +00003915 unsigned NumElts = VT.getVectorNumElements();
3916 WhichResult = (M[0] == 0 ? 0 : 1);
3917 unsigned Idx = WhichResult * NumElts / 2;
3918 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003919 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3920 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003921 return false;
3922 Idx += 1;
3923 }
3924
3925 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003926 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003927 return false;
3928
3929 return true;
3930}
3931
Bob Wilson324f4f12009-12-03 06:40:55 +00003932/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3933/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3934/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003935static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00003936 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3937 if (EltSz == 64)
3938 return false;
3939
3940 unsigned NumElts = VT.getVectorNumElements();
3941 WhichResult = (M[0] == 0 ? 0 : 1);
3942 unsigned Idx = WhichResult * NumElts / 2;
3943 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003944 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3945 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003946 return false;
3947 Idx += 1;
3948 }
3949
3950 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3951 if (VT.is64BitVector() && EltSz == 32)
3952 return false;
3953
3954 return true;
3955}
3956
Dale Johannesenf630c712010-07-29 20:10:08 +00003957// If N is an integer constant that can be moved into a register in one
3958// instruction, return an SDValue of such a constant (will become a MOV
3959// instruction). Otherwise return null.
3960static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3961 const ARMSubtarget *ST, DebugLoc dl) {
3962 uint64_t Val;
3963 if (!isa<ConstantSDNode>(N))
3964 return SDValue();
3965 Val = cast<ConstantSDNode>(N)->getZExtValue();
3966
3967 if (ST->isThumb1Only()) {
3968 if (Val <= 255 || ~Val <= 255)
3969 return DAG.getConstant(Val, MVT::i32);
3970 } else {
3971 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3972 return DAG.getConstant(Val, MVT::i32);
3973 }
3974 return SDValue();
3975}
3976
Bob Wilson5bafff32009-06-22 23:27:02 +00003977// If this is a case we can't handle, return null and let the default
3978// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003979SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3980 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003981 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003982 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003983 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003984
3985 APInt SplatBits, SplatUndef;
3986 unsigned SplatBitSize;
3987 bool HasAnyUndefs;
3988 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003989 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003990 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003991 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003992 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003993 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003994 DAG, VmovVT, VT.is128BitVector(),
3995 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003996 if (Val.getNode()) {
3997 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003998 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003999 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004000
4001 // Try an immediate VMVN.
Eli Friedman8e4d0422011-10-13 22:40:23 +00004002 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004003 Val = isNEONModifiedImm(NegatedImm,
4004 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004005 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004006 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004007 if (Val.getNode()) {
4008 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004009 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004010 }
Evan Chengeaa192a2011-11-15 02:12:34 +00004011
4012 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedman2f21e8c2011-12-15 22:56:53 +00004013 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedmaneffab8f2011-12-09 23:54:42 +00004014 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Chengeaa192a2011-11-15 02:12:34 +00004015 if (ImmVal != -1) {
4016 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4017 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4018 }
4019 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004020 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00004021 }
4022
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004023 // Scan through the operands to see if only one value is used.
4024 unsigned NumElts = VT.getVectorNumElements();
4025 bool isOnlyLowElement = true;
4026 bool usesOnlyOneValue = true;
4027 bool isConstant = true;
4028 SDValue Value;
4029 for (unsigned i = 0; i < NumElts; ++i) {
4030 SDValue V = Op.getOperand(i);
4031 if (V.getOpcode() == ISD::UNDEF)
4032 continue;
4033 if (i > 0)
4034 isOnlyLowElement = false;
4035 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4036 isConstant = false;
4037
4038 if (!Value.getNode())
4039 Value = V;
4040 else if (V != Value)
4041 usesOnlyOneValue = false;
4042 }
4043
4044 if (!Value.getNode())
4045 return DAG.getUNDEF(VT);
4046
4047 if (isOnlyLowElement)
4048 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4049
Dale Johannesenf630c712010-07-29 20:10:08 +00004050 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4051
Dale Johannesen575cd142010-10-19 20:00:17 +00004052 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4053 // i32 and try again.
4054 if (usesOnlyOneValue && EltSize <= 32) {
4055 if (!isConstant)
4056 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4057 if (VT.getVectorElementType().isFloatingPoint()) {
4058 SmallVector<SDValue, 8> Ops;
4059 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004060 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00004061 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00004062 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
4063 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00004064 Val = LowerBUILD_VECTOR(Val, DAG, ST);
4065 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004066 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004067 }
Dale Johannesen575cd142010-10-19 20:00:17 +00004068 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
4069 if (Val.getNode())
4070 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004071 }
4072
4073 // If all elements are constants and the case above didn't get hit, fall back
4074 // to the default expansion, which will generate a load from the constant
4075 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004076 if (isConstant)
4077 return SDValue();
4078
Bob Wilson11a1dff2011-01-07 21:37:30 +00004079 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4080 if (NumElts >= 4) {
4081 SDValue shuffle = ReconstructShuffle(Op, DAG);
4082 if (shuffle != SDValue())
4083 return shuffle;
4084 }
4085
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004086 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004087 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4088 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004089 if (EltSize >= 32) {
4090 // Do the expansion with floating-point types, since that is what the VFP
4091 // registers are defined to use, and since i64 is not legal.
4092 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4093 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004094 SmallVector<SDValue, 8> Ops;
4095 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004096 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004097 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004098 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004099 }
4100
4101 return SDValue();
4102}
4103
Bob Wilson11a1dff2011-01-07 21:37:30 +00004104// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004105// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004106SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4107 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004108 DebugLoc dl = Op.getDebugLoc();
4109 EVT VT = Op.getValueType();
4110 unsigned NumElts = VT.getVectorNumElements();
4111
4112 SmallVector<SDValue, 2> SourceVecs;
4113 SmallVector<unsigned, 2> MinElts;
4114 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004115
Bob Wilson11a1dff2011-01-07 21:37:30 +00004116 for (unsigned i = 0; i < NumElts; ++i) {
4117 SDValue V = Op.getOperand(i);
4118 if (V.getOpcode() == ISD::UNDEF)
4119 continue;
4120 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4121 // A shuffle can only come from building a vector from various
4122 // elements of other vectors.
4123 return SDValue();
Eli Friedman46995fa2011-10-14 23:58:49 +00004124 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
4125 VT.getVectorElementType()) {
4126 // This code doesn't know how to handle shuffles where the vector
4127 // element types do not match (this happens because type legalization
4128 // promotes the return type of EXTRACT_VECTOR_ELT).
4129 // FIXME: It might be appropriate to extend this code to handle
4130 // mismatched types.
4131 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004132 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004133
Bob Wilson11a1dff2011-01-07 21:37:30 +00004134 // Record this extraction against the appropriate vector if possible...
4135 SDValue SourceVec = V.getOperand(0);
4136 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4137 bool FoundSource = false;
4138 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4139 if (SourceVecs[j] == SourceVec) {
4140 if (MinElts[j] > EltNo)
4141 MinElts[j] = EltNo;
4142 if (MaxElts[j] < EltNo)
4143 MaxElts[j] = EltNo;
4144 FoundSource = true;
4145 break;
4146 }
4147 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004148
Bob Wilson11a1dff2011-01-07 21:37:30 +00004149 // Or record a new source if not...
4150 if (!FoundSource) {
4151 SourceVecs.push_back(SourceVec);
4152 MinElts.push_back(EltNo);
4153 MaxElts.push_back(EltNo);
4154 }
4155 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004156
Bob Wilson11a1dff2011-01-07 21:37:30 +00004157 // Currently only do something sane when at most two source vectors
4158 // involved.
4159 if (SourceVecs.size() > 2)
4160 return SDValue();
4161
4162 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4163 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004164
Bob Wilson11a1dff2011-01-07 21:37:30 +00004165 // This loop extracts the usage patterns of the source vectors
4166 // and prepares appropriate SDValues for a shuffle if possible.
4167 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4168 if (SourceVecs[i].getValueType() == VT) {
4169 // No VEXT necessary
4170 ShuffleSrcs[i] = SourceVecs[i];
4171 VEXTOffsets[i] = 0;
4172 continue;
4173 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4174 // It probably isn't worth padding out a smaller vector just to
4175 // break it down again in a shuffle.
4176 return SDValue();
4177 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004178
Bob Wilson11a1dff2011-01-07 21:37:30 +00004179 // Since only 64-bit and 128-bit vectors are legal on ARM and
4180 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004181 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4182 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004183
Bob Wilson11a1dff2011-01-07 21:37:30 +00004184 if (MaxElts[i] - MinElts[i] >= NumElts) {
4185 // Span too large for a VEXT to cope
4186 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004187 }
4188
Bob Wilson11a1dff2011-01-07 21:37:30 +00004189 if (MinElts[i] >= NumElts) {
4190 // The extraction can just take the second half
4191 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004192 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4193 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004194 DAG.getIntPtrConstant(NumElts));
4195 } else if (MaxElts[i] < NumElts) {
4196 // The extraction can just take the first half
4197 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004198 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4199 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004200 DAG.getIntPtrConstant(0));
4201 } else {
4202 // An actual VEXT is needed
4203 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004204 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4205 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004206 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004207 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4208 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004209 DAG.getIntPtrConstant(NumElts));
4210 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4211 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4212 }
4213 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004214
Bob Wilson11a1dff2011-01-07 21:37:30 +00004215 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004216
Bob Wilson11a1dff2011-01-07 21:37:30 +00004217 for (unsigned i = 0; i < NumElts; ++i) {
4218 SDValue Entry = Op.getOperand(i);
4219 if (Entry.getOpcode() == ISD::UNDEF) {
4220 Mask.push_back(-1);
4221 continue;
4222 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004223
Bob Wilson11a1dff2011-01-07 21:37:30 +00004224 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004225 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4226 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004227 if (ExtractVec == SourceVecs[0]) {
4228 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4229 } else {
4230 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4231 }
4232 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004233
Bob Wilson11a1dff2011-01-07 21:37:30 +00004234 // Final check before we try to produce nonsense...
4235 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004236 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4237 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004238
Bob Wilson11a1dff2011-01-07 21:37:30 +00004239 return SDValue();
4240}
4241
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004242/// isShuffleMaskLegal - Targets can use this to indicate that they only
4243/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4244/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4245/// are assumed to be legal.
4246bool
4247ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4248 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004249 if (VT.getVectorNumElements() == 4 &&
4250 (VT.is128BitVector() || VT.is64BitVector())) {
4251 unsigned PFIndexes[4];
4252 for (unsigned i = 0; i != 4; ++i) {
4253 if (M[i] < 0)
4254 PFIndexes[i] = 8;
4255 else
4256 PFIndexes[i] = M[i];
4257 }
4258
4259 // Compute the index in the perfect shuffle table.
4260 unsigned PFTableIndex =
4261 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4262 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4263 unsigned Cost = (PFEntry >> 30);
4264
4265 if (Cost <= 4)
4266 return true;
4267 }
4268
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004269 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004270 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004271
Bob Wilson53dd2452010-06-07 23:53:38 +00004272 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4273 return (EltSize >= 32 ||
4274 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004275 isVREVMask(M, VT, 64) ||
4276 isVREVMask(M, VT, 32) ||
4277 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004278 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004279 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004280 isVTRNMask(M, VT, WhichResult) ||
4281 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004282 isVZIPMask(M, VT, WhichResult) ||
4283 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4284 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4285 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004286}
4287
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004288/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4289/// the specified operations to build the shuffle.
4290static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4291 SDValue RHS, SelectionDAG &DAG,
4292 DebugLoc dl) {
4293 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4294 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4295 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4296
4297 enum {
4298 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4299 OP_VREV,
4300 OP_VDUP0,
4301 OP_VDUP1,
4302 OP_VDUP2,
4303 OP_VDUP3,
4304 OP_VEXT1,
4305 OP_VEXT2,
4306 OP_VEXT3,
4307 OP_VUZPL, // VUZP, left result
4308 OP_VUZPR, // VUZP, right result
4309 OP_VZIPL, // VZIP, left result
4310 OP_VZIPR, // VZIP, right result
4311 OP_VTRNL, // VTRN, left result
4312 OP_VTRNR // VTRN, right result
4313 };
4314
4315 if (OpNum == OP_COPY) {
4316 if (LHSID == (1*9+2)*9+3) return LHS;
4317 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4318 return RHS;
4319 }
4320
4321 SDValue OpLHS, OpRHS;
4322 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4323 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4324 EVT VT = OpLHS.getValueType();
4325
4326 switch (OpNum) {
4327 default: llvm_unreachable("Unknown shuffle opcode!");
4328 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004329 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004330 if (VT.getVectorElementType() == MVT::i32 ||
4331 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004332 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4333 // vrev <4 x i16> -> VREV32
4334 if (VT.getVectorElementType() == MVT::i16)
4335 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4336 // vrev <4 x i8> -> VREV16
4337 assert(VT.getVectorElementType() == MVT::i8);
4338 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004339 case OP_VDUP0:
4340 case OP_VDUP1:
4341 case OP_VDUP2:
4342 case OP_VDUP3:
4343 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004344 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004345 case OP_VEXT1:
4346 case OP_VEXT2:
4347 case OP_VEXT3:
4348 return DAG.getNode(ARMISD::VEXT, dl, VT,
4349 OpLHS, OpRHS,
4350 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4351 case OP_VUZPL:
4352 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004353 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004354 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4355 case OP_VZIPL:
4356 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004357 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004358 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4359 case OP_VTRNL:
4360 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004361 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4362 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004363 }
4364}
4365
Bill Wendling69a05a72011-03-14 23:02:38 +00004366static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004367 ArrayRef<int> ShuffleMask,
Bill Wendling69a05a72011-03-14 23:02:38 +00004368 SelectionDAG &DAG) {
4369 // Check to see if we can use the VTBL instruction.
4370 SDValue V1 = Op.getOperand(0);
4371 SDValue V2 = Op.getOperand(1);
4372 DebugLoc DL = Op.getDebugLoc();
4373
4374 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004375 for (ArrayRef<int>::iterator
Bill Wendling69a05a72011-03-14 23:02:38 +00004376 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4377 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4378
4379 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4380 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4381 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4382 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004383
Owen Anderson76706012011-04-05 21:48:57 +00004384 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004385 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4386 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004387}
4388
Bob Wilson5bafff32009-06-22 23:27:02 +00004389static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004390 SDValue V1 = Op.getOperand(0);
4391 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004392 DebugLoc dl = Op.getDebugLoc();
4393 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004394 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsond8e17572009-08-12 22:31:50 +00004395
Bob Wilson28865062009-08-13 02:13:04 +00004396 // Convert shuffles that are directly supported on NEON to target-specific
4397 // DAG nodes, instead of keeping them as shuffles and matching them again
4398 // during code selection. This is more efficient and avoids the possibility
4399 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004400 // FIXME: floating-point vectors should be canonicalized to integer vectors
4401 // of the same time so that they get CSEd properly.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004402 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004403
Bob Wilson53dd2452010-06-07 23:53:38 +00004404 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4405 if (EltSize <= 32) {
4406 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4407 int Lane = SVN->getSplatIndex();
4408 // If this is undef splat, generate it via "just" vdup, if possible.
4409 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004410
Dan Gohman65fd6562011-11-03 21:49:52 +00004411 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson53dd2452010-06-07 23:53:38 +00004412 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4413 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4414 }
Dan Gohman65fd6562011-11-03 21:49:52 +00004415 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
4416 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
4417 // reaches it).
4418 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
4419 !isa<ConstantSDNode>(V1.getOperand(0))) {
4420 bool IsScalarToVector = true;
4421 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
4422 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
4423 IsScalarToVector = false;
4424 break;
4425 }
4426 if (IsScalarToVector)
4427 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4428 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004429 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4430 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004431 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004432
4433 bool ReverseVEXT;
4434 unsigned Imm;
4435 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4436 if (ReverseVEXT)
4437 std::swap(V1, V2);
4438 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4439 DAG.getConstant(Imm, MVT::i32));
4440 }
4441
4442 if (isVREVMask(ShuffleMask, VT, 64))
4443 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4444 if (isVREVMask(ShuffleMask, VT, 32))
4445 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4446 if (isVREVMask(ShuffleMask, VT, 16))
4447 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4448
4449 // Check for Neon shuffles that modify both input vectors in place.
4450 // If both results are used, i.e., if there are two shuffles with the same
4451 // source operands and with masks corresponding to both results of one of
4452 // these operations, DAG memoization will ensure that a single node is
4453 // used for both shuffles.
4454 unsigned WhichResult;
4455 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4456 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4457 V1, V2).getValue(WhichResult);
4458 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4459 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4460 V1, V2).getValue(WhichResult);
4461 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4462 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4463 V1, V2).getValue(WhichResult);
4464
4465 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4466 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4467 V1, V1).getValue(WhichResult);
4468 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4469 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4470 V1, V1).getValue(WhichResult);
4471 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4472 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4473 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004474 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004475
Bob Wilsonc692cb72009-08-21 20:54:19 +00004476 // If the shuffle is not directly supported and it has 4 elements, use
4477 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004478 unsigned NumElts = VT.getVectorNumElements();
4479 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004480 unsigned PFIndexes[4];
4481 for (unsigned i = 0; i != 4; ++i) {
4482 if (ShuffleMask[i] < 0)
4483 PFIndexes[i] = 8;
4484 else
4485 PFIndexes[i] = ShuffleMask[i];
4486 }
4487
4488 // Compute the index in the perfect shuffle table.
4489 unsigned PFTableIndex =
4490 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004491 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4492 unsigned Cost = (PFEntry >> 30);
4493
4494 if (Cost <= 4)
4495 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4496 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004497
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004498 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004499 if (EltSize >= 32) {
4500 // Do the expansion with floating-point types, since that is what the VFP
4501 // registers are defined to use, and since i64 is not legal.
4502 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4503 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004504 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4505 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004506 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004507 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004508 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004509 Ops.push_back(DAG.getUNDEF(EltVT));
4510 else
4511 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4512 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4513 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4514 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004515 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004516 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004517 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004518 }
4519
Bill Wendling69a05a72011-03-14 23:02:38 +00004520 if (VT == MVT::v8i8) {
4521 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4522 if (NewOp.getNode())
4523 return NewOp;
4524 }
4525
Bob Wilson22cac0d2009-08-14 05:16:33 +00004526 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004527}
4528
Eli Friedman5c89cb82011-10-24 23:08:52 +00004529static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
4530 // INSERT_VECTOR_ELT is legal only for immediate indexes.
4531 SDValue Lane = Op.getOperand(2);
4532 if (!isa<ConstantSDNode>(Lane))
4533 return SDValue();
4534
4535 return Op;
4536}
4537
Bob Wilson5bafff32009-06-22 23:27:02 +00004538static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004539 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004540 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004541 if (!isa<ConstantSDNode>(Lane))
4542 return SDValue();
4543
4544 SDValue Vec = Op.getOperand(0);
4545 if (Op.getValueType() == MVT::i32 &&
4546 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4547 DebugLoc dl = Op.getDebugLoc();
4548 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4549 }
4550
4551 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004552}
4553
Bob Wilsona6d65862009-08-03 20:36:38 +00004554static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4555 // The only time a CONCAT_VECTORS operation can have legal types is when
4556 // two 64-bit vectors are concatenated to a 128-bit vector.
4557 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4558 "unexpected CONCAT_VECTORS");
4559 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004560 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004561 SDValue Op0 = Op.getOperand(0);
4562 SDValue Op1 = Op.getOperand(1);
4563 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004564 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004565 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004566 DAG.getIntPtrConstant(0));
4567 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004568 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004569 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004570 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004571 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004572}
4573
Bob Wilson626613d2010-11-23 19:38:38 +00004574/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4575/// element has been zero/sign-extended, depending on the isSigned parameter,
4576/// from an integer type half its size.
4577static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4578 bool isSigned) {
4579 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4580 EVT VT = N->getValueType(0);
4581 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4582 SDNode *BVN = N->getOperand(0).getNode();
4583 if (BVN->getValueType(0) != MVT::v4i32 ||
4584 BVN->getOpcode() != ISD::BUILD_VECTOR)
4585 return false;
4586 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4587 unsigned HiElt = 1 - LoElt;
4588 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4589 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4590 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4591 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4592 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4593 return false;
4594 if (isSigned) {
4595 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4596 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4597 return true;
4598 } else {
4599 if (Hi0->isNullValue() && Hi1->isNullValue())
4600 return true;
4601 }
4602 return false;
4603 }
4604
4605 if (N->getOpcode() != ISD::BUILD_VECTOR)
4606 return false;
4607
4608 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4609 SDNode *Elt = N->getOperand(i).getNode();
4610 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4611 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4612 unsigned HalfSize = EltSize / 2;
4613 if (isSigned) {
Bob Wilson9d45de22011-10-18 18:46:49 +00004614 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004615 return false;
4616 } else {
Bob Wilson9d45de22011-10-18 18:46:49 +00004617 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004618 return false;
4619 }
4620 continue;
4621 }
4622 return false;
4623 }
4624
4625 return true;
4626}
4627
4628/// isSignExtended - Check if a node is a vector value that is sign-extended
4629/// or a constant BUILD_VECTOR with sign-extended elements.
4630static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4631 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4632 return true;
4633 if (isExtendedBUILD_VECTOR(N, DAG, true))
4634 return true;
4635 return false;
4636}
4637
4638/// isZeroExtended - Check if a node is a vector value that is zero-extended
4639/// or a constant BUILD_VECTOR with zero-extended elements.
4640static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4641 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4642 return true;
4643 if (isExtendedBUILD_VECTOR(N, DAG, false))
4644 return true;
4645 return false;
4646}
4647
4648/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4649/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004650static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4651 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4652 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004653 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4654 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4655 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004656 LD->isNonTemporal(), LD->isInvariant(),
4657 LD->getAlignment());
Bob Wilson626613d2010-11-23 19:38:38 +00004658 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4659 // have been legalized as a BITCAST from v4i32.
4660 if (N->getOpcode() == ISD::BITCAST) {
4661 SDNode *BVN = N->getOperand(0).getNode();
4662 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4663 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4664 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4665 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4666 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4667 }
4668 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4669 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4670 EVT VT = N->getValueType(0);
4671 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4672 unsigned NumElts = VT.getVectorNumElements();
4673 MVT TruncVT = MVT::getIntegerVT(EltSize);
4674 SmallVector<SDValue, 8> Ops;
4675 for (unsigned i = 0; i != NumElts; ++i) {
4676 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4677 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004678 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004679 }
4680 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4681 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004682}
4683
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004684static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4685 unsigned Opcode = N->getOpcode();
4686 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4687 SDNode *N0 = N->getOperand(0).getNode();
4688 SDNode *N1 = N->getOperand(1).getNode();
4689 return N0->hasOneUse() && N1->hasOneUse() &&
4690 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4691 }
4692 return false;
4693}
4694
4695static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4696 unsigned Opcode = N->getOpcode();
4697 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4698 SDNode *N0 = N->getOperand(0).getNode();
4699 SDNode *N1 = N->getOperand(1).getNode();
4700 return N0->hasOneUse() && N1->hasOneUse() &&
4701 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4702 }
4703 return false;
4704}
4705
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004706static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4707 // Multiplications are only custom-lowered for 128-bit vectors so that
4708 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4709 EVT VT = Op.getValueType();
4710 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4711 SDNode *N0 = Op.getOperand(0).getNode();
4712 SDNode *N1 = Op.getOperand(1).getNode();
4713 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004714 bool isMLA = false;
4715 bool isN0SExt = isSignExtended(N0, DAG);
4716 bool isN1SExt = isSignExtended(N1, DAG);
4717 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004718 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004719 else {
4720 bool isN0ZExt = isZeroExtended(N0, DAG);
4721 bool isN1ZExt = isZeroExtended(N1, DAG);
4722 if (isN0ZExt && isN1ZExt)
4723 NewOpc = ARMISD::VMULLu;
4724 else if (isN1SExt || isN1ZExt) {
4725 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4726 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4727 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4728 NewOpc = ARMISD::VMULLs;
4729 isMLA = true;
4730 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4731 NewOpc = ARMISD::VMULLu;
4732 isMLA = true;
4733 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4734 std::swap(N0, N1);
4735 NewOpc = ARMISD::VMULLu;
4736 isMLA = true;
4737 }
4738 }
4739
4740 if (!NewOpc) {
4741 if (VT == MVT::v2i64)
4742 // Fall through to expand this. It is not legal.
4743 return SDValue();
4744 else
4745 // Other vector multiplications are legal.
4746 return Op;
4747 }
4748 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004749
4750 // Legalize to a VMULL instruction.
4751 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004752 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004753 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004754 if (!isMLA) {
4755 Op0 = SkipExtension(N0, DAG);
4756 assert(Op0.getValueType().is64BitVector() &&
4757 Op1.getValueType().is64BitVector() &&
4758 "unexpected types for extended operands to VMULL");
4759 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4760 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004761
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004762 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4763 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4764 // vmull q0, d4, d6
4765 // vmlal q0, d5, d6
4766 // is faster than
4767 // vaddl q0, d4, d5
4768 // vmovl q1, d6
4769 // vmul q0, q0, q1
4770 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4771 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4772 EVT Op1VT = Op1.getValueType();
4773 return DAG.getNode(N0->getOpcode(), DL, VT,
4774 DAG.getNode(NewOpc, DL, VT,
4775 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4776 DAG.getNode(NewOpc, DL, VT,
4777 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004778}
4779
Owen Anderson76706012011-04-05 21:48:57 +00004780static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004781LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4782 // Convert to float
4783 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4784 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4785 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4786 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4787 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4788 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4789 // Get reciprocal estimate.
4790 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004791 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004792 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4793 // Because char has a smaller range than uchar, we can actually get away
4794 // without any newton steps. This requires that we use a weird bias
4795 // of 0xb000, however (again, this has been exhaustively tested).
4796 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4797 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4798 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4799 Y = DAG.getConstant(0xb000, MVT::i32);
4800 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4801 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4802 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4803 // Convert back to short.
4804 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4805 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4806 return X;
4807}
4808
Owen Anderson76706012011-04-05 21:48:57 +00004809static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004810LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4811 SDValue N2;
4812 // Convert to float.
4813 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4814 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4815 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4816 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4817 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4818 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004819
Nate Begeman7973f352011-02-11 20:53:29 +00004820 // Use reciprocal estimate and one refinement step.
4821 // float4 recip = vrecpeq_f32(yf);
4822 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004823 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004824 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004825 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004826 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4827 N1, N2);
4828 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4829 // Because short has a smaller range than ushort, we can actually get away
4830 // with only a single newton step. This requires that we use a weird bias
4831 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004832 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004833 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4834 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004835 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004836 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4837 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4838 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4839 // Convert back to integer and return.
4840 // return vmovn_s32(vcvt_s32_f32(result));
4841 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4842 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4843 return N0;
4844}
4845
4846static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4847 EVT VT = Op.getValueType();
4848 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4849 "unexpected type for custom-lowering ISD::SDIV");
4850
4851 DebugLoc dl = Op.getDebugLoc();
4852 SDValue N0 = Op.getOperand(0);
4853 SDValue N1 = Op.getOperand(1);
4854 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004855
Nate Begeman7973f352011-02-11 20:53:29 +00004856 if (VT == MVT::v8i8) {
4857 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4858 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004859
Nate Begeman7973f352011-02-11 20:53:29 +00004860 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4861 DAG.getIntPtrConstant(4));
4862 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004863 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004864 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4865 DAG.getIntPtrConstant(0));
4866 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4867 DAG.getIntPtrConstant(0));
4868
4869 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4870 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4871
4872 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4873 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004874
Nate Begeman7973f352011-02-11 20:53:29 +00004875 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
4876 return N0;
4877 }
4878 return LowerSDIV_v4i16(N0, N1, dl, DAG);
4879}
4880
4881static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
4882 EVT VT = Op.getValueType();
4883 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4884 "unexpected type for custom-lowering ISD::UDIV");
4885
4886 DebugLoc dl = Op.getDebugLoc();
4887 SDValue N0 = Op.getOperand(0);
4888 SDValue N1 = Op.getOperand(1);
4889 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004890
Nate Begeman7973f352011-02-11 20:53:29 +00004891 if (VT == MVT::v8i8) {
4892 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
4893 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004894
Nate Begeman7973f352011-02-11 20:53:29 +00004895 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4896 DAG.getIntPtrConstant(4));
4897 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004898 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004899 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4900 DAG.getIntPtrConstant(0));
4901 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4902 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00004903
Nate Begeman7973f352011-02-11 20:53:29 +00004904 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
4905 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00004906
Nate Begeman7973f352011-02-11 20:53:29 +00004907 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4908 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004909
4910 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00004911 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
4912 N0);
4913 return N0;
4914 }
Owen Anderson76706012011-04-05 21:48:57 +00004915
Nate Begeman7973f352011-02-11 20:53:29 +00004916 // v4i16 sdiv ... Convert to float.
4917 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
4918 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
4919 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
4920 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
4921 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004922 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00004923
4924 // Use reciprocal estimate and two refinement steps.
4925 // float4 recip = vrecpeq_f32(yf);
4926 // recip *= vrecpsq_f32(yf, recip);
4927 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004928 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004929 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00004930 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004931 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004932 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004933 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00004934 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004935 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004936 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004937 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4938 // Simply multiplying by the reciprocal estimate can leave us a few ulps
4939 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
4940 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004941 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00004942 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4943 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4944 N1 = DAG.getConstant(2, MVT::i32);
4945 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4946 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4947 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4948 // Convert back to integer and return.
4949 // return vmovn_u32(vcvt_s32_f32(result));
4950 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4951 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4952 return N0;
4953}
4954
Evan Cheng342e3162011-08-30 01:34:54 +00004955static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
4956 EVT VT = Op.getNode()->getValueType(0);
4957 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
4958
4959 unsigned Opc;
4960 bool ExtraOp = false;
4961 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00004962 default: llvm_unreachable("Invalid code");
Evan Cheng342e3162011-08-30 01:34:54 +00004963 case ISD::ADDC: Opc = ARMISD::ADDC; break;
4964 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
4965 case ISD::SUBC: Opc = ARMISD::SUBC; break;
4966 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
4967 }
4968
4969 if (!ExtraOp)
4970 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
4971 Op.getOperand(1));
4972 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
4973 Op.getOperand(1), Op.getOperand(2));
4974}
4975
Eli Friedman74bf18c2011-09-15 22:26:18 +00004976static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedman7cc15662011-09-15 22:18:49 +00004977 // Monotonic load/store is legal for all targets
4978 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
4979 return Op;
4980
4981 // Aquire/Release load/store is not legal for targets without a
4982 // dmb or equivalent available.
4983 return SDValue();
4984}
4985
4986
Eli Friedman2bdffe42011-08-31 00:31:29 +00004987static void
Eli Friedman4d3f3292011-08-31 17:52:22 +00004988ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
4989 SelectionDAG &DAG, unsigned NewOp) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00004990 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +00004991 assert (Node->getValueType(0) == MVT::i64 &&
4992 "Only know how to expand i64 atomics");
Eli Friedman2bdffe42011-08-31 00:31:29 +00004993
Eli Friedman4d3f3292011-08-31 17:52:22 +00004994 SmallVector<SDValue, 6> Ops;
4995 Ops.push_back(Node->getOperand(0)); // Chain
4996 Ops.push_back(Node->getOperand(1)); // Ptr
4997 // Low part of Val1
4998 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4999 Node->getOperand(2), DAG.getIntPtrConstant(0)));
5000 // High part of Val1
5001 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5002 Node->getOperand(2), DAG.getIntPtrConstant(1)));
Andrew Trick3af7a672011-09-20 03:06:13 +00005003 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) {
Eli Friedman4d3f3292011-08-31 17:52:22 +00005004 // High part of Val1
5005 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5006 Node->getOperand(3), DAG.getIntPtrConstant(0)));
5007 // High part of Val2
5008 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5009 Node->getOperand(3), DAG.getIntPtrConstant(1)));
5010 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005011 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
5012 SDValue Result =
Eli Friedman4d3f3292011-08-31 17:52:22 +00005013 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64,
Eli Friedman2bdffe42011-08-31 00:31:29 +00005014 cast<MemSDNode>(Node)->getMemOperand());
Eli Friedman4d3f3292011-08-31 17:52:22 +00005015 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) };
Eli Friedman2bdffe42011-08-31 00:31:29 +00005016 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
5017 Results.push_back(Result.getValue(2));
5018}
5019
Dan Gohmand858e902010-04-17 15:26:15 +00005020SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005021 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005022 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00005023 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00005024 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005025 case ISD::GlobalAddress:
5026 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
5027 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00005028 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00005029 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00005030 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
5031 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005032 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00005033 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00005034 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00005035 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00005036 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00005037 case ISD::SINT_TO_FP:
5038 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
5039 case ISD::FP_TO_SINT:
5040 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005041 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00005042 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00005043 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005044 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00005045 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00005046 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00005047 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
5048 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00005049 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005050 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00005051 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00005052 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00005053 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00005054 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00005055 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00005056 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Duncan Sands28b77e92011-09-06 19:07:46 +00005057 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00005058 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005059 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedman5c89cb82011-10-24 23:08:52 +00005060 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005061 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00005062 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00005063 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005064 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00005065 case ISD::SDIV: return LowerSDIV(Op, DAG);
5066 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00005067 case ISD::ADDC:
5068 case ISD::ADDE:
5069 case ISD::SUBC:
5070 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Eli Friedman7cc15662011-09-15 22:18:49 +00005071 case ISD::ATOMIC_LOAD:
Eli Friedman74bf18c2011-09-15 22:26:18 +00005072 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005073 }
Evan Chenga8e29892007-01-19 07:51:42 +00005074}
5075
Duncan Sands1607f052008-12-01 11:39:25 +00005076/// ReplaceNodeResults - Replace the results of node with an illegal result
5077/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00005078void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
5079 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005080 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00005081 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00005082 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00005083 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005084 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005085 case ISD::BITCAST:
5086 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005087 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005088 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00005089 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00005090 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005091 break;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005092 case ISD::ATOMIC_LOAD_ADD:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005093 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005094 return;
5095 case ISD::ATOMIC_LOAD_AND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005096 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005097 return;
5098 case ISD::ATOMIC_LOAD_NAND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005099 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005100 return;
5101 case ISD::ATOMIC_LOAD_OR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005102 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005103 return;
5104 case ISD::ATOMIC_LOAD_SUB:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005105 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005106 return;
5107 case ISD::ATOMIC_LOAD_XOR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005108 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005109 return;
5110 case ISD::ATOMIC_SWAP:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005111 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005112 return;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005113 case ISD::ATOMIC_CMP_SWAP:
5114 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG);
5115 return;
Duncan Sands1607f052008-12-01 11:39:25 +00005116 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00005117 if (Res.getNode())
5118 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00005119}
Chris Lattner27a6c732007-11-24 07:07:01 +00005120
Evan Chenga8e29892007-01-19 07:51:42 +00005121//===----------------------------------------------------------------------===//
5122// ARM Scheduler Hooks
5123//===----------------------------------------------------------------------===//
5124
5125MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005126ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
5127 MachineBasicBlock *BB,
5128 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005129 unsigned dest = MI->getOperand(0).getReg();
5130 unsigned ptr = MI->getOperand(1).getReg();
5131 unsigned oldval = MI->getOperand(2).getReg();
5132 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005133 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5134 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005135 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005136
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005137 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5138 unsigned scratch =
Cameron Zwarich141ec632011-05-18 02:29:50 +00005139 MRI.createVirtualRegister(isThumb2 ? ARM::rGPRRegisterClass
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005140 : ARM::GPRRegisterClass);
5141
5142 if (isThumb2) {
Cameron Zwarich141ec632011-05-18 02:29:50 +00005143 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5144 MRI.constrainRegClass(oldval, ARM::rGPRRegisterClass);
5145 MRI.constrainRegClass(newval, ARM::rGPRRegisterClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005146 }
5147
Jim Grosbach5278eb82009-12-11 01:42:04 +00005148 unsigned ldrOpc, strOpc;
5149 switch (Size) {
5150 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005151 case 1:
5152 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00005153 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005154 break;
5155 case 2:
5156 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5157 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5158 break;
5159 case 4:
5160 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5161 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5162 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00005163 }
5164
5165 MachineFunction *MF = BB->getParent();
5166 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5167 MachineFunction::iterator It = BB;
5168 ++It; // insert the new blocks after the current block
5169
5170 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5171 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5172 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5173 MF->insert(It, loop1MBB);
5174 MF->insert(It, loop2MBB);
5175 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005176
5177 // Transfer the remainder of BB and its successor edges to exitMBB.
5178 exitMBB->splice(exitMBB->begin(), BB,
5179 llvm::next(MachineBasicBlock::iterator(MI)),
5180 BB->end());
5181 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005182
5183 // thisMBB:
5184 // ...
5185 // fallthrough --> loop1MBB
5186 BB->addSuccessor(loop1MBB);
5187
5188 // loop1MBB:
5189 // ldrex dest, [ptr]
5190 // cmp dest, oldval
5191 // bne exitMBB
5192 BB = loop1MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005193 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5194 if (ldrOpc == ARM::t2LDREX)
5195 MIB.addImm(0);
5196 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005197 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005198 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005199 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5200 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005201 BB->addSuccessor(loop2MBB);
5202 BB->addSuccessor(exitMBB);
5203
5204 // loop2MBB:
5205 // strex scratch, newval, [ptr]
5206 // cmp scratch, #0
5207 // bne loop1MBB
5208 BB = loop2MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005209 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr);
5210 if (strOpc == ARM::t2STREX)
5211 MIB.addImm(0);
5212 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005213 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005214 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005215 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5216 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005217 BB->addSuccessor(loop1MBB);
5218 BB->addSuccessor(exitMBB);
5219
5220 // exitMBB:
5221 // ...
5222 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005223
Dan Gohman14152b42010-07-06 20:24:04 +00005224 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005225
Jim Grosbach5278eb82009-12-11 01:42:04 +00005226 return BB;
5227}
5228
5229MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005230ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5231 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005232 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5233 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5234
5235 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005236 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005237 MachineFunction::iterator It = BB;
5238 ++It;
5239
5240 unsigned dest = MI->getOperand(0).getReg();
5241 unsigned ptr = MI->getOperand(1).getReg();
5242 unsigned incr = MI->getOperand(2).getReg();
5243 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005244 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005245
5246 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5247 if (isThumb2) {
5248 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5249 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5250 }
5251
Jim Grosbachc3c23542009-12-14 04:22:04 +00005252 unsigned ldrOpc, strOpc;
5253 switch (Size) {
5254 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005255 case 1:
5256 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005257 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005258 break;
5259 case 2:
5260 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5261 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5262 break;
5263 case 4:
5264 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5265 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5266 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005267 }
5268
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005269 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5270 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5271 MF->insert(It, loopMBB);
5272 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005273
5274 // Transfer the remainder of BB and its successor edges to exitMBB.
5275 exitMBB->splice(exitMBB->begin(), BB,
5276 llvm::next(MachineBasicBlock::iterator(MI)),
5277 BB->end());
5278 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005279
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005280 TargetRegisterClass *TRC =
5281 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5282 unsigned scratch = MRI.createVirtualRegister(TRC);
5283 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005284
5285 // thisMBB:
5286 // ...
5287 // fallthrough --> loopMBB
5288 BB->addSuccessor(loopMBB);
5289
5290 // loopMBB:
5291 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005292 // <binop> scratch2, dest, incr
5293 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005294 // cmp scratch, #0
5295 // bne- loopMBB
5296 // fallthrough --> exitMBB
5297 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005298 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5299 if (ldrOpc == ARM::t2LDREX)
5300 MIB.addImm(0);
5301 AddDefaultPred(MIB);
Jim Grosbachc67b5562009-12-15 00:12:35 +00005302 if (BinOpcode) {
5303 // operand order needs to go the other way for NAND
5304 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5305 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5306 addReg(incr).addReg(dest)).addReg(0);
5307 else
5308 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5309 addReg(dest).addReg(incr)).addReg(0);
5310 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005311
Jim Grosbachb6aed502011-09-09 18:37:27 +00005312 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5313 if (strOpc == ARM::t2STREX)
5314 MIB.addImm(0);
5315 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005316 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005317 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005318 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5319 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005320
5321 BB->addSuccessor(loopMBB);
5322 BB->addSuccessor(exitMBB);
5323
5324 // exitMBB:
5325 // ...
5326 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005327
Dan Gohman14152b42010-07-06 20:24:04 +00005328 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005329
Jim Grosbachc3c23542009-12-14 04:22:04 +00005330 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005331}
5332
Jim Grosbachf7da8822011-04-26 19:44:18 +00005333MachineBasicBlock *
5334ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5335 MachineBasicBlock *BB,
5336 unsigned Size,
5337 bool signExtend,
5338 ARMCC::CondCodes Cond) const {
5339 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5340
5341 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5342 MachineFunction *MF = BB->getParent();
5343 MachineFunction::iterator It = BB;
5344 ++It;
5345
5346 unsigned dest = MI->getOperand(0).getReg();
5347 unsigned ptr = MI->getOperand(1).getReg();
5348 unsigned incr = MI->getOperand(2).getReg();
5349 unsigned oldval = dest;
5350 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005351 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005352
5353 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5354 if (isThumb2) {
5355 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5356 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5357 }
5358
Jim Grosbachf7da8822011-04-26 19:44:18 +00005359 unsigned ldrOpc, strOpc, extendOpc;
5360 switch (Size) {
5361 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5362 case 1:
5363 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5364 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005365 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005366 break;
5367 case 2:
5368 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5369 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005370 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005371 break;
5372 case 4:
5373 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5374 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5375 extendOpc = 0;
5376 break;
5377 }
5378
5379 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5380 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5381 MF->insert(It, loopMBB);
5382 MF->insert(It, exitMBB);
5383
5384 // Transfer the remainder of BB and its successor edges to exitMBB.
5385 exitMBB->splice(exitMBB->begin(), BB,
5386 llvm::next(MachineBasicBlock::iterator(MI)),
5387 BB->end());
5388 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5389
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005390 TargetRegisterClass *TRC =
5391 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5392 unsigned scratch = MRI.createVirtualRegister(TRC);
5393 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005394
5395 // thisMBB:
5396 // ...
5397 // fallthrough --> loopMBB
5398 BB->addSuccessor(loopMBB);
5399
5400 // loopMBB:
5401 // ldrex dest, ptr
5402 // (sign extend dest, if required)
5403 // cmp dest, incr
5404 // cmov.cond scratch2, dest, incr
5405 // strex scratch, scratch2, ptr
5406 // cmp scratch, #0
5407 // bne- loopMBB
5408 // fallthrough --> exitMBB
5409 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005410 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5411 if (ldrOpc == ARM::t2LDREX)
5412 MIB.addImm(0);
5413 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005414
5415 // Sign extend the value, if necessary.
5416 if (signExtend && extendOpc) {
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005417 oldval = MRI.createVirtualRegister(ARM::GPRRegisterClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005418 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5419 .addReg(dest)
5420 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005421 }
5422
5423 // Build compare and cmov instructions.
5424 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5425 .addReg(oldval).addReg(incr));
5426 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5427 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5428
Jim Grosbachb6aed502011-09-09 18:37:27 +00005429 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5430 if (strOpc == ARM::t2STREX)
5431 MIB.addImm(0);
5432 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005433 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5434 .addReg(scratch).addImm(0));
5435 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5436 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5437
5438 BB->addSuccessor(loopMBB);
5439 BB->addSuccessor(exitMBB);
5440
5441 // exitMBB:
5442 // ...
5443 BB = exitMBB;
5444
5445 MI->eraseFromParent(); // The instruction is gone now.
5446
5447 return BB;
5448}
5449
Eli Friedman2bdffe42011-08-31 00:31:29 +00005450MachineBasicBlock *
5451ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
5452 unsigned Op1, unsigned Op2,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005453 bool NeedsCarry, bool IsCmpxchg) const {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005454 // This also handles ATOMIC_SWAP, indicated by Op1==0.
5455 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5456
5457 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5458 MachineFunction *MF = BB->getParent();
5459 MachineFunction::iterator It = BB;
5460 ++It;
5461
5462 unsigned destlo = MI->getOperand(0).getReg();
5463 unsigned desthi = MI->getOperand(1).getReg();
5464 unsigned ptr = MI->getOperand(2).getReg();
5465 unsigned vallo = MI->getOperand(3).getReg();
5466 unsigned valhi = MI->getOperand(4).getReg();
5467 DebugLoc dl = MI->getDebugLoc();
5468 bool isThumb2 = Subtarget->isThumb2();
5469
5470 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5471 if (isThumb2) {
5472 MRI.constrainRegClass(destlo, ARM::rGPRRegisterClass);
5473 MRI.constrainRegClass(desthi, ARM::rGPRRegisterClass);
5474 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5475 }
5476
5477 unsigned ldrOpc = isThumb2 ? ARM::t2LDREXD : ARM::LDREXD;
5478 unsigned strOpc = isThumb2 ? ARM::t2STREXD : ARM::STREXD;
5479
5480 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman7df496d2011-09-01 22:27:41 +00005481 MachineBasicBlock *contBB = 0, *cont2BB = 0;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005482 if (IsCmpxchg) {
5483 contBB = MF->CreateMachineBasicBlock(LLVM_BB);
5484 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB);
5485 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005486 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5487 MF->insert(It, loopMBB);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005488 if (IsCmpxchg) {
5489 MF->insert(It, contBB);
5490 MF->insert(It, cont2BB);
5491 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005492 MF->insert(It, exitMBB);
5493
5494 // Transfer the remainder of BB and its successor edges to exitMBB.
5495 exitMBB->splice(exitMBB->begin(), BB,
5496 llvm::next(MachineBasicBlock::iterator(MI)),
5497 BB->end());
5498 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5499
5500 TargetRegisterClass *TRC =
5501 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5502 unsigned storesuccess = MRI.createVirtualRegister(TRC);
5503
5504 // thisMBB:
5505 // ...
5506 // fallthrough --> loopMBB
5507 BB->addSuccessor(loopMBB);
5508
5509 // loopMBB:
5510 // ldrexd r2, r3, ptr
5511 // <binopa> r0, r2, incr
5512 // <binopb> r1, r3, incr
5513 // strexd storesuccess, r0, r1, ptr
5514 // cmp storesuccess, #0
5515 // bne- loopMBB
5516 // fallthrough --> exitMBB
5517 //
5518 // Note that the registers are explicitly specified because there is not any
5519 // way to force the register allocator to allocate a register pair.
5520 //
Andrew Trick3af7a672011-09-20 03:06:13 +00005521 // FIXME: The hardcoded registers are not necessary for Thumb2, but we
Eli Friedman2bdffe42011-08-31 00:31:29 +00005522 // need to properly enforce the restriction that the two output registers
5523 // for ldrexd must be different.
5524 BB = loopMBB;
5525 // Load
5526 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc))
5527 .addReg(ARM::R2, RegState::Define)
5528 .addReg(ARM::R3, RegState::Define).addReg(ptr));
5529 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
5530 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo).addReg(ARM::R2);
5531 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi).addReg(ARM::R3);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005532
5533 if (IsCmpxchg) {
5534 // Add early exit
5535 for (unsigned i = 0; i < 2; i++) {
5536 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr :
5537 ARM::CMPrr))
5538 .addReg(i == 0 ? destlo : desthi)
5539 .addReg(i == 0 ? vallo : valhi));
5540 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5541 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5542 BB->addSuccessor(exitMBB);
5543 BB->addSuccessor(i == 0 ? contBB : cont2BB);
5544 BB = (i == 0 ? contBB : cont2BB);
5545 }
5546
5547 // Copy to physregs for strexd
5548 unsigned setlo = MI->getOperand(5).getReg();
5549 unsigned sethi = MI->getOperand(6).getReg();
5550 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(setlo);
5551 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(sethi);
5552 } else if (Op1) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005553 // Perform binary operation
5554 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), ARM::R0)
5555 .addReg(destlo).addReg(vallo))
5556 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
5557 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), ARM::R1)
5558 .addReg(desthi).addReg(valhi)).addReg(0);
5559 } else {
5560 // Copy to physregs for strexd
5561 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(vallo);
5562 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(valhi);
5563 }
5564
5565 // Store
5566 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), storesuccess)
5567 .addReg(ARM::R0).addReg(ARM::R1).addReg(ptr));
5568 // Cmp+jump
5569 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5570 .addReg(storesuccess).addImm(0));
5571 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5572 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5573
5574 BB->addSuccessor(loopMBB);
5575 BB->addSuccessor(exitMBB);
5576
5577 // exitMBB:
5578 // ...
5579 BB = exitMBB;
5580
5581 MI->eraseFromParent(); // The instruction is gone now.
5582
5583 return BB;
5584}
5585
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005586/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
5587/// registers the function context.
5588void ARMTargetLowering::
5589SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
5590 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005591 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5592 DebugLoc dl = MI->getDebugLoc();
5593 MachineFunction *MF = MBB->getParent();
5594 MachineRegisterInfo *MRI = &MF->getRegInfo();
5595 MachineConstantPool *MCP = MF->getConstantPool();
5596 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5597 const Function *F = MF->getFunction();
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005598
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005599 bool isThumb = Subtarget->isThumb();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005600 bool isThumb2 = Subtarget->isThumb2();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005601
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005602 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005603 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005604 ARMConstantPoolValue *CPV =
5605 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
5606 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
5607
5608 const TargetRegisterClass *TRC =
5609 isThumb ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5610
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005611 // Grab constant pool and fixed stack memory operands.
5612 MachineMemOperand *CPMMO =
5613 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
5614 MachineMemOperand::MOLoad, 4, 4);
5615
5616 MachineMemOperand *FIMMOSt =
5617 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
5618 MachineMemOperand::MOStore, 4, 4);
5619
5620 // Load the address of the dispatch MBB into the jump buffer.
5621 if (isThumb2) {
5622 // Incoming value: jbuf
5623 // ldr.n r5, LCPI1_1
5624 // orr r5, r5, #1
5625 // add r5, pc
5626 // str r5, [$jbuf, #+4] ; &jbuf[1]
5627 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5628 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
5629 .addConstantPoolIndex(CPI)
5630 .addMemOperand(CPMMO));
5631 // Set the low bit because of thumb mode.
5632 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5633 AddDefaultCC(
5634 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
5635 .addReg(NewVReg1, RegState::Kill)
5636 .addImm(0x01)));
5637 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5638 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
5639 .addReg(NewVReg2, RegState::Kill)
5640 .addImm(PCLabelId);
5641 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
5642 .addReg(NewVReg3, RegState::Kill)
5643 .addFrameIndex(FI)
5644 .addImm(36) // &jbuf[1] :: pc
5645 .addMemOperand(FIMMOSt));
5646 } else if (isThumb) {
5647 // Incoming value: jbuf
5648 // ldr.n r1, LCPI1_4
5649 // add r1, pc
5650 // mov r2, #1
5651 // orrs r1, r2
5652 // add r2, $jbuf, #+4 ; &jbuf[1]
5653 // str r1, [r2]
5654 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5655 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
5656 .addConstantPoolIndex(CPI)
5657 .addMemOperand(CPMMO));
5658 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5659 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
5660 .addReg(NewVReg1, RegState::Kill)
5661 .addImm(PCLabelId);
5662 // Set the low bit because of thumb mode.
5663 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5664 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
5665 .addReg(ARM::CPSR, RegState::Define)
5666 .addImm(1));
5667 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5668 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
5669 .addReg(ARM::CPSR, RegState::Define)
5670 .addReg(NewVReg2, RegState::Kill)
5671 .addReg(NewVReg3, RegState::Kill));
5672 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5673 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
5674 .addFrameIndex(FI)
5675 .addImm(36)); // &jbuf[1] :: pc
5676 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
5677 .addReg(NewVReg4, RegState::Kill)
5678 .addReg(NewVReg5, RegState::Kill)
5679 .addImm(0)
5680 .addMemOperand(FIMMOSt));
5681 } else {
5682 // Incoming value: jbuf
5683 // ldr r1, LCPI1_1
5684 // add r1, pc, r1
5685 // str r1, [$jbuf, #+4] ; &jbuf[1]
5686 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5687 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
5688 .addConstantPoolIndex(CPI)
5689 .addImm(0)
5690 .addMemOperand(CPMMO));
5691 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5692 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
5693 .addReg(NewVReg1, RegState::Kill)
5694 .addImm(PCLabelId));
5695 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
5696 .addReg(NewVReg2, RegState::Kill)
5697 .addFrameIndex(FI)
5698 .addImm(36) // &jbuf[1] :: pc
5699 .addMemOperand(FIMMOSt));
5700 }
5701}
5702
5703MachineBasicBlock *ARMTargetLowering::
5704EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
5705 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5706 DebugLoc dl = MI->getDebugLoc();
5707 MachineFunction *MF = MBB->getParent();
5708 MachineRegisterInfo *MRI = &MF->getRegInfo();
5709 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5710 MachineFrameInfo *MFI = MF->getFrameInfo();
5711 int FI = MFI->getFunctionContextIndex();
5712
5713 const TargetRegisterClass *TRC =
5714 Subtarget->isThumb() ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5715
Bill Wendling04f15b42011-10-06 21:29:56 +00005716 // Get a mapping of the call site numbers to all of the landing pads they're
5717 // associated with.
Bill Wendling2a850152011-10-05 00:02:33 +00005718 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
5719 unsigned MaxCSNum = 0;
5720 MachineModuleInfo &MMI = MF->getMMI();
5721 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E; ++BB) {
5722 if (!BB->isLandingPad()) continue;
5723
5724 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
5725 // pad.
5726 for (MachineBasicBlock::iterator
5727 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
5728 if (!II->isEHLabel()) continue;
5729
5730 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendling5cbef192011-10-05 23:28:57 +00005731 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling2a850152011-10-05 00:02:33 +00005732
Bill Wendling5cbef192011-10-05 23:28:57 +00005733 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
5734 for (SmallVectorImpl<unsigned>::iterator
5735 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
5736 CSI != CSE; ++CSI) {
5737 CallSiteNumToLPad[*CSI].push_back(BB);
5738 MaxCSNum = std::max(MaxCSNum, *CSI);
5739 }
Bill Wendling2a850152011-10-05 00:02:33 +00005740 break;
5741 }
5742 }
5743
5744 // Get an ordered list of the machine basic blocks for the jump table.
5745 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling2acf6382011-10-07 23:18:02 +00005746 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling2a850152011-10-05 00:02:33 +00005747 LPadList.reserve(CallSiteNumToLPad.size());
5748 for (unsigned I = 1; I <= MaxCSNum; ++I) {
5749 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
5750 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00005751 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling2a850152011-10-05 00:02:33 +00005752 LPadList.push_back(*II);
Bill Wendling2acf6382011-10-07 23:18:02 +00005753 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
5754 }
Bill Wendling2a850152011-10-05 00:02:33 +00005755 }
5756
Bill Wendling5cbef192011-10-05 23:28:57 +00005757 assert(!LPadList.empty() &&
5758 "No landing pad destinations for the dispatch jump table!");
5759
Bill Wendling04f15b42011-10-06 21:29:56 +00005760 // Create the jump table and associated information.
Bill Wendling2a850152011-10-05 00:02:33 +00005761 MachineJumpTableInfo *JTI =
5762 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
5763 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
5764 unsigned UId = AFI->createJumpTableUId();
5765
Bill Wendling04f15b42011-10-06 21:29:56 +00005766 // Create the MBBs for the dispatch code.
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005767
5768 // Shove the dispatch's address into the return slot in the function context.
5769 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
5770 DispatchBB->setIsLandingPad();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005771
Bill Wendlingbb734682011-10-05 00:39:32 +00005772 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Bill Wendling083a8eb2011-10-06 23:37:36 +00005773 BuildMI(TrapBB, dl, TII->get(Subtarget->isThumb() ? ARM::tTRAP : ARM::TRAP));
Bill Wendlingbb734682011-10-05 00:39:32 +00005774 DispatchBB->addSuccessor(TrapBB);
5775
5776 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
5777 DispatchBB->addSuccessor(DispContBB);
Bill Wendling2a850152011-10-05 00:02:33 +00005778
Bill Wendlinga48ed4f2011-10-17 21:32:56 +00005779 // Insert and MBBs.
Bill Wendling930193c2011-10-06 00:53:33 +00005780 MF->insert(MF->end(), DispatchBB);
5781 MF->insert(MF->end(), DispContBB);
5782 MF->insert(MF->end(), TrapBB);
Bill Wendling930193c2011-10-06 00:53:33 +00005783
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005784 // Insert code into the entry block that creates and registers the function
5785 // context.
5786 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
5787
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005788 MachineMemOperand *FIMMOLd =
Bill Wendling04f15b42011-10-06 21:29:56 +00005789 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendling083a8eb2011-10-06 23:37:36 +00005790 MachineMemOperand::MOLoad |
5791 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling930193c2011-10-06 00:53:33 +00005792
Bob Wilsonf4aea8f2011-12-22 23:39:48 +00005793 if (AFI->isThumb1OnlyFunction())
5794 BuildMI(DispatchBB, dl, TII->get(ARM::tInt_eh_sjlj_dispatchsetup));
5795 else if (!Subtarget->hasVFP2())
5796 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup_nofp));
5797 else
5798 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
Bob Wilsoneaab6ef2011-11-16 07:11:57 +00005799
Bill Wendling952cb502011-10-18 22:49:07 +00005800 unsigned NumLPads = LPadList.size();
Bill Wendling95ce2e92011-10-06 22:53:00 +00005801 if (Subtarget->isThumb2()) {
5802 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5803 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
5804 .addFrameIndex(FI)
5805 .addImm(4)
5806 .addMemOperand(FIMMOLd));
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005807
Bill Wendling952cb502011-10-18 22:49:07 +00005808 if (NumLPads < 256) {
5809 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
5810 .addReg(NewVReg1)
5811 .addImm(LPadList.size()));
5812 } else {
5813 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5814 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00005815 .addImm(NumLPads & 0xFFFF));
5816
5817 unsigned VReg2 = VReg1;
5818 if ((NumLPads & 0xFFFF0000) != 0) {
5819 VReg2 = MRI->createVirtualRegister(TRC);
5820 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
5821 .addReg(VReg1)
5822 .addImm(NumLPads >> 16));
5823 }
5824
Bill Wendling952cb502011-10-18 22:49:07 +00005825 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
5826 .addReg(NewVReg1)
5827 .addReg(VReg2));
5828 }
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005829
Bill Wendling95ce2e92011-10-06 22:53:00 +00005830 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
5831 .addMBB(TrapBB)
5832 .addImm(ARMCC::HI)
5833 .addReg(ARM::CPSR);
Bill Wendlingbb734682011-10-05 00:39:32 +00005834
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005835 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5836 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005837 .addJumpTableIndex(MJTI)
5838 .addImm(UId));
Bill Wendling2a850152011-10-05 00:02:33 +00005839
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005840 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005841 AddDefaultCC(
5842 AddDefaultPred(
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005843 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
5844 .addReg(NewVReg3, RegState::Kill)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005845 .addReg(NewVReg1)
5846 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
5847
5848 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005849 .addReg(NewVReg4, RegState::Kill)
Bill Wendling2a850152011-10-05 00:02:33 +00005850 .addReg(NewVReg1)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005851 .addJumpTableIndex(MJTI)
5852 .addImm(UId);
5853 } else if (Subtarget->isThumb()) {
Bill Wendling083a8eb2011-10-06 23:37:36 +00005854 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5855 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
5856 .addFrameIndex(FI)
5857 .addImm(1)
5858 .addMemOperand(FIMMOLd));
Bill Wendlingf1083d42011-10-07 22:08:37 +00005859
Bill Wendlinga5871dc2011-10-18 23:11:05 +00005860 if (NumLPads < 256) {
5861 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
5862 .addReg(NewVReg1)
5863 .addImm(NumLPads));
5864 } else {
5865 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling922ad782011-10-19 09:24:02 +00005866 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
5867 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
5868
5869 // MachineConstantPool wants an explicit alignment.
5870 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
5871 if (Align == 0)
5872 Align = getTargetData()->getTypeAllocSize(C->getType());
5873 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendlinga5871dc2011-10-18 23:11:05 +00005874
5875 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5876 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
5877 .addReg(VReg1, RegState::Define)
5878 .addConstantPoolIndex(Idx));
5879 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
5880 .addReg(NewVReg1)
5881 .addReg(VReg1));
5882 }
5883
Bill Wendling083a8eb2011-10-06 23:37:36 +00005884 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
5885 .addMBB(TrapBB)
5886 .addImm(ARMCC::HI)
5887 .addReg(ARM::CPSR);
5888
5889 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5890 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
5891 .addReg(ARM::CPSR, RegState::Define)
5892 .addReg(NewVReg1)
5893 .addImm(2));
5894
5895 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling217f0e92011-10-06 23:41:14 +00005896 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendling083a8eb2011-10-06 23:37:36 +00005897 .addJumpTableIndex(MJTI)
5898 .addImm(UId));
5899
5900 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5901 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
5902 .addReg(ARM::CPSR, RegState::Define)
5903 .addReg(NewVReg2, RegState::Kill)
5904 .addReg(NewVReg3));
5905
5906 MachineMemOperand *JTMMOLd =
5907 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
5908 MachineMemOperand::MOLoad, 4, 4);
5909
5910 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5911 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
5912 .addReg(NewVReg4, RegState::Kill)
5913 .addImm(0)
5914 .addMemOperand(JTMMOLd));
5915
5916 unsigned NewVReg6 = MRI->createVirtualRegister(TRC);
5917 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
5918 .addReg(ARM::CPSR, RegState::Define)
5919 .addReg(NewVReg5, RegState::Kill)
5920 .addReg(NewVReg3));
5921
5922 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
5923 .addReg(NewVReg6, RegState::Kill)
5924 .addJumpTableIndex(MJTI)
5925 .addImm(UId);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005926 } else {
5927 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5928 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
5929 .addFrameIndex(FI)
5930 .addImm(4)
5931 .addMemOperand(FIMMOLd));
Bill Wendling564392b2011-10-18 22:11:18 +00005932
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005933 if (NumLPads < 256) {
5934 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
5935 .addReg(NewVReg1)
5936 .addImm(NumLPads));
Bill Wendling922ad782011-10-19 09:24:02 +00005937 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005938 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5939 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00005940 .addImm(NumLPads & 0xFFFF));
5941
5942 unsigned VReg2 = VReg1;
5943 if ((NumLPads & 0xFFFF0000) != 0) {
5944 VReg2 = MRI->createVirtualRegister(TRC);
5945 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
5946 .addReg(VReg1)
5947 .addImm(NumLPads >> 16));
5948 }
5949
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005950 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
5951 .addReg(NewVReg1)
5952 .addReg(VReg2));
Bill Wendling922ad782011-10-19 09:24:02 +00005953 } else {
5954 MachineConstantPool *ConstantPool = MF->getConstantPool();
5955 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
5956 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
5957
5958 // MachineConstantPool wants an explicit alignment.
5959 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
5960 if (Align == 0)
5961 Align = getTargetData()->getTypeAllocSize(C->getType());
5962 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
5963
5964 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5965 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
5966 .addReg(VReg1, RegState::Define)
Bill Wendling767f8be2011-10-20 20:37:11 +00005967 .addConstantPoolIndex(Idx)
5968 .addImm(0));
Bill Wendling922ad782011-10-19 09:24:02 +00005969 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
5970 .addReg(NewVReg1)
5971 .addReg(VReg1, RegState::Kill));
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005972 }
5973
Bill Wendling95ce2e92011-10-06 22:53:00 +00005974 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
5975 .addMBB(TrapBB)
5976 .addImm(ARMCC::HI)
5977 .addReg(ARM::CPSR);
Bill Wendling2a850152011-10-05 00:02:33 +00005978
Bill Wendling564392b2011-10-18 22:11:18 +00005979 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005980 AddDefaultCC(
Bill Wendling564392b2011-10-18 22:11:18 +00005981 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005982 .addReg(NewVReg1)
5983 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling564392b2011-10-18 22:11:18 +00005984 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5985 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005986 .addJumpTableIndex(MJTI)
5987 .addImm(UId));
5988
5989 MachineMemOperand *JTMMOLd =
5990 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
5991 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling564392b2011-10-18 22:11:18 +00005992 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005993 AddDefaultPred(
Bill Wendling564392b2011-10-18 22:11:18 +00005994 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
5995 .addReg(NewVReg3, RegState::Kill)
5996 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005997 .addImm(0)
5998 .addMemOperand(JTMMOLd));
5999
6000 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
Bill Wendling564392b2011-10-18 22:11:18 +00006001 .addReg(NewVReg5, RegState::Kill)
6002 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006003 .addJumpTableIndex(MJTI)
6004 .addImm(UId);
6005 }
Bill Wendling2a850152011-10-05 00:02:33 +00006006
Bill Wendlingbb734682011-10-05 00:39:32 +00006007 // Add the jump table entries as successors to the MBB.
Bill Wendling2acf6382011-10-07 23:18:02 +00006008 MachineBasicBlock *PrevMBB = 0;
Bill Wendlingbb734682011-10-05 00:39:32 +00006009 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006010 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6011 MachineBasicBlock *CurMBB = *I;
6012 if (PrevMBB != CurMBB)
6013 DispContBB->addSuccessor(CurMBB);
6014 PrevMBB = CurMBB;
6015 }
6016
Bill Wendling24bb9252011-10-17 05:25:09 +00006017 // N.B. the order the invoke BBs are processed in doesn't matter here.
Bill Wendling969c9ef2011-10-14 23:34:37 +00006018 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6019 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
6020 const unsigned *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006021 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling2acf6382011-10-07 23:18:02 +00006022 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6023 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6024 MachineBasicBlock *BB = *I;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006025
6026 // Remove the landing pad successor from the invoke block and replace it
6027 // with the new dispatch block.
Bill Wendlingde39d862011-10-26 07:16:18 +00006028 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6029 BB->succ_end());
6030 while (!Successors.empty()) {
6031 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling2acf6382011-10-07 23:18:02 +00006032 if (SMBB->isLandingPad()) {
6033 BB->removeSuccessor(SMBB);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006034 MBBLPads.push_back(SMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006035 }
6036 }
6037
6038 BB->addSuccessor(DispatchBB);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006039
6040 // Find the invoke call and mark all of the callee-saved registers as
6041 // 'implicit defined' so that they're spilled. This prevents code from
6042 // moving instructions to before the EH block, where they will never be
6043 // executed.
6044 for (MachineBasicBlock::reverse_iterator
6045 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006046 if (!II->isCall()) continue;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006047
6048 DenseMap<unsigned, bool> DefRegs;
6049 for (MachineInstr::mop_iterator
6050 OI = II->operands_begin(), OE = II->operands_end();
6051 OI != OE; ++OI) {
6052 if (!OI->isReg()) continue;
6053 DefRegs[OI->getReg()] = true;
6054 }
6055
6056 MachineInstrBuilder MIB(&*II);
6057
Bill Wendling5d798592011-10-14 23:55:44 +00006058 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006059 unsigned Reg = SavedRegs[i];
6060 if (Subtarget->isThumb2() &&
6061 !ARM::tGPRRegisterClass->contains(Reg) &&
6062 !ARM::hGPRRegisterClass->contains(Reg))
6063 continue;
6064 else if (Subtarget->isThumb1Only() &&
6065 !ARM::tGPRRegisterClass->contains(Reg))
6066 continue;
6067 else if (!Subtarget->isThumb() &&
6068 !ARM::GPRRegisterClass->contains(Reg))
6069 continue;
6070 if (!DefRegs[Reg])
6071 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling5d798592011-10-14 23:55:44 +00006072 }
Bill Wendling969c9ef2011-10-14 23:34:37 +00006073
6074 break;
6075 }
Bill Wendling2acf6382011-10-07 23:18:02 +00006076 }
Bill Wendlingbb734682011-10-05 00:39:32 +00006077
Bill Wendlingf7b02072011-10-18 18:30:49 +00006078 // Mark all former landing pads as non-landing pads. The dispatch is the only
6079 // landing pad now.
6080 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6081 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6082 (*I)->setIsLandingPad(false);
6083
Bill Wendlingbb734682011-10-05 00:39:32 +00006084 // The instruction is gone now.
6085 MI->eraseFromParent();
6086
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006087 return MBB;
6088}
6089
Evan Cheng218977b2010-07-13 19:27:42 +00006090static
6091MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6092 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6093 E = MBB->succ_end(); I != E; ++I)
6094 if (*I != Succ)
6095 return *I;
6096 llvm_unreachable("Expecting a BB with two successors!");
6097}
6098
Jim Grosbache801dc42009-12-12 01:40:06 +00006099MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006100ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00006101 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006102 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00006103 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006104 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00006105 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00006106 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00006107 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00006108 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00006109 }
Jim Grosbachee2c2a42011-09-16 21:55:56 +00006110 // The Thumb2 pre-indexed stores have the same MI operands, they just
6111 // define them differently in the .td files from the isel patterns, so
6112 // they need pseudos.
6113 case ARM::t2STR_preidx:
6114 MI->setDesc(TII->get(ARM::t2STR_PRE));
6115 return BB;
6116 case ARM::t2STRB_preidx:
6117 MI->setDesc(TII->get(ARM::t2STRB_PRE));
6118 return BB;
6119 case ARM::t2STRH_preidx:
6120 MI->setDesc(TII->get(ARM::t2STRH_PRE));
6121 return BB;
6122
Jim Grosbach19dec202011-08-05 20:35:44 +00006123 case ARM::STRi_preidx:
6124 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00006125 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00006126 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
6127 // Decode the offset.
6128 unsigned Offset = MI->getOperand(4).getImm();
6129 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
6130 Offset = ARM_AM::getAM2Offset(Offset);
6131 if (isSub)
6132 Offset = -Offset;
6133
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006134 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00006135 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00006136 .addOperand(MI->getOperand(0)) // Rn_wb
6137 .addOperand(MI->getOperand(1)) // Rt
6138 .addOperand(MI->getOperand(2)) // Rn
6139 .addImm(Offset) // offset (skip GPR==zero_reg)
6140 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006141 .addOperand(MI->getOperand(6))
6142 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00006143 MI->eraseFromParent();
6144 return BB;
6145 }
6146 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00006147 case ARM::STRBr_preidx:
6148 case ARM::STRH_preidx: {
6149 unsigned NewOpc;
6150 switch (MI->getOpcode()) {
6151 default: llvm_unreachable("unexpected opcode!");
6152 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
6153 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
6154 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
6155 }
Jim Grosbach19dec202011-08-05 20:35:44 +00006156 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
6157 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
6158 MIB.addOperand(MI->getOperand(i));
6159 MI->eraseFromParent();
6160 return BB;
6161 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006162 case ARM::ATOMIC_LOAD_ADD_I8:
6163 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6164 case ARM::ATOMIC_LOAD_ADD_I16:
6165 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6166 case ARM::ATOMIC_LOAD_ADD_I32:
6167 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006168
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006169 case ARM::ATOMIC_LOAD_AND_I8:
6170 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6171 case ARM::ATOMIC_LOAD_AND_I16:
6172 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6173 case ARM::ATOMIC_LOAD_AND_I32:
6174 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006175
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006176 case ARM::ATOMIC_LOAD_OR_I8:
6177 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6178 case ARM::ATOMIC_LOAD_OR_I16:
6179 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6180 case ARM::ATOMIC_LOAD_OR_I32:
6181 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006182
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006183 case ARM::ATOMIC_LOAD_XOR_I8:
6184 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6185 case ARM::ATOMIC_LOAD_XOR_I16:
6186 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6187 case ARM::ATOMIC_LOAD_XOR_I32:
6188 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006189
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006190 case ARM::ATOMIC_LOAD_NAND_I8:
6191 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6192 case ARM::ATOMIC_LOAD_NAND_I16:
6193 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6194 case ARM::ATOMIC_LOAD_NAND_I32:
6195 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006196
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006197 case ARM::ATOMIC_LOAD_SUB_I8:
6198 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6199 case ARM::ATOMIC_LOAD_SUB_I16:
6200 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6201 case ARM::ATOMIC_LOAD_SUB_I32:
6202 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006203
Jim Grosbachf7da8822011-04-26 19:44:18 +00006204 case ARM::ATOMIC_LOAD_MIN_I8:
6205 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
6206 case ARM::ATOMIC_LOAD_MIN_I16:
6207 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
6208 case ARM::ATOMIC_LOAD_MIN_I32:
6209 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
6210
6211 case ARM::ATOMIC_LOAD_MAX_I8:
6212 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
6213 case ARM::ATOMIC_LOAD_MAX_I16:
6214 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
6215 case ARM::ATOMIC_LOAD_MAX_I32:
6216 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
6217
6218 case ARM::ATOMIC_LOAD_UMIN_I8:
6219 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
6220 case ARM::ATOMIC_LOAD_UMIN_I16:
6221 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
6222 case ARM::ATOMIC_LOAD_UMIN_I32:
6223 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
6224
6225 case ARM::ATOMIC_LOAD_UMAX_I8:
6226 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
6227 case ARM::ATOMIC_LOAD_UMAX_I16:
6228 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
6229 case ARM::ATOMIC_LOAD_UMAX_I32:
6230 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
6231
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006232 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
6233 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
6234 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00006235
6236 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
6237 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
6238 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006239
Eli Friedman2bdffe42011-08-31 00:31:29 +00006240
6241 case ARM::ATOMADD6432:
6242 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006243 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr,
6244 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006245 case ARM::ATOMSUB6432:
6246 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006247 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6248 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006249 case ARM::ATOMOR6432:
6250 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006251 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006252 case ARM::ATOMXOR6432:
6253 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006254 isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006255 case ARM::ATOMAND6432:
6256 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006257 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006258 case ARM::ATOMSWAP6432:
6259 return EmitAtomicBinary64(MI, BB, 0, 0, false);
Eli Friedman4d3f3292011-08-31 17:52:22 +00006260 case ARM::ATOMCMPXCHG6432:
6261 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
6262 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6263 /*NeedsCarry*/ false, /*IsCmpxchg*/true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006264
Evan Cheng007ea272009-08-12 05:17:19 +00006265 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00006266 // To "insert" a SELECT_CC instruction, we actually have to insert the
6267 // diamond control-flow pattern. The incoming instruction knows the
6268 // destination vreg to set, the condition code register to branch on, the
6269 // true/false values to select between, and a branch opcode to use.
6270 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006271 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00006272 ++It;
6273
6274 // thisMBB:
6275 // ...
6276 // TrueVal = ...
6277 // cmpTY ccX, r1, r2
6278 // bCC copy1MBB
6279 // fallthrough --> copy0MBB
6280 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006281 MachineFunction *F = BB->getParent();
6282 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6283 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00006284 F->insert(It, copy0MBB);
6285 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006286
6287 // Transfer the remainder of BB and its successor edges to sinkMBB.
6288 sinkMBB->splice(sinkMBB->begin(), BB,
6289 llvm::next(MachineBasicBlock::iterator(MI)),
6290 BB->end());
6291 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6292
Dan Gohman258c58c2010-07-06 15:49:48 +00006293 BB->addSuccessor(copy0MBB);
6294 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00006295
Dan Gohman14152b42010-07-06 20:24:04 +00006296 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
6297 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
6298
Evan Chenga8e29892007-01-19 07:51:42 +00006299 // copy0MBB:
6300 // %FalseValue = ...
6301 // # fallthrough to sinkMBB
6302 BB = copy0MBB;
6303
6304 // Update machine-CFG edges
6305 BB->addSuccessor(sinkMBB);
6306
6307 // sinkMBB:
6308 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6309 // ...
6310 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00006311 BuildMI(*BB, BB->begin(), dl,
6312 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00006313 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6314 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6315
Dan Gohman14152b42010-07-06 20:24:04 +00006316 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00006317 return BB;
6318 }
Evan Cheng86198642009-08-07 00:34:42 +00006319
Evan Cheng218977b2010-07-13 19:27:42 +00006320 case ARM::BCCi64:
6321 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00006322 // If there is an unconditional branch to the other successor, remove it.
6323 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00006324
Evan Cheng218977b2010-07-13 19:27:42 +00006325 // Compare both parts that make up the double comparison separately for
6326 // equality.
6327 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
6328
6329 unsigned LHS1 = MI->getOperand(1).getReg();
6330 unsigned LHS2 = MI->getOperand(2).getReg();
6331 if (RHSisZero) {
6332 AddDefaultPred(BuildMI(BB, dl,
6333 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6334 .addReg(LHS1).addImm(0));
6335 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6336 .addReg(LHS2).addImm(0)
6337 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6338 } else {
6339 unsigned RHS1 = MI->getOperand(3).getReg();
6340 unsigned RHS2 = MI->getOperand(4).getReg();
6341 AddDefaultPred(BuildMI(BB, dl,
6342 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6343 .addReg(LHS1).addReg(RHS1));
6344 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6345 .addReg(LHS2).addReg(RHS2)
6346 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6347 }
6348
6349 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
6350 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
6351 if (MI->getOperand(0).getImm() == ARMCC::NE)
6352 std::swap(destMBB, exitMBB);
6353
6354 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6355 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson51f6a7a2011-09-09 21:48:23 +00006356 if (isThumb2)
6357 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
6358 else
6359 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng218977b2010-07-13 19:27:42 +00006360
6361 MI->eraseFromParent(); // The pseudo instruction is gone now.
6362 return BB;
6363 }
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006364
Bill Wendling5bc85282011-10-17 20:37:20 +00006365 case ARM::Int_eh_sjlj_setjmp:
6366 case ARM::Int_eh_sjlj_setjmp_nofp:
6367 case ARM::tInt_eh_sjlj_setjmp:
6368 case ARM::t2Int_eh_sjlj_setjmp:
6369 case ARM::t2Int_eh_sjlj_setjmp_nofp:
6370 EmitSjLjDispatchBlock(MI, BB);
6371 return BB;
6372
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006373 case ARM::ABS:
6374 case ARM::t2ABS: {
6375 // To insert an ABS instruction, we have to insert the
6376 // diamond control-flow pattern. The incoming instruction knows the
6377 // source vreg to test against 0, the destination vreg to set,
6378 // the condition code register to branch on, the
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006379 // true/false values to select between, and a branch opcode to use.
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006380 // It transforms
6381 // V1 = ABS V0
6382 // into
6383 // V2 = MOVS V0
6384 // BCC (branch to SinkBB if V0 >= 0)
6385 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006386 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006387 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6388 MachineFunction::iterator BBI = BB;
6389 ++BBI;
6390 MachineFunction *Fn = BB->getParent();
6391 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6392 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6393 Fn->insert(BBI, RSBBB);
6394 Fn->insert(BBI, SinkBB);
6395
6396 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
6397 unsigned int ABSDstReg = MI->getOperand(0).getReg();
6398 bool isThumb2 = Subtarget->isThumb2();
6399 MachineRegisterInfo &MRI = Fn->getRegInfo();
6400 // In Thumb mode S must not be specified if source register is the SP or
6401 // PC and if destination register is the SP, so restrict register class
6402 unsigned NewMovDstReg = MRI.createVirtualRegister(
6403 isThumb2 ? ARM::rGPRRegisterClass : ARM::GPRRegisterClass);
6404 unsigned NewRsbDstReg = MRI.createVirtualRegister(
6405 isThumb2 ? ARM::rGPRRegisterClass : ARM::GPRRegisterClass);
6406
6407 // Transfer the remainder of BB and its successor edges to sinkMBB.
6408 SinkBB->splice(SinkBB->begin(), BB,
6409 llvm::next(MachineBasicBlock::iterator(MI)),
6410 BB->end());
6411 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
6412
6413 BB->addSuccessor(RSBBB);
6414 BB->addSuccessor(SinkBB);
6415
6416 // fall through to SinkMBB
6417 RSBBB->addSuccessor(SinkBB);
6418
6419 // insert a movs at the end of BB
6420 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVr : ARM::MOVr),
6421 NewMovDstReg)
6422 .addReg(ABSSrcReg, RegState::Kill)
6423 .addImm((unsigned)ARMCC::AL).addReg(0)
6424 .addReg(ARM::CPSR, RegState::Define);
6425
6426 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006427 BuildMI(BB, dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006428 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
6429 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
6430
6431 // insert rsbri in RSBBB
6432 // Note: BCC and rsbri will be converted into predicated rsbmi
6433 // by if-conversion pass
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006434 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006435 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
6436 .addReg(NewMovDstReg, RegState::Kill)
6437 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
6438
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006439 // insert PHI in SinkBB,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006440 // reuse ABSDstReg to not change uses of ABS instruction
6441 BuildMI(*SinkBB, SinkBB->begin(), dl,
6442 TII->get(ARM::PHI), ABSDstReg)
6443 .addReg(NewRsbDstReg).addMBB(RSBBB)
6444 .addReg(NewMovDstReg).addMBB(BB);
6445
6446 // remove ABS instruction
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006447 MI->eraseFromParent();
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006448
6449 // return last added BB
6450 return SinkBB;
6451 }
Evan Chenga8e29892007-01-19 07:51:42 +00006452 }
6453}
6454
Evan Cheng37fefc22011-08-30 19:09:48 +00006455void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
6456 SDNode *Node) const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006457 if (!MI->hasPostISelHook()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006458 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
6459 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
6460 return;
6461 }
6462
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006463 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick4815d562011-09-20 03:17:40 +00006464 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
6465 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
6466 // operand is still set to noreg. If needed, set the optional operand's
6467 // register to CPSR, and remove the redundant implicit def.
Andrew Trick3be654f2011-09-21 02:20:46 +00006468 //
Andrew Trick90b7b122011-10-18 19:18:52 +00006469 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick4815d562011-09-20 03:17:40 +00006470
Andrew Trick3be654f2011-09-21 02:20:46 +00006471 // Rename pseudo opcodes.
6472 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
6473 if (NewOpc) {
6474 const ARMBaseInstrInfo *TII =
6475 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick90b7b122011-10-18 19:18:52 +00006476 MCID = &TII->get(NewOpc);
6477
6478 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
6479 "converted opcode should be the same except for cc_out");
6480
6481 MI->setDesc(*MCID);
6482
6483 // Add the optional cc_out operand
6484 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick3be654f2011-09-21 02:20:46 +00006485 }
Andrew Trick90b7b122011-10-18 19:18:52 +00006486 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick4815d562011-09-20 03:17:40 +00006487
6488 // Any ARM instruction that sets the 's' bit should specify an optional
6489 // "cc_out" operand in the last operand position.
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006490 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006491 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006492 return;
6493 }
Andrew Trick3be654f2011-09-21 02:20:46 +00006494 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
6495 // since we already have an optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006496 bool definesCPSR = false;
6497 bool deadCPSR = false;
Andrew Trick90b7b122011-10-18 19:18:52 +00006498 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick4815d562011-09-20 03:17:40 +00006499 i != e; ++i) {
6500 const MachineOperand &MO = MI->getOperand(i);
6501 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
6502 definesCPSR = true;
6503 if (MO.isDead())
6504 deadCPSR = true;
6505 MI->RemoveOperand(i);
6506 break;
Evan Cheng37fefc22011-08-30 19:09:48 +00006507 }
6508 }
Andrew Trick4815d562011-09-20 03:17:40 +00006509 if (!definesCPSR) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006510 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006511 return;
6512 }
6513 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick3be654f2011-09-21 02:20:46 +00006514 if (deadCPSR) {
6515 assert(!MI->getOperand(ccOutIdx).getReg() &&
6516 "expect uninitialized optional cc_out operand");
Andrew Trick4815d562011-09-20 03:17:40 +00006517 return;
Andrew Trick3be654f2011-09-21 02:20:46 +00006518 }
Andrew Trick4815d562011-09-20 03:17:40 +00006519
Andrew Trick3be654f2011-09-21 02:20:46 +00006520 // If this instruction was defined with an optional CPSR def and its dag node
6521 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006522 MachineOperand &MO = MI->getOperand(ccOutIdx);
6523 MO.setReg(ARM::CPSR);
6524 MO.setIsDef(true);
Evan Cheng37fefc22011-08-30 19:09:48 +00006525}
6526
Evan Chenga8e29892007-01-19 07:51:42 +00006527//===----------------------------------------------------------------------===//
6528// ARM Optimization Hooks
6529//===----------------------------------------------------------------------===//
6530
Chris Lattnerd1980a52009-03-12 06:52:53 +00006531static
6532SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
6533 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00006534 SelectionDAG &DAG = DCI.DAG;
6535 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00006536 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00006537 unsigned Opc = N->getOpcode();
6538 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
6539 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
6540 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
6541 ISD::CondCode CC = ISD::SETCC_INVALID;
6542
6543 if (isSlctCC) {
6544 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
6545 } else {
6546 SDValue CCOp = Slct.getOperand(0);
6547 if (CCOp.getOpcode() == ISD::SETCC)
6548 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
6549 }
6550
6551 bool DoXform = false;
6552 bool InvCC = false;
6553 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
6554 "Bad input!");
6555
6556 if (LHS.getOpcode() == ISD::Constant &&
6557 cast<ConstantSDNode>(LHS)->isNullValue()) {
6558 DoXform = true;
6559 } else if (CC != ISD::SETCC_INVALID &&
6560 RHS.getOpcode() == ISD::Constant &&
6561 cast<ConstantSDNode>(RHS)->isNullValue()) {
6562 std::swap(LHS, RHS);
6563 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006564 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00006565 Op0.getOperand(0).getValueType();
6566 bool isInt = OpVT.isInteger();
6567 CC = ISD::getSetCCInverse(CC, isInt);
6568
6569 if (!TLI.isCondCodeLegal(CC, OpVT))
6570 return SDValue(); // Inverse operator isn't legal.
6571
6572 DoXform = true;
6573 InvCC = true;
6574 }
6575
6576 if (DoXform) {
6577 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
6578 if (isSlctCC)
6579 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
6580 Slct.getOperand(0), Slct.getOperand(1), CC);
6581 SDValue CCOp = Slct.getOperand(0);
6582 if (InvCC)
6583 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
6584 CCOp.getOperand(0), CCOp.getOperand(1), CC);
6585 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
6586 CCOp, OtherOp, Result);
6587 }
6588 return SDValue();
6589}
6590
Eric Christopherfa6f5912011-06-29 21:10:36 +00006591// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00006592// (only after legalization).
6593static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
6594 TargetLowering::DAGCombinerInfo &DCI,
6595 const ARMSubtarget *Subtarget) {
6596
6597 // Only perform optimization if after legalize, and if NEON is available. We
6598 // also expected both operands to be BUILD_VECTORs.
6599 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
6600 || N0.getOpcode() != ISD::BUILD_VECTOR
6601 || N1.getOpcode() != ISD::BUILD_VECTOR)
6602 return SDValue();
6603
6604 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
6605 EVT VT = N->getValueType(0);
6606 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
6607 return SDValue();
6608
6609 // Check that the vector operands are of the right form.
6610 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
6611 // operands, where N is the size of the formed vector.
6612 // Each EXTRACT_VECTOR should have the same input vector and odd or even
6613 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00006614
6615 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00006616 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00006617 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00006618 SDValue Vec = N0->getOperand(0)->getOperand(0);
6619 SDNode *V = Vec.getNode();
6620 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00006621
Eric Christopherfa6f5912011-06-29 21:10:36 +00006622 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00006623 // check to see if each of their operands are an EXTRACT_VECTOR with
6624 // the same vector and appropriate index.
6625 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
6626 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
6627 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00006628
Tanya Lattner189531f2011-06-14 23:48:48 +00006629 SDValue ExtVec0 = N0->getOperand(i);
6630 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00006631
Tanya Lattner189531f2011-06-14 23:48:48 +00006632 // First operand is the vector, verify its the same.
6633 if (V != ExtVec0->getOperand(0).getNode() ||
6634 V != ExtVec1->getOperand(0).getNode())
6635 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00006636
Tanya Lattner189531f2011-06-14 23:48:48 +00006637 // Second is the constant, verify its correct.
6638 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
6639 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00006640
Tanya Lattner189531f2011-06-14 23:48:48 +00006641 // For the constant, we want to see all the even or all the odd.
6642 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
6643 || C1->getZExtValue() != nextIndex+1)
6644 return SDValue();
6645
6646 // Increment index.
6647 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00006648 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00006649 return SDValue();
6650 }
6651
6652 // Create VPADDL node.
6653 SelectionDAG &DAG = DCI.DAG;
6654 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00006655
6656 // Build operand list.
6657 SmallVector<SDValue, 8> Ops;
6658 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
6659 TLI.getPointerTy()));
6660
6661 // Input is the vector.
6662 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00006663
Tanya Lattner189531f2011-06-14 23:48:48 +00006664 // Get widened type and narrowed type.
6665 MVT widenType;
6666 unsigned numElem = VT.getVectorNumElements();
6667 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
6668 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
6669 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
6670 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
6671 default:
Craig Topperbc219812012-02-07 02:50:20 +00006672 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattner189531f2011-06-14 23:48:48 +00006673 }
6674
6675 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
6676 widenType, &Ops[0], Ops.size());
6677 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
6678}
6679
Bob Wilson3d5792a2010-07-29 20:34:14 +00006680/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
6681/// operands N0 and N1. This is a helper for PerformADDCombine that is
6682/// called with the default operands, and if that fails, with commuted
6683/// operands.
6684static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00006685 TargetLowering::DAGCombinerInfo &DCI,
6686 const ARMSubtarget *Subtarget){
6687
6688 // Attempt to create vpaddl for this add.
6689 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
6690 if (Result.getNode())
6691 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00006692
Chris Lattnerd1980a52009-03-12 06:52:53 +00006693 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
6694 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
6695 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
6696 if (Result.getNode()) return Result;
6697 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00006698 return SDValue();
6699}
6700
Bob Wilson3d5792a2010-07-29 20:34:14 +00006701/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
6702///
6703static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00006704 TargetLowering::DAGCombinerInfo &DCI,
6705 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00006706 SDValue N0 = N->getOperand(0);
6707 SDValue N1 = N->getOperand(1);
6708
6709 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00006710 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00006711 if (Result.getNode())
6712 return Result;
6713
6714 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00006715 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00006716}
6717
Chris Lattnerd1980a52009-03-12 06:52:53 +00006718/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00006719///
Chris Lattnerd1980a52009-03-12 06:52:53 +00006720static SDValue PerformSUBCombine(SDNode *N,
6721 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00006722 SDValue N0 = N->getOperand(0);
6723 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00006724
Chris Lattnerd1980a52009-03-12 06:52:53 +00006725 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
6726 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
6727 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
6728 if (Result.getNode()) return Result;
6729 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00006730
Chris Lattnerd1980a52009-03-12 06:52:53 +00006731 return SDValue();
6732}
6733
Evan Cheng463d3582011-03-31 19:38:48 +00006734/// PerformVMULCombine
6735/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
6736/// special multiplier accumulator forwarding.
6737/// vmul d3, d0, d2
6738/// vmla d3, d1, d2
6739/// is faster than
6740/// vadd d3, d0, d1
6741/// vmul d3, d3, d2
6742static SDValue PerformVMULCombine(SDNode *N,
6743 TargetLowering::DAGCombinerInfo &DCI,
6744 const ARMSubtarget *Subtarget) {
6745 if (!Subtarget->hasVMLxForwarding())
6746 return SDValue();
6747
6748 SelectionDAG &DAG = DCI.DAG;
6749 SDValue N0 = N->getOperand(0);
6750 SDValue N1 = N->getOperand(1);
6751 unsigned Opcode = N0.getOpcode();
6752 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
6753 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00006754 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00006755 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
6756 Opcode != ISD::FADD && Opcode != ISD::FSUB)
6757 return SDValue();
6758 std::swap(N0, N1);
6759 }
6760
6761 EVT VT = N->getValueType(0);
6762 DebugLoc DL = N->getDebugLoc();
6763 SDValue N00 = N0->getOperand(0);
6764 SDValue N01 = N0->getOperand(1);
6765 return DAG.getNode(Opcode, DL, VT,
6766 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
6767 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
6768}
6769
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006770static SDValue PerformMULCombine(SDNode *N,
6771 TargetLowering::DAGCombinerInfo &DCI,
6772 const ARMSubtarget *Subtarget) {
6773 SelectionDAG &DAG = DCI.DAG;
6774
6775 if (Subtarget->isThumb1Only())
6776 return SDValue();
6777
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006778 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
6779 return SDValue();
6780
6781 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00006782 if (VT.is64BitVector() || VT.is128BitVector())
6783 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006784 if (VT != MVT::i32)
6785 return SDValue();
6786
6787 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
6788 if (!C)
6789 return SDValue();
6790
6791 uint64_t MulAmt = C->getZExtValue();
6792 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
6793 ShiftAmt = ShiftAmt & (32 - 1);
6794 SDValue V = N->getOperand(0);
6795 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006796
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006797 SDValue Res;
6798 MulAmt >>= ShiftAmt;
6799 if (isPowerOf2_32(MulAmt - 1)) {
6800 // (mul x, 2^N + 1) => (add (shl x, N), x)
6801 Res = DAG.getNode(ISD::ADD, DL, VT,
6802 V, DAG.getNode(ISD::SHL, DL, VT,
6803 V, DAG.getConstant(Log2_32(MulAmt-1),
6804 MVT::i32)));
6805 } else if (isPowerOf2_32(MulAmt + 1)) {
6806 // (mul x, 2^N - 1) => (sub (shl x, N), x)
6807 Res = DAG.getNode(ISD::SUB, DL, VT,
6808 DAG.getNode(ISD::SHL, DL, VT,
6809 V, DAG.getConstant(Log2_32(MulAmt+1),
6810 MVT::i32)),
6811 V);
6812 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006813 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006814
6815 if (ShiftAmt != 0)
6816 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
6817 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006818
6819 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006820 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006821 return SDValue();
6822}
6823
Owen Anderson080c0922010-11-05 19:27:46 +00006824static SDValue PerformANDCombine(SDNode *N,
6825 TargetLowering::DAGCombinerInfo &DCI) {
Owen Anderson76706012011-04-05 21:48:57 +00006826
Owen Anderson080c0922010-11-05 19:27:46 +00006827 // Attempt to use immediate-form VBIC
6828 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
6829 DebugLoc dl = N->getDebugLoc();
6830 EVT VT = N->getValueType(0);
6831 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006832
Tanya Lattner0433b212011-04-07 15:24:20 +00006833 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
6834 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00006835
Owen Anderson080c0922010-11-05 19:27:46 +00006836 APInt SplatBits, SplatUndef;
6837 unsigned SplatBitSize;
6838 bool HasAnyUndefs;
6839 if (BVN &&
6840 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
6841 if (SplatBitSize <= 64) {
6842 EVT VbicVT;
6843 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
6844 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006845 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00006846 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00006847 if (Val.getNode()) {
6848 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006849 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00006850 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006851 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00006852 }
6853 }
6854 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006855
Owen Anderson080c0922010-11-05 19:27:46 +00006856 return SDValue();
6857}
6858
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006859/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
6860static SDValue PerformORCombine(SDNode *N,
6861 TargetLowering::DAGCombinerInfo &DCI,
6862 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00006863 // Attempt to use immediate-form VORR
6864 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
6865 DebugLoc dl = N->getDebugLoc();
6866 EVT VT = N->getValueType(0);
6867 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006868
Tanya Lattner0433b212011-04-07 15:24:20 +00006869 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
6870 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00006871
Owen Anderson60f48702010-11-03 23:15:26 +00006872 APInt SplatBits, SplatUndef;
6873 unsigned SplatBitSize;
6874 bool HasAnyUndefs;
6875 if (BVN && Subtarget->hasNEON() &&
6876 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
6877 if (SplatBitSize <= 64) {
6878 EVT VorrVT;
6879 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
6880 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00006881 DAG, VorrVT, VT.is128BitVector(),
6882 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00006883 if (Val.getNode()) {
6884 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006885 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00006886 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006887 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00006888 }
6889 }
6890 }
6891
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00006892 SDValue N0 = N->getOperand(0);
6893 if (N0.getOpcode() != ISD::AND)
6894 return SDValue();
6895 SDValue N1 = N->getOperand(1);
6896
6897 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
6898 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
6899 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
6900 APInt SplatUndef;
6901 unsigned SplatBitSize;
6902 bool HasAnyUndefs;
6903
6904 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
6905 APInt SplatBits0;
6906 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
6907 HasAnyUndefs) && !HasAnyUndefs) {
6908 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
6909 APInt SplatBits1;
6910 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
6911 HasAnyUndefs) && !HasAnyUndefs &&
6912 SplatBits0 == ~SplatBits1) {
6913 // Canonicalize the vector type to make instruction selection simpler.
6914 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
6915 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
6916 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00006917 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00006918 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
6919 }
6920 }
6921 }
6922
Jim Grosbach54238562010-07-17 03:30:54 +00006923 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
6924 // reasonable.
6925
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006926 // BFI is only available on V6T2+
6927 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
6928 return SDValue();
6929
Jim Grosbach54238562010-07-17 03:30:54 +00006930 DebugLoc DL = N->getDebugLoc();
6931 // 1) or (and A, mask), val => ARMbfi A, val, mask
6932 // iff (val & mask) == val
6933 //
6934 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
6935 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00006936 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00006937 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00006938 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00006939 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006940
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006941 if (VT != MVT::i32)
6942 return SDValue();
6943
Evan Cheng30fb13f2010-12-13 20:32:54 +00006944 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00006945
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006946 // The value and the mask need to be constants so we can verify this is
6947 // actually a bitfield set. If the mask is 0xffff, we can do better
6948 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00006949 SDValue MaskOp = N0.getOperand(1);
6950 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
6951 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006952 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00006953 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006954 if (Mask == 0xffff)
6955 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006956 SDValue Res;
6957 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00006958 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
6959 if (N1C) {
6960 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00006961 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00006962 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006963
Evan Chenga9688c42010-12-11 04:11:38 +00006964 if (ARM::isBitFieldInvertedMask(Mask)) {
6965 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006966
Evan Cheng30fb13f2010-12-13 20:32:54 +00006967 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00006968 DAG.getConstant(Val, MVT::i32),
6969 DAG.getConstant(Mask, MVT::i32));
6970
6971 // Do not add new nodes to DAG combiner worklist.
6972 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00006973 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00006974 }
Jim Grosbach54238562010-07-17 03:30:54 +00006975 } else if (N1.getOpcode() == ISD::AND) {
6976 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00006977 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
6978 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00006979 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00006980 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006981
Eric Christopher29aeed12011-03-26 01:21:03 +00006982 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
6983 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00006984 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00006985 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00006986 // The pack halfword instruction works better for masks that fit it,
6987 // so use that when it's available.
6988 if (Subtarget->hasT2ExtractPack() &&
6989 (Mask == 0xffff || Mask == 0xffff0000))
6990 return SDValue();
6991 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00006992 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00006993 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00006994 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00006995 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00006996 DAG.getConstant(Mask, MVT::i32));
6997 // Do not add new nodes to DAG combiner worklist.
6998 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00006999 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007000 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007001 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007002 // The pack halfword instruction works better for masks that fit it,
7003 // so use that when it's available.
7004 if (Subtarget->hasT2ExtractPack() &&
7005 (Mask2 == 0xffff || Mask2 == 0xffff0000))
7006 return SDValue();
7007 // 2b
7008 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007009 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00007010 DAG.getConstant(lsb, MVT::i32));
7011 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00007012 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00007013 // Do not add new nodes to DAG combiner worklist.
7014 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007015 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007016 }
7017 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007018
Evan Cheng30fb13f2010-12-13 20:32:54 +00007019 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
7020 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
7021 ARM::isBitFieldInvertedMask(~Mask)) {
7022 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
7023 // where lsb(mask) == #shamt and masked bits of B are known zero.
7024 SDValue ShAmt = N00.getOperand(1);
7025 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
7026 unsigned LSB = CountTrailingZeros_32(Mask);
7027 if (ShAmtC != LSB)
7028 return SDValue();
7029
7030 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
7031 DAG.getConstant(~Mask, MVT::i32));
7032
7033 // Do not add new nodes to DAG combiner worklist.
7034 DCI.CombineTo(N, Res, false);
7035 }
7036
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007037 return SDValue();
7038}
7039
Evan Chengbf188ae2011-06-15 01:12:31 +00007040/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
7041/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00007042static SDValue PerformBFICombine(SDNode *N,
7043 TargetLowering::DAGCombinerInfo &DCI) {
7044 SDValue N1 = N->getOperand(1);
7045 if (N1.getOpcode() == ISD::AND) {
7046 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7047 if (!N11C)
7048 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007049 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
7050 unsigned LSB = CountTrailingZeros_32(~InvMask);
7051 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
7052 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00007053 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007054 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00007055 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
7056 N->getOperand(0), N1.getOperand(0),
7057 N->getOperand(2));
7058 }
7059 return SDValue();
7060}
7061
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007062/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
7063/// ARMISD::VMOVRRD.
7064static SDValue PerformVMOVRRDCombine(SDNode *N,
7065 TargetLowering::DAGCombinerInfo &DCI) {
7066 // vmovrrd(vmovdrr x, y) -> x,y
7067 SDValue InDouble = N->getOperand(0);
7068 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
7069 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00007070
7071 // vmovrrd(load f64) -> (load i32), (load i32)
7072 SDNode *InNode = InDouble.getNode();
7073 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
7074 InNode->getValueType(0) == MVT::f64 &&
7075 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
7076 !cast<LoadSDNode>(InNode)->isVolatile()) {
7077 // TODO: Should this be done for non-FrameIndex operands?
7078 LoadSDNode *LD = cast<LoadSDNode>(InNode);
7079
7080 SelectionDAG &DAG = DCI.DAG;
7081 DebugLoc DL = LD->getDebugLoc();
7082 SDValue BasePtr = LD->getBasePtr();
7083 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
7084 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007085 LD->isNonTemporal(), LD->isInvariant(),
7086 LD->getAlignment());
Cameron Zwarich4071a712011-04-02 02:40:43 +00007087
7088 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7089 DAG.getConstant(4, MVT::i32));
7090 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
7091 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007092 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich4071a712011-04-02 02:40:43 +00007093 std::min(4U, LD->getAlignment() / 2));
7094
7095 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
7096 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
7097 DCI.RemoveFromWorklist(LD);
7098 DAG.DeleteNode(LD);
7099 return Result;
7100 }
7101
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007102 return SDValue();
7103}
7104
7105/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
7106/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
7107static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
7108 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
7109 SDValue Op0 = N->getOperand(0);
7110 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007111 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007112 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007113 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007114 Op1 = Op1.getOperand(0);
7115 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
7116 Op0.getNode() == Op1.getNode() &&
7117 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007118 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007119 N->getValueType(0), Op0.getOperand(0));
7120 return SDValue();
7121}
7122
Bob Wilson31600902010-12-21 06:43:19 +00007123/// PerformSTORECombine - Target-specific dag combine xforms for
7124/// ISD::STORE.
7125static SDValue PerformSTORECombine(SDNode *N,
7126 TargetLowering::DAGCombinerInfo &DCI) {
7127 // Bitcast an i64 store extracted from a vector to f64.
7128 // Otherwise, the i64 value will be legalized to a pair of i32 values.
7129 StoreSDNode *St = cast<StoreSDNode>(N);
7130 SDValue StVal = St->getValue();
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007131 if (!ISD::isNormalStore(St) || St->isVolatile())
7132 return SDValue();
7133
7134 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
7135 StVal.getNode()->hasOneUse() && !St->isVolatile()) {
7136 SelectionDAG &DAG = DCI.DAG;
7137 DebugLoc DL = St->getDebugLoc();
7138 SDValue BasePtr = St->getBasePtr();
7139 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
7140 StVal.getNode()->getOperand(0), BasePtr,
7141 St->getPointerInfo(), St->isVolatile(),
7142 St->isNonTemporal(), St->getAlignment());
7143
7144 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7145 DAG.getConstant(4, MVT::i32));
7146 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
7147 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
7148 St->isNonTemporal(),
7149 std::min(4U, St->getAlignment() / 2));
7150 }
7151
7152 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00007153 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
7154 return SDValue();
7155
7156 SelectionDAG &DAG = DCI.DAG;
7157 DebugLoc dl = StVal.getDebugLoc();
7158 SDValue IntVec = StVal.getOperand(0);
7159 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7160 IntVec.getValueType().getVectorNumElements());
7161 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
7162 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
7163 Vec, StVal.getOperand(1));
7164 dl = N->getDebugLoc();
7165 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
7166 // Make the DAGCombiner fold the bitcasts.
7167 DCI.AddToWorklist(Vec.getNode());
7168 DCI.AddToWorklist(ExtElt.getNode());
7169 DCI.AddToWorklist(V.getNode());
7170 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
7171 St->getPointerInfo(), St->isVolatile(),
7172 St->isNonTemporal(), St->getAlignment(),
7173 St->getTBAAInfo());
7174}
7175
7176/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
7177/// are normal, non-volatile loads. If so, it is profitable to bitcast an
7178/// i64 vector to have f64 elements, since the value can then be loaded
7179/// directly into a VFP register.
7180static bool hasNormalLoadOperand(SDNode *N) {
7181 unsigned NumElts = N->getValueType(0).getVectorNumElements();
7182 for (unsigned i = 0; i < NumElts; ++i) {
7183 SDNode *Elt = N->getOperand(i).getNode();
7184 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
7185 return true;
7186 }
7187 return false;
7188}
7189
Bob Wilson75f02882010-09-17 22:59:05 +00007190/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
7191/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00007192static SDValue PerformBUILD_VECTORCombine(SDNode *N,
7193 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00007194 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
7195 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
7196 // into a pair of GPRs, which is fine when the value is used as a scalar,
7197 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00007198 SelectionDAG &DAG = DCI.DAG;
7199 if (N->getNumOperands() == 2) {
7200 SDValue RV = PerformVMOVDRRCombine(N, DAG);
7201 if (RV.getNode())
7202 return RV;
7203 }
Bob Wilson75f02882010-09-17 22:59:05 +00007204
Bob Wilson31600902010-12-21 06:43:19 +00007205 // Load i64 elements as f64 values so that type legalization does not split
7206 // them up into i32 values.
7207 EVT VT = N->getValueType(0);
7208 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
7209 return SDValue();
7210 DebugLoc dl = N->getDebugLoc();
7211 SmallVector<SDValue, 8> Ops;
7212 unsigned NumElts = VT.getVectorNumElements();
7213 for (unsigned i = 0; i < NumElts; ++i) {
7214 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
7215 Ops.push_back(V);
7216 // Make the DAGCombiner fold the bitcast.
7217 DCI.AddToWorklist(V.getNode());
7218 }
7219 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
7220 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
7221 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
7222}
7223
7224/// PerformInsertEltCombine - Target-specific dag combine xforms for
7225/// ISD::INSERT_VECTOR_ELT.
7226static SDValue PerformInsertEltCombine(SDNode *N,
7227 TargetLowering::DAGCombinerInfo &DCI) {
7228 // Bitcast an i64 load inserted into a vector to f64.
7229 // Otherwise, the i64 value will be legalized to a pair of i32 values.
7230 EVT VT = N->getValueType(0);
7231 SDNode *Elt = N->getOperand(1).getNode();
7232 if (VT.getVectorElementType() != MVT::i64 ||
7233 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
7234 return SDValue();
7235
7236 SelectionDAG &DAG = DCI.DAG;
7237 DebugLoc dl = N->getDebugLoc();
7238 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7239 VT.getVectorNumElements());
7240 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
7241 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
7242 // Make the DAGCombiner fold the bitcasts.
7243 DCI.AddToWorklist(Vec.getNode());
7244 DCI.AddToWorklist(V.getNode());
7245 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
7246 Vec, V, N->getOperand(2));
7247 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00007248}
7249
Bob Wilsonf20700c2010-10-27 20:38:28 +00007250/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
7251/// ISD::VECTOR_SHUFFLE.
7252static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
7253 // The LLVM shufflevector instruction does not require the shuffle mask
7254 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
7255 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
7256 // operands do not match the mask length, they are extended by concatenating
7257 // them with undef vectors. That is probably the right thing for other
7258 // targets, but for NEON it is better to concatenate two double-register
7259 // size vector operands into a single quad-register size vector. Do that
7260 // transformation here:
7261 // shuffle(concat(v1, undef), concat(v2, undef)) ->
7262 // shuffle(concat(v1, v2), undef)
7263 SDValue Op0 = N->getOperand(0);
7264 SDValue Op1 = N->getOperand(1);
7265 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
7266 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
7267 Op0.getNumOperands() != 2 ||
7268 Op1.getNumOperands() != 2)
7269 return SDValue();
7270 SDValue Concat0Op1 = Op0.getOperand(1);
7271 SDValue Concat1Op1 = Op1.getOperand(1);
7272 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
7273 Concat1Op1.getOpcode() != ISD::UNDEF)
7274 return SDValue();
7275 // Skip the transformation if any of the types are illegal.
7276 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7277 EVT VT = N->getValueType(0);
7278 if (!TLI.isTypeLegal(VT) ||
7279 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
7280 !TLI.isTypeLegal(Concat1Op1.getValueType()))
7281 return SDValue();
7282
7283 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
7284 Op0.getOperand(0), Op1.getOperand(0));
7285 // Translate the shuffle mask.
7286 SmallVector<int, 16> NewMask;
7287 unsigned NumElts = VT.getVectorNumElements();
7288 unsigned HalfElts = NumElts/2;
7289 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
7290 for (unsigned n = 0; n < NumElts; ++n) {
7291 int MaskElt = SVN->getMaskElt(n);
7292 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007293 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00007294 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007295 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00007296 NewElt = HalfElts + MaskElt - NumElts;
7297 NewMask.push_back(NewElt);
7298 }
7299 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
7300 DAG.getUNDEF(VT), NewMask.data());
7301}
7302
Bob Wilson1c3ef902011-02-07 17:43:21 +00007303/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
7304/// NEON load/store intrinsics to merge base address updates.
7305static SDValue CombineBaseUpdate(SDNode *N,
7306 TargetLowering::DAGCombinerInfo &DCI) {
7307 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7308 return SDValue();
7309
7310 SelectionDAG &DAG = DCI.DAG;
7311 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
7312 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
7313 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
7314 SDValue Addr = N->getOperand(AddrOpIdx);
7315
7316 // Search for a use of the address operand that is an increment.
7317 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
7318 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
7319 SDNode *User = *UI;
7320 if (User->getOpcode() != ISD::ADD ||
7321 UI.getUse().getResNo() != Addr.getResNo())
7322 continue;
7323
7324 // Check that the add is independent of the load/store. Otherwise, folding
7325 // it would create a cycle.
7326 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
7327 continue;
7328
7329 // Find the new opcode for the updating load/store.
7330 bool isLoad = true;
7331 bool isLaneOp = false;
7332 unsigned NewOpc = 0;
7333 unsigned NumVecs = 0;
7334 if (isIntrinsic) {
7335 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
7336 switch (IntNo) {
Craig Topperbc219812012-02-07 02:50:20 +00007337 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00007338 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
7339 NumVecs = 1; break;
7340 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
7341 NumVecs = 2; break;
7342 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
7343 NumVecs = 3; break;
7344 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
7345 NumVecs = 4; break;
7346 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
7347 NumVecs = 2; isLaneOp = true; break;
7348 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
7349 NumVecs = 3; isLaneOp = true; break;
7350 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
7351 NumVecs = 4; isLaneOp = true; break;
7352 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
7353 NumVecs = 1; isLoad = false; break;
7354 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
7355 NumVecs = 2; isLoad = false; break;
7356 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
7357 NumVecs = 3; isLoad = false; break;
7358 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
7359 NumVecs = 4; isLoad = false; break;
7360 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
7361 NumVecs = 2; isLoad = false; isLaneOp = true; break;
7362 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
7363 NumVecs = 3; isLoad = false; isLaneOp = true; break;
7364 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
7365 NumVecs = 4; isLoad = false; isLaneOp = true; break;
7366 }
7367 } else {
7368 isLaneOp = true;
7369 switch (N->getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00007370 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00007371 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
7372 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
7373 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
7374 }
7375 }
7376
7377 // Find the size of memory referenced by the load/store.
7378 EVT VecTy;
7379 if (isLoad)
7380 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00007381 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00007382 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
7383 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
7384 if (isLaneOp)
7385 NumBytes /= VecTy.getVectorNumElements();
7386
7387 // If the increment is a constant, it must match the memory ref size.
7388 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
7389 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
7390 uint64_t IncVal = CInc->getZExtValue();
7391 if (IncVal != NumBytes)
7392 continue;
7393 } else if (NumBytes >= 3 * 16) {
7394 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
7395 // separate instructions that make it harder to use a non-constant update.
7396 continue;
7397 }
7398
7399 // Create the new updating load/store node.
7400 EVT Tys[6];
7401 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
7402 unsigned n;
7403 for (n = 0; n < NumResultVecs; ++n)
7404 Tys[n] = VecTy;
7405 Tys[n++] = MVT::i32;
7406 Tys[n] = MVT::Other;
7407 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
7408 SmallVector<SDValue, 8> Ops;
7409 Ops.push_back(N->getOperand(0)); // incoming chain
7410 Ops.push_back(N->getOperand(AddrOpIdx));
7411 Ops.push_back(Inc);
7412 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
7413 Ops.push_back(N->getOperand(i));
7414 }
7415 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
7416 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
7417 Ops.data(), Ops.size(),
7418 MemInt->getMemoryVT(),
7419 MemInt->getMemOperand());
7420
7421 // Update the uses.
7422 std::vector<SDValue> NewResults;
7423 for (unsigned i = 0; i < NumResultVecs; ++i) {
7424 NewResults.push_back(SDValue(UpdN.getNode(), i));
7425 }
7426 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
7427 DCI.CombineTo(N, NewResults);
7428 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
7429
7430 break;
Owen Anderson76706012011-04-05 21:48:57 +00007431 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00007432 return SDValue();
7433}
7434
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007435/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
7436/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
7437/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
7438/// return true.
7439static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
7440 SelectionDAG &DAG = DCI.DAG;
7441 EVT VT = N->getValueType(0);
7442 // vldN-dup instructions only support 64-bit vectors for N > 1.
7443 if (!VT.is64BitVector())
7444 return false;
7445
7446 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
7447 SDNode *VLD = N->getOperand(0).getNode();
7448 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
7449 return false;
7450 unsigned NumVecs = 0;
7451 unsigned NewOpc = 0;
7452 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
7453 if (IntNo == Intrinsic::arm_neon_vld2lane) {
7454 NumVecs = 2;
7455 NewOpc = ARMISD::VLD2DUP;
7456 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
7457 NumVecs = 3;
7458 NewOpc = ARMISD::VLD3DUP;
7459 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
7460 NumVecs = 4;
7461 NewOpc = ARMISD::VLD4DUP;
7462 } else {
7463 return false;
7464 }
7465
7466 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
7467 // numbers match the load.
7468 unsigned VLDLaneNo =
7469 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
7470 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
7471 UI != UE; ++UI) {
7472 // Ignore uses of the chain result.
7473 if (UI.getUse().getResNo() == NumVecs)
7474 continue;
7475 SDNode *User = *UI;
7476 if (User->getOpcode() != ARMISD::VDUPLANE ||
7477 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
7478 return false;
7479 }
7480
7481 // Create the vldN-dup node.
7482 EVT Tys[5];
7483 unsigned n;
7484 for (n = 0; n < NumVecs; ++n)
7485 Tys[n] = VT;
7486 Tys[n] = MVT::Other;
7487 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
7488 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
7489 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
7490 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
7491 Ops, 2, VLDMemInt->getMemoryVT(),
7492 VLDMemInt->getMemOperand());
7493
7494 // Update the uses.
7495 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
7496 UI != UE; ++UI) {
7497 unsigned ResNo = UI.getUse().getResNo();
7498 // Ignore uses of the chain result.
7499 if (ResNo == NumVecs)
7500 continue;
7501 SDNode *User = *UI;
7502 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
7503 }
7504
7505 // Now the vldN-lane intrinsic is dead except for its chain result.
7506 // Update uses of the chain.
7507 std::vector<SDValue> VLDDupResults;
7508 for (unsigned n = 0; n < NumVecs; ++n)
7509 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
7510 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
7511 DCI.CombineTo(VLD, VLDDupResults);
7512
7513 return true;
7514}
7515
Bob Wilson9e82bf12010-07-14 01:22:12 +00007516/// PerformVDUPLANECombine - Target-specific dag combine xforms for
7517/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007518static SDValue PerformVDUPLANECombine(SDNode *N,
7519 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00007520 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007521
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007522 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
7523 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
7524 if (CombineVLDDUP(N, DCI))
7525 return SDValue(N, 0);
7526
7527 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
7528 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007529 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00007530 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00007531 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00007532 return SDValue();
7533
7534 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
7535 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
7536 // The canonical VMOV for a zero vector uses a 32-bit element size.
7537 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7538 unsigned EltBits;
7539 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
7540 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007541 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007542 if (EltSize > VT.getVectorElementType().getSizeInBits())
7543 return SDValue();
7544
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007545 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007546}
7547
Eric Christopherfa6f5912011-06-29 21:10:36 +00007548// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00007549// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
7550static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
7551{
Chad Rosier118c9a02011-06-28 17:26:57 +00007552 integerPart cN;
7553 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00007554 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
7555 I != E; I++) {
7556 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
7557 if (!C)
7558 return false;
7559
Eric Christopherfa6f5912011-06-29 21:10:36 +00007560 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00007561 APFloat APF = C->getValueAPF();
7562 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
7563 != APFloat::opOK || !isExact)
7564 return false;
7565
7566 c0 = (I == 0) ? cN : c0;
7567 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
7568 return false;
7569 }
7570 C = c0;
7571 return true;
7572}
7573
7574/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
7575/// can replace combinations of VMUL and VCVT (floating-point to integer)
7576/// when the VMUL has a constant operand that is a power of 2.
7577///
7578/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
7579/// vmul.f32 d16, d17, d16
7580/// vcvt.s32.f32 d16, d16
7581/// becomes:
7582/// vcvt.s32.f32 d16, d16, #3
7583static SDValue PerformVCVTCombine(SDNode *N,
7584 TargetLowering::DAGCombinerInfo &DCI,
7585 const ARMSubtarget *Subtarget) {
7586 SelectionDAG &DAG = DCI.DAG;
7587 SDValue Op = N->getOperand(0);
7588
7589 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
7590 Op.getOpcode() != ISD::FMUL)
7591 return SDValue();
7592
7593 uint64_t C;
7594 SDValue N0 = Op->getOperand(0);
7595 SDValue ConstVec = Op->getOperand(1);
7596 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
7597
Eric Christopherfa6f5912011-06-29 21:10:36 +00007598 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00007599 !isConstVecPow2(ConstVec, isSigned, C))
7600 return SDValue();
7601
7602 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
7603 Intrinsic::arm_neon_vcvtfp2fxu;
7604 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7605 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00007606 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00007607 DAG.getConstant(Log2_64(C), MVT::i32));
7608}
7609
7610/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
7611/// can replace combinations of VCVT (integer to floating-point) and VDIV
7612/// when the VDIV has a constant operand that is a power of 2.
7613///
7614/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
7615/// vcvt.f32.s32 d16, d16
7616/// vdiv.f32 d16, d17, d16
7617/// becomes:
7618/// vcvt.f32.s32 d16, d16, #3
7619static SDValue PerformVDIVCombine(SDNode *N,
7620 TargetLowering::DAGCombinerInfo &DCI,
7621 const ARMSubtarget *Subtarget) {
7622 SelectionDAG &DAG = DCI.DAG;
7623 SDValue Op = N->getOperand(0);
7624 unsigned OpOpcode = Op.getNode()->getOpcode();
7625
7626 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
7627 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
7628 return SDValue();
7629
7630 uint64_t C;
7631 SDValue ConstVec = N->getOperand(1);
7632 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
7633
7634 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
7635 !isConstVecPow2(ConstVec, isSigned, C))
7636 return SDValue();
7637
Eric Christopherfa6f5912011-06-29 21:10:36 +00007638 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00007639 Intrinsic::arm_neon_vcvtfxu2fp;
7640 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7641 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00007642 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00007643 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
7644}
7645
7646/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00007647/// operand of a vector shift operation, where all the elements of the
7648/// build_vector must have the same constant integer value.
7649static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
7650 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007651 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00007652 Op = Op.getOperand(0);
7653 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
7654 APInt SplatBits, SplatUndef;
7655 unsigned SplatBitSize;
7656 bool HasAnyUndefs;
7657 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
7658 HasAnyUndefs, ElementBits) ||
7659 SplatBitSize > ElementBits)
7660 return false;
7661 Cnt = SplatBits.getSExtValue();
7662 return true;
7663}
7664
7665/// isVShiftLImm - Check if this is a valid build_vector for the immediate
7666/// operand of a vector shift left operation. That value must be in the range:
7667/// 0 <= Value < ElementBits for a left shift; or
7668/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00007669static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00007670 assert(VT.isVector() && "vector shift count is not a vector type");
7671 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
7672 if (! getVShiftImm(Op, ElementBits, Cnt))
7673 return false;
7674 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
7675}
7676
7677/// isVShiftRImm - Check if this is a valid build_vector for the immediate
7678/// operand of a vector shift right operation. For a shift opcode, the value
7679/// is positive, but for an intrinsic the value count must be negative. The
7680/// absolute value must be in the range:
7681/// 1 <= |Value| <= ElementBits for a right shift; or
7682/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00007683static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00007684 int64_t &Cnt) {
7685 assert(VT.isVector() && "vector shift count is not a vector type");
7686 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
7687 if (! getVShiftImm(Op, ElementBits, Cnt))
7688 return false;
7689 if (isIntrinsic)
7690 Cnt = -Cnt;
7691 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
7692}
7693
7694/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
7695static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
7696 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
7697 switch (IntNo) {
7698 default:
7699 // Don't do anything for most intrinsics.
7700 break;
7701
7702 // Vector shifts: check for immediate versions and lower them.
7703 // Note: This is done during DAG combining instead of DAG legalizing because
7704 // the build_vectors for 64-bit vector element shift counts are generally
7705 // not legal, and it is hard to see their values after they get legalized to
7706 // loads from a constant pool.
7707 case Intrinsic::arm_neon_vshifts:
7708 case Intrinsic::arm_neon_vshiftu:
7709 case Intrinsic::arm_neon_vshiftls:
7710 case Intrinsic::arm_neon_vshiftlu:
7711 case Intrinsic::arm_neon_vshiftn:
7712 case Intrinsic::arm_neon_vrshifts:
7713 case Intrinsic::arm_neon_vrshiftu:
7714 case Intrinsic::arm_neon_vrshiftn:
7715 case Intrinsic::arm_neon_vqshifts:
7716 case Intrinsic::arm_neon_vqshiftu:
7717 case Intrinsic::arm_neon_vqshiftsu:
7718 case Intrinsic::arm_neon_vqshiftns:
7719 case Intrinsic::arm_neon_vqshiftnu:
7720 case Intrinsic::arm_neon_vqshiftnsu:
7721 case Intrinsic::arm_neon_vqrshiftns:
7722 case Intrinsic::arm_neon_vqrshiftnu:
7723 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00007724 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007725 int64_t Cnt;
7726 unsigned VShiftOpc = 0;
7727
7728 switch (IntNo) {
7729 case Intrinsic::arm_neon_vshifts:
7730 case Intrinsic::arm_neon_vshiftu:
7731 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
7732 VShiftOpc = ARMISD::VSHL;
7733 break;
7734 }
7735 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
7736 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
7737 ARMISD::VSHRs : ARMISD::VSHRu);
7738 break;
7739 }
7740 return SDValue();
7741
7742 case Intrinsic::arm_neon_vshiftls:
7743 case Intrinsic::arm_neon_vshiftlu:
7744 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
7745 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007746 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007747
7748 case Intrinsic::arm_neon_vrshifts:
7749 case Intrinsic::arm_neon_vrshiftu:
7750 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
7751 break;
7752 return SDValue();
7753
7754 case Intrinsic::arm_neon_vqshifts:
7755 case Intrinsic::arm_neon_vqshiftu:
7756 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
7757 break;
7758 return SDValue();
7759
7760 case Intrinsic::arm_neon_vqshiftsu:
7761 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
7762 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007763 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007764
7765 case Intrinsic::arm_neon_vshiftn:
7766 case Intrinsic::arm_neon_vrshiftn:
7767 case Intrinsic::arm_neon_vqshiftns:
7768 case Intrinsic::arm_neon_vqshiftnu:
7769 case Intrinsic::arm_neon_vqshiftnsu:
7770 case Intrinsic::arm_neon_vqrshiftns:
7771 case Intrinsic::arm_neon_vqrshiftnu:
7772 case Intrinsic::arm_neon_vqrshiftnsu:
7773 // Narrowing shifts require an immediate right shift.
7774 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
7775 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00007776 llvm_unreachable("invalid shift count for narrowing vector shift "
7777 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007778
7779 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00007780 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00007781 }
7782
7783 switch (IntNo) {
7784 case Intrinsic::arm_neon_vshifts:
7785 case Intrinsic::arm_neon_vshiftu:
7786 // Opcode already set above.
7787 break;
7788 case Intrinsic::arm_neon_vshiftls:
7789 case Intrinsic::arm_neon_vshiftlu:
7790 if (Cnt == VT.getVectorElementType().getSizeInBits())
7791 VShiftOpc = ARMISD::VSHLLi;
7792 else
7793 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
7794 ARMISD::VSHLLs : ARMISD::VSHLLu);
7795 break;
7796 case Intrinsic::arm_neon_vshiftn:
7797 VShiftOpc = ARMISD::VSHRN; break;
7798 case Intrinsic::arm_neon_vrshifts:
7799 VShiftOpc = ARMISD::VRSHRs; break;
7800 case Intrinsic::arm_neon_vrshiftu:
7801 VShiftOpc = ARMISD::VRSHRu; break;
7802 case Intrinsic::arm_neon_vrshiftn:
7803 VShiftOpc = ARMISD::VRSHRN; break;
7804 case Intrinsic::arm_neon_vqshifts:
7805 VShiftOpc = ARMISD::VQSHLs; break;
7806 case Intrinsic::arm_neon_vqshiftu:
7807 VShiftOpc = ARMISD::VQSHLu; break;
7808 case Intrinsic::arm_neon_vqshiftsu:
7809 VShiftOpc = ARMISD::VQSHLsu; break;
7810 case Intrinsic::arm_neon_vqshiftns:
7811 VShiftOpc = ARMISD::VQSHRNs; break;
7812 case Intrinsic::arm_neon_vqshiftnu:
7813 VShiftOpc = ARMISD::VQSHRNu; break;
7814 case Intrinsic::arm_neon_vqshiftnsu:
7815 VShiftOpc = ARMISD::VQSHRNsu; break;
7816 case Intrinsic::arm_neon_vqrshiftns:
7817 VShiftOpc = ARMISD::VQRSHRNs; break;
7818 case Intrinsic::arm_neon_vqrshiftnu:
7819 VShiftOpc = ARMISD::VQRSHRNu; break;
7820 case Intrinsic::arm_neon_vqrshiftnsu:
7821 VShiftOpc = ARMISD::VQRSHRNsu; break;
7822 }
7823
7824 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007825 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007826 }
7827
7828 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00007829 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007830 int64_t Cnt;
7831 unsigned VShiftOpc = 0;
7832
7833 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
7834 VShiftOpc = ARMISD::VSLI;
7835 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
7836 VShiftOpc = ARMISD::VSRI;
7837 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00007838 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007839 }
7840
7841 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
7842 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00007843 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007844 }
7845
7846 case Intrinsic::arm_neon_vqrshifts:
7847 case Intrinsic::arm_neon_vqrshiftu:
7848 // No immediate versions of these to check for.
7849 break;
7850 }
7851
7852 return SDValue();
7853}
7854
7855/// PerformShiftCombine - Checks for immediate versions of vector shifts and
7856/// lowers them. As with the vector shift intrinsics, this is done during DAG
7857/// combining instead of DAG legalizing because the build_vectors for 64-bit
7858/// vector element shift counts are generally not legal, and it is hard to see
7859/// their values after they get legalized to loads from a constant pool.
7860static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
7861 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00007862 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00007863
7864 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00007865 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7866 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00007867 return SDValue();
7868
7869 assert(ST->hasNEON() && "unexpected vector shift");
7870 int64_t Cnt;
7871
7872 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007873 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00007874
7875 case ISD::SHL:
7876 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
7877 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007878 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007879 break;
7880
7881 case ISD::SRA:
7882 case ISD::SRL:
7883 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
7884 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
7885 ARMISD::VSHRs : ARMISD::VSHRu);
7886 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007887 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007888 }
7889 }
7890 return SDValue();
7891}
7892
7893/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
7894/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
7895static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
7896 const ARMSubtarget *ST) {
7897 SDValue N0 = N->getOperand(0);
7898
7899 // Check for sign- and zero-extensions of vector extract operations of 8-
7900 // and 16-bit vector elements. NEON supports these directly. They are
7901 // handled during DAG combining because type legalization will promote them
7902 // to 32-bit types and it is messy to recognize the operations after that.
7903 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
7904 SDValue Vec = N0.getOperand(0);
7905 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00007906 EVT VT = N->getValueType(0);
7907 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007908 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7909
Owen Anderson825b72b2009-08-11 20:47:22 +00007910 if (VT == MVT::i32 &&
7911 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00007912 TLI.isTypeLegal(Vec.getValueType()) &&
7913 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00007914
7915 unsigned Opc = 0;
7916 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007917 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00007918 case ISD::SIGN_EXTEND:
7919 Opc = ARMISD::VGETLANEs;
7920 break;
7921 case ISD::ZERO_EXTEND:
7922 case ISD::ANY_EXTEND:
7923 Opc = ARMISD::VGETLANEu;
7924 break;
7925 }
7926 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
7927 }
7928 }
7929
7930 return SDValue();
7931}
7932
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007933/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
7934/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
7935static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
7936 const ARMSubtarget *ST) {
7937 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00007938 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007939 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
7940 // a NaN; only do the transformation when it matches that behavior.
7941
7942 // For now only do this when using NEON for FP operations; if using VFP, it
7943 // is not obvious that the benefit outweighs the cost of switching to the
7944 // NEON pipeline.
7945 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
7946 N->getValueType(0) != MVT::f32)
7947 return SDValue();
7948
7949 SDValue CondLHS = N->getOperand(0);
7950 SDValue CondRHS = N->getOperand(1);
7951 SDValue LHS = N->getOperand(2);
7952 SDValue RHS = N->getOperand(3);
7953 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
7954
7955 unsigned Opcode = 0;
7956 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00007957 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007958 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00007959 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007960 IsReversed = true ; // x CC y ? y : x
7961 } else {
7962 return SDValue();
7963 }
7964
Bob Wilsone742bb52010-02-24 22:15:53 +00007965 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007966 switch (CC) {
7967 default: break;
7968 case ISD::SETOLT:
7969 case ISD::SETOLE:
7970 case ISD::SETLT:
7971 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007972 case ISD::SETULT:
7973 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00007974 // If LHS is NaN, an ordered comparison will be false and the result will
7975 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
7976 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
7977 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
7978 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
7979 break;
7980 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
7981 // will return -0, so vmin can only be used for unsafe math or if one of
7982 // the operands is known to be nonzero.
7983 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00007984 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00007985 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
7986 break;
7987 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007988 break;
7989
7990 case ISD::SETOGT:
7991 case ISD::SETOGE:
7992 case ISD::SETGT:
7993 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007994 case ISD::SETUGT:
7995 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00007996 // If LHS is NaN, an ordered comparison will be false and the result will
7997 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
7998 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
7999 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
8000 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8001 break;
8002 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
8003 // will return +0, so vmax can only be used for unsafe math or if one of
8004 // the operands is known to be nonzero.
8005 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008006 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00008007 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8008 break;
8009 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008010 break;
8011 }
8012
8013 if (!Opcode)
8014 return SDValue();
8015 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
8016}
8017
Evan Chenge721f5c2011-07-13 00:42:17 +00008018/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
8019SDValue
8020ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
8021 SDValue Cmp = N->getOperand(4);
8022 if (Cmp.getOpcode() != ARMISD::CMPZ)
8023 // Only looking at EQ and NE cases.
8024 return SDValue();
8025
8026 EVT VT = N->getValueType(0);
8027 DebugLoc dl = N->getDebugLoc();
8028 SDValue LHS = Cmp.getOperand(0);
8029 SDValue RHS = Cmp.getOperand(1);
8030 SDValue FalseVal = N->getOperand(0);
8031 SDValue TrueVal = N->getOperand(1);
8032 SDValue ARMcc = N->getOperand(2);
Jim Grosbachb04546f2011-09-13 20:30:37 +00008033 ARMCC::CondCodes CC =
8034 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chenge721f5c2011-07-13 00:42:17 +00008035
8036 // Simplify
8037 // mov r1, r0
8038 // cmp r1, x
8039 // mov r0, y
8040 // moveq r0, x
8041 // to
8042 // cmp r0, x
8043 // movne r0, y
8044 //
8045 // mov r1, r0
8046 // cmp r1, x
8047 // mov r0, x
8048 // movne r0, y
8049 // to
8050 // cmp r0, x
8051 // movne r0, y
8052 /// FIXME: Turn this into a target neutral optimization?
8053 SDValue Res;
Evan Cheng9b88d2d2011-09-28 23:16:31 +00008054 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chenge721f5c2011-07-13 00:42:17 +00008055 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
8056 N->getOperand(3), Cmp);
8057 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
8058 SDValue ARMcc;
8059 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
8060 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
8061 N->getOperand(3), NewCmp);
8062 }
8063
8064 if (Res.getNode()) {
8065 APInt KnownZero, KnownOne;
8066 APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits());
8067 DAG.ComputeMaskedBits(SDValue(N,0), Mask, KnownZero, KnownOne);
8068 // Capture demanded bits information that would be otherwise lost.
8069 if (KnownZero == 0xfffffffe)
8070 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8071 DAG.getValueType(MVT::i1));
8072 else if (KnownZero == 0xffffff00)
8073 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8074 DAG.getValueType(MVT::i8));
8075 else if (KnownZero == 0xffff0000)
8076 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8077 DAG.getValueType(MVT::i16));
8078 }
8079
8080 return Res;
8081}
8082
Dan Gohman475871a2008-07-27 21:46:04 +00008083SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008084 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008085 switch (N->getOpcode()) {
8086 default: break;
Tanya Lattner189531f2011-06-14 23:48:48 +00008087 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008088 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00008089 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008090 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00008091 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00008092 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00008093 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008094 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00008095 case ISD::STORE: return PerformSTORECombine(N, DCI);
8096 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
8097 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00008098 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008099 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00008100 case ISD::FP_TO_SINT:
8101 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
8102 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008103 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00008104 case ISD::SHL:
8105 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008106 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00008107 case ISD::SIGN_EXTEND:
8108 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008109 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
8110 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00008111 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00008112 case ARMISD::VLD2DUP:
8113 case ARMISD::VLD3DUP:
8114 case ARMISD::VLD4DUP:
8115 return CombineBaseUpdate(N, DCI);
8116 case ISD::INTRINSIC_VOID:
8117 case ISD::INTRINSIC_W_CHAIN:
8118 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
8119 case Intrinsic::arm_neon_vld1:
8120 case Intrinsic::arm_neon_vld2:
8121 case Intrinsic::arm_neon_vld3:
8122 case Intrinsic::arm_neon_vld4:
8123 case Intrinsic::arm_neon_vld2lane:
8124 case Intrinsic::arm_neon_vld3lane:
8125 case Intrinsic::arm_neon_vld4lane:
8126 case Intrinsic::arm_neon_vst1:
8127 case Intrinsic::arm_neon_vst2:
8128 case Intrinsic::arm_neon_vst3:
8129 case Intrinsic::arm_neon_vst4:
8130 case Intrinsic::arm_neon_vst2lane:
8131 case Intrinsic::arm_neon_vst3lane:
8132 case Intrinsic::arm_neon_vst4lane:
8133 return CombineBaseUpdate(N, DCI);
8134 default: break;
8135 }
8136 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008137 }
Dan Gohman475871a2008-07-27 21:46:04 +00008138 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008139}
8140
Evan Cheng31959b12011-02-02 01:06:55 +00008141bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
8142 EVT VT) const {
8143 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
8144}
8145
Bill Wendlingaf566342009-08-15 21:21:19 +00008146bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00008147 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00008148 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00008149
8150 switch (VT.getSimpleVT().SimpleTy) {
8151 default:
8152 return false;
8153 case MVT::i8:
8154 case MVT::i16:
8155 case MVT::i32:
8156 return true;
8157 // FIXME: VLD1 etc with standard alignment is legal.
8158 }
8159}
8160
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008161static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
8162 unsigned AlignCheck) {
8163 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
8164 (DstAlign == 0 || DstAlign % AlignCheck == 0));
8165}
8166
8167EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
8168 unsigned DstAlign, unsigned SrcAlign,
Lang Hamesa1e78882011-11-02 23:37:04 +00008169 bool IsZeroVal,
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008170 bool MemcpyStrSrc,
8171 MachineFunction &MF) const {
8172 const Function *F = MF.getFunction();
8173
8174 // See if we can use NEON instructions for this...
Lang Hamesa1e78882011-11-02 23:37:04 +00008175 if (IsZeroVal &&
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008176 !F->hasFnAttr(Attribute::NoImplicitFloat) &&
8177 Subtarget->hasNEON()) {
8178 if (memOpAlign(SrcAlign, DstAlign, 16) && Size >= 16) {
8179 return MVT::v4i32;
8180 } else if (memOpAlign(SrcAlign, DstAlign, 8) && Size >= 8) {
8181 return MVT::v2i32;
8182 }
8183 }
8184
Lang Hames5207bf22011-11-08 18:56:23 +00008185 // Lowering to i32/i16 if the size permits.
8186 if (Size >= 4) {
8187 return MVT::i32;
8188 } else if (Size >= 2) {
8189 return MVT::i16;
8190 }
8191
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008192 // Let the target-independent logic figure it out.
8193 return MVT::Other;
8194}
8195
Evan Chenge6c835f2009-08-14 20:09:37 +00008196static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
8197 if (V < 0)
8198 return false;
8199
8200 unsigned Scale = 1;
8201 switch (VT.getSimpleVT().SimpleTy) {
8202 default: return false;
8203 case MVT::i1:
8204 case MVT::i8:
8205 // Scale == 1;
8206 break;
8207 case MVT::i16:
8208 // Scale == 2;
8209 Scale = 2;
8210 break;
8211 case MVT::i32:
8212 // Scale == 4;
8213 Scale = 4;
8214 break;
8215 }
8216
8217 if ((V & (Scale - 1)) != 0)
8218 return false;
8219 V /= Scale;
8220 return V == (V & ((1LL << 5) - 1));
8221}
8222
8223static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
8224 const ARMSubtarget *Subtarget) {
8225 bool isNeg = false;
8226 if (V < 0) {
8227 isNeg = true;
8228 V = - V;
8229 }
8230
8231 switch (VT.getSimpleVT().SimpleTy) {
8232 default: return false;
8233 case MVT::i1:
8234 case MVT::i8:
8235 case MVT::i16:
8236 case MVT::i32:
8237 // + imm12 or - imm8
8238 if (isNeg)
8239 return V == (V & ((1LL << 8) - 1));
8240 return V == (V & ((1LL << 12) - 1));
8241 case MVT::f32:
8242 case MVT::f64:
8243 // Same as ARM mode. FIXME: NEON?
8244 if (!Subtarget->hasVFP2())
8245 return false;
8246 if ((V & 3) != 0)
8247 return false;
8248 V >>= 2;
8249 return V == (V & ((1LL << 8) - 1));
8250 }
8251}
8252
Evan Chengb01fad62007-03-12 23:30:29 +00008253/// isLegalAddressImmediate - Return true if the integer value can be used
8254/// as the offset of the target addressing mode for load / store of the
8255/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00008256static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00008257 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00008258 if (V == 0)
8259 return true;
8260
Evan Cheng65011532009-03-09 19:15:00 +00008261 if (!VT.isSimple())
8262 return false;
8263
Evan Chenge6c835f2009-08-14 20:09:37 +00008264 if (Subtarget->isThumb1Only())
8265 return isLegalT1AddressImmediate(V, VT);
8266 else if (Subtarget->isThumb2())
8267 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00008268
Evan Chenge6c835f2009-08-14 20:09:37 +00008269 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00008270 if (V < 0)
8271 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00008272 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00008273 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008274 case MVT::i1:
8275 case MVT::i8:
8276 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00008277 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008278 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008279 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00008280 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008281 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008282 case MVT::f32:
8283 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00008284 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00008285 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00008286 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00008287 return false;
8288 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008289 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00008290 }
Evan Chenga8e29892007-01-19 07:51:42 +00008291}
8292
Evan Chenge6c835f2009-08-14 20:09:37 +00008293bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
8294 EVT VT) const {
8295 int Scale = AM.Scale;
8296 if (Scale < 0)
8297 return false;
8298
8299 switch (VT.getSimpleVT().SimpleTy) {
8300 default: return false;
8301 case MVT::i1:
8302 case MVT::i8:
8303 case MVT::i16:
8304 case MVT::i32:
8305 if (Scale == 1)
8306 return true;
8307 // r + r << imm
8308 Scale = Scale & ~1;
8309 return Scale == 2 || Scale == 4 || Scale == 8;
8310 case MVT::i64:
8311 // r + r
8312 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
8313 return true;
8314 return false;
8315 case MVT::isVoid:
8316 // Note, we allow "void" uses (basically, uses that aren't loads or
8317 // stores), because arm allows folding a scale into many arithmetic
8318 // operations. This should be made more precise and revisited later.
8319
8320 // Allow r << imm, but the imm has to be a multiple of two.
8321 if (Scale & 1) return false;
8322 return isPowerOf2_32(Scale);
8323 }
8324}
8325
Chris Lattner37caf8c2007-04-09 23:33:39 +00008326/// isLegalAddressingMode - Return true if the addressing mode represented
8327/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008328bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008329 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008330 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00008331 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00008332 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008333
Chris Lattner37caf8c2007-04-09 23:33:39 +00008334 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008335 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008336 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008337
Chris Lattner37caf8c2007-04-09 23:33:39 +00008338 switch (AM.Scale) {
8339 case 0: // no scale reg, must be "r+i" or "r", or "i".
8340 break;
8341 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00008342 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00008343 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008344 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00008345 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008346 // ARM doesn't support any R+R*scale+imm addr modes.
8347 if (AM.BaseOffs)
8348 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008349
Bob Wilson2c7dab12009-04-08 17:55:28 +00008350 if (!VT.isSimple())
8351 return false;
8352
Evan Chenge6c835f2009-08-14 20:09:37 +00008353 if (Subtarget->isThumb2())
8354 return isLegalT2ScaledAddressingMode(AM, VT);
8355
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008356 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00008357 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00008358 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008359 case MVT::i1:
8360 case MVT::i8:
8361 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008362 if (Scale < 0) Scale = -Scale;
8363 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008364 return true;
8365 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00008366 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008367 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00008368 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00008369 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008370 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008371 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00008372 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008373
Owen Anderson825b72b2009-08-11 20:47:22 +00008374 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00008375 // Note, we allow "void" uses (basically, uses that aren't loads or
8376 // stores), because arm allows folding a scale into many arithmetic
8377 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008378
Chris Lattner37caf8c2007-04-09 23:33:39 +00008379 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00008380 if (Scale & 1) return false;
8381 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00008382 }
Evan Chengb01fad62007-03-12 23:30:29 +00008383 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00008384 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00008385}
8386
Evan Cheng77e47512009-11-11 19:05:52 +00008387/// isLegalICmpImmediate - Return true if the specified immediate is legal
8388/// icmp immediate, that is the target has icmp instructions which can compare
8389/// a register against the immediate without having to materialize the
8390/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00008391bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00008392 if (!Subtarget->isThumb())
8393 return ARM_AM::getSOImmVal(Imm) != -1;
8394 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00008395 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00008396 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00008397}
8398
Dan Gohmancca82142011-05-03 00:46:49 +00008399/// isLegalAddImmediate - Return true if the specified immediate is legal
8400/// add immediate, that is the target has add instructions which can add
8401/// a register with the immediate without having to materialize the
8402/// immediate into a register.
8403bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
8404 return ARM_AM::getSOImmVal(Imm) != -1;
8405}
8406
Owen Andersone50ed302009-08-10 22:56:29 +00008407static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00008408 bool isSEXTLoad, SDValue &Base,
8409 SDValue &Offset, bool &isInc,
8410 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00008411 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
8412 return false;
8413
Owen Anderson825b72b2009-08-11 20:47:22 +00008414 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00008415 // AddressingMode 3
8416 Base = Ptr->getOperand(0);
8417 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008418 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00008419 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008420 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00008421 isInc = false;
8422 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8423 return true;
8424 }
8425 }
8426 isInc = (Ptr->getOpcode() == ISD::ADD);
8427 Offset = Ptr->getOperand(1);
8428 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00008429 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00008430 // AddressingMode 2
8431 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008432 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00008433 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008434 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00008435 isInc = false;
8436 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8437 Base = Ptr->getOperand(0);
8438 return true;
8439 }
8440 }
8441
8442 if (Ptr->getOpcode() == ISD::ADD) {
8443 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00008444 ARM_AM::ShiftOpc ShOpcVal=
8445 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00008446 if (ShOpcVal != ARM_AM::no_shift) {
8447 Base = Ptr->getOperand(1);
8448 Offset = Ptr->getOperand(0);
8449 } else {
8450 Base = Ptr->getOperand(0);
8451 Offset = Ptr->getOperand(1);
8452 }
8453 return true;
8454 }
8455
8456 isInc = (Ptr->getOpcode() == ISD::ADD);
8457 Base = Ptr->getOperand(0);
8458 Offset = Ptr->getOperand(1);
8459 return true;
8460 }
8461
Jim Grosbache5165492009-11-09 00:11:35 +00008462 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00008463 return false;
8464}
8465
Owen Andersone50ed302009-08-10 22:56:29 +00008466static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00008467 bool isSEXTLoad, SDValue &Base,
8468 SDValue &Offset, bool &isInc,
8469 SelectionDAG &DAG) {
8470 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
8471 return false;
8472
8473 Base = Ptr->getOperand(0);
8474 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
8475 int RHSC = (int)RHS->getZExtValue();
8476 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
8477 assert(Ptr->getOpcode() == ISD::ADD);
8478 isInc = false;
8479 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8480 return true;
8481 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
8482 isInc = Ptr->getOpcode() == ISD::ADD;
8483 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
8484 return true;
8485 }
8486 }
8487
8488 return false;
8489}
8490
Evan Chenga8e29892007-01-19 07:51:42 +00008491/// getPreIndexedAddressParts - returns true by value, base pointer and
8492/// offset pointer and addressing mode by reference if the node's address
8493/// can be legally represented as pre-indexed load / store address.
8494bool
Dan Gohman475871a2008-07-27 21:46:04 +00008495ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
8496 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00008497 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00008498 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008499 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00008500 return false;
8501
Owen Andersone50ed302009-08-10 22:56:29 +00008502 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00008503 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00008504 bool isSEXTLoad = false;
8505 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
8506 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008507 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00008508 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
8509 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
8510 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008511 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00008512 } else
8513 return false;
8514
8515 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00008516 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00008517 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00008518 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
8519 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00008520 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00008521 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00008522 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00008523 if (!isLegal)
8524 return false;
8525
8526 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
8527 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00008528}
8529
8530/// getPostIndexedAddressParts - returns true by value, base pointer and
8531/// offset pointer and addressing mode by reference if this node can be
8532/// combined with a load / store to form a post-indexed load / store.
8533bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00008534 SDValue &Base,
8535 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00008536 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00008537 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008538 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00008539 return false;
8540
Owen Andersone50ed302009-08-10 22:56:29 +00008541 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00008542 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00008543 bool isSEXTLoad = false;
8544 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008545 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00008546 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00008547 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
8548 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008549 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00008550 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00008551 } else
8552 return false;
8553
8554 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00008555 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00008556 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00008557 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00008558 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00008559 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00008560 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
8561 isInc, DAG);
8562 if (!isLegal)
8563 return false;
8564
Evan Cheng28dad2a2010-05-18 21:31:17 +00008565 if (Ptr != Base) {
8566 // Swap base ptr and offset to catch more post-index load / store when
8567 // it's legal. In Thumb2 mode, offset must be an immediate.
8568 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
8569 !Subtarget->isThumb2())
8570 std::swap(Base, Offset);
8571
8572 // Post-indexed load / store update the base pointer.
8573 if (Ptr != Base)
8574 return false;
8575 }
8576
Evan Chenge88d5ce2009-07-02 07:28:31 +00008577 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
8578 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00008579}
8580
Dan Gohman475871a2008-07-27 21:46:04 +00008581void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00008582 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008583 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008584 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008585 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00008586 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008587 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00008588 switch (Op.getOpcode()) {
8589 default: break;
8590 case ARMISD::CMOV: {
8591 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00008592 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00008593 if (KnownZero == 0 && KnownOne == 0) return;
8594
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008595 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00008596 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
8597 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00008598 KnownZero &= KnownZeroRHS;
8599 KnownOne &= KnownOneRHS;
8600 return;
8601 }
8602 }
8603}
8604
8605//===----------------------------------------------------------------------===//
8606// ARM Inline Assembly Support
8607//===----------------------------------------------------------------------===//
8608
Evan Cheng55d42002011-01-08 01:24:27 +00008609bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
8610 // Looking for "rev" which is V6+.
8611 if (!Subtarget->hasV6Ops())
8612 return false;
8613
8614 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
8615 std::string AsmStr = IA->getAsmString();
8616 SmallVector<StringRef, 4> AsmPieces;
8617 SplitString(AsmStr, AsmPieces, ";\n");
8618
8619 switch (AsmPieces.size()) {
8620 default: return false;
8621 case 1:
8622 AsmStr = AsmPieces[0];
8623 AsmPieces.clear();
8624 SplitString(AsmStr, AsmPieces, " \t,");
8625
8626 // rev $0, $1
8627 if (AsmPieces.size() == 3 &&
8628 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
8629 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008630 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00008631 if (Ty && Ty->getBitWidth() == 32)
8632 return IntrinsicLowering::LowerToByteSwap(CI);
8633 }
8634 break;
8635 }
8636
8637 return false;
8638}
8639
Evan Chenga8e29892007-01-19 07:51:42 +00008640/// getConstraintType - Given a constraint letter, return the type of
8641/// constraint it is for this target.
8642ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00008643ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
8644 if (Constraint.size() == 1) {
8645 switch (Constraint[0]) {
8646 default: break;
8647 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008648 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00008649 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00008650 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008651 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00008652 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00008653 // An address with a single base register. Due to the way we
8654 // currently handle addresses it is the same as an 'r' memory constraint.
8655 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00008656 }
Eric Christopher1312ca82011-06-21 22:10:57 +00008657 } else if (Constraint.size() == 2) {
8658 switch (Constraint[0]) {
8659 default: break;
8660 // All 'U+' constraints are addresses.
8661 case 'U': return C_Memory;
8662 }
Evan Chenga8e29892007-01-19 07:51:42 +00008663 }
Chris Lattner4234f572007-03-25 02:14:49 +00008664 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00008665}
8666
John Thompson44ab89e2010-10-29 17:29:13 +00008667/// Examine constraint type and operand type and determine a weight value.
8668/// This object must already have been set up with the operand type
8669/// and the current alternative constraint selected.
8670TargetLowering::ConstraintWeight
8671ARMTargetLowering::getSingleConstraintMatchWeight(
8672 AsmOperandInfo &info, const char *constraint) const {
8673 ConstraintWeight weight = CW_Invalid;
8674 Value *CallOperandVal = info.CallOperandVal;
8675 // If we don't have a value, we can't do a match,
8676 // but allow it at the lowest weight.
8677 if (CallOperandVal == NULL)
8678 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008679 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00008680 // Look at the constraint type.
8681 switch (*constraint) {
8682 default:
8683 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
8684 break;
8685 case 'l':
8686 if (type->isIntegerTy()) {
8687 if (Subtarget->isThumb())
8688 weight = CW_SpecificReg;
8689 else
8690 weight = CW_Register;
8691 }
8692 break;
8693 case 'w':
8694 if (type->isFloatingPointTy())
8695 weight = CW_Register;
8696 break;
8697 }
8698 return weight;
8699}
8700
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008701typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
8702RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00008703ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00008704 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00008705 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00008706 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00008707 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00008708 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00008709 if (Subtarget->isThumb())
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008710 return RCPair(0U, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +00008711 else
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008712 return RCPair(0U, ARM::GPRRegisterClass);
Eric Christopher73744df2011-06-30 23:23:01 +00008713 case 'h': // High regs or no regs.
8714 if (Subtarget->isThumb())
Andrew Trick3af7a672011-09-20 03:06:13 +00008715 return RCPair(0U, ARM::hGPRRegisterClass);
Eric Christopher1070f822011-07-01 00:19:27 +00008716 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008717 case 'r':
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008718 return RCPair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008719 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00008720 if (VT == MVT::f32)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008721 return RCPair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00008722 if (VT.getSizeInBits() == 64)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008723 return RCPair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00008724 if (VT.getSizeInBits() == 128)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008725 return RCPair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008726 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00008727 case 'x':
8728 if (VT == MVT::f32)
Andrew Trick3af7a672011-09-20 03:06:13 +00008729 return RCPair(0U, ARM::SPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008730 if (VT.getSizeInBits() == 64)
Andrew Trick3af7a672011-09-20 03:06:13 +00008731 return RCPair(0U, ARM::DPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008732 if (VT.getSizeInBits() == 128)
Andrew Trick3af7a672011-09-20 03:06:13 +00008733 return RCPair(0U, ARM::QPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008734 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008735 case 't':
8736 if (VT == MVT::f32)
Andrew Trick3af7a672011-09-20 03:06:13 +00008737 return RCPair(0U, ARM::SPRRegisterClass);
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008738 break;
Evan Chenga8e29892007-01-19 07:51:42 +00008739 }
8740 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00008741 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00008742 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00008743
Evan Chenga8e29892007-01-19 07:51:42 +00008744 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
8745}
8746
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008747/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
8748/// vector. If it is invalid, don't add anything to Ops.
8749void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00008750 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008751 std::vector<SDValue>&Ops,
8752 SelectionDAG &DAG) const {
8753 SDValue Result(0, 0);
8754
Eric Christopher100c8332011-06-02 23:16:42 +00008755 // Currently only support length 1 constraints.
8756 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00008757
Eric Christopher100c8332011-06-02 23:16:42 +00008758 char ConstraintLetter = Constraint[0];
8759 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008760 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00008761 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008762 case 'I': case 'J': case 'K': case 'L':
8763 case 'M': case 'N': case 'O':
8764 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
8765 if (!C)
8766 return;
8767
8768 int64_t CVal64 = C->getSExtValue();
8769 int CVal = (int) CVal64;
8770 // None of these constraints allow values larger than 32 bits. Check
8771 // that the value fits in an int.
8772 if (CVal != CVal64)
8773 return;
8774
Eric Christopher100c8332011-06-02 23:16:42 +00008775 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00008776 case 'j':
Andrew Trick3af7a672011-09-20 03:06:13 +00008777 // Constant suitable for movw, must be between 0 and
8778 // 65535.
8779 if (Subtarget->hasV6T2Ops())
8780 if (CVal >= 0 && CVal <= 65535)
8781 break;
8782 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008783 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008784 if (Subtarget->isThumb1Only()) {
8785 // This must be a constant between 0 and 255, for ADD
8786 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008787 if (CVal >= 0 && CVal <= 255)
8788 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008789 } else if (Subtarget->isThumb2()) {
8790 // A constant that can be used as an immediate value in a
8791 // data-processing instruction.
8792 if (ARM_AM::getT2SOImmVal(CVal) != -1)
8793 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008794 } else {
8795 // A constant that can be used as an immediate value in a
8796 // data-processing instruction.
8797 if (ARM_AM::getSOImmVal(CVal) != -1)
8798 break;
8799 }
8800 return;
8801
8802 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008803 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008804 // This must be a constant between -255 and -1, for negated ADD
8805 // immediates. This can be used in GCC with an "n" modifier that
8806 // prints the negated value, for use with SUB instructions. It is
8807 // not useful otherwise but is implemented for compatibility.
8808 if (CVal >= -255 && CVal <= -1)
8809 break;
8810 } else {
8811 // This must be a constant between -4095 and 4095. It is not clear
8812 // what this constraint is intended for. Implemented for
8813 // compatibility with GCC.
8814 if (CVal >= -4095 && CVal <= 4095)
8815 break;
8816 }
8817 return;
8818
8819 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008820 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008821 // A 32-bit value where only one byte has a nonzero value. Exclude
8822 // zero to match GCC. This constraint is used by GCC internally for
8823 // constants that can be loaded with a move/shift combination.
8824 // It is not useful otherwise but is implemented for compatibility.
8825 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
8826 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008827 } else if (Subtarget->isThumb2()) {
8828 // A constant whose bitwise inverse can be used as an immediate
8829 // value in a data-processing instruction. This can be used in GCC
8830 // with a "B" modifier that prints the inverted value, for use with
8831 // BIC and MVN instructions. It is not useful otherwise but is
8832 // implemented for compatibility.
8833 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
8834 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008835 } else {
8836 // A constant whose bitwise inverse can be used as an immediate
8837 // value in a data-processing instruction. This can be used in GCC
8838 // with a "B" modifier that prints the inverted value, for use with
8839 // BIC and MVN instructions. It is not useful otherwise but is
8840 // implemented for compatibility.
8841 if (ARM_AM::getSOImmVal(~CVal) != -1)
8842 break;
8843 }
8844 return;
8845
8846 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008847 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008848 // This must be a constant between -7 and 7,
8849 // for 3-operand ADD/SUB immediate instructions.
8850 if (CVal >= -7 && CVal < 7)
8851 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008852 } else if (Subtarget->isThumb2()) {
8853 // A constant whose negation can be used as an immediate value in a
8854 // data-processing instruction. This can be used in GCC with an "n"
8855 // modifier that prints the negated value, for use with SUB
8856 // instructions. It is not useful otherwise but is implemented for
8857 // compatibility.
8858 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
8859 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008860 } else {
8861 // A constant whose negation can be used as an immediate value in a
8862 // data-processing instruction. This can be used in GCC with an "n"
8863 // modifier that prints the negated value, for use with SUB
8864 // instructions. It is not useful otherwise but is implemented for
8865 // compatibility.
8866 if (ARM_AM::getSOImmVal(-CVal) != -1)
8867 break;
8868 }
8869 return;
8870
8871 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008872 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008873 // This must be a multiple of 4 between 0 and 1020, for
8874 // ADD sp + immediate.
8875 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
8876 break;
8877 } else {
8878 // A power of two or a constant between 0 and 32. This is used in
8879 // GCC for the shift amount on shifted register operands, but it is
8880 // useful in general for any shift amounts.
8881 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
8882 break;
8883 }
8884 return;
8885
8886 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008887 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008888 // This must be a constant between 0 and 31, for shift amounts.
8889 if (CVal >= 0 && CVal <= 31)
8890 break;
8891 }
8892 return;
8893
8894 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008895 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008896 // This must be a multiple of 4 between -508 and 508, for
8897 // ADD/SUB sp = sp + immediate.
8898 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
8899 break;
8900 }
8901 return;
8902 }
8903 Result = DAG.getTargetConstant(CVal, Op.getValueType());
8904 break;
8905 }
8906
8907 if (Result.getNode()) {
8908 Ops.push_back(Result);
8909 return;
8910 }
Dale Johannesen1784d162010-06-25 21:55:36 +00008911 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008912}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00008913
8914bool
8915ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
8916 // The ARM target isn't yet aware of offsets.
8917 return false;
8918}
Evan Cheng39382422009-10-28 01:44:26 +00008919
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008920bool ARM::isBitFieldInvertedMask(unsigned v) {
8921 if (v == 0xffffffff)
8922 return 0;
8923 // there can be 1's on either or both "outsides", all the "inside"
8924 // bits must be 0's
8925 unsigned int lsb = 0, msb = 31;
8926 while (v & (1 << msb)) --msb;
8927 while (v & (1 << lsb)) ++lsb;
8928 for (unsigned int i = lsb; i <= msb; ++i) {
8929 if (v & (1 << i))
8930 return 0;
8931 }
8932 return 1;
8933}
8934
Evan Cheng39382422009-10-28 01:44:26 +00008935/// isFPImmLegal - Returns true if the target can instruction select the
8936/// specified FP immediate natively. If false, the legalizer will
8937/// materialize the FP immediate as a load from a constant pool.
8938bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
8939 if (!Subtarget->hasVFP3())
8940 return false;
8941 if (VT == MVT::f32)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00008942 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00008943 if (VT == MVT::f64)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00008944 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00008945 return false;
8946}
Bob Wilson65ffec42010-09-21 17:56:22 +00008947
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008948/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00008949/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
8950/// specified in the intrinsic calls.
8951bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
8952 const CallInst &I,
8953 unsigned Intrinsic) const {
8954 switch (Intrinsic) {
8955 case Intrinsic::arm_neon_vld1:
8956 case Intrinsic::arm_neon_vld2:
8957 case Intrinsic::arm_neon_vld3:
8958 case Intrinsic::arm_neon_vld4:
8959 case Intrinsic::arm_neon_vld2lane:
8960 case Intrinsic::arm_neon_vld3lane:
8961 case Intrinsic::arm_neon_vld4lane: {
8962 Info.opc = ISD::INTRINSIC_W_CHAIN;
8963 // Conservatively set memVT to the entire set of vectors loaded.
8964 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
8965 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
8966 Info.ptrVal = I.getArgOperand(0);
8967 Info.offset = 0;
8968 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
8969 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
8970 Info.vol = false; // volatile loads with NEON intrinsics not supported
8971 Info.readMem = true;
8972 Info.writeMem = false;
8973 return true;
8974 }
8975 case Intrinsic::arm_neon_vst1:
8976 case Intrinsic::arm_neon_vst2:
8977 case Intrinsic::arm_neon_vst3:
8978 case Intrinsic::arm_neon_vst4:
8979 case Intrinsic::arm_neon_vst2lane:
8980 case Intrinsic::arm_neon_vst3lane:
8981 case Intrinsic::arm_neon_vst4lane: {
8982 Info.opc = ISD::INTRINSIC_VOID;
8983 // Conservatively set memVT to the entire set of vectors stored.
8984 unsigned NumElts = 0;
8985 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008986 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +00008987 if (!ArgTy->isVectorTy())
8988 break;
8989 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
8990 }
8991 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
8992 Info.ptrVal = I.getArgOperand(0);
8993 Info.offset = 0;
8994 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
8995 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
8996 Info.vol = false; // volatile stores with NEON intrinsics not supported
8997 Info.readMem = false;
8998 Info.writeMem = true;
8999 return true;
9000 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009001 case Intrinsic::arm_strexd: {
9002 Info.opc = ISD::INTRINSIC_W_CHAIN;
9003 Info.memVT = MVT::i64;
9004 Info.ptrVal = I.getArgOperand(2);
9005 Info.offset = 0;
9006 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009007 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009008 Info.readMem = false;
9009 Info.writeMem = true;
9010 return true;
9011 }
9012 case Intrinsic::arm_ldrexd: {
9013 Info.opc = ISD::INTRINSIC_W_CHAIN;
9014 Info.memVT = MVT::i64;
9015 Info.ptrVal = I.getArgOperand(0);
9016 Info.offset = 0;
9017 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009018 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009019 Info.readMem = true;
9020 Info.writeMem = false;
9021 return true;
9022 }
Bob Wilson65ffec42010-09-21 17:56:22 +00009023 default:
9024 break;
9025 }
9026
9027 return false;
9028}