blob: 9c7848397d2251c9962874871e778f17bed82ec7 [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");
389 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
390 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
391 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
392 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
393 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
394 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
395 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
396 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
397 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
398 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
399 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
400
401 // Integer division functions
402 // RTABI chapter 4.3.1
403 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
404 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
405 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
406 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
407 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
408 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
409 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
410 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
411 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
412 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS);
413 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000414 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS);
Renato Golin1ec11fb2011-05-22 21:41:23 +0000415
416 // Memory operations
417 // RTABI chapter 4.3.4
418 setLibcallName(RTLIB::MEMCPY, "__aeabi_memcpy");
419 setLibcallName(RTLIB::MEMMOVE, "__aeabi_memmove");
420 setLibcallName(RTLIB::MEMSET, "__aeabi_memset");
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000421 }
422
Bob Wilson2fef4572011-10-07 16:59:21 +0000423 // Use divmod compiler-rt calls for iOS 5.0 and later.
424 if (Subtarget->getTargetTriple().getOS() == Triple::IOS &&
425 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
426 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
427 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
428 }
429
David Goodwinf1daf7d2009-07-08 23:10:31 +0000430 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000431 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000432 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000433 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000434 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
435 !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000436 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000437 if (!Subtarget->isFPOnlySP())
438 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000439
Owen Anderson825b72b2009-08-11 20:47:22 +0000440 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000441 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000442
Eli Friedman9f1f26a2011-11-08 01:43:53 +0000443 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
444 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
445 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
446 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
447 setTruncStoreAction((MVT::SimpleValueType)VT,
448 (MVT::SimpleValueType)InnerVT, Expand);
449 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
450 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
451 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
452 }
453
Bob Wilson5bafff32009-06-22 23:27:02 +0000454 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000455 addDRTypeForNEON(MVT::v2f32);
456 addDRTypeForNEON(MVT::v8i8);
457 addDRTypeForNEON(MVT::v4i16);
458 addDRTypeForNEON(MVT::v2i32);
459 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000460
Owen Anderson825b72b2009-08-11 20:47:22 +0000461 addQRTypeForNEON(MVT::v4f32);
462 addQRTypeForNEON(MVT::v2f64);
463 addQRTypeForNEON(MVT::v16i8);
464 addQRTypeForNEON(MVT::v8i16);
465 addQRTypeForNEON(MVT::v4i32);
466 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000467
Bob Wilson74dc72e2009-09-15 23:55:57 +0000468 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
469 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000470 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
471 // supported for v4f32.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000472 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
473 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
474 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000475 // FIXME: Code duplication: FDIV and FREM are expanded always, see
476 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000477 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
478 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000479 // FIXME: Create unittest.
480 // In another words, find a way when "copysign" appears in DAG with vector
481 // operands.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000482 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000483 // FIXME: Code duplication: SETCC has custom operation action, see
484 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sands28b77e92011-09-06 19:07:46 +0000485 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000486 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000487 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
488 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
489 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
490 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
491 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
492 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
493 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
494 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
495 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
496 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
497 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
498 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000499 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000500 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
501 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
502 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
503 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
504 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000505
506 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
507 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
508 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
509 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
510 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
511 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
512 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
513 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
514 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
515 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Bob Wilson74dc72e2009-09-15 23:55:57 +0000516
Bob Wilson642b3292009-09-16 00:32:15 +0000517 // Neon does not support some operations on v1i64 and v2i64 types.
518 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000519 // Custom handling for some quad-vector types to detect VMULL.
520 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
521 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
522 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begeman7973f352011-02-11 20:53:29 +0000523 // Custom handling for some vector types to avoid expensive expansions
524 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
525 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
526 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
527 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000528 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
529 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000530 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
531 // a destination type that is wider than the source.
532 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
533 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000534
Bob Wilson1c3ef902011-02-07 17:43:21 +0000535 setTargetDAGCombine(ISD::INTRINSIC_VOID);
536 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000537 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
538 setTargetDAGCombine(ISD::SHL);
539 setTargetDAGCombine(ISD::SRL);
540 setTargetDAGCombine(ISD::SRA);
541 setTargetDAGCombine(ISD::SIGN_EXTEND);
542 setTargetDAGCombine(ISD::ZERO_EXTEND);
543 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000544 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000545 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000546 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000547 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
548 setTargetDAGCombine(ISD::STORE);
Chad Rosieref01edf2011-06-24 19:23:04 +0000549 setTargetDAGCombine(ISD::FP_TO_SINT);
550 setTargetDAGCombine(ISD::FP_TO_UINT);
551 setTargetDAGCombine(ISD::FDIV);
Nadav Rotem004a24b2011-10-15 20:03:12 +0000552
553 setLoadExtAction(ISD::EXTLOAD, MVT::v4i8, Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000554 }
555
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000556 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000557
558 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000559 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000560
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000561 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000562 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000563
Evan Chenga8e29892007-01-19 07:51:42 +0000564 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000565 if (!Subtarget->isThumb1Only()) {
566 for (unsigned im = (unsigned)ISD::PRE_INC;
567 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000568 setIndexedLoadAction(im, MVT::i1, Legal);
569 setIndexedLoadAction(im, MVT::i8, Legal);
570 setIndexedLoadAction(im, MVT::i16, Legal);
571 setIndexedLoadAction(im, MVT::i32, Legal);
572 setIndexedStoreAction(im, MVT::i1, Legal);
573 setIndexedStoreAction(im, MVT::i8, Legal);
574 setIndexedStoreAction(im, MVT::i16, Legal);
575 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000576 }
Evan Chenga8e29892007-01-19 07:51:42 +0000577 }
578
579 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000580 setOperationAction(ISD::MUL, MVT::i64, Expand);
581 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000582 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000583 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
584 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000585 }
Jim Grosbacha7603982011-07-01 21:12:19 +0000586 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
587 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopher2cc40132011-04-19 18:49:19 +0000588 setOperationAction(ISD::MULHS, MVT::i32, Expand);
589
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000590 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000591 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000592 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000593 setOperationAction(ISD::SRL, MVT::i64, Custom);
594 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000595
Evan Cheng342e3162011-08-30 01:34:54 +0000596 if (!Subtarget->isThumb1Only()) {
597 // FIXME: We should do this for Thumb1 as well.
598 setOperationAction(ISD::ADDC, MVT::i32, Custom);
599 setOperationAction(ISD::ADDE, MVT::i32, Custom);
600 setOperationAction(ISD::SUBC, MVT::i32, Custom);
601 setOperationAction(ISD::SUBE, MVT::i32, Custom);
602 }
603
Evan Chenga8e29892007-01-19 07:51:42 +0000604 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000605 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000606 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000607 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000608 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000609 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000610
Chandler Carruth63974b22011-12-13 01:56:10 +0000611 // These just redirect to CTTZ and CTLZ on ARM.
612 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
613 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
614
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000615 // Only ARMv6 has BSWAP.
616 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000617 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000618
Evan Chenga8e29892007-01-19 07:51:42 +0000619 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000620 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000621 // v7M has a hardware divider
622 setOperationAction(ISD::SDIV, MVT::i32, Expand);
623 setOperationAction(ISD::UDIV, MVT::i32, Expand);
624 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000625 setOperationAction(ISD::SREM, MVT::i32, Expand);
626 setOperationAction(ISD::UREM, MVT::i32, Expand);
627 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
628 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000629
Owen Anderson825b72b2009-08-11 20:47:22 +0000630 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
631 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
632 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
633 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000634 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000635
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000636 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000637
Evan Chenga8e29892007-01-19 07:51:42 +0000638 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000639 setOperationAction(ISD::VASTART, MVT::Other, Custom);
640 setOperationAction(ISD::VAARG, MVT::Other, Expand);
641 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
642 setOperationAction(ISD::VAEND, MVT::Other, Expand);
643 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
644 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000645 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000646 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
647 setExceptionPointerRegister(ARM::R0);
648 setExceptionSelectorRegister(ARM::R1);
649
Evan Cheng3a1588a2010-04-15 22:20:34 +0000650 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000651 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
652 // the default expansion.
Eli Friedman4db5aca2011-08-29 18:23:02 +0000653 // FIXME: This should be checking for v6k, not just v6.
Evan Cheng11db0682010-08-11 06:22:01 +0000654 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000655 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000656 // membarrier needs custom lowering; the rest are legal and handled
657 // normally.
658 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000659 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Eli Friedman2bdffe42011-08-31 00:31:29 +0000660 // Custom lowering for 64-bit ops
661 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
662 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
663 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
664 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
665 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
666 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Eli Friedman4d3f3292011-08-31 17:52:22 +0000667 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Eli Friedman26689ac2011-08-03 21:06:02 +0000668 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
669 setInsertFencesForAtomic(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000670 } else {
671 // Set them all for expansion, which will force libcalls.
672 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000673 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000674 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000675 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000676 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000677 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000678 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000679 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000680 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000681 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000682 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000683 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000684 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000685 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedman7cc15662011-09-15 22:18:49 +0000686 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
687 // Unordered/Monotonic case.
688 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
689 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000690 // Since the libcalls include locking, fold in the fences
691 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000692 }
Evan Chenga8e29892007-01-19 07:51:42 +0000693
Evan Cheng416941d2010-11-04 05:19:35 +0000694 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000695
Eli Friedmana2c6f452010-06-26 04:36:50 +0000696 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
697 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000698 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
699 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000700 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000701 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000702
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000703 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
704 !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000705 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
706 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000707 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000708 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
709 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000710
711 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000712 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000713 if (Subtarget->isTargetDarwin()) {
714 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
715 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000716 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000717 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000718
Owen Anderson825b72b2009-08-11 20:47:22 +0000719 setOperationAction(ISD::SETCC, MVT::i32, Expand);
720 setOperationAction(ISD::SETCC, MVT::f32, Expand);
721 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000722 setOperationAction(ISD::SELECT, MVT::i32, Custom);
723 setOperationAction(ISD::SELECT, MVT::f32, Custom);
724 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000725 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
726 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
727 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000728
Owen Anderson825b72b2009-08-11 20:47:22 +0000729 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
730 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
731 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
732 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
733 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000734
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000735 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000736 setOperationAction(ISD::FSIN, MVT::f64, Expand);
737 setOperationAction(ISD::FSIN, MVT::f32, Expand);
738 setOperationAction(ISD::FCOS, MVT::f32, Expand);
739 setOperationAction(ISD::FCOS, MVT::f64, Expand);
740 setOperationAction(ISD::FREM, MVT::f64, Expand);
741 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000742 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
743 !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000744 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
745 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000746 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000747 setOperationAction(ISD::FPOW, MVT::f64, Expand);
748 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000749
Cameron Zwarich33390842011-07-08 21:39:21 +0000750 setOperationAction(ISD::FMA, MVT::f64, Expand);
751 setOperationAction(ISD::FMA, MVT::f32, Expand);
752
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000753 // Various VFP goodness
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000754 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000755 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
756 if (Subtarget->hasVFP2()) {
757 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
758 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
759 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
760 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
761 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000762 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000763 if (!Subtarget->hasFP16()) {
764 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
765 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000766 }
Evan Cheng110cf482008-04-01 01:50:16 +0000767 }
Evan Chenga8e29892007-01-19 07:51:42 +0000768
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000769 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000770 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000771 setTargetDAGCombine(ISD::ADD);
772 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000773 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000774
Owen Anderson080c0922010-11-05 19:27:46 +0000775 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON())
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000776 setTargetDAGCombine(ISD::OR);
Owen Anderson080c0922010-11-05 19:27:46 +0000777 if (Subtarget->hasNEON())
778 setTargetDAGCombine(ISD::AND);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000779
Evan Chenga8e29892007-01-19 07:51:42 +0000780 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000781
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000782 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
783 !Subtarget->hasVFP2())
Evan Chengf7d87ee2010-05-21 00:43:17 +0000784 setSchedulingPreference(Sched::RegPressure);
785 else
786 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000787
Evan Cheng05219282011-01-06 06:52:41 +0000788 //// temporary - rewrite interface to use type
789 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Lang Hames75757f92011-10-26 20:56:52 +0000790 maxStoresPerMemset = 16;
791 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengf6799392010-06-26 01:52:05 +0000792
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000793 // On ARM arguments smaller than 4 bytes are extended, so all arguments
794 // are at least 4 bytes aligned.
795 setMinStackArgumentAlignment(4);
796
Evan Chengfff606d2010-09-24 19:07:23 +0000797 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000798
799 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000800}
801
Andrew Trick32cec0a2011-01-19 02:35:27 +0000802// FIXME: It might make sense to define the representative register class as the
803// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
804// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
805// SPR's representative would be DPR_VFP2. This should work well if register
806// pressure tracking were modified such that a register use would increment the
807// pressure of the register class's representative and all of it's super
808// classes' representatives transitively. We have not implemented this because
809// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000810// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000811// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000812std::pair<const TargetRegisterClass*, uint8_t>
813ARMTargetLowering::findRepresentativeClass(EVT VT) const{
814 const TargetRegisterClass *RRC = 0;
815 uint8_t Cost = 1;
816 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000817 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000818 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000819 // Use DPR as representative register class for all floating point
820 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
821 // the cost is 1 for both f32 and f64.
822 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000823 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000824 RRC = ARM::DPRRegisterClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000825 // When NEON is used for SP, only half of the register file is available
826 // because operations that define both SP and DP results will be constrained
827 // to the VFP2 class (D0-D15). We currently model this constraint prior to
828 // coalescing by double-counting the SP regs. See the FIXME above.
829 if (Subtarget->useNEONForSinglePrecisionFP())
830 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000831 break;
832 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
833 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000834 RRC = ARM::DPRRegisterClass;
835 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000836 break;
837 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000838 RRC = ARM::DPRRegisterClass;
839 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000840 break;
841 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000842 RRC = ARM::DPRRegisterClass;
843 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000844 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000845 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000846 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000847}
848
Evan Chenga8e29892007-01-19 07:51:42 +0000849const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
850 switch (Opcode) {
851 default: return 0;
852 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000853 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000854 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000855 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
856 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000857 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000858 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
859 case ARMISD::tCALL: return "ARMISD::tCALL";
860 case ARMISD::BRCOND: return "ARMISD::BRCOND";
861 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000862 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000863 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
864 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
865 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000866 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000867 case ARMISD::CMPFP: return "ARMISD::CMPFP";
868 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000869 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000870 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
871 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000872
Jim Grosbach3482c802010-01-18 19:58:49 +0000873 case ARMISD::RBIT: return "ARMISD::RBIT";
874
Bob Wilson76a312b2010-03-19 22:51:32 +0000875 case ARMISD::FTOSI: return "ARMISD::FTOSI";
876 case ARMISD::FTOUI: return "ARMISD::FTOUI";
877 case ARMISD::SITOF: return "ARMISD::SITOF";
878 case ARMISD::UITOF: return "ARMISD::UITOF";
879
Evan Chenga8e29892007-01-19 07:51:42 +0000880 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
881 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
882 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000883
Evan Cheng342e3162011-08-30 01:34:54 +0000884 case ARMISD::ADDC: return "ARMISD::ADDC";
885 case ARMISD::ADDE: return "ARMISD::ADDE";
886 case ARMISD::SUBC: return "ARMISD::SUBC";
887 case ARMISD::SUBE: return "ARMISD::SUBE";
888
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000889 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
890 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000891
Evan Chengc5942082009-10-28 06:55:03 +0000892 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
893 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
894
Dale Johannesen51e28e62010-06-03 21:09:53 +0000895 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000896
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000897 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000898
Evan Cheng86198642009-08-07 00:34:42 +0000899 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
900
Jim Grosbach3728e962009-12-10 00:11:09 +0000901 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000902 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000903
Evan Chengdfed19f2010-11-03 06:34:55 +0000904 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
905
Bob Wilson5bafff32009-06-22 23:27:02 +0000906 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000907 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000908 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000909 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
910 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000911 case ARMISD::VCGEU: return "ARMISD::VCGEU";
912 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000913 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
914 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000915 case ARMISD::VCGTU: return "ARMISD::VCGTU";
916 case ARMISD::VTST: return "ARMISD::VTST";
917
918 case ARMISD::VSHL: return "ARMISD::VSHL";
919 case ARMISD::VSHRs: return "ARMISD::VSHRs";
920 case ARMISD::VSHRu: return "ARMISD::VSHRu";
921 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
922 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
923 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
924 case ARMISD::VSHRN: return "ARMISD::VSHRN";
925 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
926 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
927 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
928 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
929 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
930 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
931 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
932 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
933 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
934 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
935 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
936 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
937 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
938 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000939 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000940 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Chengeaa192a2011-11-15 02:12:34 +0000941 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000942 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000943 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000944 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000945 case ARMISD::VREV64: return "ARMISD::VREV64";
946 case ARMISD::VREV32: return "ARMISD::VREV32";
947 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000948 case ARMISD::VZIP: return "ARMISD::VZIP";
949 case ARMISD::VUZP: return "ARMISD::VUZP";
950 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000951 case ARMISD::VTBL1: return "ARMISD::VTBL1";
952 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000953 case ARMISD::VMULLs: return "ARMISD::VMULLs";
954 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000955 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000956 case ARMISD::FMAX: return "ARMISD::FMAX";
957 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000958 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000959 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
960 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000961 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000962 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
963 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
964 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +0000965 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
966 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
967 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
968 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
969 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
970 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
971 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
972 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
973 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
974 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
975 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
976 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
977 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
978 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
979 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
980 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
981 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +0000982 }
983}
984
Duncan Sands28b77e92011-09-06 19:07:46 +0000985EVT ARMTargetLowering::getSetCCResultType(EVT VT) const {
986 if (!VT.isVector()) return getPointerTy();
987 return VT.changeVectorElementTypeToInteger();
988}
989
Evan Cheng06b666c2010-05-15 02:18:07 +0000990/// getRegClassFor - Return the register class that should be used for the
991/// specified value type.
992TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
993 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
994 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
995 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000996 if (Subtarget->hasNEON()) {
997 if (VT == MVT::v4i64)
998 return ARM::QQPRRegisterClass;
999 else if (VT == MVT::v8i64)
1000 return ARM::QQQQPRRegisterClass;
1001 }
Evan Cheng06b666c2010-05-15 02:18:07 +00001002 return TargetLowering::getRegClassFor(VT);
1003}
1004
Eric Christopherab695882010-07-21 22:26:11 +00001005// Create a fast isel object.
1006FastISel *
1007ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
1008 return ARM::createFastISel(funcInfo);
1009}
1010
Anton Korobeynikovcec36f42010-07-24 21:52:08 +00001011/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1012/// be used for loads / stores from the global.
1013unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1014 return (Subtarget->isThumb1Only() ? 127 : 4095);
1015}
1016
Evan Cheng1cc39842010-05-20 23:26:43 +00001017Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +00001018 unsigned NumVals = N->getNumValues();
1019 if (!NumVals)
1020 return Sched::RegPressure;
1021
1022 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +00001023 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001024 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +00001025 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +00001026 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman692c1d82011-10-24 17:55:11 +00001027 return Sched::ILP;
Evan Cheng1cc39842010-05-20 23:26:43 +00001028 }
Evan Chengc10f5432010-05-28 23:25:23 +00001029
1030 if (!N->isMachineOpcode())
1031 return Sched::RegPressure;
1032
1033 // Load are scheduled for latency even if there instruction itinerary
1034 // is not available.
1035 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +00001036 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +00001037
Evan Chenge837dea2011-06-28 19:10:37 +00001038 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +00001039 return Sched::RegPressure;
1040 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +00001041 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman692c1d82011-10-24 17:55:11 +00001042 return Sched::ILP;
Evan Chengc10f5432010-05-28 23:25:23 +00001043
Evan Cheng1cc39842010-05-20 23:26:43 +00001044 return Sched::RegPressure;
1045}
1046
Evan Chenga8e29892007-01-19 07:51:42 +00001047//===----------------------------------------------------------------------===//
1048// Lowering Code
1049//===----------------------------------------------------------------------===//
1050
Evan Chenga8e29892007-01-19 07:51:42 +00001051/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1052static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1053 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001054 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +00001055 case ISD::SETNE: return ARMCC::NE;
1056 case ISD::SETEQ: return ARMCC::EQ;
1057 case ISD::SETGT: return ARMCC::GT;
1058 case ISD::SETGE: return ARMCC::GE;
1059 case ISD::SETLT: return ARMCC::LT;
1060 case ISD::SETLE: return ARMCC::LE;
1061 case ISD::SETUGT: return ARMCC::HI;
1062 case ISD::SETUGE: return ARMCC::HS;
1063 case ISD::SETULT: return ARMCC::LO;
1064 case ISD::SETULE: return ARMCC::LS;
1065 }
1066}
1067
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001068/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1069static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001070 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001071 CondCode2 = ARMCC::AL;
1072 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001073 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001074 case ISD::SETEQ:
1075 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1076 case ISD::SETGT:
1077 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1078 case ISD::SETGE:
1079 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1080 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001081 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001082 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1083 case ISD::SETO: CondCode = ARMCC::VC; break;
1084 case ISD::SETUO: CondCode = ARMCC::VS; break;
1085 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1086 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1087 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1088 case ISD::SETLT:
1089 case ISD::SETULT: CondCode = ARMCC::LT; break;
1090 case ISD::SETLE:
1091 case ISD::SETULE: CondCode = ARMCC::LE; break;
1092 case ISD::SETNE:
1093 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1094 }
Evan Chenga8e29892007-01-19 07:51:42 +00001095}
1096
Bob Wilson1f595bb2009-04-17 19:07:39 +00001097//===----------------------------------------------------------------------===//
1098// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001099//===----------------------------------------------------------------------===//
1100
1101#include "ARMGenCallingConv.inc"
1102
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001103/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1104/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001105CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001106 bool Return,
1107 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001108 switch (CC) {
1109 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001110 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001111 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001112 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001113 if (!Subtarget->isAAPCS_ABI())
1114 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1115 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1116 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1117 }
1118 // Fallthrough
1119 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001120 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001121 if (!Subtarget->isAAPCS_ABI())
1122 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1123 else if (Subtarget->hasVFP2() &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001124 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1125 !isVarArg)
Evan Cheng76f920d2010-10-22 18:23:05 +00001126 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1127 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1128 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001129 case CallingConv::ARM_AAPCS_VFP:
Evan Cheng76f920d2010-10-22 18:23:05 +00001130 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001131 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001132 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001133 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001134 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001135 }
1136}
1137
Dan Gohman98ca4f22009-08-05 01:29:28 +00001138/// LowerCallResult - Lower the result values of a call into the
1139/// appropriate copies out of appropriate physical registers.
1140SDValue
1141ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001142 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001143 const SmallVectorImpl<ISD::InputArg> &Ins,
1144 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001145 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001146
Bob Wilson1f595bb2009-04-17 19:07:39 +00001147 // Assign locations to each value returned by this call.
1148 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001149 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1150 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001151 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001152 CCAssignFnForNode(CallConv, /* Return*/ true,
1153 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001154
1155 // Copy all of the result registers out of their specified physreg.
1156 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1157 CCValAssign VA = RVLocs[i];
1158
Bob Wilson80915242009-04-25 00:33:20 +00001159 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001160 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001161 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001162 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001163 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001164 Chain = Lo.getValue(1);
1165 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001166 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001167 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001168 InFlag);
1169 Chain = Hi.getValue(1);
1170 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001171 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001172
Owen Anderson825b72b2009-08-11 20:47:22 +00001173 if (VA.getLocVT() == MVT::v2f64) {
1174 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1175 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1176 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001177
1178 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001179 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001180 Chain = Lo.getValue(1);
1181 InFlag = Lo.getValue(2);
1182 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001183 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001184 Chain = Hi.getValue(1);
1185 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001186 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001187 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1188 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001189 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001190 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001191 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1192 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001193 Chain = Val.getValue(1);
1194 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001195 }
Bob Wilson80915242009-04-25 00:33:20 +00001196
1197 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001198 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001199 case CCValAssign::Full: break;
1200 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001201 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001202 break;
1203 }
1204
Dan Gohman98ca4f22009-08-05 01:29:28 +00001205 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001206 }
1207
Dan Gohman98ca4f22009-08-05 01:29:28 +00001208 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001209}
1210
Bob Wilsondee46d72009-04-17 20:35:10 +00001211/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001212SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001213ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1214 SDValue StackPtr, SDValue Arg,
1215 DebugLoc dl, SelectionDAG &DAG,
1216 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001217 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001218 unsigned LocMemOffset = VA.getLocMemOffset();
1219 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1220 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001221 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001222 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001223 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001224}
1225
Dan Gohman98ca4f22009-08-05 01:29:28 +00001226void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001227 SDValue Chain, SDValue &Arg,
1228 RegsToPassVector &RegsToPass,
1229 CCValAssign &VA, CCValAssign &NextVA,
1230 SDValue &StackPtr,
1231 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001232 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001233
Jim Grosbache5165492009-11-09 00:11:35 +00001234 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001235 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001236 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1237
1238 if (NextVA.isRegLoc())
1239 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1240 else {
1241 assert(NextVA.isMemLoc());
1242 if (StackPtr.getNode() == 0)
1243 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1244
Dan Gohman98ca4f22009-08-05 01:29:28 +00001245 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1246 dl, DAG, NextVA,
1247 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001248 }
1249}
1250
Dan Gohman98ca4f22009-08-05 01:29:28 +00001251/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001252/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1253/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001254SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001255ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001256 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001257 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001258 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001259 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001260 const SmallVectorImpl<ISD::InputArg> &Ins,
1261 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001262 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001263 MachineFunction &MF = DAG.getMachineFunction();
1264 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1265 bool IsSibCall = false;
Bob Wilson6d2f9ce2011-10-07 17:17:49 +00001266 // Disable tail calls if they're not supported.
1267 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Bob Wilson703af3a2010-08-13 22:43:33 +00001268 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001269 if (isTailCall) {
1270 // Check if it's really possible to do a tail call.
1271 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1272 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001273 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001274 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1275 // detected sibcalls.
1276 if (isTailCall) {
1277 ++NumTailCalls;
1278 IsSibCall = true;
1279 }
1280 }
Evan Chenga8e29892007-01-19 07:51:42 +00001281
Bob Wilson1f595bb2009-04-17 19:07:39 +00001282 // Analyze operands of the call, assigning locations to each operand.
1283 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001284 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1285 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001286 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001287 CCAssignFnForNode(CallConv, /* Return*/ false,
1288 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001289
Bob Wilson1f595bb2009-04-17 19:07:39 +00001290 // Get a count of how many bytes are to be pushed on the stack.
1291 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001292
Dale Johannesen51e28e62010-06-03 21:09:53 +00001293 // For tail calls, memory operands are available in our caller's stack.
1294 if (IsSibCall)
1295 NumBytes = 0;
1296
Evan Chenga8e29892007-01-19 07:51:42 +00001297 // Adjust the stack pointer for the new arguments...
1298 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001299 if (!IsSibCall)
1300 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001301
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001302 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001303
Bob Wilson5bafff32009-06-22 23:27:02 +00001304 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001305 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001306
Bob Wilson1f595bb2009-04-17 19:07:39 +00001307 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001308 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001309 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1310 i != e;
1311 ++i, ++realArgIdx) {
1312 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001313 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001314 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001315 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001316
Bob Wilson1f595bb2009-04-17 19:07:39 +00001317 // Promote the value if needed.
1318 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001319 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001320 case CCValAssign::Full: break;
1321 case CCValAssign::SExt:
1322 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1323 break;
1324 case CCValAssign::ZExt:
1325 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1326 break;
1327 case CCValAssign::AExt:
1328 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1329 break;
1330 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001331 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001332 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001333 }
1334
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001335 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001336 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001337 if (VA.getLocVT() == MVT::v2f64) {
1338 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1339 DAG.getConstant(0, MVT::i32));
1340 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1341 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001342
Dan Gohman98ca4f22009-08-05 01:29:28 +00001343 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001344 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1345
1346 VA = ArgLocs[++i]; // skip ahead to next loc
1347 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001348 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001349 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1350 } else {
1351 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001352
Dan Gohman98ca4f22009-08-05 01:29:28 +00001353 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1354 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001355 }
1356 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001357 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001358 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001359 }
1360 } else if (VA.isRegLoc()) {
1361 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001362 } else if (isByVal) {
1363 assert(VA.isMemLoc());
1364 unsigned offset = 0;
1365
1366 // True if this byval aggregate will be split between registers
1367 // and memory.
1368 if (CCInfo.isFirstByValRegValid()) {
1369 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1370 unsigned int i, j;
1371 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1372 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1373 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1374 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1375 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001376 false, false, false, 0);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001377 MemOpChains.push_back(Load.getValue(1));
1378 RegsToPass.push_back(std::make_pair(j, Load));
1379 }
1380 offset = ARM::R4 - CCInfo.getFirstByValReg();
1381 CCInfo.clearFirstByValReg();
1382 }
1383
1384 unsigned LocMemOffset = VA.getLocMemOffset();
1385 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1386 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1387 StkPtrOff);
1388 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1389 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1390 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1391 MVT::i32);
1392 MemOpChains.push_back(DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode,
1393 Flags.getByValAlign(),
1394 /*isVolatile=*/false,
Dan Gohman65fd6562011-11-03 21:49:52 +00001395 /*AlwaysInline=*/false,
Stuart Hastingsc7315872011-04-20 16:47:52 +00001396 MachinePointerInfo(0),
1397 MachinePointerInfo(0)));
1398
1399 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001400 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001401
Dan Gohman98ca4f22009-08-05 01:29:28 +00001402 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1403 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001404 }
Evan Chenga8e29892007-01-19 07:51:42 +00001405 }
1406
1407 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001408 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001409 &MemOpChains[0], MemOpChains.size());
1410
1411 // Build a sequence of copy-to-reg nodes chained together with token chain
1412 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001413 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001414 // Tail call byval lowering might overwrite argument registers so in case of
1415 // tail call optimization the copies to registers are lowered later.
1416 if (!isTailCall)
1417 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1418 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1419 RegsToPass[i].second, InFlag);
1420 InFlag = Chain.getValue(1);
1421 }
Evan Chenga8e29892007-01-19 07:51:42 +00001422
Dale Johannesen51e28e62010-06-03 21:09:53 +00001423 // For tail calls lower the arguments to the 'real' stack slot.
1424 if (isTailCall) {
1425 // Force all the incoming stack arguments to be loaded from the stack
1426 // before any new outgoing arguments are stored to the stack, because the
1427 // outgoing stack slots may alias the incoming argument stack slots, and
1428 // the alias isn't otherwise explicit. This is slightly more conservative
1429 // than necessary, because it means that each store effectively depends
1430 // on every argument instead of just those arguments it would clobber.
1431
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001432 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001433 InFlag = SDValue();
1434 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1435 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1436 RegsToPass[i].second, InFlag);
1437 InFlag = Chain.getValue(1);
1438 }
1439 InFlag =SDValue();
1440 }
1441
Bill Wendling056292f2008-09-16 21:48:12 +00001442 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1443 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1444 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001445 bool isDirect = false;
1446 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001447 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001448 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001449
1450 if (EnableARMLongCalls) {
1451 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1452 && "long-calls with non-static relocation model!");
1453 // Handle a global address or an external symbol. If it's not one of
1454 // those, the target's already in a register, so we don't need to do
1455 // anything extra.
1456 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001457 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001458 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001459 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001460 ARMConstantPoolValue *CPV =
1461 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1462
Jim Grosbache7b52522010-04-14 22:28:31 +00001463 // Get the address of the callee into a register
1464 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1465 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1466 Callee = DAG.getLoad(getPointerTy(), dl,
1467 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001468 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001469 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001470 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1471 const char *Sym = S->getSymbol();
1472
1473 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001474 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001475 ARMConstantPoolValue *CPV =
1476 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1477 ARMPCLabelIndex, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001478 // Get the address of the callee into a register
1479 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1480 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1481 Callee = DAG.getLoad(getPointerTy(), dl,
1482 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001483 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001484 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001485 }
1486 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001487 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001488 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001489 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001490 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001491 getTargetMachine().getRelocationModel() != Reloc::Static;
1492 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001493 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001494 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001495 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001496 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001497 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001498 ARMConstantPoolValue *CPV =
1499 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001500 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001501 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001502 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001503 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001504 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001505 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001506 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001507 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001508 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001509 } else {
1510 // On ELF targets for PIC code, direct calls should go through the PLT
1511 unsigned OpFlags = 0;
1512 if (Subtarget->isTargetELF() &&
1513 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1514 OpFlags = ARMII::MO_PLT;
1515 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1516 }
Bill Wendling056292f2008-09-16 21:48:12 +00001517 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001518 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001519 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001520 getTargetMachine().getRelocationModel() != Reloc::Static;
1521 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001522 // tBX takes a register source operand.
1523 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001524 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001525 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001526 ARMConstantPoolValue *CPV =
1527 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1528 ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001529 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001530 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001531 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001532 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001533 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001534 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001535 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001536 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001537 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001538 } else {
1539 unsigned OpFlags = 0;
1540 // On ELF targets for PIC code, direct calls should go through the PLT
1541 if (Subtarget->isTargetELF() &&
1542 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1543 OpFlags = ARMII::MO_PLT;
1544 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1545 }
Evan Chenga8e29892007-01-19 07:51:42 +00001546 }
1547
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001548 // FIXME: handle tail calls differently.
1549 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001550 if (Subtarget->isThumb()) {
1551 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001552 CallOpc = ARMISD::CALL_NOLINK;
1553 else
1554 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1555 } else {
1556 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001557 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1558 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001559 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001560
Dan Gohman475871a2008-07-27 21:46:04 +00001561 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001562 Ops.push_back(Chain);
1563 Ops.push_back(Callee);
1564
1565 // Add argument registers to the end of the list so that they are known live
1566 // into the call.
1567 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1568 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1569 RegsToPass[i].second.getValueType()));
1570
Gabor Greifba36cb52008-08-28 21:40:38 +00001571 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001572 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001573
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001574 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001575 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001576 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001577
Duncan Sands4bdcb612008-07-02 17:40:58 +00001578 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001579 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001580 InFlag = Chain.getValue(1);
1581
Chris Lattnere563bbc2008-10-11 22:08:30 +00001582 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1583 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001584 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001585 InFlag = Chain.getValue(1);
1586
Bob Wilson1f595bb2009-04-17 19:07:39 +00001587 // Handle result values, copying them out of physregs into vregs that we
1588 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001589 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1590 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001591}
1592
Stuart Hastingsf222e592011-02-28 17:17:53 +00001593/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001594/// on the stack. Remember the next parameter register to allocate,
1595/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001596/// this.
1597void
Stuart Hastingsc7315872011-04-20 16:47:52 +00001598llvm::ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
1599 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1600 assert((State->getCallOrPrologue() == Prologue ||
1601 State->getCallOrPrologue() == Call) &&
1602 "unhandled ParmContext");
1603 if ((!State->isFirstByValRegValid()) &&
1604 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1605 State->setFirstByValReg(reg);
1606 // At a call site, a byval parameter that is split between
1607 // registers and memory needs its size truncated here. In a
1608 // function prologue, such byval parameters are reassembled in
1609 // memory, and are not truncated.
1610 if (State->getCallOrPrologue() == Call) {
1611 unsigned excess = 4 * (ARM::R4 - reg);
1612 assert(size >= excess && "expected larger existing stack allocation");
1613 size -= excess;
1614 }
1615 }
1616 // Confiscate any remaining parameter registers to preclude their
1617 // assignment to subsequent parameters.
1618 while (State->AllocateReg(GPRArgRegs, 4))
1619 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001620}
1621
Dale Johannesen51e28e62010-06-03 21:09:53 +00001622/// MatchingStackOffset - Return true if the given stack call argument is
1623/// already available in the same position (relatively) of the caller's
1624/// incoming argument stack.
1625static
1626bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1627 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1628 const ARMInstrInfo *TII) {
1629 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1630 int FI = INT_MAX;
1631 if (Arg.getOpcode() == ISD::CopyFromReg) {
1632 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001633 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001634 return false;
1635 MachineInstr *Def = MRI->getVRegDef(VR);
1636 if (!Def)
1637 return false;
1638 if (!Flags.isByVal()) {
1639 if (!TII->isLoadFromStackSlot(Def, FI))
1640 return false;
1641 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001642 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001643 }
1644 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1645 if (Flags.isByVal())
1646 // ByVal argument is passed in as a pointer but it's now being
1647 // dereferenced. e.g.
1648 // define @foo(%struct.X* %A) {
1649 // tail call @bar(%struct.X* byval %A)
1650 // }
1651 return false;
1652 SDValue Ptr = Ld->getBasePtr();
1653 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1654 if (!FINode)
1655 return false;
1656 FI = FINode->getIndex();
1657 } else
1658 return false;
1659
1660 assert(FI != INT_MAX);
1661 if (!MFI->isFixedObjectIndex(FI))
1662 return false;
1663 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1664}
1665
1666/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1667/// for tail call optimization. Targets which want to do tail call
1668/// optimization should implement this function.
1669bool
1670ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1671 CallingConv::ID CalleeCC,
1672 bool isVarArg,
1673 bool isCalleeStructRet,
1674 bool isCallerStructRet,
1675 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001676 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001677 const SmallVectorImpl<ISD::InputArg> &Ins,
1678 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001679 const Function *CallerF = DAG.getMachineFunction().getFunction();
1680 CallingConv::ID CallerCC = CallerF->getCallingConv();
1681 bool CCMatch = CallerCC == CalleeCC;
1682
1683 // Look for obvious safe cases to perform tail call optimization that do not
1684 // require ABI changes. This is what gcc calls sibcall.
1685
Jim Grosbach7616b642010-06-16 23:45:49 +00001686 // Do not sibcall optimize vararg calls unless the call site is not passing
1687 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001688 if (isVarArg && !Outs.empty())
1689 return false;
1690
1691 // Also avoid sibcall optimization if either caller or callee uses struct
1692 // return semantics.
1693 if (isCalleeStructRet || isCallerStructRet)
1694 return false;
1695
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001696 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001697 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1698 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1699 // support in the assembler and linker to be used. This would need to be
1700 // fixed to fully support tail calls in Thumb1.
1701 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001702 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1703 // LR. This means if we need to reload LR, it takes an extra instructions,
1704 // which outweighs the value of the tail call; but here we don't know yet
1705 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001706 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001707 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001708
1709 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1710 // but we need to make sure there are enough registers; the only valid
1711 // registers are the 4 used for parameters. We don't currently do this
1712 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001713 if (Subtarget->isThumb1Only())
1714 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001715
Dale Johannesen51e28e62010-06-03 21:09:53 +00001716 // If the calling conventions do not match, then we'd better make sure the
1717 // results are returned in the same way as what the caller expects.
1718 if (!CCMatch) {
1719 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001720 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1721 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001722 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1723
1724 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001725 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1726 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001727 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1728
1729 if (RVLocs1.size() != RVLocs2.size())
1730 return false;
1731 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1732 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1733 return false;
1734 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1735 return false;
1736 if (RVLocs1[i].isRegLoc()) {
1737 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1738 return false;
1739 } else {
1740 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1741 return false;
1742 }
1743 }
1744 }
1745
1746 // If the callee takes no arguments then go on to check the results of the
1747 // call.
1748 if (!Outs.empty()) {
1749 // Check if stack adjustment is needed. For now, do not do this if any
1750 // argument is passed on the stack.
1751 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001752 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1753 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001754 CCInfo.AnalyzeCallOperands(Outs,
1755 CCAssignFnForNode(CalleeCC, false, isVarArg));
1756 if (CCInfo.getNextStackOffset()) {
1757 MachineFunction &MF = DAG.getMachineFunction();
1758
1759 // Check if the arguments are already laid out in the right way as
1760 // the caller's fixed stack objects.
1761 MachineFrameInfo *MFI = MF.getFrameInfo();
1762 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1763 const ARMInstrInfo *TII =
1764 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001765 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1766 i != e;
1767 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001768 CCValAssign &VA = ArgLocs[i];
1769 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001770 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001771 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001772 if (VA.getLocInfo() == CCValAssign::Indirect)
1773 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001774 if (VA.needsCustom()) {
1775 // f64 and vector types are split into multiple registers or
1776 // register/stack-slot combinations. The types will not match
1777 // the registers; give up on memory f64 refs until we figure
1778 // out what to do about this.
1779 if (!VA.isRegLoc())
1780 return false;
1781 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001782 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001783 if (RegVT == MVT::v2f64) {
1784 if (!ArgLocs[++i].isRegLoc())
1785 return false;
1786 if (!ArgLocs[++i].isRegLoc())
1787 return false;
1788 }
1789 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001790 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1791 MFI, MRI, TII))
1792 return false;
1793 }
1794 }
1795 }
1796 }
1797
1798 return true;
1799}
1800
Dan Gohman98ca4f22009-08-05 01:29:28 +00001801SDValue
1802ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001803 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001804 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001805 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001806 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001807
Bob Wilsondee46d72009-04-17 20:35:10 +00001808 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001809 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001810
Bob Wilsondee46d72009-04-17 20:35:10 +00001811 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001812 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1813 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001814
Dan Gohman98ca4f22009-08-05 01:29:28 +00001815 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001816 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1817 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001818
1819 // If this is the first return lowered for this function, add
1820 // the regs to the liveout set for the function.
1821 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1822 for (unsigned i = 0; i != RVLocs.size(); ++i)
1823 if (RVLocs[i].isRegLoc())
1824 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001825 }
1826
Bob Wilson1f595bb2009-04-17 19:07:39 +00001827 SDValue Flag;
1828
1829 // Copy the result values into the output registers.
1830 for (unsigned i = 0, realRVLocIdx = 0;
1831 i != RVLocs.size();
1832 ++i, ++realRVLocIdx) {
1833 CCValAssign &VA = RVLocs[i];
1834 assert(VA.isRegLoc() && "Can only return in registers!");
1835
Dan Gohmanc9403652010-07-07 15:54:55 +00001836 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001837
1838 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001839 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001840 case CCValAssign::Full: break;
1841 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001842 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001843 break;
1844 }
1845
Bob Wilson1f595bb2009-04-17 19:07:39 +00001846 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001847 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001848 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001849 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1850 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001851 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001852 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001853
1854 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1855 Flag = Chain.getValue(1);
1856 VA = RVLocs[++i]; // skip ahead to next loc
1857 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1858 HalfGPRs.getValue(1), Flag);
1859 Flag = Chain.getValue(1);
1860 VA = RVLocs[++i]; // skip ahead to next loc
1861
1862 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001863 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1864 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001865 }
1866 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1867 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001868 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001869 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001870 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001871 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001872 VA = RVLocs[++i]; // skip ahead to next loc
1873 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1874 Flag);
1875 } else
1876 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1877
Bob Wilsondee46d72009-04-17 20:35:10 +00001878 // Guarantee that all emitted copies are
1879 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001880 Flag = Chain.getValue(1);
1881 }
1882
1883 SDValue result;
1884 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001885 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001886 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001887 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001888
1889 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001890}
1891
Evan Cheng3d2125c2010-11-30 23:55:39 +00001892bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1893 if (N->getNumValues() != 1)
1894 return false;
1895 if (!N->hasNUsesOfValue(1, 0))
1896 return false;
1897
1898 unsigned NumCopies = 0;
1899 SDNode* Copies[2];
1900 SDNode *Use = *N->use_begin();
1901 if (Use->getOpcode() == ISD::CopyToReg) {
1902 Copies[NumCopies++] = Use;
1903 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1904 // f64 returned in a pair of GPRs.
1905 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1906 UI != UE; ++UI) {
1907 if (UI->getOpcode() != ISD::CopyToReg)
1908 return false;
1909 Copies[UI.getUse().getResNo()] = *UI;
1910 ++NumCopies;
1911 }
1912 } else if (Use->getOpcode() == ISD::BITCAST) {
1913 // f32 returned in a single GPR.
1914 if (!Use->hasNUsesOfValue(1, 0))
1915 return false;
1916 Use = *Use->use_begin();
1917 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1918 return false;
1919 Copies[NumCopies++] = Use;
1920 } else {
1921 return false;
1922 }
1923
1924 if (NumCopies != 1 && NumCopies != 2)
1925 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001926
1927 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001928 for (unsigned i = 0; i < NumCopies; ++i) {
1929 SDNode *Copy = Copies[i];
1930 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1931 UI != UE; ++UI) {
1932 if (UI->getOpcode() == ISD::CopyToReg) {
1933 SDNode *Use = *UI;
1934 if (Use == Copies[0] || Use == Copies[1])
1935 continue;
1936 return false;
1937 }
1938 if (UI->getOpcode() != ARMISD::RET_FLAG)
1939 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001940 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001941 }
1942 }
1943
Evan Cheng1bf891a2010-12-01 22:59:46 +00001944 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001945}
1946
Evan Cheng485fafc2011-03-21 01:19:09 +00001947bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1948 if (!EnableARMTailCalls)
1949 return false;
1950
1951 if (!CI->isTailCall())
1952 return false;
1953
1954 return !Subtarget->isThumb1Only();
1955}
1956
Bob Wilsonb62d2572009-11-03 00:02:05 +00001957// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1958// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1959// one of the above mentioned nodes. It has to be wrapped because otherwise
1960// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1961// be used to form addressing mode. These wrapped nodes will be selected
1962// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001963static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001964 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001965 // FIXME there is no actual debug info here
1966 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001967 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001968 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001969 if (CP->isMachineConstantPoolEntry())
1970 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1971 CP->getAlignment());
1972 else
1973 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1974 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001975 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001976}
1977
Jim Grosbache1102ca2010-07-19 17:20:38 +00001978unsigned ARMTargetLowering::getJumpTableEncoding() const {
1979 return MachineJumpTableInfo::EK_Inline;
1980}
1981
Dan Gohmand858e902010-04-17 15:26:15 +00001982SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1983 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001984 MachineFunction &MF = DAG.getMachineFunction();
1985 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1986 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001987 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001988 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001989 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001990 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1991 SDValue CPAddr;
1992 if (RelocM == Reloc::Static) {
1993 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1994 } else {
1995 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001996 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001997 ARMConstantPoolValue *CPV =
1998 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
1999 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson907eebd2009-11-02 20:59:23 +00002000 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2001 }
2002 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2003 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002004 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002005 false, false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00002006 if (RelocM == Reloc::Static)
2007 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00002008 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00002009 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00002010}
2011
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002012// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00002013SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002014ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002015 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002016 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002017 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002018 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00002019 MachineFunction &MF = DAG.getMachineFunction();
2020 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002021 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002022 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002023 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2024 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002025 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002026 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00002027 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002028 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002029 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002030 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002031
Evan Chenge7e0d622009-11-06 22:24:13 +00002032 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002033 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002034
2035 // call __tls_get_addr.
2036 ArgListTy Args;
2037 ArgListEntry Entry;
2038 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002039 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002040 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002041 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00002042 std::pair<SDValue, SDValue> CallResult =
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002043 LowerCallTo(Chain, (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00002044 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002045 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002046 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002047 return CallResult.first;
2048}
2049
2050// Lower ISD::GlobalTLSAddress using the "initial exec" or
2051// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00002052SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002053ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002054 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002055 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002056 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002057 SDValue Offset;
2058 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002059 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002060 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002061 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002062
Chris Lattner4fb63d02009-07-15 04:12:33 +00002063 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002064 MachineFunction &MF = DAG.getMachineFunction();
2065 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002066 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002067 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002068 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2069 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002070 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2071 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2072 true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002073 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002074 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002075 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002076 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002077 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002078 Chain = Offset.getValue(1);
2079
Evan Chenge7e0d622009-11-06 22:24:13 +00002080 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002081 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002082
Evan Cheng9eda6892009-10-31 03:39:36 +00002083 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002084 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002085 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002086 } else {
2087 // local exec model
Bill Wendling5bb77992011-10-01 08:00:54 +00002088 ARMConstantPoolValue *CPV =
2089 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002090 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002091 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002092 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002093 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002094 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002095 }
2096
2097 // The address of the thread local variable is the add of the thread
2098 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002099 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002100}
2101
Dan Gohman475871a2008-07-27 21:46:04 +00002102SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002103ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002104 // TODO: implement the "local dynamic" model
2105 assert(Subtarget->isTargetELF() &&
2106 "TLS not implemented for non-ELF targets");
2107 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2108 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
2109 // otherwise use the "Local Exec" TLS Model
2110 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
2111 return LowerToTLSGeneralDynamicModel(GA, DAG);
2112 else
2113 return LowerToTLSExecModels(GA, DAG);
2114}
2115
Dan Gohman475871a2008-07-27 21:46:04 +00002116SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002117 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002118 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002119 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002120 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002121 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2122 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002123 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002124 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002125 ARMConstantPoolConstant::Create(GV,
2126 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002127 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002128 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002129 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002130 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002131 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002132 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002133 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002134 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002135 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002136 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002137 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002138 MachinePointerInfo::getGOT(),
2139 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002140 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002141 }
2142
2143 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloy015cca62011-10-26 08:53:19 +00002144 // pair. This is always cheaper.
2145 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002146 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002147 // FIXME: Once remat is capable of dealing with instructions with register
2148 // operands, expand this into two nodes.
2149 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2150 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002151 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002152 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2153 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2154 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2155 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002156 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002157 }
2158}
2159
Dan Gohman475871a2008-07-27 21:46:04 +00002160SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002161 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002162 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002163 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002164 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002165 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002166 MachineFunction &MF = DAG.getMachineFunction();
2167 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2168
Evan Cheng4abce0c2011-05-27 20:11:27 +00002169 // FIXME: Enable this for static codegen when tool issues are fixed.
Evan Chengf31151f2011-10-26 01:17:44 +00002170 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002171 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002172 // FIXME: Once remat is capable of dealing with instructions with register
2173 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002174 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002175 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2176 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2177
Evan Cheng53519f02011-01-21 18:55:51 +00002178 unsigned Wrapper = (RelocM == Reloc::PIC_)
2179 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2180 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002181 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002182 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2183 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002184 MachinePointerInfo::getGOT(),
2185 false, false, false, 0);
Evan Chengfc8475b2011-01-19 02:16:49 +00002186 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002187 }
2188
2189 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002190 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002191 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002192 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002193 } else {
2194 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002195 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2196 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002197 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue,
2198 PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002199 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002200 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002201 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002202
Evan Cheng9eda6892009-10-31 03:39:36 +00002203 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002204 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002205 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002206 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002207
2208 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002209 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002210 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002211 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002212
Evan Cheng63476a82009-09-03 07:04:02 +00002213 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002214 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002215 false, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002216
2217 return Result;
2218}
2219
Dan Gohman475871a2008-07-27 21:46:04 +00002220SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002221 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002222 assert(Subtarget->isTargetELF() &&
2223 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002224 MachineFunction &MF = DAG.getMachineFunction();
2225 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002226 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002227 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002228 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002229 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingfe31e672011-10-01 08:58:29 +00002230 ARMConstantPoolValue *CPV =
2231 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2232 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002233 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002234 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002235 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002236 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002237 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002238 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002239 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002240}
2241
Jim Grosbach0e0da732009-05-12 23:59:14 +00002242SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002243ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2244 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002245 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendlingce370cf2011-10-07 21:25:38 +00002246 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2247 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002248 Op.getOperand(1), Val);
2249}
2250
2251SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002252ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2253 DebugLoc dl = Op.getDebugLoc();
2254 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2255 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2256}
2257
2258SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002259ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002260 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002261 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002262 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002263 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002264 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002265 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002266 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002267 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2268 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002269 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002270 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002271 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002272 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002273 EVT PtrVT = getPointerTy();
2274 DebugLoc dl = Op.getDebugLoc();
2275 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2276 SDValue CPAddr;
2277 unsigned PCAdj = (RelocM != Reloc::PIC_)
2278 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002279 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002280 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2281 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002282 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002283 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002284 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002285 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002286 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002287 false, false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002288
2289 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002290 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002291 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2292 }
2293 return Result;
2294 }
Evan Cheng92e39162011-03-29 23:06:19 +00002295 case Intrinsic::arm_neon_vmulls:
2296 case Intrinsic::arm_neon_vmullu: {
2297 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2298 ? ARMISD::VMULLs : ARMISD::VMULLu;
2299 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2300 Op.getOperand(1), Op.getOperand(2));
2301 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002302 }
2303}
2304
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002305static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002306 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002307 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002308 if (!Subtarget->hasDataBarrier()) {
2309 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2310 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2311 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002312 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002313 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002314 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002315 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002316 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002317
2318 SDValue Op5 = Op.getOperand(5);
2319 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2320 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2321 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2322 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2323
2324 ARM_MB::MemBOpt DMBOpt;
2325 if (isDeviceBarrier)
2326 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2327 else
2328 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2329 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2330 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002331}
2332
Eli Friedman26689ac2011-08-03 21:06:02 +00002333
2334static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2335 const ARMSubtarget *Subtarget) {
2336 // FIXME: handle "fence singlethread" more efficiently.
2337 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002338 if (!Subtarget->hasDataBarrier()) {
2339 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2340 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2341 // here.
2342 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2343 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002344 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002345 DAG.getConstant(0, MVT::i32));
2346 }
2347
Eli Friedman26689ac2011-08-03 21:06:02 +00002348 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002349 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002350}
2351
Evan Chengdfed19f2010-11-03 06:34:55 +00002352static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2353 const ARMSubtarget *Subtarget) {
2354 // ARM pre v5TE and Thumb1 does not have preload instructions.
2355 if (!(Subtarget->isThumb2() ||
2356 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2357 // Just preserve the chain.
2358 return Op.getOperand(0);
2359
2360 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002361 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2362 if (!isRead &&
2363 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2364 // ARMv7 with MP extension has PLDW.
2365 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002366
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002367 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2368 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002369 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002370 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002371 isData = ~isData & 1;
2372 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002373
2374 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002375 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2376 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002377}
2378
Dan Gohman1e93df62010-04-17 14:41:14 +00002379static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2380 MachineFunction &MF = DAG.getMachineFunction();
2381 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2382
Evan Chenga8e29892007-01-19 07:51:42 +00002383 // vastart just stores the address of the VarArgsFrameIndex slot into the
2384 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002385 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002386 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002387 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002388 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002389 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2390 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002391}
2392
Dan Gohman475871a2008-07-27 21:46:04 +00002393SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002394ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2395 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002396 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002397 MachineFunction &MF = DAG.getMachineFunction();
2398 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2399
2400 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002401 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002402 RC = ARM::tGPRRegisterClass;
2403 else
2404 RC = ARM::GPRRegisterClass;
2405
2406 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002407 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002408 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002409
2410 SDValue ArgValue2;
2411 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002412 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002413 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002414
2415 // Create load node to retrieve arguments from the stack.
2416 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002417 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002418 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002419 false, false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002420 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002421 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002422 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002423 }
2424
Jim Grosbache5165492009-11-09 00:11:35 +00002425 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002426}
2427
Stuart Hastingsc7315872011-04-20 16:47:52 +00002428void
2429ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2430 unsigned &VARegSize, unsigned &VARegSaveSize)
2431 const {
2432 unsigned NumGPRs;
2433 if (CCInfo.isFirstByValRegValid())
2434 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2435 else {
2436 unsigned int firstUnalloced;
2437 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2438 sizeof(GPRArgRegs) /
2439 sizeof(GPRArgRegs[0]));
2440 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2441 }
2442
2443 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2444 VARegSize = NumGPRs * 4;
2445 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2446}
2447
2448// The remaining GPRs hold either the beginning of variable-argument
2449// data, or the beginning of an aggregate passed by value (usuall
2450// byval). Either way, we allocate stack slots adjacent to the data
2451// provided by our caller, and store the unallocated registers there.
2452// If this is a variadic function, the va_list pointer will begin with
2453// these values; otherwise, this reassembles a (byval) structure that
2454// was split between registers and memory.
2455void
2456ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2457 DebugLoc dl, SDValue &Chain,
2458 unsigned ArgOffset) const {
2459 MachineFunction &MF = DAG.getMachineFunction();
2460 MachineFrameInfo *MFI = MF.getFrameInfo();
2461 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2462 unsigned firstRegToSaveIndex;
2463 if (CCInfo.isFirstByValRegValid())
2464 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2465 else {
2466 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2467 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2468 }
2469
2470 unsigned VARegSize, VARegSaveSize;
2471 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2472 if (VARegSaveSize) {
2473 // If this function is vararg, store any remaining integer argument regs
2474 // to their spots on the stack so that they may be loaded by deferencing
2475 // the result of va_next.
2476 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002477 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2478 ArgOffset + VARegSaveSize
2479 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002480 false));
2481 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2482 getPointerTy());
2483
2484 SmallVector<SDValue, 4> MemOps;
2485 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
2486 TargetRegisterClass *RC;
2487 if (AFI->isThumb1OnlyFunction())
2488 RC = ARM::tGPRRegisterClass;
2489 else
2490 RC = ARM::GPRRegisterClass;
2491
2492 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2493 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2494 SDValue Store =
2495 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002496 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002497 false, false, 0);
2498 MemOps.push_back(Store);
2499 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2500 DAG.getConstant(4, getPointerTy()));
2501 }
2502 if (!MemOps.empty())
2503 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2504 &MemOps[0], MemOps.size());
2505 } else
2506 // This will point to the next argument passed via stack.
2507 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2508}
2509
Bob Wilson5bafff32009-06-22 23:27:02 +00002510SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002511ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002512 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002513 const SmallVectorImpl<ISD::InputArg>
2514 &Ins,
2515 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002516 SmallVectorImpl<SDValue> &InVals)
2517 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002518 MachineFunction &MF = DAG.getMachineFunction();
2519 MachineFrameInfo *MFI = MF.getFrameInfo();
2520
Bob Wilson1f595bb2009-04-17 19:07:39 +00002521 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2522
2523 // Assign locations to all of the incoming arguments.
2524 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002525 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2526 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002527 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002528 CCAssignFnForNode(CallConv, /* Return*/ false,
2529 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002530
2531 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002532 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002533
Stuart Hastingsf222e592011-02-28 17:17:53 +00002534 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002535 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2536 CCValAssign &VA = ArgLocs[i];
2537
Bob Wilsondee46d72009-04-17 20:35:10 +00002538 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002539 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002540 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002541
Bob Wilson1f595bb2009-04-17 19:07:39 +00002542 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002543 // f64 and vector types are split up into multiple registers or
2544 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002545 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002546 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002547 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002548 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002549 SDValue ArgValue2;
2550 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002551 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002552 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2553 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002554 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002555 false, false, false, 0);
Bob Wilson6a234f02010-04-13 22:03:22 +00002556 } else {
2557 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2558 Chain, DAG, dl);
2559 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002560 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2561 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002562 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002563 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002564 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2565 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002566 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002567
Bob Wilson5bafff32009-06-22 23:27:02 +00002568 } else {
2569 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002570
Owen Anderson825b72b2009-08-11 20:47:22 +00002571 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002572 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002573 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002574 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002575 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002576 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002577 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002578 RC = (AFI->isThumb1OnlyFunction() ?
2579 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002580 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002581 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002582
2583 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002584 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002585 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002586 }
2587
2588 // If this is an 8 or 16-bit value, it is really passed promoted
2589 // to 32 bits. Insert an assert[sz]ext to capture this, then
2590 // truncate to the right size.
2591 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002592 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002593 case CCValAssign::Full: break;
2594 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002595 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002596 break;
2597 case CCValAssign::SExt:
2598 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2599 DAG.getValueType(VA.getValVT()));
2600 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2601 break;
2602 case CCValAssign::ZExt:
2603 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2604 DAG.getValueType(VA.getValVT()));
2605 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2606 break;
2607 }
2608
Dan Gohman98ca4f22009-08-05 01:29:28 +00002609 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002610
2611 } else { // VA.isRegLoc()
2612
2613 // sanity check
2614 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002615 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002616
Stuart Hastingsf222e592011-02-28 17:17:53 +00002617 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002618
Stuart Hastingsf222e592011-02-28 17:17:53 +00002619 // Some Ins[] entries become multiple ArgLoc[] entries.
2620 // Process them only once.
2621 if (index != lastInsIndex)
2622 {
2623 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002624 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002625 // This can be changed with more analysis.
2626 // In case of tail call optimization mark all arguments mutable.
2627 // Since they could be overwritten by lowering of arguments in case of
2628 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002629 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002630 unsigned VARegSize, VARegSaveSize;
2631 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2632 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2633 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002634 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002635 int FI = MFI->CreateFixedObject(Bytes,
2636 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002637 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2638 } else {
2639 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2640 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002641
Stuart Hastingsf222e592011-02-28 17:17:53 +00002642 // Create load nodes to retrieve arguments from the stack.
2643 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2644 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2645 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002646 false, false, false, 0));
Stuart Hastingsf222e592011-02-28 17:17:53 +00002647 }
2648 lastInsIndex = index;
2649 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002650 }
2651 }
2652
2653 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002654 if (isVarArg)
2655 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002656
Dan Gohman98ca4f22009-08-05 01:29:28 +00002657 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002658}
2659
2660/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002661static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002662 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002663 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002664 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002665 // Maybe this has already been legalized into the constant pool?
2666 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002667 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002668 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002669 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002670 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002671 }
2672 }
2673 return false;
2674}
2675
Evan Chenga8e29892007-01-19 07:51:42 +00002676/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2677/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002678SDValue
2679ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002680 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002681 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002682 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002683 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002684 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002685 // Constant does not fit, try adjusting it by one?
2686 switch (CC) {
2687 default: break;
2688 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002689 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002690 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002691 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002692 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002693 }
2694 break;
2695 case ISD::SETULT:
2696 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002697 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002698 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002699 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002700 }
2701 break;
2702 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002703 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002704 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002705 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002706 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002707 }
2708 break;
2709 case ISD::SETULE:
2710 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002711 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002712 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002713 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002714 }
2715 break;
2716 }
2717 }
2718 }
2719
2720 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002721 ARMISD::NodeType CompareType;
2722 switch (CondCode) {
2723 default:
2724 CompareType = ARMISD::CMP;
2725 break;
2726 case ARMCC::EQ:
2727 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002728 // Uses only Z Flag
2729 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002730 break;
2731 }
Evan Cheng218977b2010-07-13 19:27:42 +00002732 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002733 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002734}
2735
2736/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002737SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002738ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002739 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002740 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002741 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002742 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002743 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002744 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2745 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002746}
2747
Bob Wilson79f56c92011-03-08 01:17:20 +00002748/// duplicateCmp - Glue values can have only one use, so this function
2749/// duplicates a comparison node.
2750SDValue
2751ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2752 unsigned Opc = Cmp.getOpcode();
2753 DebugLoc DL = Cmp.getDebugLoc();
2754 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2755 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2756
2757 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2758 Cmp = Cmp.getOperand(0);
2759 Opc = Cmp.getOpcode();
2760 if (Opc == ARMISD::CMPFP)
2761 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2762 else {
2763 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2764 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2765 }
2766 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2767}
2768
Bill Wendlingde2b1512010-08-11 08:43:16 +00002769SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2770 SDValue Cond = Op.getOperand(0);
2771 SDValue SelectTrue = Op.getOperand(1);
2772 SDValue SelectFalse = Op.getOperand(2);
2773 DebugLoc dl = Op.getDebugLoc();
2774
2775 // Convert:
2776 //
2777 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2778 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2779 //
2780 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2781 const ConstantSDNode *CMOVTrue =
2782 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2783 const ConstantSDNode *CMOVFalse =
2784 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2785
2786 if (CMOVTrue && CMOVFalse) {
2787 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2788 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2789
2790 SDValue True;
2791 SDValue False;
2792 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2793 True = SelectTrue;
2794 False = SelectFalse;
2795 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2796 True = SelectFalse;
2797 False = SelectTrue;
2798 }
2799
2800 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002801 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002802 SDValue ARMcc = Cond.getOperand(2);
2803 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002804 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002805 assert(True.getValueType() == VT);
2806 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002807 }
2808 }
2809 }
2810
2811 return DAG.getSelectCC(dl, Cond,
2812 DAG.getConstant(0, Cond.getValueType()),
2813 SelectTrue, SelectFalse, ISD::SETNE);
2814}
2815
Dan Gohmand858e902010-04-17 15:26:15 +00002816SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002817 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002818 SDValue LHS = Op.getOperand(0);
2819 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002820 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002821 SDValue TrueVal = Op.getOperand(2);
2822 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002823 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002824
Owen Anderson825b72b2009-08-11 20:47:22 +00002825 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002826 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002827 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002828 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Jim Grosbachb04546f2011-09-13 20:30:37 +00002829 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002830 }
2831
2832 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002833 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002834
Evan Cheng218977b2010-07-13 19:27:42 +00002835 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2836 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002837 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002838 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002839 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002840 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002841 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002842 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002843 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002844 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002845 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002846 }
2847 return Result;
2848}
2849
Evan Cheng218977b2010-07-13 19:27:42 +00002850/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2851/// to morph to an integer compare sequence.
2852static bool canChangeToInt(SDValue Op, bool &SeenZero,
2853 const ARMSubtarget *Subtarget) {
2854 SDNode *N = Op.getNode();
2855 if (!N->hasOneUse())
2856 // Otherwise it requires moving the value from fp to integer registers.
2857 return false;
2858 if (!N->getNumValues())
2859 return false;
2860 EVT VT = Op.getValueType();
2861 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2862 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2863 // vmrs are very slow, e.g. cortex-a8.
2864 return false;
2865
2866 if (isFloatingPointZero(Op)) {
2867 SeenZero = true;
2868 return true;
2869 }
2870 return ISD::isNormalLoad(N);
2871}
2872
2873static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2874 if (isFloatingPointZero(Op))
2875 return DAG.getConstant(0, MVT::i32);
2876
2877 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2878 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002879 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002880 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002881 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002882
2883 llvm_unreachable("Unknown VFP cmp argument!");
2884}
2885
2886static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2887 SDValue &RetVal1, SDValue &RetVal2) {
2888 if (isFloatingPointZero(Op)) {
2889 RetVal1 = DAG.getConstant(0, MVT::i32);
2890 RetVal2 = DAG.getConstant(0, MVT::i32);
2891 return;
2892 }
2893
2894 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2895 SDValue Ptr = Ld->getBasePtr();
2896 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2897 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002898 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002899 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002900 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002901
2902 EVT PtrType = Ptr.getValueType();
2903 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2904 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2905 PtrType, Ptr, DAG.getConstant(4, PtrType));
2906 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2907 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002908 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002909 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002910 Ld->isInvariant(), NewAlign);
Evan Cheng218977b2010-07-13 19:27:42 +00002911 return;
2912 }
2913
2914 llvm_unreachable("Unknown VFP cmp argument!");
2915}
2916
2917/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2918/// f32 and even f64 comparisons to integer ones.
2919SDValue
2920ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2921 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002922 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002923 SDValue LHS = Op.getOperand(2);
2924 SDValue RHS = Op.getOperand(3);
2925 SDValue Dest = Op.getOperand(4);
2926 DebugLoc dl = Op.getDebugLoc();
2927
2928 bool SeenZero = false;
2929 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2930 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002931 // If one of the operand is zero, it's safe to ignore the NaN case since
2932 // we only care about equality comparisons.
2933 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Bob Wilson1b772f92011-03-08 01:17:16 +00002934 // If unsafe fp math optimization is enabled and there are no other uses of
2935 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00002936 // to an integer comparison.
2937 if (CC == ISD::SETOEQ)
2938 CC = ISD::SETEQ;
2939 else if (CC == ISD::SETUNE)
2940 CC = ISD::SETNE;
2941
2942 SDValue ARMcc;
2943 if (LHS.getValueType() == MVT::f32) {
2944 LHS = bitcastf32Toi32(LHS, DAG);
2945 RHS = bitcastf32Toi32(RHS, DAG);
2946 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2947 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2948 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2949 Chain, Dest, ARMcc, CCR, Cmp);
2950 }
2951
2952 SDValue LHS1, LHS2;
2953 SDValue RHS1, RHS2;
2954 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2955 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2956 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2957 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002958 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002959 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2960 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2961 }
2962
2963 return SDValue();
2964}
2965
2966SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2967 SDValue Chain = Op.getOperand(0);
2968 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2969 SDValue LHS = Op.getOperand(2);
2970 SDValue RHS = Op.getOperand(3);
2971 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002972 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002973
Owen Anderson825b72b2009-08-11 20:47:22 +00002974 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002975 SDValue ARMcc;
2976 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002977 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002978 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002979 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002980 }
2981
Owen Anderson825b72b2009-08-11 20:47:22 +00002982 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002983
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002984 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng218977b2010-07-13 19:27:42 +00002985 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2986 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2987 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2988 if (Result.getNode())
2989 return Result;
2990 }
2991
Evan Chenga8e29892007-01-19 07:51:42 +00002992 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002993 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002994
Evan Cheng218977b2010-07-13 19:27:42 +00002995 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2996 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002997 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002998 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002999 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00003000 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003001 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00003002 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3003 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00003004 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003005 }
3006 return Res;
3007}
3008
Dan Gohmand858e902010-04-17 15:26:15 +00003009SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00003010 SDValue Chain = Op.getOperand(0);
3011 SDValue Table = Op.getOperand(1);
3012 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003013 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003014
Owen Andersone50ed302009-08-10 22:56:29 +00003015 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00003016 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3017 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00003018 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00003019 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00003020 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00003021 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3022 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00003023 if (Subtarget->isThumb2()) {
3024 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3025 // which does another jump to the destination. This also makes it easier
3026 // to translate it to TBB / TBH later.
3027 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00003028 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00003029 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003030 }
Evan Cheng66ac5312009-07-25 00:33:29 +00003031 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00003032 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003033 MachinePointerInfo::getJumpTable(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003034 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003035 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003036 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00003037 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003038 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00003039 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003040 MachinePointerInfo::getJumpTable(),
3041 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003042 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003043 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003044 }
Evan Chenga8e29892007-01-19 07:51:42 +00003045}
3046
Eli Friedman14e809c2011-11-09 23:36:02 +00003047static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
3048 EVT VT = Op.getValueType();
3049 assert(VT.getVectorElementType() == MVT::i32 && "Unexpected custom lowering");
3050
3051 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3052 return Op;
3053 return DAG.UnrollVectorOp(Op.getNode());
3054}
3055
Bob Wilson76a312b2010-03-19 22:51:32 +00003056static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman14e809c2011-11-09 23:36:02 +00003057 EVT VT = Op.getValueType();
3058 if (VT.isVector())
3059 return LowerVectorFP_TO_INT(Op, DAG);
3060
Bob Wilson76a312b2010-03-19 22:51:32 +00003061 DebugLoc dl = Op.getDebugLoc();
3062 unsigned Opc;
3063
3064 switch (Op.getOpcode()) {
3065 default:
3066 assert(0 && "Invalid opcode!");
3067 case ISD::FP_TO_SINT:
3068 Opc = ARMISD::FTOSI;
3069 break;
3070 case ISD::FP_TO_UINT:
3071 Opc = ARMISD::FTOUI;
3072 break;
3073 }
3074 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003075 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003076}
3077
Cameron Zwarich3007d332011-03-29 21:41:55 +00003078static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3079 EVT VT = Op.getValueType();
3080 DebugLoc dl = Op.getDebugLoc();
3081
Eli Friedman14e809c2011-11-09 23:36:02 +00003082 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3083 if (VT.getVectorElementType() == MVT::f32)
3084 return Op;
3085 return DAG.UnrollVectorOp(Op.getNode());
3086 }
3087
Duncan Sands1f6a3292011-08-12 14:54:45 +00003088 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3089 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003090 if (VT != MVT::v4f32)
3091 return DAG.UnrollVectorOp(Op.getNode());
3092
3093 unsigned CastOpc;
3094 unsigned Opc;
3095 switch (Op.getOpcode()) {
3096 default:
3097 assert(0 && "Invalid opcode!");
3098 case ISD::SINT_TO_FP:
3099 CastOpc = ISD::SIGN_EXTEND;
3100 Opc = ISD::SINT_TO_FP;
3101 break;
3102 case ISD::UINT_TO_FP:
3103 CastOpc = ISD::ZERO_EXTEND;
3104 Opc = ISD::UINT_TO_FP;
3105 break;
3106 }
3107
3108 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3109 return DAG.getNode(Opc, dl, VT, Op);
3110}
3111
Bob Wilson76a312b2010-03-19 22:51:32 +00003112static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3113 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003114 if (VT.isVector())
3115 return LowerVectorINT_TO_FP(Op, DAG);
3116
Bob Wilson76a312b2010-03-19 22:51:32 +00003117 DebugLoc dl = Op.getDebugLoc();
3118 unsigned Opc;
3119
3120 switch (Op.getOpcode()) {
3121 default:
3122 assert(0 && "Invalid opcode!");
3123 case ISD::SINT_TO_FP:
3124 Opc = ARMISD::SITOF;
3125 break;
3126 case ISD::UINT_TO_FP:
3127 Opc = ARMISD::UITOF;
3128 break;
3129 }
3130
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003131 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003132 return DAG.getNode(Opc, dl, VT, Op);
3133}
3134
Evan Cheng515fe3a2010-07-08 02:08:50 +00003135SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003136 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003137 SDValue Tmp0 = Op.getOperand(0);
3138 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003139 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003140 EVT VT = Op.getValueType();
3141 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003142 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3143 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3144 bool UseNEON = !InGPR && Subtarget->hasNEON();
3145
3146 if (UseNEON) {
3147 // Use VBSL to copy the sign bit.
3148 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3149 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3150 DAG.getTargetConstant(EncodedVal, MVT::i32));
3151 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3152 if (VT == MVT::f64)
3153 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3154 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3155 DAG.getConstant(32, MVT::i32));
3156 else /*if (VT == MVT::f32)*/
3157 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3158 if (SrcVT == MVT::f32) {
3159 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3160 if (VT == MVT::f64)
3161 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3162 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3163 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003164 } else if (VT == MVT::f32)
3165 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3166 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3167 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003168 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3169 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3170
3171 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3172 MVT::i32);
3173 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3174 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3175 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003176
Evan Chenge573fb32011-02-23 02:24:55 +00003177 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3178 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3179 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003180 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003181 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3182 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3183 DAG.getConstant(0, MVT::i32));
3184 } else {
3185 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3186 }
3187
3188 return Res;
3189 }
Evan Chengc143dd42011-02-11 02:28:55 +00003190
3191 // Bitcast operand 1 to i32.
3192 if (SrcVT == MVT::f64)
3193 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3194 &Tmp1, 1).getValue(1);
3195 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3196
Evan Chenge573fb32011-02-23 02:24:55 +00003197 // Or in the signbit with integer operations.
3198 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3199 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3200 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3201 if (VT == MVT::f32) {
3202 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3203 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3204 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3205 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003206 }
3207
Evan Chenge573fb32011-02-23 02:24:55 +00003208 // f64: Or the high part with signbit and then combine two parts.
3209 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3210 &Tmp0, 1);
3211 SDValue Lo = Tmp0.getValue(0);
3212 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3213 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3214 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003215}
3216
Evan Cheng2457f2c2010-05-22 01:47:14 +00003217SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3218 MachineFunction &MF = DAG.getMachineFunction();
3219 MachineFrameInfo *MFI = MF.getFrameInfo();
3220 MFI->setReturnAddressIsTaken(true);
3221
3222 EVT VT = Op.getValueType();
3223 DebugLoc dl = Op.getDebugLoc();
3224 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3225 if (Depth) {
3226 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3227 SDValue Offset = DAG.getConstant(4, MVT::i32);
3228 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3229 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003230 MachinePointerInfo(), false, false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003231 }
3232
3233 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003234 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003235 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3236}
3237
Dan Gohmand858e902010-04-17 15:26:15 +00003238SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003239 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3240 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003241
Owen Andersone50ed302009-08-10 22:56:29 +00003242 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003243 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3244 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003245 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003246 ? ARM::R7 : ARM::R11;
3247 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3248 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003249 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3250 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003251 false, false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003252 return FrameAddr;
3253}
3254
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003255/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003256/// expand a bit convert where either the source or destination type is i64 to
3257/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3258/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3259/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003260static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003261 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3262 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003263 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003264
Bob Wilson9f3f0612010-04-17 05:30:19 +00003265 // This function is only supposed to be called for i64 types, either as the
3266 // source or destination of the bit convert.
3267 EVT SrcVT = Op.getValueType();
3268 EVT DstVT = N->getValueType(0);
3269 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003270 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003271
Bob Wilson9f3f0612010-04-17 05:30:19 +00003272 // Turn i64->f64 into VMOVDRR.
3273 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003274 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3275 DAG.getConstant(0, MVT::i32));
3276 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3277 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003278 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003279 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003280 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003281
Jim Grosbache5165492009-11-09 00:11:35 +00003282 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003283 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3284 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3285 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3286 // Merge the pieces into a single i64 value.
3287 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3288 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003289
Bob Wilson9f3f0612010-04-17 05:30:19 +00003290 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003291}
3292
Bob Wilson5bafff32009-06-22 23:27:02 +00003293/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003294/// Zero vectors are used to represent vector negation and in those cases
3295/// will be implemented with the NEON VNEG instruction. However, VNEG does
3296/// not support i64 elements, so sometimes the zero vectors will need to be
3297/// explicitly constructed. Regardless, use a canonical VMOV to create the
3298/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003299static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003300 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003301 // The canonical modified immediate encoding of a zero vector is....0!
3302 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3303 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3304 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003305 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003306}
3307
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003308/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3309/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003310SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3311 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003312 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3313 EVT VT = Op.getValueType();
3314 unsigned VTBits = VT.getSizeInBits();
3315 DebugLoc dl = Op.getDebugLoc();
3316 SDValue ShOpLo = Op.getOperand(0);
3317 SDValue ShOpHi = Op.getOperand(1);
3318 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003319 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003320 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003321
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003322 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3323
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003324 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3325 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3326 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3327 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3328 DAG.getConstant(VTBits, MVT::i32));
3329 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3330 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003331 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003332
3333 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3334 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003335 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003336 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003337 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003338 CCR, Cmp);
3339
3340 SDValue Ops[2] = { Lo, Hi };
3341 return DAG.getMergeValues(Ops, 2, dl);
3342}
3343
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003344/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3345/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003346SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3347 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003348 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3349 EVT VT = Op.getValueType();
3350 unsigned VTBits = VT.getSizeInBits();
3351 DebugLoc dl = Op.getDebugLoc();
3352 SDValue ShOpLo = Op.getOperand(0);
3353 SDValue ShOpHi = Op.getOperand(1);
3354 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003355 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003356
3357 assert(Op.getOpcode() == ISD::SHL_PARTS);
3358 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3359 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3360 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3361 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3362 DAG.getConstant(VTBits, MVT::i32));
3363 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3364 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3365
3366 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3367 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3368 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003369 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003370 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003371 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003372 CCR, Cmp);
3373
3374 SDValue Ops[2] = { Lo, Hi };
3375 return DAG.getMergeValues(Ops, 2, dl);
3376}
3377
Jim Grosbach4725ca72010-09-08 03:54:02 +00003378SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003379 SelectionDAG &DAG) const {
3380 // The rounding mode is in bits 23:22 of the FPSCR.
3381 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3382 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3383 // so that the shift + and get folded into a bitfield extract.
3384 DebugLoc dl = Op.getDebugLoc();
3385 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3386 DAG.getConstant(Intrinsic::arm_get_fpscr,
3387 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003388 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003389 DAG.getConstant(1U << 22, MVT::i32));
3390 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3391 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003392 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003393 DAG.getConstant(3, MVT::i32));
3394}
3395
Jim Grosbach3482c802010-01-18 19:58:49 +00003396static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3397 const ARMSubtarget *ST) {
3398 EVT VT = N->getValueType(0);
3399 DebugLoc dl = N->getDebugLoc();
3400
3401 if (!ST->hasV6T2Ops())
3402 return SDValue();
3403
3404 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3405 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3406}
3407
Bob Wilson5bafff32009-06-22 23:27:02 +00003408static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3409 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003410 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003411 DebugLoc dl = N->getDebugLoc();
3412
Bob Wilsond5448bb2010-11-18 21:16:28 +00003413 if (!VT.isVector())
3414 return SDValue();
3415
Bob Wilson5bafff32009-06-22 23:27:02 +00003416 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003417 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003418
Bob Wilsond5448bb2010-11-18 21:16:28 +00003419 // Left shifts translate directly to the vshiftu intrinsic.
3420 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003421 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003422 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3423 N->getOperand(0), N->getOperand(1));
3424
3425 assert((N->getOpcode() == ISD::SRA ||
3426 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3427
3428 // NEON uses the same intrinsics for both left and right shifts. For
3429 // right shifts, the shift amounts are negative, so negate the vector of
3430 // shift amounts.
3431 EVT ShiftVT = N->getOperand(1).getValueType();
3432 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3433 getZeroVector(ShiftVT, DAG, dl),
3434 N->getOperand(1));
3435 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3436 Intrinsic::arm_neon_vshifts :
3437 Intrinsic::arm_neon_vshiftu);
3438 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3439 DAG.getConstant(vshiftInt, MVT::i32),
3440 N->getOperand(0), NegatedCount);
3441}
3442
3443static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3444 const ARMSubtarget *ST) {
3445 EVT VT = N->getValueType(0);
3446 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003447
Eli Friedmance392eb2009-08-22 03:13:10 +00003448 // We can get here for a node like i32 = ISD::SHL i32, i64
3449 if (VT != MVT::i64)
3450 return SDValue();
3451
3452 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003453 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003454
Chris Lattner27a6c732007-11-24 07:07:01 +00003455 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3456 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003457 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003458 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003459
Chris Lattner27a6c732007-11-24 07:07:01 +00003460 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003461 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003462
Chris Lattner27a6c732007-11-24 07:07:01 +00003463 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003464 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003465 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003466 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003467 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003468
Chris Lattner27a6c732007-11-24 07:07:01 +00003469 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3470 // captures the result into a carry flag.
3471 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003472 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003473
Chris Lattner27a6c732007-11-24 07:07:01 +00003474 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003475 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003476
Chris Lattner27a6c732007-11-24 07:07:01 +00003477 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003478 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003479}
3480
Bob Wilson5bafff32009-06-22 23:27:02 +00003481static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3482 SDValue TmpOp0, TmpOp1;
3483 bool Invert = false;
3484 bool Swap = false;
3485 unsigned Opc = 0;
3486
3487 SDValue Op0 = Op.getOperand(0);
3488 SDValue Op1 = Op.getOperand(1);
3489 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003490 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003491 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3492 DebugLoc dl = Op.getDebugLoc();
3493
3494 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3495 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003496 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003497 case ISD::SETUNE:
3498 case ISD::SETNE: Invert = true; // Fallthrough
3499 case ISD::SETOEQ:
3500 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3501 case ISD::SETOLT:
3502 case ISD::SETLT: Swap = true; // Fallthrough
3503 case ISD::SETOGT:
3504 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3505 case ISD::SETOLE:
3506 case ISD::SETLE: Swap = true; // Fallthrough
3507 case ISD::SETOGE:
3508 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3509 case ISD::SETUGE: Swap = true; // Fallthrough
3510 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3511 case ISD::SETUGT: Swap = true; // Fallthrough
3512 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3513 case ISD::SETUEQ: Invert = true; // Fallthrough
3514 case ISD::SETONE:
3515 // Expand this to (OLT | OGT).
3516 TmpOp0 = Op0;
3517 TmpOp1 = Op1;
3518 Opc = ISD::OR;
3519 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3520 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3521 break;
3522 case ISD::SETUO: Invert = true; // Fallthrough
3523 case ISD::SETO:
3524 // Expand this to (OLT | OGE).
3525 TmpOp0 = Op0;
3526 TmpOp1 = Op1;
3527 Opc = ISD::OR;
3528 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3529 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3530 break;
3531 }
3532 } else {
3533 // Integer comparisons.
3534 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003535 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003536 case ISD::SETNE: Invert = true;
3537 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3538 case ISD::SETLT: Swap = true;
3539 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3540 case ISD::SETLE: Swap = true;
3541 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3542 case ISD::SETULT: Swap = true;
3543 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3544 case ISD::SETULE: Swap = true;
3545 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3546 }
3547
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003548 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003549 if (Opc == ARMISD::VCEQ) {
3550
3551 SDValue AndOp;
3552 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3553 AndOp = Op0;
3554 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3555 AndOp = Op1;
3556
3557 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003558 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003559 AndOp = AndOp.getOperand(0);
3560
3561 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3562 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003563 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3564 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003565 Invert = !Invert;
3566 }
3567 }
3568 }
3569
3570 if (Swap)
3571 std::swap(Op0, Op1);
3572
Owen Andersonc24cb352010-11-08 23:21:22 +00003573 // If one of the operands is a constant vector zero, attempt to fold the
3574 // comparison to a specialized compare-against-zero form.
3575 SDValue SingleOp;
3576 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3577 SingleOp = Op0;
3578 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3579 if (Opc == ARMISD::VCGE)
3580 Opc = ARMISD::VCLEZ;
3581 else if (Opc == ARMISD::VCGT)
3582 Opc = ARMISD::VCLTZ;
3583 SingleOp = Op1;
3584 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003585
Owen Andersonc24cb352010-11-08 23:21:22 +00003586 SDValue Result;
3587 if (SingleOp.getNode()) {
3588 switch (Opc) {
3589 case ARMISD::VCEQ:
3590 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3591 case ARMISD::VCGE:
3592 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3593 case ARMISD::VCLEZ:
3594 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3595 case ARMISD::VCGT:
3596 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3597 case ARMISD::VCLTZ:
3598 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3599 default:
3600 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3601 }
3602 } else {
3603 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3604 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003605
3606 if (Invert)
3607 Result = DAG.getNOT(dl, Result, VT);
3608
3609 return Result;
3610}
3611
Bob Wilsond3c42842010-06-14 22:19:57 +00003612/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3613/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003614/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003615static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3616 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003617 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003618 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003619
Bob Wilson827b2102010-06-15 19:05:35 +00003620 // SplatBitSize is set to the smallest size that splats the vector, so a
3621 // zero vector will always have SplatBitSize == 8. However, NEON modified
3622 // immediate instructions others than VMOV do not support the 8-bit encoding
3623 // of a zero vector, and the default encoding of zero is supposed to be the
3624 // 32-bit version.
3625 if (SplatBits == 0)
3626 SplatBitSize = 32;
3627
Bob Wilson5bafff32009-06-22 23:27:02 +00003628 switch (SplatBitSize) {
3629 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003630 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003631 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003632 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003633 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003634 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003635 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003636 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003637 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003638
3639 case 16:
3640 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003641 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003642 if ((SplatBits & ~0xff) == 0) {
3643 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003644 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003645 Imm = SplatBits;
3646 break;
3647 }
3648 if ((SplatBits & ~0xff00) == 0) {
3649 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003650 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003651 Imm = SplatBits >> 8;
3652 break;
3653 }
3654 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003655
3656 case 32:
3657 // NEON's 32-bit VMOV supports splat values where:
3658 // * only one byte is nonzero, or
3659 // * the least significant byte is 0xff and the second byte is nonzero, or
3660 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003661 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003662 if ((SplatBits & ~0xff) == 0) {
3663 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003664 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003665 Imm = SplatBits;
3666 break;
3667 }
3668 if ((SplatBits & ~0xff00) == 0) {
3669 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003670 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003671 Imm = SplatBits >> 8;
3672 break;
3673 }
3674 if ((SplatBits & ~0xff0000) == 0) {
3675 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003676 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003677 Imm = SplatBits >> 16;
3678 break;
3679 }
3680 if ((SplatBits & ~0xff000000) == 0) {
3681 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003682 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003683 Imm = SplatBits >> 24;
3684 break;
3685 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003686
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003687 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3688 if (type == OtherModImm) return SDValue();
3689
Bob Wilson5bafff32009-06-22 23:27:02 +00003690 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003691 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3692 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003693 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003694 Imm = SplatBits >> 8;
3695 SplatBits |= 0xff;
3696 break;
3697 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003698
3699 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003700 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3701 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003702 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003703 Imm = SplatBits >> 16;
3704 SplatBits |= 0xffff;
3705 break;
3706 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003707
3708 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3709 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3710 // VMOV.I32. A (very) minor optimization would be to replicate the value
3711 // and fall through here to test for a valid 64-bit splat. But, then the
3712 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003713 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003714
3715 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003716 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003717 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003718 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003719 uint64_t BitMask = 0xff;
3720 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003721 unsigned ImmMask = 1;
3722 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003723 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003724 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003725 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003726 Imm |= ImmMask;
3727 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003728 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003729 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003730 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003731 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003732 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003733 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003734 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003735 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003736 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003737 break;
3738 }
3739
Bob Wilson1a913ed2010-06-11 21:34:50 +00003740 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003741 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003742 return SDValue();
3743 }
3744
Bob Wilsoncba270d2010-07-13 21:16:48 +00003745 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3746 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003747}
3748
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003749static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3750 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003751 unsigned NumElts = VT.getVectorNumElements();
3752 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003753
3754 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3755 if (M[0] < 0)
3756 return false;
3757
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003758 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003759
3760 // If this is a VEXT shuffle, the immediate value is the index of the first
3761 // element. The other shuffle indices must be the successive elements after
3762 // the first one.
3763 unsigned ExpectedElt = Imm;
3764 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003765 // Increment the expected index. If it wraps around, it may still be
3766 // a VEXT but the source vectors must be swapped.
3767 ExpectedElt += 1;
3768 if (ExpectedElt == NumElts * 2) {
3769 ExpectedElt = 0;
3770 ReverseVEXT = true;
3771 }
3772
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003773 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003774 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003775 return false;
3776 }
3777
3778 // Adjust the index value if the source operands will be swapped.
3779 if (ReverseVEXT)
3780 Imm -= NumElts;
3781
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003782 return true;
3783}
3784
Bob Wilson8bb9e482009-07-26 00:39:34 +00003785/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3786/// instruction with the specified blocksize. (The order of the elements
3787/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003788static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3789 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003790 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3791 "Only possible block sizes for VREV are: 16, 32, 64");
3792
Bob Wilson8bb9e482009-07-26 00:39:34 +00003793 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003794 if (EltSz == 64)
3795 return false;
3796
3797 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003798 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003799 // If the first shuffle index is UNDEF, be optimistic.
3800 if (M[0] < 0)
3801 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003802
3803 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3804 return false;
3805
3806 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003807 if (M[i] < 0) continue; // ignore UNDEF indices
3808 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003809 return false;
3810 }
3811
3812 return true;
3813}
3814
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003815static bool isVTBLMask(const SmallVectorImpl<int> &M, EVT VT) {
3816 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3817 // range, then 0 is placed into the resulting vector. So pretty much any mask
3818 // of 8 elements can work here.
3819 return VT == MVT::v8i8 && M.size() == 8;
3820}
3821
Bob Wilsonc692cb72009-08-21 20:54:19 +00003822static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3823 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003824 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3825 if (EltSz == 64)
3826 return false;
3827
Bob Wilsonc692cb72009-08-21 20:54:19 +00003828 unsigned NumElts = VT.getVectorNumElements();
3829 WhichResult = (M[0] == 0 ? 0 : 1);
3830 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003831 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3832 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003833 return false;
3834 }
3835 return true;
3836}
3837
Bob Wilson324f4f12009-12-03 06:40:55 +00003838/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3839/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3840/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3841static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3842 unsigned &WhichResult) {
3843 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3844 if (EltSz == 64)
3845 return false;
3846
3847 unsigned NumElts = VT.getVectorNumElements();
3848 WhichResult = (M[0] == 0 ? 0 : 1);
3849 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003850 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3851 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003852 return false;
3853 }
3854 return true;
3855}
3856
Bob Wilsonc692cb72009-08-21 20:54:19 +00003857static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3858 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003859 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3860 if (EltSz == 64)
3861 return false;
3862
Bob Wilsonc692cb72009-08-21 20:54:19 +00003863 unsigned NumElts = VT.getVectorNumElements();
3864 WhichResult = (M[0] == 0 ? 0 : 1);
3865 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003866 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003867 if ((unsigned) M[i] != 2 * i + WhichResult)
3868 return false;
3869 }
3870
3871 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003872 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003873 return false;
3874
3875 return true;
3876}
3877
Bob Wilson324f4f12009-12-03 06:40:55 +00003878/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3879/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3880/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3881static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3882 unsigned &WhichResult) {
3883 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3884 if (EltSz == 64)
3885 return false;
3886
3887 unsigned Half = VT.getVectorNumElements() / 2;
3888 WhichResult = (M[0] == 0 ? 0 : 1);
3889 for (unsigned j = 0; j != 2; ++j) {
3890 unsigned Idx = WhichResult;
3891 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003892 int MIdx = M[i + j * Half];
3893 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003894 return false;
3895 Idx += 2;
3896 }
3897 }
3898
3899 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3900 if (VT.is64BitVector() && EltSz == 32)
3901 return false;
3902
3903 return true;
3904}
3905
Bob Wilsonc692cb72009-08-21 20:54:19 +00003906static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3907 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003908 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3909 if (EltSz == 64)
3910 return false;
3911
Bob Wilsonc692cb72009-08-21 20:54:19 +00003912 unsigned NumElts = VT.getVectorNumElements();
3913 WhichResult = (M[0] == 0 ? 0 : 1);
3914 unsigned Idx = WhichResult * NumElts / 2;
3915 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003916 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3917 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003918 return false;
3919 Idx += 1;
3920 }
3921
3922 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003923 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003924 return false;
3925
3926 return true;
3927}
3928
Bob Wilson324f4f12009-12-03 06:40:55 +00003929/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3930/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3931/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3932static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3933 unsigned &WhichResult) {
3934 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3935 if (EltSz == 64)
3936 return false;
3937
3938 unsigned NumElts = VT.getVectorNumElements();
3939 WhichResult = (M[0] == 0 ? 0 : 1);
3940 unsigned Idx = WhichResult * NumElts / 2;
3941 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003942 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3943 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003944 return false;
3945 Idx += 1;
3946 }
3947
3948 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3949 if (VT.is64BitVector() && EltSz == 32)
3950 return false;
3951
3952 return true;
3953}
3954
Dale Johannesenf630c712010-07-29 20:10:08 +00003955// If N is an integer constant that can be moved into a register in one
3956// instruction, return an SDValue of such a constant (will become a MOV
3957// instruction). Otherwise return null.
3958static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3959 const ARMSubtarget *ST, DebugLoc dl) {
3960 uint64_t Val;
3961 if (!isa<ConstantSDNode>(N))
3962 return SDValue();
3963 Val = cast<ConstantSDNode>(N)->getZExtValue();
3964
3965 if (ST->isThumb1Only()) {
3966 if (Val <= 255 || ~Val <= 255)
3967 return DAG.getConstant(Val, MVT::i32);
3968 } else {
3969 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3970 return DAG.getConstant(Val, MVT::i32);
3971 }
3972 return SDValue();
3973}
3974
Bob Wilson5bafff32009-06-22 23:27:02 +00003975// If this is a case we can't handle, return null and let the default
3976// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003977SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3978 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003979 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003980 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003981 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003982
3983 APInt SplatBits, SplatUndef;
3984 unsigned SplatBitSize;
3985 bool HasAnyUndefs;
3986 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003987 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003988 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003989 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003990 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003991 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003992 DAG, VmovVT, VT.is128BitVector(),
3993 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003994 if (Val.getNode()) {
3995 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003996 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003997 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003998
3999 // Try an immediate VMVN.
Eli Friedman8e4d0422011-10-13 22:40:23 +00004000 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004001 Val = isNEONModifiedImm(NegatedImm,
4002 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004003 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004004 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004005 if (Val.getNode()) {
4006 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004007 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004008 }
Evan Chengeaa192a2011-11-15 02:12:34 +00004009
4010 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedman2f21e8c2011-12-15 22:56:53 +00004011 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedmaneffab8f2011-12-09 23:54:42 +00004012 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Chengeaa192a2011-11-15 02:12:34 +00004013 if (ImmVal != -1) {
4014 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4015 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4016 }
4017 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004018 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00004019 }
4020
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004021 // Scan through the operands to see if only one value is used.
4022 unsigned NumElts = VT.getVectorNumElements();
4023 bool isOnlyLowElement = true;
4024 bool usesOnlyOneValue = true;
4025 bool isConstant = true;
4026 SDValue Value;
4027 for (unsigned i = 0; i < NumElts; ++i) {
4028 SDValue V = Op.getOperand(i);
4029 if (V.getOpcode() == ISD::UNDEF)
4030 continue;
4031 if (i > 0)
4032 isOnlyLowElement = false;
4033 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4034 isConstant = false;
4035
4036 if (!Value.getNode())
4037 Value = V;
4038 else if (V != Value)
4039 usesOnlyOneValue = false;
4040 }
4041
4042 if (!Value.getNode())
4043 return DAG.getUNDEF(VT);
4044
4045 if (isOnlyLowElement)
4046 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4047
Dale Johannesenf630c712010-07-29 20:10:08 +00004048 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4049
Dale Johannesen575cd142010-10-19 20:00:17 +00004050 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4051 // i32 and try again.
4052 if (usesOnlyOneValue && EltSize <= 32) {
4053 if (!isConstant)
4054 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4055 if (VT.getVectorElementType().isFloatingPoint()) {
4056 SmallVector<SDValue, 8> Ops;
4057 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004058 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00004059 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00004060 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
4061 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00004062 Val = LowerBUILD_VECTOR(Val, DAG, ST);
4063 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004064 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004065 }
Dale Johannesen575cd142010-10-19 20:00:17 +00004066 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
4067 if (Val.getNode())
4068 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004069 }
4070
4071 // If all elements are constants and the case above didn't get hit, fall back
4072 // to the default expansion, which will generate a load from the constant
4073 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004074 if (isConstant)
4075 return SDValue();
4076
Bob Wilson11a1dff2011-01-07 21:37:30 +00004077 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4078 if (NumElts >= 4) {
4079 SDValue shuffle = ReconstructShuffle(Op, DAG);
4080 if (shuffle != SDValue())
4081 return shuffle;
4082 }
4083
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004084 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004085 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4086 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004087 if (EltSize >= 32) {
4088 // Do the expansion with floating-point types, since that is what the VFP
4089 // registers are defined to use, and since i64 is not legal.
4090 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4091 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004092 SmallVector<SDValue, 8> Ops;
4093 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004094 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004095 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004096 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004097 }
4098
4099 return SDValue();
4100}
4101
Bob Wilson11a1dff2011-01-07 21:37:30 +00004102// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004103// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004104SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4105 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004106 DebugLoc dl = Op.getDebugLoc();
4107 EVT VT = Op.getValueType();
4108 unsigned NumElts = VT.getVectorNumElements();
4109
4110 SmallVector<SDValue, 2> SourceVecs;
4111 SmallVector<unsigned, 2> MinElts;
4112 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004113
Bob Wilson11a1dff2011-01-07 21:37:30 +00004114 for (unsigned i = 0; i < NumElts; ++i) {
4115 SDValue V = Op.getOperand(i);
4116 if (V.getOpcode() == ISD::UNDEF)
4117 continue;
4118 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4119 // A shuffle can only come from building a vector from various
4120 // elements of other vectors.
4121 return SDValue();
Eli Friedman46995fa2011-10-14 23:58:49 +00004122 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
4123 VT.getVectorElementType()) {
4124 // This code doesn't know how to handle shuffles where the vector
4125 // element types do not match (this happens because type legalization
4126 // promotes the return type of EXTRACT_VECTOR_ELT).
4127 // FIXME: It might be appropriate to extend this code to handle
4128 // mismatched types.
4129 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004130 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004131
Bob Wilson11a1dff2011-01-07 21:37:30 +00004132 // Record this extraction against the appropriate vector if possible...
4133 SDValue SourceVec = V.getOperand(0);
4134 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4135 bool FoundSource = false;
4136 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4137 if (SourceVecs[j] == SourceVec) {
4138 if (MinElts[j] > EltNo)
4139 MinElts[j] = EltNo;
4140 if (MaxElts[j] < EltNo)
4141 MaxElts[j] = EltNo;
4142 FoundSource = true;
4143 break;
4144 }
4145 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004146
Bob Wilson11a1dff2011-01-07 21:37:30 +00004147 // Or record a new source if not...
4148 if (!FoundSource) {
4149 SourceVecs.push_back(SourceVec);
4150 MinElts.push_back(EltNo);
4151 MaxElts.push_back(EltNo);
4152 }
4153 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004154
Bob Wilson11a1dff2011-01-07 21:37:30 +00004155 // Currently only do something sane when at most two source vectors
4156 // involved.
4157 if (SourceVecs.size() > 2)
4158 return SDValue();
4159
4160 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4161 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004162
Bob Wilson11a1dff2011-01-07 21:37:30 +00004163 // This loop extracts the usage patterns of the source vectors
4164 // and prepares appropriate SDValues for a shuffle if possible.
4165 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4166 if (SourceVecs[i].getValueType() == VT) {
4167 // No VEXT necessary
4168 ShuffleSrcs[i] = SourceVecs[i];
4169 VEXTOffsets[i] = 0;
4170 continue;
4171 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4172 // It probably isn't worth padding out a smaller vector just to
4173 // break it down again in a shuffle.
4174 return SDValue();
4175 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004176
Bob Wilson11a1dff2011-01-07 21:37:30 +00004177 // Since only 64-bit and 128-bit vectors are legal on ARM and
4178 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004179 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4180 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004181
Bob Wilson11a1dff2011-01-07 21:37:30 +00004182 if (MaxElts[i] - MinElts[i] >= NumElts) {
4183 // Span too large for a VEXT to cope
4184 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004185 }
4186
Bob Wilson11a1dff2011-01-07 21:37:30 +00004187 if (MinElts[i] >= NumElts) {
4188 // The extraction can just take the second half
4189 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004190 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4191 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004192 DAG.getIntPtrConstant(NumElts));
4193 } else if (MaxElts[i] < NumElts) {
4194 // The extraction can just take the first half
4195 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004196 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4197 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004198 DAG.getIntPtrConstant(0));
4199 } else {
4200 // An actual VEXT is needed
4201 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004202 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4203 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004204 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004205 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4206 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004207 DAG.getIntPtrConstant(NumElts));
4208 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4209 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4210 }
4211 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004212
Bob Wilson11a1dff2011-01-07 21:37:30 +00004213 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004214
Bob Wilson11a1dff2011-01-07 21:37:30 +00004215 for (unsigned i = 0; i < NumElts; ++i) {
4216 SDValue Entry = Op.getOperand(i);
4217 if (Entry.getOpcode() == ISD::UNDEF) {
4218 Mask.push_back(-1);
4219 continue;
4220 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004221
Bob Wilson11a1dff2011-01-07 21:37:30 +00004222 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004223 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4224 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004225 if (ExtractVec == SourceVecs[0]) {
4226 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4227 } else {
4228 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4229 }
4230 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004231
Bob Wilson11a1dff2011-01-07 21:37:30 +00004232 // Final check before we try to produce nonsense...
4233 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004234 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4235 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004236
Bob Wilson11a1dff2011-01-07 21:37:30 +00004237 return SDValue();
4238}
4239
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004240/// isShuffleMaskLegal - Targets can use this to indicate that they only
4241/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4242/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4243/// are assumed to be legal.
4244bool
4245ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4246 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004247 if (VT.getVectorNumElements() == 4 &&
4248 (VT.is128BitVector() || VT.is64BitVector())) {
4249 unsigned PFIndexes[4];
4250 for (unsigned i = 0; i != 4; ++i) {
4251 if (M[i] < 0)
4252 PFIndexes[i] = 8;
4253 else
4254 PFIndexes[i] = M[i];
4255 }
4256
4257 // Compute the index in the perfect shuffle table.
4258 unsigned PFTableIndex =
4259 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4260 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4261 unsigned Cost = (PFEntry >> 30);
4262
4263 if (Cost <= 4)
4264 return true;
4265 }
4266
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004267 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004268 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004269
Bob Wilson53dd2452010-06-07 23:53:38 +00004270 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4271 return (EltSize >= 32 ||
4272 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004273 isVREVMask(M, VT, 64) ||
4274 isVREVMask(M, VT, 32) ||
4275 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004276 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004277 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004278 isVTRNMask(M, VT, WhichResult) ||
4279 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004280 isVZIPMask(M, VT, WhichResult) ||
4281 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4282 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4283 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004284}
4285
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004286/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4287/// the specified operations to build the shuffle.
4288static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4289 SDValue RHS, SelectionDAG &DAG,
4290 DebugLoc dl) {
4291 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4292 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4293 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4294
4295 enum {
4296 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4297 OP_VREV,
4298 OP_VDUP0,
4299 OP_VDUP1,
4300 OP_VDUP2,
4301 OP_VDUP3,
4302 OP_VEXT1,
4303 OP_VEXT2,
4304 OP_VEXT3,
4305 OP_VUZPL, // VUZP, left result
4306 OP_VUZPR, // VUZP, right result
4307 OP_VZIPL, // VZIP, left result
4308 OP_VZIPR, // VZIP, right result
4309 OP_VTRNL, // VTRN, left result
4310 OP_VTRNR // VTRN, right result
4311 };
4312
4313 if (OpNum == OP_COPY) {
4314 if (LHSID == (1*9+2)*9+3) return LHS;
4315 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4316 return RHS;
4317 }
4318
4319 SDValue OpLHS, OpRHS;
4320 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4321 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4322 EVT VT = OpLHS.getValueType();
4323
4324 switch (OpNum) {
4325 default: llvm_unreachable("Unknown shuffle opcode!");
4326 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004327 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004328 if (VT.getVectorElementType() == MVT::i32 ||
4329 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004330 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4331 // vrev <4 x i16> -> VREV32
4332 if (VT.getVectorElementType() == MVT::i16)
4333 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4334 // vrev <4 x i8> -> VREV16
4335 assert(VT.getVectorElementType() == MVT::i8);
4336 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004337 case OP_VDUP0:
4338 case OP_VDUP1:
4339 case OP_VDUP2:
4340 case OP_VDUP3:
4341 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004342 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004343 case OP_VEXT1:
4344 case OP_VEXT2:
4345 case OP_VEXT3:
4346 return DAG.getNode(ARMISD::VEXT, dl, VT,
4347 OpLHS, OpRHS,
4348 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4349 case OP_VUZPL:
4350 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004351 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004352 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4353 case OP_VZIPL:
4354 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004355 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004356 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4357 case OP_VTRNL:
4358 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004359 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4360 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004361 }
4362}
4363
Bill Wendling69a05a72011-03-14 23:02:38 +00004364static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
4365 SmallVectorImpl<int> &ShuffleMask,
4366 SelectionDAG &DAG) {
4367 // Check to see if we can use the VTBL instruction.
4368 SDValue V1 = Op.getOperand(0);
4369 SDValue V2 = Op.getOperand(1);
4370 DebugLoc DL = Op.getDebugLoc();
4371
4372 SmallVector<SDValue, 8> VTBLMask;
4373 for (SmallVectorImpl<int>::iterator
4374 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4375 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4376
4377 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4378 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4379 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4380 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004381
Owen Anderson76706012011-04-05 21:48:57 +00004382 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004383 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4384 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004385}
4386
Bob Wilson5bafff32009-06-22 23:27:02 +00004387static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004388 SDValue V1 = Op.getOperand(0);
4389 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004390 DebugLoc dl = Op.getDebugLoc();
4391 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004392 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004393 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00004394
Bob Wilson28865062009-08-13 02:13:04 +00004395 // Convert shuffles that are directly supported on NEON to target-specific
4396 // DAG nodes, instead of keeping them as shuffles and matching them again
4397 // during code selection. This is more efficient and avoids the possibility
4398 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004399 // FIXME: floating-point vectors should be canonicalized to integer vectors
4400 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004401 SVN->getMask(ShuffleMask);
4402
Bob Wilson53dd2452010-06-07 23:53:38 +00004403 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4404 if (EltSize <= 32) {
4405 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4406 int Lane = SVN->getSplatIndex();
4407 // If this is undef splat, generate it via "just" vdup, if possible.
4408 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004409
Dan Gohman65fd6562011-11-03 21:49:52 +00004410 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson53dd2452010-06-07 23:53:38 +00004411 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4412 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4413 }
Dan Gohman65fd6562011-11-03 21:49:52 +00004414 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
4415 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
4416 // reaches it).
4417 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
4418 !isa<ConstantSDNode>(V1.getOperand(0))) {
4419 bool IsScalarToVector = true;
4420 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
4421 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
4422 IsScalarToVector = false;
4423 break;
4424 }
4425 if (IsScalarToVector)
4426 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4427 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004428 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4429 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004430 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004431
4432 bool ReverseVEXT;
4433 unsigned Imm;
4434 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4435 if (ReverseVEXT)
4436 std::swap(V1, V2);
4437 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4438 DAG.getConstant(Imm, MVT::i32));
4439 }
4440
4441 if (isVREVMask(ShuffleMask, VT, 64))
4442 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4443 if (isVREVMask(ShuffleMask, VT, 32))
4444 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4445 if (isVREVMask(ShuffleMask, VT, 16))
4446 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4447
4448 // Check for Neon shuffles that modify both input vectors in place.
4449 // If both results are used, i.e., if there are two shuffles with the same
4450 // source operands and with masks corresponding to both results of one of
4451 // these operations, DAG memoization will ensure that a single node is
4452 // used for both shuffles.
4453 unsigned WhichResult;
4454 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4455 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4456 V1, V2).getValue(WhichResult);
4457 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4458 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4459 V1, V2).getValue(WhichResult);
4460 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4461 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4462 V1, V2).getValue(WhichResult);
4463
4464 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4465 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4466 V1, V1).getValue(WhichResult);
4467 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4468 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4469 V1, V1).getValue(WhichResult);
4470 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4471 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4472 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004473 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004474
Bob Wilsonc692cb72009-08-21 20:54:19 +00004475 // If the shuffle is not directly supported and it has 4 elements, use
4476 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004477 unsigned NumElts = VT.getVectorNumElements();
4478 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004479 unsigned PFIndexes[4];
4480 for (unsigned i = 0; i != 4; ++i) {
4481 if (ShuffleMask[i] < 0)
4482 PFIndexes[i] = 8;
4483 else
4484 PFIndexes[i] = ShuffleMask[i];
4485 }
4486
4487 // Compute the index in the perfect shuffle table.
4488 unsigned PFTableIndex =
4489 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004490 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4491 unsigned Cost = (PFEntry >> 30);
4492
4493 if (Cost <= 4)
4494 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4495 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004496
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004497 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004498 if (EltSize >= 32) {
4499 // Do the expansion with floating-point types, since that is what the VFP
4500 // registers are defined to use, and since i64 is not legal.
4501 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4502 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004503 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4504 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004505 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004506 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004507 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004508 Ops.push_back(DAG.getUNDEF(EltVT));
4509 else
4510 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4511 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4512 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4513 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004514 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004515 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004516 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004517 }
4518
Bill Wendling69a05a72011-03-14 23:02:38 +00004519 if (VT == MVT::v8i8) {
4520 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4521 if (NewOp.getNode())
4522 return NewOp;
4523 }
4524
Bob Wilson22cac0d2009-08-14 05:16:33 +00004525 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004526}
4527
Eli Friedman5c89cb82011-10-24 23:08:52 +00004528static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
4529 // INSERT_VECTOR_ELT is legal only for immediate indexes.
4530 SDValue Lane = Op.getOperand(2);
4531 if (!isa<ConstantSDNode>(Lane))
4532 return SDValue();
4533
4534 return Op;
4535}
4536
Bob Wilson5bafff32009-06-22 23:27:02 +00004537static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004538 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004539 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004540 if (!isa<ConstantSDNode>(Lane))
4541 return SDValue();
4542
4543 SDValue Vec = Op.getOperand(0);
4544 if (Op.getValueType() == MVT::i32 &&
4545 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4546 DebugLoc dl = Op.getDebugLoc();
4547 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4548 }
4549
4550 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004551}
4552
Bob Wilsona6d65862009-08-03 20:36:38 +00004553static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4554 // The only time a CONCAT_VECTORS operation can have legal types is when
4555 // two 64-bit vectors are concatenated to a 128-bit vector.
4556 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4557 "unexpected CONCAT_VECTORS");
4558 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004559 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004560 SDValue Op0 = Op.getOperand(0);
4561 SDValue Op1 = Op.getOperand(1);
4562 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004563 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004564 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004565 DAG.getIntPtrConstant(0));
4566 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004567 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004568 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004569 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004570 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004571}
4572
Bob Wilson626613d2010-11-23 19:38:38 +00004573/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4574/// element has been zero/sign-extended, depending on the isSigned parameter,
4575/// from an integer type half its size.
4576static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4577 bool isSigned) {
4578 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4579 EVT VT = N->getValueType(0);
4580 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4581 SDNode *BVN = N->getOperand(0).getNode();
4582 if (BVN->getValueType(0) != MVT::v4i32 ||
4583 BVN->getOpcode() != ISD::BUILD_VECTOR)
4584 return false;
4585 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4586 unsigned HiElt = 1 - LoElt;
4587 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4588 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4589 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4590 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4591 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4592 return false;
4593 if (isSigned) {
4594 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4595 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4596 return true;
4597 } else {
4598 if (Hi0->isNullValue() && Hi1->isNullValue())
4599 return true;
4600 }
4601 return false;
4602 }
4603
4604 if (N->getOpcode() != ISD::BUILD_VECTOR)
4605 return false;
4606
4607 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4608 SDNode *Elt = N->getOperand(i).getNode();
4609 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4610 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4611 unsigned HalfSize = EltSize / 2;
4612 if (isSigned) {
Bob Wilson9d45de22011-10-18 18:46:49 +00004613 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004614 return false;
4615 } else {
Bob Wilson9d45de22011-10-18 18:46:49 +00004616 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004617 return false;
4618 }
4619 continue;
4620 }
4621 return false;
4622 }
4623
4624 return true;
4625}
4626
4627/// isSignExtended - Check if a node is a vector value that is sign-extended
4628/// or a constant BUILD_VECTOR with sign-extended elements.
4629static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4630 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4631 return true;
4632 if (isExtendedBUILD_VECTOR(N, DAG, true))
4633 return true;
4634 return false;
4635}
4636
4637/// isZeroExtended - Check if a node is a vector value that is zero-extended
4638/// or a constant BUILD_VECTOR with zero-extended elements.
4639static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4640 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4641 return true;
4642 if (isExtendedBUILD_VECTOR(N, DAG, false))
4643 return true;
4644 return false;
4645}
4646
4647/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4648/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004649static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4650 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4651 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004652 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4653 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4654 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004655 LD->isNonTemporal(), LD->isInvariant(),
4656 LD->getAlignment());
Bob Wilson626613d2010-11-23 19:38:38 +00004657 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4658 // have been legalized as a BITCAST from v4i32.
4659 if (N->getOpcode() == ISD::BITCAST) {
4660 SDNode *BVN = N->getOperand(0).getNode();
4661 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4662 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4663 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4664 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4665 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4666 }
4667 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4668 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4669 EVT VT = N->getValueType(0);
4670 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4671 unsigned NumElts = VT.getVectorNumElements();
4672 MVT TruncVT = MVT::getIntegerVT(EltSize);
4673 SmallVector<SDValue, 8> Ops;
4674 for (unsigned i = 0; i != NumElts; ++i) {
4675 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4676 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004677 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004678 }
4679 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4680 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004681}
4682
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004683static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4684 unsigned Opcode = N->getOpcode();
4685 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4686 SDNode *N0 = N->getOperand(0).getNode();
4687 SDNode *N1 = N->getOperand(1).getNode();
4688 return N0->hasOneUse() && N1->hasOneUse() &&
4689 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4690 }
4691 return false;
4692}
4693
4694static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4695 unsigned Opcode = N->getOpcode();
4696 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4697 SDNode *N0 = N->getOperand(0).getNode();
4698 SDNode *N1 = N->getOperand(1).getNode();
4699 return N0->hasOneUse() && N1->hasOneUse() &&
4700 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4701 }
4702 return false;
4703}
4704
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004705static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4706 // Multiplications are only custom-lowered for 128-bit vectors so that
4707 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4708 EVT VT = Op.getValueType();
4709 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4710 SDNode *N0 = Op.getOperand(0).getNode();
4711 SDNode *N1 = Op.getOperand(1).getNode();
4712 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004713 bool isMLA = false;
4714 bool isN0SExt = isSignExtended(N0, DAG);
4715 bool isN1SExt = isSignExtended(N1, DAG);
4716 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004717 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004718 else {
4719 bool isN0ZExt = isZeroExtended(N0, DAG);
4720 bool isN1ZExt = isZeroExtended(N1, DAG);
4721 if (isN0ZExt && isN1ZExt)
4722 NewOpc = ARMISD::VMULLu;
4723 else if (isN1SExt || isN1ZExt) {
4724 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4725 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4726 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4727 NewOpc = ARMISD::VMULLs;
4728 isMLA = true;
4729 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4730 NewOpc = ARMISD::VMULLu;
4731 isMLA = true;
4732 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4733 std::swap(N0, N1);
4734 NewOpc = ARMISD::VMULLu;
4735 isMLA = true;
4736 }
4737 }
4738
4739 if (!NewOpc) {
4740 if (VT == MVT::v2i64)
4741 // Fall through to expand this. It is not legal.
4742 return SDValue();
4743 else
4744 // Other vector multiplications are legal.
4745 return Op;
4746 }
4747 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004748
4749 // Legalize to a VMULL instruction.
4750 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004751 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004752 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004753 if (!isMLA) {
4754 Op0 = SkipExtension(N0, DAG);
4755 assert(Op0.getValueType().is64BitVector() &&
4756 Op1.getValueType().is64BitVector() &&
4757 "unexpected types for extended operands to VMULL");
4758 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4759 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004760
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004761 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4762 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4763 // vmull q0, d4, d6
4764 // vmlal q0, d5, d6
4765 // is faster than
4766 // vaddl q0, d4, d5
4767 // vmovl q1, d6
4768 // vmul q0, q0, q1
4769 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4770 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4771 EVT Op1VT = Op1.getValueType();
4772 return DAG.getNode(N0->getOpcode(), DL, VT,
4773 DAG.getNode(NewOpc, DL, VT,
4774 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4775 DAG.getNode(NewOpc, DL, VT,
4776 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004777}
4778
Owen Anderson76706012011-04-05 21:48:57 +00004779static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004780LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4781 // Convert to float
4782 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4783 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4784 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4785 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4786 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4787 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4788 // Get reciprocal estimate.
4789 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004790 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004791 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4792 // Because char has a smaller range than uchar, we can actually get away
4793 // without any newton steps. This requires that we use a weird bias
4794 // of 0xb000, however (again, this has been exhaustively tested).
4795 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4796 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4797 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4798 Y = DAG.getConstant(0xb000, MVT::i32);
4799 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4800 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4801 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4802 // Convert back to short.
4803 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4804 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4805 return X;
4806}
4807
Owen Anderson76706012011-04-05 21:48:57 +00004808static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004809LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4810 SDValue N2;
4811 // Convert to float.
4812 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4813 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4814 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4815 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4816 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4817 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004818
Nate Begeman7973f352011-02-11 20:53:29 +00004819 // Use reciprocal estimate and one refinement step.
4820 // float4 recip = vrecpeq_f32(yf);
4821 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004822 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004823 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004824 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004825 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4826 N1, N2);
4827 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4828 // Because short has a smaller range than ushort, we can actually get away
4829 // with only a single newton step. This requires that we use a weird bias
4830 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004831 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004832 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4833 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004834 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004835 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4836 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4837 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4838 // Convert back to integer and return.
4839 // return vmovn_s32(vcvt_s32_f32(result));
4840 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4841 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4842 return N0;
4843}
4844
4845static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4846 EVT VT = Op.getValueType();
4847 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4848 "unexpected type for custom-lowering ISD::SDIV");
4849
4850 DebugLoc dl = Op.getDebugLoc();
4851 SDValue N0 = Op.getOperand(0);
4852 SDValue N1 = Op.getOperand(1);
4853 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004854
Nate Begeman7973f352011-02-11 20:53:29 +00004855 if (VT == MVT::v8i8) {
4856 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4857 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004858
Nate Begeman7973f352011-02-11 20:53:29 +00004859 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4860 DAG.getIntPtrConstant(4));
4861 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004862 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004863 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4864 DAG.getIntPtrConstant(0));
4865 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4866 DAG.getIntPtrConstant(0));
4867
4868 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4869 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4870
4871 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4872 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004873
Nate Begeman7973f352011-02-11 20:53:29 +00004874 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
4875 return N0;
4876 }
4877 return LowerSDIV_v4i16(N0, N1, dl, DAG);
4878}
4879
4880static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
4881 EVT VT = Op.getValueType();
4882 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4883 "unexpected type for custom-lowering ISD::UDIV");
4884
4885 DebugLoc dl = Op.getDebugLoc();
4886 SDValue N0 = Op.getOperand(0);
4887 SDValue N1 = Op.getOperand(1);
4888 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004889
Nate Begeman7973f352011-02-11 20:53:29 +00004890 if (VT == MVT::v8i8) {
4891 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
4892 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004893
Nate Begeman7973f352011-02-11 20:53:29 +00004894 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4895 DAG.getIntPtrConstant(4));
4896 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004897 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004898 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4899 DAG.getIntPtrConstant(0));
4900 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4901 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00004902
Nate Begeman7973f352011-02-11 20:53:29 +00004903 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
4904 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00004905
Nate Begeman7973f352011-02-11 20:53:29 +00004906 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4907 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004908
4909 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00004910 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
4911 N0);
4912 return N0;
4913 }
Owen Anderson76706012011-04-05 21:48:57 +00004914
Nate Begeman7973f352011-02-11 20:53:29 +00004915 // v4i16 sdiv ... Convert to float.
4916 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
4917 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
4918 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
4919 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
4920 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004921 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00004922
4923 // Use reciprocal estimate and two refinement steps.
4924 // float4 recip = vrecpeq_f32(yf);
4925 // recip *= vrecpsq_f32(yf, recip);
4926 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004927 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004928 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00004929 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004930 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004931 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004932 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00004933 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004934 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004935 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004936 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4937 // Simply multiplying by the reciprocal estimate can leave us a few ulps
4938 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
4939 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004940 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00004941 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4942 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4943 N1 = DAG.getConstant(2, MVT::i32);
4944 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4945 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4946 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4947 // Convert back to integer and return.
4948 // return vmovn_u32(vcvt_s32_f32(result));
4949 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4950 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4951 return N0;
4952}
4953
Evan Cheng342e3162011-08-30 01:34:54 +00004954static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
4955 EVT VT = Op.getNode()->getValueType(0);
4956 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
4957
4958 unsigned Opc;
4959 bool ExtraOp = false;
4960 switch (Op.getOpcode()) {
4961 default: assert(0 && "Invalid code");
4962 case ISD::ADDC: Opc = ARMISD::ADDC; break;
4963 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
4964 case ISD::SUBC: Opc = ARMISD::SUBC; break;
4965 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
4966 }
4967
4968 if (!ExtraOp)
4969 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
4970 Op.getOperand(1));
4971 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
4972 Op.getOperand(1), Op.getOperand(2));
4973}
4974
Eli Friedman74bf18c2011-09-15 22:26:18 +00004975static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedman7cc15662011-09-15 22:18:49 +00004976 // Monotonic load/store is legal for all targets
4977 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
4978 return Op;
4979
4980 // Aquire/Release load/store is not legal for targets without a
4981 // dmb or equivalent available.
4982 return SDValue();
4983}
4984
4985
Eli Friedman2bdffe42011-08-31 00:31:29 +00004986static void
Eli Friedman4d3f3292011-08-31 17:52:22 +00004987ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
4988 SelectionDAG &DAG, unsigned NewOp) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00004989 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +00004990 assert (Node->getValueType(0) == MVT::i64 &&
4991 "Only know how to expand i64 atomics");
Eli Friedman2bdffe42011-08-31 00:31:29 +00004992
Eli Friedman4d3f3292011-08-31 17:52:22 +00004993 SmallVector<SDValue, 6> Ops;
4994 Ops.push_back(Node->getOperand(0)); // Chain
4995 Ops.push_back(Node->getOperand(1)); // Ptr
4996 // Low part of Val1
4997 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4998 Node->getOperand(2), DAG.getIntPtrConstant(0)));
4999 // High part of Val1
5000 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5001 Node->getOperand(2), DAG.getIntPtrConstant(1)));
Andrew Trick3af7a672011-09-20 03:06:13 +00005002 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) {
Eli Friedman4d3f3292011-08-31 17:52:22 +00005003 // High part of Val1
5004 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5005 Node->getOperand(3), DAG.getIntPtrConstant(0)));
5006 // High part of Val2
5007 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5008 Node->getOperand(3), DAG.getIntPtrConstant(1)));
5009 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005010 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
5011 SDValue Result =
Eli Friedman4d3f3292011-08-31 17:52:22 +00005012 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64,
Eli Friedman2bdffe42011-08-31 00:31:29 +00005013 cast<MemSDNode>(Node)->getMemOperand());
Eli Friedman4d3f3292011-08-31 17:52:22 +00005014 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) };
Eli Friedman2bdffe42011-08-31 00:31:29 +00005015 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
5016 Results.push_back(Result.getValue(2));
5017}
5018
Dan Gohmand858e902010-04-17 15:26:15 +00005019SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005020 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005021 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00005022 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00005023 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005024 case ISD::GlobalAddress:
5025 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
5026 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00005027 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00005028 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00005029 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
5030 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005031 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00005032 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00005033 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00005034 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00005035 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00005036 case ISD::SINT_TO_FP:
5037 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
5038 case ISD::FP_TO_SINT:
5039 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005040 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00005041 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00005042 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005043 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00005044 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00005045 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00005046 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
5047 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00005048 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005049 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00005050 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00005051 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00005052 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00005053 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00005054 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00005055 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Duncan Sands28b77e92011-09-06 19:07:46 +00005056 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00005057 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005058 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedman5c89cb82011-10-24 23:08:52 +00005059 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005060 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00005061 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00005062 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005063 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00005064 case ISD::SDIV: return LowerSDIV(Op, DAG);
5065 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00005066 case ISD::ADDC:
5067 case ISD::ADDE:
5068 case ISD::SUBC:
5069 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Eli Friedman7cc15662011-09-15 22:18:49 +00005070 case ISD::ATOMIC_LOAD:
Eli Friedman74bf18c2011-09-15 22:26:18 +00005071 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005072 }
Dan Gohman475871a2008-07-27 21:46:04 +00005073 return SDValue();
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!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00005085 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005086 case ISD::BITCAST:
5087 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005088 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005089 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00005090 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00005091 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005092 break;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005093 case ISD::ATOMIC_LOAD_ADD:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005094 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005095 return;
5096 case ISD::ATOMIC_LOAD_AND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005097 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005098 return;
5099 case ISD::ATOMIC_LOAD_NAND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005100 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005101 return;
5102 case ISD::ATOMIC_LOAD_OR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005103 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005104 return;
5105 case ISD::ATOMIC_LOAD_SUB:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005106 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005107 return;
5108 case ISD::ATOMIC_LOAD_XOR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005109 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005110 return;
5111 case ISD::ATOMIC_SWAP:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005112 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005113 return;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005114 case ISD::ATOMIC_CMP_SWAP:
5115 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG);
5116 return;
Duncan Sands1607f052008-12-01 11:39:25 +00005117 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00005118 if (Res.getNode())
5119 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00005120}
Chris Lattner27a6c732007-11-24 07:07:01 +00005121
Evan Chenga8e29892007-01-19 07:51:42 +00005122//===----------------------------------------------------------------------===//
5123// ARM Scheduler Hooks
5124//===----------------------------------------------------------------------===//
5125
5126MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005127ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
5128 MachineBasicBlock *BB,
5129 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005130 unsigned dest = MI->getOperand(0).getReg();
5131 unsigned ptr = MI->getOperand(1).getReg();
5132 unsigned oldval = MI->getOperand(2).getReg();
5133 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005134 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5135 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005136 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005137
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005138 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5139 unsigned scratch =
Cameron Zwarich141ec632011-05-18 02:29:50 +00005140 MRI.createVirtualRegister(isThumb2 ? ARM::rGPRRegisterClass
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005141 : ARM::GPRRegisterClass);
5142
5143 if (isThumb2) {
Cameron Zwarich141ec632011-05-18 02:29:50 +00005144 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5145 MRI.constrainRegClass(oldval, ARM::rGPRRegisterClass);
5146 MRI.constrainRegClass(newval, ARM::rGPRRegisterClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005147 }
5148
Jim Grosbach5278eb82009-12-11 01:42:04 +00005149 unsigned ldrOpc, strOpc;
5150 switch (Size) {
5151 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005152 case 1:
5153 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00005154 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005155 break;
5156 case 2:
5157 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5158 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5159 break;
5160 case 4:
5161 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5162 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5163 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00005164 }
5165
5166 MachineFunction *MF = BB->getParent();
5167 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5168 MachineFunction::iterator It = BB;
5169 ++It; // insert the new blocks after the current block
5170
5171 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5172 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5173 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5174 MF->insert(It, loop1MBB);
5175 MF->insert(It, loop2MBB);
5176 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005177
5178 // Transfer the remainder of BB and its successor edges to exitMBB.
5179 exitMBB->splice(exitMBB->begin(), BB,
5180 llvm::next(MachineBasicBlock::iterator(MI)),
5181 BB->end());
5182 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005183
5184 // thisMBB:
5185 // ...
5186 // fallthrough --> loop1MBB
5187 BB->addSuccessor(loop1MBB);
5188
5189 // loop1MBB:
5190 // ldrex dest, [ptr]
5191 // cmp dest, oldval
5192 // bne exitMBB
5193 BB = loop1MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005194 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5195 if (ldrOpc == ARM::t2LDREX)
5196 MIB.addImm(0);
5197 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005198 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005199 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005200 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5201 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005202 BB->addSuccessor(loop2MBB);
5203 BB->addSuccessor(exitMBB);
5204
5205 // loop2MBB:
5206 // strex scratch, newval, [ptr]
5207 // cmp scratch, #0
5208 // bne loop1MBB
5209 BB = loop2MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005210 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr);
5211 if (strOpc == ARM::t2STREX)
5212 MIB.addImm(0);
5213 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005214 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005215 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005216 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5217 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005218 BB->addSuccessor(loop1MBB);
5219 BB->addSuccessor(exitMBB);
5220
5221 // exitMBB:
5222 // ...
5223 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005224
Dan Gohman14152b42010-07-06 20:24:04 +00005225 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005226
Jim Grosbach5278eb82009-12-11 01:42:04 +00005227 return BB;
5228}
5229
5230MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005231ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5232 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005233 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5234 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5235
5236 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005237 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005238 MachineFunction::iterator It = BB;
5239 ++It;
5240
5241 unsigned dest = MI->getOperand(0).getReg();
5242 unsigned ptr = MI->getOperand(1).getReg();
5243 unsigned incr = MI->getOperand(2).getReg();
5244 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005245 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005246
5247 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5248 if (isThumb2) {
5249 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5250 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5251 }
5252
Jim Grosbachc3c23542009-12-14 04:22:04 +00005253 unsigned ldrOpc, strOpc;
5254 switch (Size) {
5255 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005256 case 1:
5257 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005258 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005259 break;
5260 case 2:
5261 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5262 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5263 break;
5264 case 4:
5265 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5266 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5267 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005268 }
5269
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005270 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5271 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5272 MF->insert(It, loopMBB);
5273 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005274
5275 // Transfer the remainder of BB and its successor edges to exitMBB.
5276 exitMBB->splice(exitMBB->begin(), BB,
5277 llvm::next(MachineBasicBlock::iterator(MI)),
5278 BB->end());
5279 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005280
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005281 TargetRegisterClass *TRC =
5282 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5283 unsigned scratch = MRI.createVirtualRegister(TRC);
5284 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005285
5286 // thisMBB:
5287 // ...
5288 // fallthrough --> loopMBB
5289 BB->addSuccessor(loopMBB);
5290
5291 // loopMBB:
5292 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005293 // <binop> scratch2, dest, incr
5294 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005295 // cmp scratch, #0
5296 // bne- loopMBB
5297 // fallthrough --> exitMBB
5298 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005299 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5300 if (ldrOpc == ARM::t2LDREX)
5301 MIB.addImm(0);
5302 AddDefaultPred(MIB);
Jim Grosbachc67b5562009-12-15 00:12:35 +00005303 if (BinOpcode) {
5304 // operand order needs to go the other way for NAND
5305 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5306 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5307 addReg(incr).addReg(dest)).addReg(0);
5308 else
5309 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5310 addReg(dest).addReg(incr)).addReg(0);
5311 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005312
Jim Grosbachb6aed502011-09-09 18:37:27 +00005313 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5314 if (strOpc == ARM::t2STREX)
5315 MIB.addImm(0);
5316 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005317 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005318 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005319 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5320 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005321
5322 BB->addSuccessor(loopMBB);
5323 BB->addSuccessor(exitMBB);
5324
5325 // exitMBB:
5326 // ...
5327 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005328
Dan Gohman14152b42010-07-06 20:24:04 +00005329 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005330
Jim Grosbachc3c23542009-12-14 04:22:04 +00005331 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005332}
5333
Jim Grosbachf7da8822011-04-26 19:44:18 +00005334MachineBasicBlock *
5335ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5336 MachineBasicBlock *BB,
5337 unsigned Size,
5338 bool signExtend,
5339 ARMCC::CondCodes Cond) const {
5340 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5341
5342 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5343 MachineFunction *MF = BB->getParent();
5344 MachineFunction::iterator It = BB;
5345 ++It;
5346
5347 unsigned dest = MI->getOperand(0).getReg();
5348 unsigned ptr = MI->getOperand(1).getReg();
5349 unsigned incr = MI->getOperand(2).getReg();
5350 unsigned oldval = dest;
5351 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005352 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005353
5354 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5355 if (isThumb2) {
5356 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5357 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5358 }
5359
Jim Grosbachf7da8822011-04-26 19:44:18 +00005360 unsigned ldrOpc, strOpc, extendOpc;
5361 switch (Size) {
5362 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5363 case 1:
5364 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5365 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005366 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005367 break;
5368 case 2:
5369 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5370 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005371 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005372 break;
5373 case 4:
5374 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5375 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5376 extendOpc = 0;
5377 break;
5378 }
5379
5380 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5381 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5382 MF->insert(It, loopMBB);
5383 MF->insert(It, exitMBB);
5384
5385 // Transfer the remainder of BB and its successor edges to exitMBB.
5386 exitMBB->splice(exitMBB->begin(), BB,
5387 llvm::next(MachineBasicBlock::iterator(MI)),
5388 BB->end());
5389 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5390
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005391 TargetRegisterClass *TRC =
5392 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5393 unsigned scratch = MRI.createVirtualRegister(TRC);
5394 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005395
5396 // thisMBB:
5397 // ...
5398 // fallthrough --> loopMBB
5399 BB->addSuccessor(loopMBB);
5400
5401 // loopMBB:
5402 // ldrex dest, ptr
5403 // (sign extend dest, if required)
5404 // cmp dest, incr
5405 // cmov.cond scratch2, dest, incr
5406 // strex scratch, scratch2, ptr
5407 // cmp scratch, #0
5408 // bne- loopMBB
5409 // fallthrough --> exitMBB
5410 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005411 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5412 if (ldrOpc == ARM::t2LDREX)
5413 MIB.addImm(0);
5414 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005415
5416 // Sign extend the value, if necessary.
5417 if (signExtend && extendOpc) {
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005418 oldval = MRI.createVirtualRegister(ARM::GPRRegisterClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005419 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5420 .addReg(dest)
5421 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005422 }
5423
5424 // Build compare and cmov instructions.
5425 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5426 .addReg(oldval).addReg(incr));
5427 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5428 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5429
Jim Grosbachb6aed502011-09-09 18:37:27 +00005430 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5431 if (strOpc == ARM::t2STREX)
5432 MIB.addImm(0);
5433 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005434 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5435 .addReg(scratch).addImm(0));
5436 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5437 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5438
5439 BB->addSuccessor(loopMBB);
5440 BB->addSuccessor(exitMBB);
5441
5442 // exitMBB:
5443 // ...
5444 BB = exitMBB;
5445
5446 MI->eraseFromParent(); // The instruction is gone now.
5447
5448 return BB;
5449}
5450
Eli Friedman2bdffe42011-08-31 00:31:29 +00005451MachineBasicBlock *
5452ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
5453 unsigned Op1, unsigned Op2,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005454 bool NeedsCarry, bool IsCmpxchg) const {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005455 // This also handles ATOMIC_SWAP, indicated by Op1==0.
5456 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5457
5458 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5459 MachineFunction *MF = BB->getParent();
5460 MachineFunction::iterator It = BB;
5461 ++It;
5462
5463 unsigned destlo = MI->getOperand(0).getReg();
5464 unsigned desthi = MI->getOperand(1).getReg();
5465 unsigned ptr = MI->getOperand(2).getReg();
5466 unsigned vallo = MI->getOperand(3).getReg();
5467 unsigned valhi = MI->getOperand(4).getReg();
5468 DebugLoc dl = MI->getDebugLoc();
5469 bool isThumb2 = Subtarget->isThumb2();
5470
5471 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5472 if (isThumb2) {
5473 MRI.constrainRegClass(destlo, ARM::rGPRRegisterClass);
5474 MRI.constrainRegClass(desthi, ARM::rGPRRegisterClass);
5475 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5476 }
5477
5478 unsigned ldrOpc = isThumb2 ? ARM::t2LDREXD : ARM::LDREXD;
5479 unsigned strOpc = isThumb2 ? ARM::t2STREXD : ARM::STREXD;
5480
5481 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman7df496d2011-09-01 22:27:41 +00005482 MachineBasicBlock *contBB = 0, *cont2BB = 0;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005483 if (IsCmpxchg) {
5484 contBB = MF->CreateMachineBasicBlock(LLVM_BB);
5485 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB);
5486 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005487 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5488 MF->insert(It, loopMBB);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005489 if (IsCmpxchg) {
5490 MF->insert(It, contBB);
5491 MF->insert(It, cont2BB);
5492 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005493 MF->insert(It, exitMBB);
5494
5495 // Transfer the remainder of BB and its successor edges to exitMBB.
5496 exitMBB->splice(exitMBB->begin(), BB,
5497 llvm::next(MachineBasicBlock::iterator(MI)),
5498 BB->end());
5499 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5500
5501 TargetRegisterClass *TRC =
5502 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5503 unsigned storesuccess = MRI.createVirtualRegister(TRC);
5504
5505 // thisMBB:
5506 // ...
5507 // fallthrough --> loopMBB
5508 BB->addSuccessor(loopMBB);
5509
5510 // loopMBB:
5511 // ldrexd r2, r3, ptr
5512 // <binopa> r0, r2, incr
5513 // <binopb> r1, r3, incr
5514 // strexd storesuccess, r0, r1, ptr
5515 // cmp storesuccess, #0
5516 // bne- loopMBB
5517 // fallthrough --> exitMBB
5518 //
5519 // Note that the registers are explicitly specified because there is not any
5520 // way to force the register allocator to allocate a register pair.
5521 //
Andrew Trick3af7a672011-09-20 03:06:13 +00005522 // FIXME: The hardcoded registers are not necessary for Thumb2, but we
Eli Friedman2bdffe42011-08-31 00:31:29 +00005523 // need to properly enforce the restriction that the two output registers
5524 // for ldrexd must be different.
5525 BB = loopMBB;
5526 // Load
5527 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc))
5528 .addReg(ARM::R2, RegState::Define)
5529 .addReg(ARM::R3, RegState::Define).addReg(ptr));
5530 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
5531 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo).addReg(ARM::R2);
5532 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi).addReg(ARM::R3);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005533
5534 if (IsCmpxchg) {
5535 // Add early exit
5536 for (unsigned i = 0; i < 2; i++) {
5537 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr :
5538 ARM::CMPrr))
5539 .addReg(i == 0 ? destlo : desthi)
5540 .addReg(i == 0 ? vallo : valhi));
5541 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5542 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5543 BB->addSuccessor(exitMBB);
5544 BB->addSuccessor(i == 0 ? contBB : cont2BB);
5545 BB = (i == 0 ? contBB : cont2BB);
5546 }
5547
5548 // Copy to physregs for strexd
5549 unsigned setlo = MI->getOperand(5).getReg();
5550 unsigned sethi = MI->getOperand(6).getReg();
5551 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(setlo);
5552 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(sethi);
5553 } else if (Op1) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005554 // Perform binary operation
5555 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), ARM::R0)
5556 .addReg(destlo).addReg(vallo))
5557 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
5558 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), ARM::R1)
5559 .addReg(desthi).addReg(valhi)).addReg(0);
5560 } else {
5561 // Copy to physregs for strexd
5562 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(vallo);
5563 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(valhi);
5564 }
5565
5566 // Store
5567 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), storesuccess)
5568 .addReg(ARM::R0).addReg(ARM::R1).addReg(ptr));
5569 // Cmp+jump
5570 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5571 .addReg(storesuccess).addImm(0));
5572 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5573 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5574
5575 BB->addSuccessor(loopMBB);
5576 BB->addSuccessor(exitMBB);
5577
5578 // exitMBB:
5579 // ...
5580 BB = exitMBB;
5581
5582 MI->eraseFromParent(); // The instruction is gone now.
5583
5584 return BB;
5585}
5586
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005587/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
5588/// registers the function context.
5589void ARMTargetLowering::
5590SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
5591 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005592 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5593 DebugLoc dl = MI->getDebugLoc();
5594 MachineFunction *MF = MBB->getParent();
5595 MachineRegisterInfo *MRI = &MF->getRegInfo();
5596 MachineConstantPool *MCP = MF->getConstantPool();
5597 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5598 const Function *F = MF->getFunction();
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005599
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005600 bool isThumb = Subtarget->isThumb();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005601 bool isThumb2 = Subtarget->isThumb2();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005602
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005603 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005604 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005605 ARMConstantPoolValue *CPV =
5606 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
5607 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
5608
5609 const TargetRegisterClass *TRC =
5610 isThumb ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5611
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005612 // Grab constant pool and fixed stack memory operands.
5613 MachineMemOperand *CPMMO =
5614 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
5615 MachineMemOperand::MOLoad, 4, 4);
5616
5617 MachineMemOperand *FIMMOSt =
5618 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
5619 MachineMemOperand::MOStore, 4, 4);
5620
5621 // Load the address of the dispatch MBB into the jump buffer.
5622 if (isThumb2) {
5623 // Incoming value: jbuf
5624 // ldr.n r5, LCPI1_1
5625 // orr r5, r5, #1
5626 // add r5, pc
5627 // str r5, [$jbuf, #+4] ; &jbuf[1]
5628 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5629 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
5630 .addConstantPoolIndex(CPI)
5631 .addMemOperand(CPMMO));
5632 // Set the low bit because of thumb mode.
5633 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5634 AddDefaultCC(
5635 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
5636 .addReg(NewVReg1, RegState::Kill)
5637 .addImm(0x01)));
5638 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5639 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
5640 .addReg(NewVReg2, RegState::Kill)
5641 .addImm(PCLabelId);
5642 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
5643 .addReg(NewVReg3, RegState::Kill)
5644 .addFrameIndex(FI)
5645 .addImm(36) // &jbuf[1] :: pc
5646 .addMemOperand(FIMMOSt));
5647 } else if (isThumb) {
5648 // Incoming value: jbuf
5649 // ldr.n r1, LCPI1_4
5650 // add r1, pc
5651 // mov r2, #1
5652 // orrs r1, r2
5653 // add r2, $jbuf, #+4 ; &jbuf[1]
5654 // str r1, [r2]
5655 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5656 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
5657 .addConstantPoolIndex(CPI)
5658 .addMemOperand(CPMMO));
5659 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5660 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
5661 .addReg(NewVReg1, RegState::Kill)
5662 .addImm(PCLabelId);
5663 // Set the low bit because of thumb mode.
5664 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5665 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
5666 .addReg(ARM::CPSR, RegState::Define)
5667 .addImm(1));
5668 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5669 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
5670 .addReg(ARM::CPSR, RegState::Define)
5671 .addReg(NewVReg2, RegState::Kill)
5672 .addReg(NewVReg3, RegState::Kill));
5673 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5674 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
5675 .addFrameIndex(FI)
5676 .addImm(36)); // &jbuf[1] :: pc
5677 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
5678 .addReg(NewVReg4, RegState::Kill)
5679 .addReg(NewVReg5, RegState::Kill)
5680 .addImm(0)
5681 .addMemOperand(FIMMOSt));
5682 } else {
5683 // Incoming value: jbuf
5684 // ldr r1, LCPI1_1
5685 // add r1, pc, r1
5686 // str r1, [$jbuf, #+4] ; &jbuf[1]
5687 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5688 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
5689 .addConstantPoolIndex(CPI)
5690 .addImm(0)
5691 .addMemOperand(CPMMO));
5692 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5693 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
5694 .addReg(NewVReg1, RegState::Kill)
5695 .addImm(PCLabelId));
5696 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
5697 .addReg(NewVReg2, RegState::Kill)
5698 .addFrameIndex(FI)
5699 .addImm(36) // &jbuf[1] :: pc
5700 .addMemOperand(FIMMOSt));
5701 }
5702}
5703
5704MachineBasicBlock *ARMTargetLowering::
5705EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
5706 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5707 DebugLoc dl = MI->getDebugLoc();
5708 MachineFunction *MF = MBB->getParent();
5709 MachineRegisterInfo *MRI = &MF->getRegInfo();
5710 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5711 MachineFrameInfo *MFI = MF->getFrameInfo();
5712 int FI = MFI->getFunctionContextIndex();
5713
5714 const TargetRegisterClass *TRC =
5715 Subtarget->isThumb() ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5716
Bill Wendling04f15b42011-10-06 21:29:56 +00005717 // Get a mapping of the call site numbers to all of the landing pads they're
5718 // associated with.
Bill Wendling2a850152011-10-05 00:02:33 +00005719 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
5720 unsigned MaxCSNum = 0;
5721 MachineModuleInfo &MMI = MF->getMMI();
5722 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E; ++BB) {
5723 if (!BB->isLandingPad()) continue;
5724
5725 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
5726 // pad.
5727 for (MachineBasicBlock::iterator
5728 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
5729 if (!II->isEHLabel()) continue;
5730
5731 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendling5cbef192011-10-05 23:28:57 +00005732 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling2a850152011-10-05 00:02:33 +00005733
Bill Wendling5cbef192011-10-05 23:28:57 +00005734 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
5735 for (SmallVectorImpl<unsigned>::iterator
5736 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
5737 CSI != CSE; ++CSI) {
5738 CallSiteNumToLPad[*CSI].push_back(BB);
5739 MaxCSNum = std::max(MaxCSNum, *CSI);
5740 }
Bill Wendling2a850152011-10-05 00:02:33 +00005741 break;
5742 }
5743 }
5744
5745 // Get an ordered list of the machine basic blocks for the jump table.
5746 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling2acf6382011-10-07 23:18:02 +00005747 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling2a850152011-10-05 00:02:33 +00005748 LPadList.reserve(CallSiteNumToLPad.size());
5749 for (unsigned I = 1; I <= MaxCSNum; ++I) {
5750 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
5751 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00005752 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling2a850152011-10-05 00:02:33 +00005753 LPadList.push_back(*II);
Bill Wendling2acf6382011-10-07 23:18:02 +00005754 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
5755 }
Bill Wendling2a850152011-10-05 00:02:33 +00005756 }
5757
Bill Wendling5cbef192011-10-05 23:28:57 +00005758 assert(!LPadList.empty() &&
5759 "No landing pad destinations for the dispatch jump table!");
5760
Bill Wendling04f15b42011-10-06 21:29:56 +00005761 // Create the jump table and associated information.
Bill Wendling2a850152011-10-05 00:02:33 +00005762 MachineJumpTableInfo *JTI =
5763 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
5764 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
5765 unsigned UId = AFI->createJumpTableUId();
5766
Bill Wendling04f15b42011-10-06 21:29:56 +00005767 // Create the MBBs for the dispatch code.
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005768
5769 // Shove the dispatch's address into the return slot in the function context.
5770 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
5771 DispatchBB->setIsLandingPad();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005772
Bill Wendlingbb734682011-10-05 00:39:32 +00005773 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Bill Wendling083a8eb2011-10-06 23:37:36 +00005774 BuildMI(TrapBB, dl, TII->get(Subtarget->isThumb() ? ARM::tTRAP : ARM::TRAP));
Bill Wendlingbb734682011-10-05 00:39:32 +00005775 DispatchBB->addSuccessor(TrapBB);
5776
5777 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
5778 DispatchBB->addSuccessor(DispContBB);
Bill Wendling2a850152011-10-05 00:02:33 +00005779
Bill Wendlinga48ed4f2011-10-17 21:32:56 +00005780 // Insert and MBBs.
Bill Wendling930193c2011-10-06 00:53:33 +00005781 MF->insert(MF->end(), DispatchBB);
5782 MF->insert(MF->end(), DispContBB);
5783 MF->insert(MF->end(), TrapBB);
Bill Wendling930193c2011-10-06 00:53:33 +00005784
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005785 // Insert code into the entry block that creates and registers the function
5786 // context.
5787 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
5788
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005789 MachineMemOperand *FIMMOLd =
Bill Wendling04f15b42011-10-06 21:29:56 +00005790 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendling083a8eb2011-10-06 23:37:36 +00005791 MachineMemOperand::MOLoad |
5792 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling930193c2011-10-06 00:53:33 +00005793
Bob Wilsonf4aea8f2011-12-22 23:39:48 +00005794 if (AFI->isThumb1OnlyFunction())
5795 BuildMI(DispatchBB, dl, TII->get(ARM::tInt_eh_sjlj_dispatchsetup));
5796 else if (!Subtarget->hasVFP2())
5797 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup_nofp));
5798 else
5799 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
Bob Wilsoneaab6ef2011-11-16 07:11:57 +00005800
Bill Wendling952cb502011-10-18 22:49:07 +00005801 unsigned NumLPads = LPadList.size();
Bill Wendling95ce2e92011-10-06 22:53:00 +00005802 if (Subtarget->isThumb2()) {
5803 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5804 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
5805 .addFrameIndex(FI)
5806 .addImm(4)
5807 .addMemOperand(FIMMOLd));
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005808
Bill Wendling952cb502011-10-18 22:49:07 +00005809 if (NumLPads < 256) {
5810 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
5811 .addReg(NewVReg1)
5812 .addImm(LPadList.size()));
5813 } else {
5814 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5815 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00005816 .addImm(NumLPads & 0xFFFF));
5817
5818 unsigned VReg2 = VReg1;
5819 if ((NumLPads & 0xFFFF0000) != 0) {
5820 VReg2 = MRI->createVirtualRegister(TRC);
5821 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
5822 .addReg(VReg1)
5823 .addImm(NumLPads >> 16));
5824 }
5825
Bill Wendling952cb502011-10-18 22:49:07 +00005826 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
5827 .addReg(NewVReg1)
5828 .addReg(VReg2));
5829 }
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005830
Bill Wendling95ce2e92011-10-06 22:53:00 +00005831 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
5832 .addMBB(TrapBB)
5833 .addImm(ARMCC::HI)
5834 .addReg(ARM::CPSR);
Bill Wendlingbb734682011-10-05 00:39:32 +00005835
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005836 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5837 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005838 .addJumpTableIndex(MJTI)
5839 .addImm(UId));
Bill Wendling2a850152011-10-05 00:02:33 +00005840
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005841 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005842 AddDefaultCC(
5843 AddDefaultPred(
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005844 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
5845 .addReg(NewVReg3, RegState::Kill)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005846 .addReg(NewVReg1)
5847 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
5848
5849 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005850 .addReg(NewVReg4, RegState::Kill)
Bill Wendling2a850152011-10-05 00:02:33 +00005851 .addReg(NewVReg1)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005852 .addJumpTableIndex(MJTI)
5853 .addImm(UId);
5854 } else if (Subtarget->isThumb()) {
Bill Wendling083a8eb2011-10-06 23:37:36 +00005855 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5856 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
5857 .addFrameIndex(FI)
5858 .addImm(1)
5859 .addMemOperand(FIMMOLd));
Bill Wendlingf1083d42011-10-07 22:08:37 +00005860
Bill Wendlinga5871dc2011-10-18 23:11:05 +00005861 if (NumLPads < 256) {
5862 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
5863 .addReg(NewVReg1)
5864 .addImm(NumLPads));
5865 } else {
5866 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling922ad782011-10-19 09:24:02 +00005867 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
5868 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
5869
5870 // MachineConstantPool wants an explicit alignment.
5871 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
5872 if (Align == 0)
5873 Align = getTargetData()->getTypeAllocSize(C->getType());
5874 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendlinga5871dc2011-10-18 23:11:05 +00005875
5876 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5877 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
5878 .addReg(VReg1, RegState::Define)
5879 .addConstantPoolIndex(Idx));
5880 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
5881 .addReg(NewVReg1)
5882 .addReg(VReg1));
5883 }
5884
Bill Wendling083a8eb2011-10-06 23:37:36 +00005885 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
5886 .addMBB(TrapBB)
5887 .addImm(ARMCC::HI)
5888 .addReg(ARM::CPSR);
5889
5890 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5891 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
5892 .addReg(ARM::CPSR, RegState::Define)
5893 .addReg(NewVReg1)
5894 .addImm(2));
5895
5896 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling217f0e92011-10-06 23:41:14 +00005897 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendling083a8eb2011-10-06 23:37:36 +00005898 .addJumpTableIndex(MJTI)
5899 .addImm(UId));
5900
5901 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5902 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
5903 .addReg(ARM::CPSR, RegState::Define)
5904 .addReg(NewVReg2, RegState::Kill)
5905 .addReg(NewVReg3));
5906
5907 MachineMemOperand *JTMMOLd =
5908 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
5909 MachineMemOperand::MOLoad, 4, 4);
5910
5911 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5912 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
5913 .addReg(NewVReg4, RegState::Kill)
5914 .addImm(0)
5915 .addMemOperand(JTMMOLd));
5916
5917 unsigned NewVReg6 = MRI->createVirtualRegister(TRC);
5918 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
5919 .addReg(ARM::CPSR, RegState::Define)
5920 .addReg(NewVReg5, RegState::Kill)
5921 .addReg(NewVReg3));
5922
5923 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
5924 .addReg(NewVReg6, RegState::Kill)
5925 .addJumpTableIndex(MJTI)
5926 .addImm(UId);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005927 } else {
5928 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5929 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
5930 .addFrameIndex(FI)
5931 .addImm(4)
5932 .addMemOperand(FIMMOLd));
Bill Wendling564392b2011-10-18 22:11:18 +00005933
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005934 if (NumLPads < 256) {
5935 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
5936 .addReg(NewVReg1)
5937 .addImm(NumLPads));
Bill Wendling922ad782011-10-19 09:24:02 +00005938 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005939 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5940 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00005941 .addImm(NumLPads & 0xFFFF));
5942
5943 unsigned VReg2 = VReg1;
5944 if ((NumLPads & 0xFFFF0000) != 0) {
5945 VReg2 = MRI->createVirtualRegister(TRC);
5946 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
5947 .addReg(VReg1)
5948 .addImm(NumLPads >> 16));
5949 }
5950
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005951 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
5952 .addReg(NewVReg1)
5953 .addReg(VReg2));
Bill Wendling922ad782011-10-19 09:24:02 +00005954 } else {
5955 MachineConstantPool *ConstantPool = MF->getConstantPool();
5956 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
5957 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
5958
5959 // MachineConstantPool wants an explicit alignment.
5960 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
5961 if (Align == 0)
5962 Align = getTargetData()->getTypeAllocSize(C->getType());
5963 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
5964
5965 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5966 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
5967 .addReg(VReg1, RegState::Define)
Bill Wendling767f8be2011-10-20 20:37:11 +00005968 .addConstantPoolIndex(Idx)
5969 .addImm(0));
Bill Wendling922ad782011-10-19 09:24:02 +00005970 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
5971 .addReg(NewVReg1)
5972 .addReg(VReg1, RegState::Kill));
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005973 }
5974
Bill Wendling95ce2e92011-10-06 22:53:00 +00005975 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
5976 .addMBB(TrapBB)
5977 .addImm(ARMCC::HI)
5978 .addReg(ARM::CPSR);
Bill Wendling2a850152011-10-05 00:02:33 +00005979
Bill Wendling564392b2011-10-18 22:11:18 +00005980 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005981 AddDefaultCC(
Bill Wendling564392b2011-10-18 22:11:18 +00005982 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005983 .addReg(NewVReg1)
5984 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling564392b2011-10-18 22:11:18 +00005985 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5986 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005987 .addJumpTableIndex(MJTI)
5988 .addImm(UId));
5989
5990 MachineMemOperand *JTMMOLd =
5991 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
5992 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling564392b2011-10-18 22:11:18 +00005993 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005994 AddDefaultPred(
Bill Wendling564392b2011-10-18 22:11:18 +00005995 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
5996 .addReg(NewVReg3, RegState::Kill)
5997 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005998 .addImm(0)
5999 .addMemOperand(JTMMOLd));
6000
6001 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
Bill Wendling564392b2011-10-18 22:11:18 +00006002 .addReg(NewVReg5, RegState::Kill)
6003 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006004 .addJumpTableIndex(MJTI)
6005 .addImm(UId);
6006 }
Bill Wendling2a850152011-10-05 00:02:33 +00006007
Bill Wendlingbb734682011-10-05 00:39:32 +00006008 // Add the jump table entries as successors to the MBB.
Bill Wendling2acf6382011-10-07 23:18:02 +00006009 MachineBasicBlock *PrevMBB = 0;
Bill Wendlingbb734682011-10-05 00:39:32 +00006010 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006011 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6012 MachineBasicBlock *CurMBB = *I;
6013 if (PrevMBB != CurMBB)
6014 DispContBB->addSuccessor(CurMBB);
6015 PrevMBB = CurMBB;
6016 }
6017
Bill Wendling24bb9252011-10-17 05:25:09 +00006018 // N.B. the order the invoke BBs are processed in doesn't matter here.
Bill Wendling969c9ef2011-10-14 23:34:37 +00006019 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6020 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
6021 const unsigned *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006022 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling2acf6382011-10-07 23:18:02 +00006023 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6024 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6025 MachineBasicBlock *BB = *I;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006026
6027 // Remove the landing pad successor from the invoke block and replace it
6028 // with the new dispatch block.
Bill Wendlingde39d862011-10-26 07:16:18 +00006029 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6030 BB->succ_end());
6031 while (!Successors.empty()) {
6032 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling2acf6382011-10-07 23:18:02 +00006033 if (SMBB->isLandingPad()) {
6034 BB->removeSuccessor(SMBB);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006035 MBBLPads.push_back(SMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006036 }
6037 }
6038
6039 BB->addSuccessor(DispatchBB);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006040
6041 // Find the invoke call and mark all of the callee-saved registers as
6042 // 'implicit defined' so that they're spilled. This prevents code from
6043 // moving instructions to before the EH block, where they will never be
6044 // executed.
6045 for (MachineBasicBlock::reverse_iterator
6046 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006047 if (!II->isCall()) continue;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006048
6049 DenseMap<unsigned, bool> DefRegs;
6050 for (MachineInstr::mop_iterator
6051 OI = II->operands_begin(), OE = II->operands_end();
6052 OI != OE; ++OI) {
6053 if (!OI->isReg()) continue;
6054 DefRegs[OI->getReg()] = true;
6055 }
6056
6057 MachineInstrBuilder MIB(&*II);
6058
Bill Wendling5d798592011-10-14 23:55:44 +00006059 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006060 unsigned Reg = SavedRegs[i];
6061 if (Subtarget->isThumb2() &&
6062 !ARM::tGPRRegisterClass->contains(Reg) &&
6063 !ARM::hGPRRegisterClass->contains(Reg))
6064 continue;
6065 else if (Subtarget->isThumb1Only() &&
6066 !ARM::tGPRRegisterClass->contains(Reg))
6067 continue;
6068 else if (!Subtarget->isThumb() &&
6069 !ARM::GPRRegisterClass->contains(Reg))
6070 continue;
6071 if (!DefRegs[Reg])
6072 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling5d798592011-10-14 23:55:44 +00006073 }
Bill Wendling969c9ef2011-10-14 23:34:37 +00006074
6075 break;
6076 }
Bill Wendling2acf6382011-10-07 23:18:02 +00006077 }
Bill Wendlingbb734682011-10-05 00:39:32 +00006078
Bill Wendlingf7b02072011-10-18 18:30:49 +00006079 // Mark all former landing pads as non-landing pads. The dispatch is the only
6080 // landing pad now.
6081 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6082 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6083 (*I)->setIsLandingPad(false);
6084
Bill Wendlingbb734682011-10-05 00:39:32 +00006085 // The instruction is gone now.
6086 MI->eraseFromParent();
6087
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006088 return MBB;
6089}
6090
Evan Cheng218977b2010-07-13 19:27:42 +00006091static
6092MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6093 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6094 E = MBB->succ_end(); I != E; ++I)
6095 if (*I != Succ)
6096 return *I;
6097 llvm_unreachable("Expecting a BB with two successors!");
6098}
6099
Jim Grosbache801dc42009-12-12 01:40:06 +00006100MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006101ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00006102 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006103 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00006104 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006105 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00006106 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00006107 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00006108 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00006109 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00006110 }
Jim Grosbachee2c2a42011-09-16 21:55:56 +00006111 // The Thumb2 pre-indexed stores have the same MI operands, they just
6112 // define them differently in the .td files from the isel patterns, so
6113 // they need pseudos.
6114 case ARM::t2STR_preidx:
6115 MI->setDesc(TII->get(ARM::t2STR_PRE));
6116 return BB;
6117 case ARM::t2STRB_preidx:
6118 MI->setDesc(TII->get(ARM::t2STRB_PRE));
6119 return BB;
6120 case ARM::t2STRH_preidx:
6121 MI->setDesc(TII->get(ARM::t2STRH_PRE));
6122 return BB;
6123
Jim Grosbach19dec202011-08-05 20:35:44 +00006124 case ARM::STRi_preidx:
6125 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00006126 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00006127 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
6128 // Decode the offset.
6129 unsigned Offset = MI->getOperand(4).getImm();
6130 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
6131 Offset = ARM_AM::getAM2Offset(Offset);
6132 if (isSub)
6133 Offset = -Offset;
6134
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006135 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00006136 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00006137 .addOperand(MI->getOperand(0)) // Rn_wb
6138 .addOperand(MI->getOperand(1)) // Rt
6139 .addOperand(MI->getOperand(2)) // Rn
6140 .addImm(Offset) // offset (skip GPR==zero_reg)
6141 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006142 .addOperand(MI->getOperand(6))
6143 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00006144 MI->eraseFromParent();
6145 return BB;
6146 }
6147 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00006148 case ARM::STRBr_preidx:
6149 case ARM::STRH_preidx: {
6150 unsigned NewOpc;
6151 switch (MI->getOpcode()) {
6152 default: llvm_unreachable("unexpected opcode!");
6153 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
6154 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
6155 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
6156 }
Jim Grosbach19dec202011-08-05 20:35:44 +00006157 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
6158 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
6159 MIB.addOperand(MI->getOperand(i));
6160 MI->eraseFromParent();
6161 return BB;
6162 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006163 case ARM::ATOMIC_LOAD_ADD_I8:
6164 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6165 case ARM::ATOMIC_LOAD_ADD_I16:
6166 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6167 case ARM::ATOMIC_LOAD_ADD_I32:
6168 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006169
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006170 case ARM::ATOMIC_LOAD_AND_I8:
6171 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6172 case ARM::ATOMIC_LOAD_AND_I16:
6173 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6174 case ARM::ATOMIC_LOAD_AND_I32:
6175 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006176
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006177 case ARM::ATOMIC_LOAD_OR_I8:
6178 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6179 case ARM::ATOMIC_LOAD_OR_I16:
6180 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6181 case ARM::ATOMIC_LOAD_OR_I32:
6182 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006183
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006184 case ARM::ATOMIC_LOAD_XOR_I8:
6185 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6186 case ARM::ATOMIC_LOAD_XOR_I16:
6187 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6188 case ARM::ATOMIC_LOAD_XOR_I32:
6189 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006190
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006191 case ARM::ATOMIC_LOAD_NAND_I8:
6192 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6193 case ARM::ATOMIC_LOAD_NAND_I16:
6194 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6195 case ARM::ATOMIC_LOAD_NAND_I32:
6196 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006197
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006198 case ARM::ATOMIC_LOAD_SUB_I8:
6199 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6200 case ARM::ATOMIC_LOAD_SUB_I16:
6201 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6202 case ARM::ATOMIC_LOAD_SUB_I32:
6203 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006204
Jim Grosbachf7da8822011-04-26 19:44:18 +00006205 case ARM::ATOMIC_LOAD_MIN_I8:
6206 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
6207 case ARM::ATOMIC_LOAD_MIN_I16:
6208 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
6209 case ARM::ATOMIC_LOAD_MIN_I32:
6210 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
6211
6212 case ARM::ATOMIC_LOAD_MAX_I8:
6213 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
6214 case ARM::ATOMIC_LOAD_MAX_I16:
6215 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
6216 case ARM::ATOMIC_LOAD_MAX_I32:
6217 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
6218
6219 case ARM::ATOMIC_LOAD_UMIN_I8:
6220 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
6221 case ARM::ATOMIC_LOAD_UMIN_I16:
6222 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
6223 case ARM::ATOMIC_LOAD_UMIN_I32:
6224 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
6225
6226 case ARM::ATOMIC_LOAD_UMAX_I8:
6227 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
6228 case ARM::ATOMIC_LOAD_UMAX_I16:
6229 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
6230 case ARM::ATOMIC_LOAD_UMAX_I32:
6231 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
6232
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006233 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
6234 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
6235 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00006236
6237 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
6238 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
6239 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006240
Eli Friedman2bdffe42011-08-31 00:31:29 +00006241
6242 case ARM::ATOMADD6432:
6243 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006244 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr,
6245 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006246 case ARM::ATOMSUB6432:
6247 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006248 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6249 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006250 case ARM::ATOMOR6432:
6251 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006252 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006253 case ARM::ATOMXOR6432:
6254 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006255 isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006256 case ARM::ATOMAND6432:
6257 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006258 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006259 case ARM::ATOMSWAP6432:
6260 return EmitAtomicBinary64(MI, BB, 0, 0, false);
Eli Friedman4d3f3292011-08-31 17:52:22 +00006261 case ARM::ATOMCMPXCHG6432:
6262 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
6263 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6264 /*NeedsCarry*/ false, /*IsCmpxchg*/true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006265
Evan Cheng007ea272009-08-12 05:17:19 +00006266 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00006267 // To "insert" a SELECT_CC instruction, we actually have to insert the
6268 // diamond control-flow pattern. The incoming instruction knows the
6269 // destination vreg to set, the condition code register to branch on, the
6270 // true/false values to select between, and a branch opcode to use.
6271 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006272 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00006273 ++It;
6274
6275 // thisMBB:
6276 // ...
6277 // TrueVal = ...
6278 // cmpTY ccX, r1, r2
6279 // bCC copy1MBB
6280 // fallthrough --> copy0MBB
6281 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006282 MachineFunction *F = BB->getParent();
6283 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6284 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00006285 F->insert(It, copy0MBB);
6286 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006287
6288 // Transfer the remainder of BB and its successor edges to sinkMBB.
6289 sinkMBB->splice(sinkMBB->begin(), BB,
6290 llvm::next(MachineBasicBlock::iterator(MI)),
6291 BB->end());
6292 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6293
Dan Gohman258c58c2010-07-06 15:49:48 +00006294 BB->addSuccessor(copy0MBB);
6295 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00006296
Dan Gohman14152b42010-07-06 20:24:04 +00006297 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
6298 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
6299
Evan Chenga8e29892007-01-19 07:51:42 +00006300 // copy0MBB:
6301 // %FalseValue = ...
6302 // # fallthrough to sinkMBB
6303 BB = copy0MBB;
6304
6305 // Update machine-CFG edges
6306 BB->addSuccessor(sinkMBB);
6307
6308 // sinkMBB:
6309 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6310 // ...
6311 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00006312 BuildMI(*BB, BB->begin(), dl,
6313 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00006314 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6315 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6316
Dan Gohman14152b42010-07-06 20:24:04 +00006317 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00006318 return BB;
6319 }
Evan Cheng86198642009-08-07 00:34:42 +00006320
Evan Cheng218977b2010-07-13 19:27:42 +00006321 case ARM::BCCi64:
6322 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00006323 // If there is an unconditional branch to the other successor, remove it.
6324 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00006325
Evan Cheng218977b2010-07-13 19:27:42 +00006326 // Compare both parts that make up the double comparison separately for
6327 // equality.
6328 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
6329
6330 unsigned LHS1 = MI->getOperand(1).getReg();
6331 unsigned LHS2 = MI->getOperand(2).getReg();
6332 if (RHSisZero) {
6333 AddDefaultPred(BuildMI(BB, dl,
6334 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6335 .addReg(LHS1).addImm(0));
6336 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6337 .addReg(LHS2).addImm(0)
6338 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6339 } else {
6340 unsigned RHS1 = MI->getOperand(3).getReg();
6341 unsigned RHS2 = MI->getOperand(4).getReg();
6342 AddDefaultPred(BuildMI(BB, dl,
6343 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6344 .addReg(LHS1).addReg(RHS1));
6345 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6346 .addReg(LHS2).addReg(RHS2)
6347 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6348 }
6349
6350 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
6351 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
6352 if (MI->getOperand(0).getImm() == ARMCC::NE)
6353 std::swap(destMBB, exitMBB);
6354
6355 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6356 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson51f6a7a2011-09-09 21:48:23 +00006357 if (isThumb2)
6358 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
6359 else
6360 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng218977b2010-07-13 19:27:42 +00006361
6362 MI->eraseFromParent(); // The pseudo instruction is gone now.
6363 return BB;
6364 }
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006365
Bill Wendling5bc85282011-10-17 20:37:20 +00006366 case ARM::Int_eh_sjlj_setjmp:
6367 case ARM::Int_eh_sjlj_setjmp_nofp:
6368 case ARM::tInt_eh_sjlj_setjmp:
6369 case ARM::t2Int_eh_sjlj_setjmp:
6370 case ARM::t2Int_eh_sjlj_setjmp_nofp:
6371 EmitSjLjDispatchBlock(MI, BB);
6372 return BB;
6373
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006374 case ARM::ABS:
6375 case ARM::t2ABS: {
6376 // To insert an ABS instruction, we have to insert the
6377 // diamond control-flow pattern. The incoming instruction knows the
6378 // source vreg to test against 0, the destination vreg to set,
6379 // the condition code register to branch on, the
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006380 // true/false values to select between, and a branch opcode to use.
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006381 // It transforms
6382 // V1 = ABS V0
6383 // into
6384 // V2 = MOVS V0
6385 // BCC (branch to SinkBB if V0 >= 0)
6386 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006387 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006388 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6389 MachineFunction::iterator BBI = BB;
6390 ++BBI;
6391 MachineFunction *Fn = BB->getParent();
6392 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6393 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6394 Fn->insert(BBI, RSBBB);
6395 Fn->insert(BBI, SinkBB);
6396
6397 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
6398 unsigned int ABSDstReg = MI->getOperand(0).getReg();
6399 bool isThumb2 = Subtarget->isThumb2();
6400 MachineRegisterInfo &MRI = Fn->getRegInfo();
6401 // In Thumb mode S must not be specified if source register is the SP or
6402 // PC and if destination register is the SP, so restrict register class
6403 unsigned NewMovDstReg = MRI.createVirtualRegister(
6404 isThumb2 ? ARM::rGPRRegisterClass : ARM::GPRRegisterClass);
6405 unsigned NewRsbDstReg = MRI.createVirtualRegister(
6406 isThumb2 ? ARM::rGPRRegisterClass : ARM::GPRRegisterClass);
6407
6408 // Transfer the remainder of BB and its successor edges to sinkMBB.
6409 SinkBB->splice(SinkBB->begin(), BB,
6410 llvm::next(MachineBasicBlock::iterator(MI)),
6411 BB->end());
6412 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
6413
6414 BB->addSuccessor(RSBBB);
6415 BB->addSuccessor(SinkBB);
6416
6417 // fall through to SinkMBB
6418 RSBBB->addSuccessor(SinkBB);
6419
6420 // insert a movs at the end of BB
6421 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVr : ARM::MOVr),
6422 NewMovDstReg)
6423 .addReg(ABSSrcReg, RegState::Kill)
6424 .addImm((unsigned)ARMCC::AL).addReg(0)
6425 .addReg(ARM::CPSR, RegState::Define);
6426
6427 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006428 BuildMI(BB, dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006429 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
6430 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
6431
6432 // insert rsbri in RSBBB
6433 // Note: BCC and rsbri will be converted into predicated rsbmi
6434 // by if-conversion pass
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006435 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006436 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
6437 .addReg(NewMovDstReg, RegState::Kill)
6438 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
6439
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006440 // insert PHI in SinkBB,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006441 // reuse ABSDstReg to not change uses of ABS instruction
6442 BuildMI(*SinkBB, SinkBB->begin(), dl,
6443 TII->get(ARM::PHI), ABSDstReg)
6444 .addReg(NewRsbDstReg).addMBB(RSBBB)
6445 .addReg(NewMovDstReg).addMBB(BB);
6446
6447 // remove ABS instruction
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006448 MI->eraseFromParent();
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006449
6450 // return last added BB
6451 return SinkBB;
6452 }
Evan Chenga8e29892007-01-19 07:51:42 +00006453 }
6454}
6455
Evan Cheng37fefc22011-08-30 19:09:48 +00006456void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
6457 SDNode *Node) const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006458 if (!MI->hasPostISelHook()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006459 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
6460 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
6461 return;
6462 }
6463
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006464 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick4815d562011-09-20 03:17:40 +00006465 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
6466 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
6467 // operand is still set to noreg. If needed, set the optional operand's
6468 // register to CPSR, and remove the redundant implicit def.
Andrew Trick3be654f2011-09-21 02:20:46 +00006469 //
Andrew Trick90b7b122011-10-18 19:18:52 +00006470 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick4815d562011-09-20 03:17:40 +00006471
Andrew Trick3be654f2011-09-21 02:20:46 +00006472 // Rename pseudo opcodes.
6473 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
6474 if (NewOpc) {
6475 const ARMBaseInstrInfo *TII =
6476 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick90b7b122011-10-18 19:18:52 +00006477 MCID = &TII->get(NewOpc);
6478
6479 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
6480 "converted opcode should be the same except for cc_out");
6481
6482 MI->setDesc(*MCID);
6483
6484 // Add the optional cc_out operand
6485 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick3be654f2011-09-21 02:20:46 +00006486 }
Andrew Trick90b7b122011-10-18 19:18:52 +00006487 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick4815d562011-09-20 03:17:40 +00006488
6489 // Any ARM instruction that sets the 's' bit should specify an optional
6490 // "cc_out" operand in the last operand position.
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006491 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006492 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006493 return;
6494 }
Andrew Trick3be654f2011-09-21 02:20:46 +00006495 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
6496 // since we already have an optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006497 bool definesCPSR = false;
6498 bool deadCPSR = false;
Andrew Trick90b7b122011-10-18 19:18:52 +00006499 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick4815d562011-09-20 03:17:40 +00006500 i != e; ++i) {
6501 const MachineOperand &MO = MI->getOperand(i);
6502 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
6503 definesCPSR = true;
6504 if (MO.isDead())
6505 deadCPSR = true;
6506 MI->RemoveOperand(i);
6507 break;
Evan Cheng37fefc22011-08-30 19:09:48 +00006508 }
6509 }
Andrew Trick4815d562011-09-20 03:17:40 +00006510 if (!definesCPSR) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006511 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006512 return;
6513 }
6514 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick3be654f2011-09-21 02:20:46 +00006515 if (deadCPSR) {
6516 assert(!MI->getOperand(ccOutIdx).getReg() &&
6517 "expect uninitialized optional cc_out operand");
Andrew Trick4815d562011-09-20 03:17:40 +00006518 return;
Andrew Trick3be654f2011-09-21 02:20:46 +00006519 }
Andrew Trick4815d562011-09-20 03:17:40 +00006520
Andrew Trick3be654f2011-09-21 02:20:46 +00006521 // If this instruction was defined with an optional CPSR def and its dag node
6522 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006523 MachineOperand &MO = MI->getOperand(ccOutIdx);
6524 MO.setReg(ARM::CPSR);
6525 MO.setIsDef(true);
Evan Cheng37fefc22011-08-30 19:09:48 +00006526}
6527
Evan Chenga8e29892007-01-19 07:51:42 +00006528//===----------------------------------------------------------------------===//
6529// ARM Optimization Hooks
6530//===----------------------------------------------------------------------===//
6531
Chris Lattnerd1980a52009-03-12 06:52:53 +00006532static
6533SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
6534 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00006535 SelectionDAG &DAG = DCI.DAG;
6536 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00006537 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00006538 unsigned Opc = N->getOpcode();
6539 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
6540 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
6541 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
6542 ISD::CondCode CC = ISD::SETCC_INVALID;
6543
6544 if (isSlctCC) {
6545 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
6546 } else {
6547 SDValue CCOp = Slct.getOperand(0);
6548 if (CCOp.getOpcode() == ISD::SETCC)
6549 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
6550 }
6551
6552 bool DoXform = false;
6553 bool InvCC = false;
6554 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
6555 "Bad input!");
6556
6557 if (LHS.getOpcode() == ISD::Constant &&
6558 cast<ConstantSDNode>(LHS)->isNullValue()) {
6559 DoXform = true;
6560 } else if (CC != ISD::SETCC_INVALID &&
6561 RHS.getOpcode() == ISD::Constant &&
6562 cast<ConstantSDNode>(RHS)->isNullValue()) {
6563 std::swap(LHS, RHS);
6564 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006565 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00006566 Op0.getOperand(0).getValueType();
6567 bool isInt = OpVT.isInteger();
6568 CC = ISD::getSetCCInverse(CC, isInt);
6569
6570 if (!TLI.isCondCodeLegal(CC, OpVT))
6571 return SDValue(); // Inverse operator isn't legal.
6572
6573 DoXform = true;
6574 InvCC = true;
6575 }
6576
6577 if (DoXform) {
6578 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
6579 if (isSlctCC)
6580 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
6581 Slct.getOperand(0), Slct.getOperand(1), CC);
6582 SDValue CCOp = Slct.getOperand(0);
6583 if (InvCC)
6584 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
6585 CCOp.getOperand(0), CCOp.getOperand(1), CC);
6586 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
6587 CCOp, OtherOp, Result);
6588 }
6589 return SDValue();
6590}
6591
Eric Christopherfa6f5912011-06-29 21:10:36 +00006592// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00006593// (only after legalization).
6594static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
6595 TargetLowering::DAGCombinerInfo &DCI,
6596 const ARMSubtarget *Subtarget) {
6597
6598 // Only perform optimization if after legalize, and if NEON is available. We
6599 // also expected both operands to be BUILD_VECTORs.
6600 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
6601 || N0.getOpcode() != ISD::BUILD_VECTOR
6602 || N1.getOpcode() != ISD::BUILD_VECTOR)
6603 return SDValue();
6604
6605 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
6606 EVT VT = N->getValueType(0);
6607 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
6608 return SDValue();
6609
6610 // Check that the vector operands are of the right form.
6611 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
6612 // operands, where N is the size of the formed vector.
6613 // Each EXTRACT_VECTOR should have the same input vector and odd or even
6614 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00006615
6616 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00006617 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00006618 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00006619 SDValue Vec = N0->getOperand(0)->getOperand(0);
6620 SDNode *V = Vec.getNode();
6621 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00006622
Eric Christopherfa6f5912011-06-29 21:10:36 +00006623 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00006624 // check to see if each of their operands are an EXTRACT_VECTOR with
6625 // the same vector and appropriate index.
6626 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
6627 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
6628 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00006629
Tanya Lattner189531f2011-06-14 23:48:48 +00006630 SDValue ExtVec0 = N0->getOperand(i);
6631 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00006632
Tanya Lattner189531f2011-06-14 23:48:48 +00006633 // First operand is the vector, verify its the same.
6634 if (V != ExtVec0->getOperand(0).getNode() ||
6635 V != ExtVec1->getOperand(0).getNode())
6636 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00006637
Tanya Lattner189531f2011-06-14 23:48:48 +00006638 // Second is the constant, verify its correct.
6639 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
6640 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00006641
Tanya Lattner189531f2011-06-14 23:48:48 +00006642 // For the constant, we want to see all the even or all the odd.
6643 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
6644 || C1->getZExtValue() != nextIndex+1)
6645 return SDValue();
6646
6647 // Increment index.
6648 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00006649 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00006650 return SDValue();
6651 }
6652
6653 // Create VPADDL node.
6654 SelectionDAG &DAG = DCI.DAG;
6655 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00006656
6657 // Build operand list.
6658 SmallVector<SDValue, 8> Ops;
6659 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
6660 TLI.getPointerTy()));
6661
6662 // Input is the vector.
6663 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00006664
Tanya Lattner189531f2011-06-14 23:48:48 +00006665 // Get widened type and narrowed type.
6666 MVT widenType;
6667 unsigned numElem = VT.getVectorNumElements();
6668 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
6669 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
6670 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
6671 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
6672 default:
6673 assert(0 && "Invalid vector element type for padd optimization.");
6674 }
6675
6676 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
6677 widenType, &Ops[0], Ops.size());
6678 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
6679}
6680
Bob Wilson3d5792a2010-07-29 20:34:14 +00006681/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
6682/// operands N0 and N1. This is a helper for PerformADDCombine that is
6683/// called with the default operands, and if that fails, with commuted
6684/// operands.
6685static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00006686 TargetLowering::DAGCombinerInfo &DCI,
6687 const ARMSubtarget *Subtarget){
6688
6689 // Attempt to create vpaddl for this add.
6690 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
6691 if (Result.getNode())
6692 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00006693
Chris Lattnerd1980a52009-03-12 06:52:53 +00006694 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
6695 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
6696 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
6697 if (Result.getNode()) return Result;
6698 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00006699 return SDValue();
6700}
6701
Bob Wilson3d5792a2010-07-29 20:34:14 +00006702/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
6703///
6704static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00006705 TargetLowering::DAGCombinerInfo &DCI,
6706 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00006707 SDValue N0 = N->getOperand(0);
6708 SDValue N1 = N->getOperand(1);
6709
6710 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00006711 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00006712 if (Result.getNode())
6713 return Result;
6714
6715 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00006716 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00006717}
6718
Chris Lattnerd1980a52009-03-12 06:52:53 +00006719/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00006720///
Chris Lattnerd1980a52009-03-12 06:52:53 +00006721static SDValue PerformSUBCombine(SDNode *N,
6722 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00006723 SDValue N0 = N->getOperand(0);
6724 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00006725
Chris Lattnerd1980a52009-03-12 06:52:53 +00006726 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
6727 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
6728 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
6729 if (Result.getNode()) return Result;
6730 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00006731
Chris Lattnerd1980a52009-03-12 06:52:53 +00006732 return SDValue();
6733}
6734
Evan Cheng463d3582011-03-31 19:38:48 +00006735/// PerformVMULCombine
6736/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
6737/// special multiplier accumulator forwarding.
6738/// vmul d3, d0, d2
6739/// vmla d3, d1, d2
6740/// is faster than
6741/// vadd d3, d0, d1
6742/// vmul d3, d3, d2
6743static SDValue PerformVMULCombine(SDNode *N,
6744 TargetLowering::DAGCombinerInfo &DCI,
6745 const ARMSubtarget *Subtarget) {
6746 if (!Subtarget->hasVMLxForwarding())
6747 return SDValue();
6748
6749 SelectionDAG &DAG = DCI.DAG;
6750 SDValue N0 = N->getOperand(0);
6751 SDValue N1 = N->getOperand(1);
6752 unsigned Opcode = N0.getOpcode();
6753 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
6754 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00006755 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00006756 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
6757 Opcode != ISD::FADD && Opcode != ISD::FSUB)
6758 return SDValue();
6759 std::swap(N0, N1);
6760 }
6761
6762 EVT VT = N->getValueType(0);
6763 DebugLoc DL = N->getDebugLoc();
6764 SDValue N00 = N0->getOperand(0);
6765 SDValue N01 = N0->getOperand(1);
6766 return DAG.getNode(Opcode, DL, VT,
6767 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
6768 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
6769}
6770
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006771static SDValue PerformMULCombine(SDNode *N,
6772 TargetLowering::DAGCombinerInfo &DCI,
6773 const ARMSubtarget *Subtarget) {
6774 SelectionDAG &DAG = DCI.DAG;
6775
6776 if (Subtarget->isThumb1Only())
6777 return SDValue();
6778
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006779 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
6780 return SDValue();
6781
6782 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00006783 if (VT.is64BitVector() || VT.is128BitVector())
6784 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006785 if (VT != MVT::i32)
6786 return SDValue();
6787
6788 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
6789 if (!C)
6790 return SDValue();
6791
6792 uint64_t MulAmt = C->getZExtValue();
6793 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
6794 ShiftAmt = ShiftAmt & (32 - 1);
6795 SDValue V = N->getOperand(0);
6796 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006797
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006798 SDValue Res;
6799 MulAmt >>= ShiftAmt;
6800 if (isPowerOf2_32(MulAmt - 1)) {
6801 // (mul x, 2^N + 1) => (add (shl x, N), x)
6802 Res = DAG.getNode(ISD::ADD, DL, VT,
6803 V, DAG.getNode(ISD::SHL, DL, VT,
6804 V, DAG.getConstant(Log2_32(MulAmt-1),
6805 MVT::i32)));
6806 } else if (isPowerOf2_32(MulAmt + 1)) {
6807 // (mul x, 2^N - 1) => (sub (shl x, N), x)
6808 Res = DAG.getNode(ISD::SUB, DL, VT,
6809 DAG.getNode(ISD::SHL, DL, VT,
6810 V, DAG.getConstant(Log2_32(MulAmt+1),
6811 MVT::i32)),
6812 V);
6813 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006814 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006815
6816 if (ShiftAmt != 0)
6817 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
6818 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006819
6820 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006821 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006822 return SDValue();
6823}
6824
Owen Anderson080c0922010-11-05 19:27:46 +00006825static SDValue PerformANDCombine(SDNode *N,
6826 TargetLowering::DAGCombinerInfo &DCI) {
Owen Anderson76706012011-04-05 21:48:57 +00006827
Owen Anderson080c0922010-11-05 19:27:46 +00006828 // Attempt to use immediate-form VBIC
6829 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
6830 DebugLoc dl = N->getDebugLoc();
6831 EVT VT = N->getValueType(0);
6832 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006833
Tanya Lattner0433b212011-04-07 15:24:20 +00006834 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
6835 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00006836
Owen Anderson080c0922010-11-05 19:27:46 +00006837 APInt SplatBits, SplatUndef;
6838 unsigned SplatBitSize;
6839 bool HasAnyUndefs;
6840 if (BVN &&
6841 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
6842 if (SplatBitSize <= 64) {
6843 EVT VbicVT;
6844 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
6845 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006846 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00006847 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00006848 if (Val.getNode()) {
6849 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006850 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00006851 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006852 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00006853 }
6854 }
6855 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006856
Owen Anderson080c0922010-11-05 19:27:46 +00006857 return SDValue();
6858}
6859
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006860/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
6861static SDValue PerformORCombine(SDNode *N,
6862 TargetLowering::DAGCombinerInfo &DCI,
6863 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00006864 // Attempt to use immediate-form VORR
6865 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
6866 DebugLoc dl = N->getDebugLoc();
6867 EVT VT = N->getValueType(0);
6868 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006869
Tanya Lattner0433b212011-04-07 15:24:20 +00006870 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
6871 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00006872
Owen Anderson60f48702010-11-03 23:15:26 +00006873 APInt SplatBits, SplatUndef;
6874 unsigned SplatBitSize;
6875 bool HasAnyUndefs;
6876 if (BVN && Subtarget->hasNEON() &&
6877 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
6878 if (SplatBitSize <= 64) {
6879 EVT VorrVT;
6880 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
6881 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00006882 DAG, VorrVT, VT.is128BitVector(),
6883 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00006884 if (Val.getNode()) {
6885 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006886 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00006887 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006888 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00006889 }
6890 }
6891 }
6892
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00006893 SDValue N0 = N->getOperand(0);
6894 if (N0.getOpcode() != ISD::AND)
6895 return SDValue();
6896 SDValue N1 = N->getOperand(1);
6897
6898 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
6899 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
6900 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
6901 APInt SplatUndef;
6902 unsigned SplatBitSize;
6903 bool HasAnyUndefs;
6904
6905 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
6906 APInt SplatBits0;
6907 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
6908 HasAnyUndefs) && !HasAnyUndefs) {
6909 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
6910 APInt SplatBits1;
6911 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
6912 HasAnyUndefs) && !HasAnyUndefs &&
6913 SplatBits0 == ~SplatBits1) {
6914 // Canonicalize the vector type to make instruction selection simpler.
6915 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
6916 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
6917 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00006918 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00006919 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
6920 }
6921 }
6922 }
6923
Jim Grosbach54238562010-07-17 03:30:54 +00006924 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
6925 // reasonable.
6926
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006927 // BFI is only available on V6T2+
6928 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
6929 return SDValue();
6930
Jim Grosbach54238562010-07-17 03:30:54 +00006931 DebugLoc DL = N->getDebugLoc();
6932 // 1) or (and A, mask), val => ARMbfi A, val, mask
6933 // iff (val & mask) == val
6934 //
6935 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
6936 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00006937 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00006938 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00006939 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00006940 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006941
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006942 if (VT != MVT::i32)
6943 return SDValue();
6944
Evan Cheng30fb13f2010-12-13 20:32:54 +00006945 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00006946
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006947 // The value and the mask need to be constants so we can verify this is
6948 // actually a bitfield set. If the mask is 0xffff, we can do better
6949 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00006950 SDValue MaskOp = N0.getOperand(1);
6951 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
6952 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006953 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00006954 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006955 if (Mask == 0xffff)
6956 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006957 SDValue Res;
6958 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00006959 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
6960 if (N1C) {
6961 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00006962 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00006963 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006964
Evan Chenga9688c42010-12-11 04:11:38 +00006965 if (ARM::isBitFieldInvertedMask(Mask)) {
6966 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006967
Evan Cheng30fb13f2010-12-13 20:32:54 +00006968 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00006969 DAG.getConstant(Val, MVT::i32),
6970 DAG.getConstant(Mask, MVT::i32));
6971
6972 // Do not add new nodes to DAG combiner worklist.
6973 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00006974 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00006975 }
Jim Grosbach54238562010-07-17 03:30:54 +00006976 } else if (N1.getOpcode() == ISD::AND) {
6977 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00006978 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
6979 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00006980 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00006981 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006982
Eric Christopher29aeed12011-03-26 01:21:03 +00006983 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
6984 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00006985 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00006986 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00006987 // The pack halfword instruction works better for masks that fit it,
6988 // so use that when it's available.
6989 if (Subtarget->hasT2ExtractPack() &&
6990 (Mask == 0xffff || Mask == 0xffff0000))
6991 return SDValue();
6992 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00006993 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00006994 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00006995 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00006996 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00006997 DAG.getConstant(Mask, MVT::i32));
6998 // Do not add new nodes to DAG combiner worklist.
6999 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007000 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007001 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007002 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007003 // The pack halfword instruction works better for masks that fit it,
7004 // so use that when it's available.
7005 if (Subtarget->hasT2ExtractPack() &&
7006 (Mask2 == 0xffff || Mask2 == 0xffff0000))
7007 return SDValue();
7008 // 2b
7009 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007010 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00007011 DAG.getConstant(lsb, MVT::i32));
7012 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00007013 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00007014 // Do not add new nodes to DAG combiner worklist.
7015 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007016 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007017 }
7018 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007019
Evan Cheng30fb13f2010-12-13 20:32:54 +00007020 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
7021 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
7022 ARM::isBitFieldInvertedMask(~Mask)) {
7023 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
7024 // where lsb(mask) == #shamt and masked bits of B are known zero.
7025 SDValue ShAmt = N00.getOperand(1);
7026 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
7027 unsigned LSB = CountTrailingZeros_32(Mask);
7028 if (ShAmtC != LSB)
7029 return SDValue();
7030
7031 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
7032 DAG.getConstant(~Mask, MVT::i32));
7033
7034 // Do not add new nodes to DAG combiner worklist.
7035 DCI.CombineTo(N, Res, false);
7036 }
7037
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007038 return SDValue();
7039}
7040
Evan Chengbf188ae2011-06-15 01:12:31 +00007041/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
7042/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00007043static SDValue PerformBFICombine(SDNode *N,
7044 TargetLowering::DAGCombinerInfo &DCI) {
7045 SDValue N1 = N->getOperand(1);
7046 if (N1.getOpcode() == ISD::AND) {
7047 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7048 if (!N11C)
7049 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007050 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
7051 unsigned LSB = CountTrailingZeros_32(~InvMask);
7052 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
7053 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00007054 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007055 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00007056 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
7057 N->getOperand(0), N1.getOperand(0),
7058 N->getOperand(2));
7059 }
7060 return SDValue();
7061}
7062
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007063/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
7064/// ARMISD::VMOVRRD.
7065static SDValue PerformVMOVRRDCombine(SDNode *N,
7066 TargetLowering::DAGCombinerInfo &DCI) {
7067 // vmovrrd(vmovdrr x, y) -> x,y
7068 SDValue InDouble = N->getOperand(0);
7069 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
7070 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00007071
7072 // vmovrrd(load f64) -> (load i32), (load i32)
7073 SDNode *InNode = InDouble.getNode();
7074 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
7075 InNode->getValueType(0) == MVT::f64 &&
7076 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
7077 !cast<LoadSDNode>(InNode)->isVolatile()) {
7078 // TODO: Should this be done for non-FrameIndex operands?
7079 LoadSDNode *LD = cast<LoadSDNode>(InNode);
7080
7081 SelectionDAG &DAG = DCI.DAG;
7082 DebugLoc DL = LD->getDebugLoc();
7083 SDValue BasePtr = LD->getBasePtr();
7084 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
7085 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007086 LD->isNonTemporal(), LD->isInvariant(),
7087 LD->getAlignment());
Cameron Zwarich4071a712011-04-02 02:40:43 +00007088
7089 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7090 DAG.getConstant(4, MVT::i32));
7091 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
7092 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007093 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich4071a712011-04-02 02:40:43 +00007094 std::min(4U, LD->getAlignment() / 2));
7095
7096 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
7097 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
7098 DCI.RemoveFromWorklist(LD);
7099 DAG.DeleteNode(LD);
7100 return Result;
7101 }
7102
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007103 return SDValue();
7104}
7105
7106/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
7107/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
7108static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
7109 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
7110 SDValue Op0 = N->getOperand(0);
7111 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007112 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007113 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007114 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007115 Op1 = Op1.getOperand(0);
7116 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
7117 Op0.getNode() == Op1.getNode() &&
7118 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007119 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007120 N->getValueType(0), Op0.getOperand(0));
7121 return SDValue();
7122}
7123
Bob Wilson31600902010-12-21 06:43:19 +00007124/// PerformSTORECombine - Target-specific dag combine xforms for
7125/// ISD::STORE.
7126static SDValue PerformSTORECombine(SDNode *N,
7127 TargetLowering::DAGCombinerInfo &DCI) {
7128 // Bitcast an i64 store extracted from a vector to f64.
7129 // Otherwise, the i64 value will be legalized to a pair of i32 values.
7130 StoreSDNode *St = cast<StoreSDNode>(N);
7131 SDValue StVal = St->getValue();
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007132 if (!ISD::isNormalStore(St) || St->isVolatile())
7133 return SDValue();
7134
7135 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
7136 StVal.getNode()->hasOneUse() && !St->isVolatile()) {
7137 SelectionDAG &DAG = DCI.DAG;
7138 DebugLoc DL = St->getDebugLoc();
7139 SDValue BasePtr = St->getBasePtr();
7140 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
7141 StVal.getNode()->getOperand(0), BasePtr,
7142 St->getPointerInfo(), St->isVolatile(),
7143 St->isNonTemporal(), St->getAlignment());
7144
7145 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7146 DAG.getConstant(4, MVT::i32));
7147 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
7148 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
7149 St->isNonTemporal(),
7150 std::min(4U, St->getAlignment() / 2));
7151 }
7152
7153 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00007154 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
7155 return SDValue();
7156
7157 SelectionDAG &DAG = DCI.DAG;
7158 DebugLoc dl = StVal.getDebugLoc();
7159 SDValue IntVec = StVal.getOperand(0);
7160 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7161 IntVec.getValueType().getVectorNumElements());
7162 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
7163 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
7164 Vec, StVal.getOperand(1));
7165 dl = N->getDebugLoc();
7166 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
7167 // Make the DAGCombiner fold the bitcasts.
7168 DCI.AddToWorklist(Vec.getNode());
7169 DCI.AddToWorklist(ExtElt.getNode());
7170 DCI.AddToWorklist(V.getNode());
7171 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
7172 St->getPointerInfo(), St->isVolatile(),
7173 St->isNonTemporal(), St->getAlignment(),
7174 St->getTBAAInfo());
7175}
7176
7177/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
7178/// are normal, non-volatile loads. If so, it is profitable to bitcast an
7179/// i64 vector to have f64 elements, since the value can then be loaded
7180/// directly into a VFP register.
7181static bool hasNormalLoadOperand(SDNode *N) {
7182 unsigned NumElts = N->getValueType(0).getVectorNumElements();
7183 for (unsigned i = 0; i < NumElts; ++i) {
7184 SDNode *Elt = N->getOperand(i).getNode();
7185 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
7186 return true;
7187 }
7188 return false;
7189}
7190
Bob Wilson75f02882010-09-17 22:59:05 +00007191/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
7192/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00007193static SDValue PerformBUILD_VECTORCombine(SDNode *N,
7194 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00007195 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
7196 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
7197 // into a pair of GPRs, which is fine when the value is used as a scalar,
7198 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00007199 SelectionDAG &DAG = DCI.DAG;
7200 if (N->getNumOperands() == 2) {
7201 SDValue RV = PerformVMOVDRRCombine(N, DAG);
7202 if (RV.getNode())
7203 return RV;
7204 }
Bob Wilson75f02882010-09-17 22:59:05 +00007205
Bob Wilson31600902010-12-21 06:43:19 +00007206 // Load i64 elements as f64 values so that type legalization does not split
7207 // them up into i32 values.
7208 EVT VT = N->getValueType(0);
7209 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
7210 return SDValue();
7211 DebugLoc dl = N->getDebugLoc();
7212 SmallVector<SDValue, 8> Ops;
7213 unsigned NumElts = VT.getVectorNumElements();
7214 for (unsigned i = 0; i < NumElts; ++i) {
7215 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
7216 Ops.push_back(V);
7217 // Make the DAGCombiner fold the bitcast.
7218 DCI.AddToWorklist(V.getNode());
7219 }
7220 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
7221 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
7222 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
7223}
7224
7225/// PerformInsertEltCombine - Target-specific dag combine xforms for
7226/// ISD::INSERT_VECTOR_ELT.
7227static SDValue PerformInsertEltCombine(SDNode *N,
7228 TargetLowering::DAGCombinerInfo &DCI) {
7229 // Bitcast an i64 load inserted into a vector to f64.
7230 // Otherwise, the i64 value will be legalized to a pair of i32 values.
7231 EVT VT = N->getValueType(0);
7232 SDNode *Elt = N->getOperand(1).getNode();
7233 if (VT.getVectorElementType() != MVT::i64 ||
7234 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
7235 return SDValue();
7236
7237 SelectionDAG &DAG = DCI.DAG;
7238 DebugLoc dl = N->getDebugLoc();
7239 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7240 VT.getVectorNumElements());
7241 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
7242 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
7243 // Make the DAGCombiner fold the bitcasts.
7244 DCI.AddToWorklist(Vec.getNode());
7245 DCI.AddToWorklist(V.getNode());
7246 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
7247 Vec, V, N->getOperand(2));
7248 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00007249}
7250
Bob Wilsonf20700c2010-10-27 20:38:28 +00007251/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
7252/// ISD::VECTOR_SHUFFLE.
7253static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
7254 // The LLVM shufflevector instruction does not require the shuffle mask
7255 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
7256 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
7257 // operands do not match the mask length, they are extended by concatenating
7258 // them with undef vectors. That is probably the right thing for other
7259 // targets, but for NEON it is better to concatenate two double-register
7260 // size vector operands into a single quad-register size vector. Do that
7261 // transformation here:
7262 // shuffle(concat(v1, undef), concat(v2, undef)) ->
7263 // shuffle(concat(v1, v2), undef)
7264 SDValue Op0 = N->getOperand(0);
7265 SDValue Op1 = N->getOperand(1);
7266 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
7267 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
7268 Op0.getNumOperands() != 2 ||
7269 Op1.getNumOperands() != 2)
7270 return SDValue();
7271 SDValue Concat0Op1 = Op0.getOperand(1);
7272 SDValue Concat1Op1 = Op1.getOperand(1);
7273 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
7274 Concat1Op1.getOpcode() != ISD::UNDEF)
7275 return SDValue();
7276 // Skip the transformation if any of the types are illegal.
7277 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7278 EVT VT = N->getValueType(0);
7279 if (!TLI.isTypeLegal(VT) ||
7280 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
7281 !TLI.isTypeLegal(Concat1Op1.getValueType()))
7282 return SDValue();
7283
7284 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
7285 Op0.getOperand(0), Op1.getOperand(0));
7286 // Translate the shuffle mask.
7287 SmallVector<int, 16> NewMask;
7288 unsigned NumElts = VT.getVectorNumElements();
7289 unsigned HalfElts = NumElts/2;
7290 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
7291 for (unsigned n = 0; n < NumElts; ++n) {
7292 int MaskElt = SVN->getMaskElt(n);
7293 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007294 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00007295 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007296 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00007297 NewElt = HalfElts + MaskElt - NumElts;
7298 NewMask.push_back(NewElt);
7299 }
7300 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
7301 DAG.getUNDEF(VT), NewMask.data());
7302}
7303
Bob Wilson1c3ef902011-02-07 17:43:21 +00007304/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
7305/// NEON load/store intrinsics to merge base address updates.
7306static SDValue CombineBaseUpdate(SDNode *N,
7307 TargetLowering::DAGCombinerInfo &DCI) {
7308 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7309 return SDValue();
7310
7311 SelectionDAG &DAG = DCI.DAG;
7312 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
7313 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
7314 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
7315 SDValue Addr = N->getOperand(AddrOpIdx);
7316
7317 // Search for a use of the address operand that is an increment.
7318 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
7319 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
7320 SDNode *User = *UI;
7321 if (User->getOpcode() != ISD::ADD ||
7322 UI.getUse().getResNo() != Addr.getResNo())
7323 continue;
7324
7325 // Check that the add is independent of the load/store. Otherwise, folding
7326 // it would create a cycle.
7327 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
7328 continue;
7329
7330 // Find the new opcode for the updating load/store.
7331 bool isLoad = true;
7332 bool isLaneOp = false;
7333 unsigned NewOpc = 0;
7334 unsigned NumVecs = 0;
7335 if (isIntrinsic) {
7336 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
7337 switch (IntNo) {
7338 default: assert(0 && "unexpected intrinsic for Neon base update");
7339 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
7340 NumVecs = 1; break;
7341 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
7342 NumVecs = 2; break;
7343 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
7344 NumVecs = 3; break;
7345 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
7346 NumVecs = 4; break;
7347 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
7348 NumVecs = 2; isLaneOp = true; break;
7349 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
7350 NumVecs = 3; isLaneOp = true; break;
7351 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
7352 NumVecs = 4; isLaneOp = true; break;
7353 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
7354 NumVecs = 1; isLoad = false; break;
7355 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
7356 NumVecs = 2; isLoad = false; break;
7357 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
7358 NumVecs = 3; isLoad = false; break;
7359 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
7360 NumVecs = 4; isLoad = false; break;
7361 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
7362 NumVecs = 2; isLoad = false; isLaneOp = true; break;
7363 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
7364 NumVecs = 3; isLoad = false; isLaneOp = true; break;
7365 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
7366 NumVecs = 4; isLoad = false; isLaneOp = true; break;
7367 }
7368 } else {
7369 isLaneOp = true;
7370 switch (N->getOpcode()) {
7371 default: assert(0 && "unexpected opcode for Neon base update");
7372 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
7373 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
7374 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
7375 }
7376 }
7377
7378 // Find the size of memory referenced by the load/store.
7379 EVT VecTy;
7380 if (isLoad)
7381 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00007382 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00007383 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
7384 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
7385 if (isLaneOp)
7386 NumBytes /= VecTy.getVectorNumElements();
7387
7388 // If the increment is a constant, it must match the memory ref size.
7389 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
7390 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
7391 uint64_t IncVal = CInc->getZExtValue();
7392 if (IncVal != NumBytes)
7393 continue;
7394 } else if (NumBytes >= 3 * 16) {
7395 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
7396 // separate instructions that make it harder to use a non-constant update.
7397 continue;
7398 }
7399
7400 // Create the new updating load/store node.
7401 EVT Tys[6];
7402 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
7403 unsigned n;
7404 for (n = 0; n < NumResultVecs; ++n)
7405 Tys[n] = VecTy;
7406 Tys[n++] = MVT::i32;
7407 Tys[n] = MVT::Other;
7408 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
7409 SmallVector<SDValue, 8> Ops;
7410 Ops.push_back(N->getOperand(0)); // incoming chain
7411 Ops.push_back(N->getOperand(AddrOpIdx));
7412 Ops.push_back(Inc);
7413 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
7414 Ops.push_back(N->getOperand(i));
7415 }
7416 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
7417 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
7418 Ops.data(), Ops.size(),
7419 MemInt->getMemoryVT(),
7420 MemInt->getMemOperand());
7421
7422 // Update the uses.
7423 std::vector<SDValue> NewResults;
7424 for (unsigned i = 0; i < NumResultVecs; ++i) {
7425 NewResults.push_back(SDValue(UpdN.getNode(), i));
7426 }
7427 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
7428 DCI.CombineTo(N, NewResults);
7429 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
7430
7431 break;
Owen Anderson76706012011-04-05 21:48:57 +00007432 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00007433 return SDValue();
7434}
7435
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007436/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
7437/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
7438/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
7439/// return true.
7440static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
7441 SelectionDAG &DAG = DCI.DAG;
7442 EVT VT = N->getValueType(0);
7443 // vldN-dup instructions only support 64-bit vectors for N > 1.
7444 if (!VT.is64BitVector())
7445 return false;
7446
7447 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
7448 SDNode *VLD = N->getOperand(0).getNode();
7449 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
7450 return false;
7451 unsigned NumVecs = 0;
7452 unsigned NewOpc = 0;
7453 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
7454 if (IntNo == Intrinsic::arm_neon_vld2lane) {
7455 NumVecs = 2;
7456 NewOpc = ARMISD::VLD2DUP;
7457 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
7458 NumVecs = 3;
7459 NewOpc = ARMISD::VLD3DUP;
7460 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
7461 NumVecs = 4;
7462 NewOpc = ARMISD::VLD4DUP;
7463 } else {
7464 return false;
7465 }
7466
7467 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
7468 // numbers match the load.
7469 unsigned VLDLaneNo =
7470 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
7471 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
7472 UI != UE; ++UI) {
7473 // Ignore uses of the chain result.
7474 if (UI.getUse().getResNo() == NumVecs)
7475 continue;
7476 SDNode *User = *UI;
7477 if (User->getOpcode() != ARMISD::VDUPLANE ||
7478 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
7479 return false;
7480 }
7481
7482 // Create the vldN-dup node.
7483 EVT Tys[5];
7484 unsigned n;
7485 for (n = 0; n < NumVecs; ++n)
7486 Tys[n] = VT;
7487 Tys[n] = MVT::Other;
7488 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
7489 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
7490 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
7491 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
7492 Ops, 2, VLDMemInt->getMemoryVT(),
7493 VLDMemInt->getMemOperand());
7494
7495 // Update the uses.
7496 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
7497 UI != UE; ++UI) {
7498 unsigned ResNo = UI.getUse().getResNo();
7499 // Ignore uses of the chain result.
7500 if (ResNo == NumVecs)
7501 continue;
7502 SDNode *User = *UI;
7503 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
7504 }
7505
7506 // Now the vldN-lane intrinsic is dead except for its chain result.
7507 // Update uses of the chain.
7508 std::vector<SDValue> VLDDupResults;
7509 for (unsigned n = 0; n < NumVecs; ++n)
7510 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
7511 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
7512 DCI.CombineTo(VLD, VLDDupResults);
7513
7514 return true;
7515}
7516
Bob Wilson9e82bf12010-07-14 01:22:12 +00007517/// PerformVDUPLANECombine - Target-specific dag combine xforms for
7518/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007519static SDValue PerformVDUPLANECombine(SDNode *N,
7520 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00007521 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007522
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007523 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
7524 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
7525 if (CombineVLDDUP(N, DCI))
7526 return SDValue(N, 0);
7527
7528 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
7529 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007530 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00007531 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00007532 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00007533 return SDValue();
7534
7535 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
7536 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
7537 // The canonical VMOV for a zero vector uses a 32-bit element size.
7538 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7539 unsigned EltBits;
7540 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
7541 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007542 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007543 if (EltSize > VT.getVectorElementType().getSizeInBits())
7544 return SDValue();
7545
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007546 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007547}
7548
Eric Christopherfa6f5912011-06-29 21:10:36 +00007549// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00007550// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
7551static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
7552{
Chad Rosier118c9a02011-06-28 17:26:57 +00007553 integerPart cN;
7554 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00007555 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
7556 I != E; I++) {
7557 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
7558 if (!C)
7559 return false;
7560
Eric Christopherfa6f5912011-06-29 21:10:36 +00007561 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00007562 APFloat APF = C->getValueAPF();
7563 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
7564 != APFloat::opOK || !isExact)
7565 return false;
7566
7567 c0 = (I == 0) ? cN : c0;
7568 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
7569 return false;
7570 }
7571 C = c0;
7572 return true;
7573}
7574
7575/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
7576/// can replace combinations of VMUL and VCVT (floating-point to integer)
7577/// when the VMUL has a constant operand that is a power of 2.
7578///
7579/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
7580/// vmul.f32 d16, d17, d16
7581/// vcvt.s32.f32 d16, d16
7582/// becomes:
7583/// vcvt.s32.f32 d16, d16, #3
7584static SDValue PerformVCVTCombine(SDNode *N,
7585 TargetLowering::DAGCombinerInfo &DCI,
7586 const ARMSubtarget *Subtarget) {
7587 SelectionDAG &DAG = DCI.DAG;
7588 SDValue Op = N->getOperand(0);
7589
7590 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
7591 Op.getOpcode() != ISD::FMUL)
7592 return SDValue();
7593
7594 uint64_t C;
7595 SDValue N0 = Op->getOperand(0);
7596 SDValue ConstVec = Op->getOperand(1);
7597 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
7598
Eric Christopherfa6f5912011-06-29 21:10:36 +00007599 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00007600 !isConstVecPow2(ConstVec, isSigned, C))
7601 return SDValue();
7602
7603 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
7604 Intrinsic::arm_neon_vcvtfp2fxu;
7605 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7606 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00007607 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00007608 DAG.getConstant(Log2_64(C), MVT::i32));
7609}
7610
7611/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
7612/// can replace combinations of VCVT (integer to floating-point) and VDIV
7613/// when the VDIV has a constant operand that is a power of 2.
7614///
7615/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
7616/// vcvt.f32.s32 d16, d16
7617/// vdiv.f32 d16, d17, d16
7618/// becomes:
7619/// vcvt.f32.s32 d16, d16, #3
7620static SDValue PerformVDIVCombine(SDNode *N,
7621 TargetLowering::DAGCombinerInfo &DCI,
7622 const ARMSubtarget *Subtarget) {
7623 SelectionDAG &DAG = DCI.DAG;
7624 SDValue Op = N->getOperand(0);
7625 unsigned OpOpcode = Op.getNode()->getOpcode();
7626
7627 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
7628 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
7629 return SDValue();
7630
7631 uint64_t C;
7632 SDValue ConstVec = N->getOperand(1);
7633 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
7634
7635 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
7636 !isConstVecPow2(ConstVec, isSigned, C))
7637 return SDValue();
7638
Eric Christopherfa6f5912011-06-29 21:10:36 +00007639 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00007640 Intrinsic::arm_neon_vcvtfxu2fp;
7641 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7642 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00007643 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00007644 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
7645}
7646
7647/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00007648/// operand of a vector shift operation, where all the elements of the
7649/// build_vector must have the same constant integer value.
7650static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
7651 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007652 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00007653 Op = Op.getOperand(0);
7654 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
7655 APInt SplatBits, SplatUndef;
7656 unsigned SplatBitSize;
7657 bool HasAnyUndefs;
7658 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
7659 HasAnyUndefs, ElementBits) ||
7660 SplatBitSize > ElementBits)
7661 return false;
7662 Cnt = SplatBits.getSExtValue();
7663 return true;
7664}
7665
7666/// isVShiftLImm - Check if this is a valid build_vector for the immediate
7667/// operand of a vector shift left operation. That value must be in the range:
7668/// 0 <= Value < ElementBits for a left shift; or
7669/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00007670static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00007671 assert(VT.isVector() && "vector shift count is not a vector type");
7672 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
7673 if (! getVShiftImm(Op, ElementBits, Cnt))
7674 return false;
7675 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
7676}
7677
7678/// isVShiftRImm - Check if this is a valid build_vector for the immediate
7679/// operand of a vector shift right operation. For a shift opcode, the value
7680/// is positive, but for an intrinsic the value count must be negative. The
7681/// absolute value must be in the range:
7682/// 1 <= |Value| <= ElementBits for a right shift; or
7683/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00007684static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00007685 int64_t &Cnt) {
7686 assert(VT.isVector() && "vector shift count is not a vector type");
7687 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
7688 if (! getVShiftImm(Op, ElementBits, Cnt))
7689 return false;
7690 if (isIntrinsic)
7691 Cnt = -Cnt;
7692 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
7693}
7694
7695/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
7696static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
7697 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
7698 switch (IntNo) {
7699 default:
7700 // Don't do anything for most intrinsics.
7701 break;
7702
7703 // Vector shifts: check for immediate versions and lower them.
7704 // Note: This is done during DAG combining instead of DAG legalizing because
7705 // the build_vectors for 64-bit vector element shift counts are generally
7706 // not legal, and it is hard to see their values after they get legalized to
7707 // loads from a constant pool.
7708 case Intrinsic::arm_neon_vshifts:
7709 case Intrinsic::arm_neon_vshiftu:
7710 case Intrinsic::arm_neon_vshiftls:
7711 case Intrinsic::arm_neon_vshiftlu:
7712 case Intrinsic::arm_neon_vshiftn:
7713 case Intrinsic::arm_neon_vrshifts:
7714 case Intrinsic::arm_neon_vrshiftu:
7715 case Intrinsic::arm_neon_vrshiftn:
7716 case Intrinsic::arm_neon_vqshifts:
7717 case Intrinsic::arm_neon_vqshiftu:
7718 case Intrinsic::arm_neon_vqshiftsu:
7719 case Intrinsic::arm_neon_vqshiftns:
7720 case Intrinsic::arm_neon_vqshiftnu:
7721 case Intrinsic::arm_neon_vqshiftnsu:
7722 case Intrinsic::arm_neon_vqrshiftns:
7723 case Intrinsic::arm_neon_vqrshiftnu:
7724 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00007725 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007726 int64_t Cnt;
7727 unsigned VShiftOpc = 0;
7728
7729 switch (IntNo) {
7730 case Intrinsic::arm_neon_vshifts:
7731 case Intrinsic::arm_neon_vshiftu:
7732 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
7733 VShiftOpc = ARMISD::VSHL;
7734 break;
7735 }
7736 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
7737 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
7738 ARMISD::VSHRs : ARMISD::VSHRu);
7739 break;
7740 }
7741 return SDValue();
7742
7743 case Intrinsic::arm_neon_vshiftls:
7744 case Intrinsic::arm_neon_vshiftlu:
7745 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
7746 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007747 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007748
7749 case Intrinsic::arm_neon_vrshifts:
7750 case Intrinsic::arm_neon_vrshiftu:
7751 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
7752 break;
7753 return SDValue();
7754
7755 case Intrinsic::arm_neon_vqshifts:
7756 case Intrinsic::arm_neon_vqshiftu:
7757 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
7758 break;
7759 return SDValue();
7760
7761 case Intrinsic::arm_neon_vqshiftsu:
7762 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
7763 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007764 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007765
7766 case Intrinsic::arm_neon_vshiftn:
7767 case Intrinsic::arm_neon_vrshiftn:
7768 case Intrinsic::arm_neon_vqshiftns:
7769 case Intrinsic::arm_neon_vqshiftnu:
7770 case Intrinsic::arm_neon_vqshiftnsu:
7771 case Intrinsic::arm_neon_vqrshiftns:
7772 case Intrinsic::arm_neon_vqrshiftnu:
7773 case Intrinsic::arm_neon_vqrshiftnsu:
7774 // Narrowing shifts require an immediate right shift.
7775 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
7776 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00007777 llvm_unreachable("invalid shift count for narrowing vector shift "
7778 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007779
7780 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00007781 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00007782 }
7783
7784 switch (IntNo) {
7785 case Intrinsic::arm_neon_vshifts:
7786 case Intrinsic::arm_neon_vshiftu:
7787 // Opcode already set above.
7788 break;
7789 case Intrinsic::arm_neon_vshiftls:
7790 case Intrinsic::arm_neon_vshiftlu:
7791 if (Cnt == VT.getVectorElementType().getSizeInBits())
7792 VShiftOpc = ARMISD::VSHLLi;
7793 else
7794 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
7795 ARMISD::VSHLLs : ARMISD::VSHLLu);
7796 break;
7797 case Intrinsic::arm_neon_vshiftn:
7798 VShiftOpc = ARMISD::VSHRN; break;
7799 case Intrinsic::arm_neon_vrshifts:
7800 VShiftOpc = ARMISD::VRSHRs; break;
7801 case Intrinsic::arm_neon_vrshiftu:
7802 VShiftOpc = ARMISD::VRSHRu; break;
7803 case Intrinsic::arm_neon_vrshiftn:
7804 VShiftOpc = ARMISD::VRSHRN; break;
7805 case Intrinsic::arm_neon_vqshifts:
7806 VShiftOpc = ARMISD::VQSHLs; break;
7807 case Intrinsic::arm_neon_vqshiftu:
7808 VShiftOpc = ARMISD::VQSHLu; break;
7809 case Intrinsic::arm_neon_vqshiftsu:
7810 VShiftOpc = ARMISD::VQSHLsu; break;
7811 case Intrinsic::arm_neon_vqshiftns:
7812 VShiftOpc = ARMISD::VQSHRNs; break;
7813 case Intrinsic::arm_neon_vqshiftnu:
7814 VShiftOpc = ARMISD::VQSHRNu; break;
7815 case Intrinsic::arm_neon_vqshiftnsu:
7816 VShiftOpc = ARMISD::VQSHRNsu; break;
7817 case Intrinsic::arm_neon_vqrshiftns:
7818 VShiftOpc = ARMISD::VQRSHRNs; break;
7819 case Intrinsic::arm_neon_vqrshiftnu:
7820 VShiftOpc = ARMISD::VQRSHRNu; break;
7821 case Intrinsic::arm_neon_vqrshiftnsu:
7822 VShiftOpc = ARMISD::VQRSHRNsu; break;
7823 }
7824
7825 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007826 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007827 }
7828
7829 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00007830 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007831 int64_t Cnt;
7832 unsigned VShiftOpc = 0;
7833
7834 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
7835 VShiftOpc = ARMISD::VSLI;
7836 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
7837 VShiftOpc = ARMISD::VSRI;
7838 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00007839 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007840 }
7841
7842 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
7843 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00007844 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007845 }
7846
7847 case Intrinsic::arm_neon_vqrshifts:
7848 case Intrinsic::arm_neon_vqrshiftu:
7849 // No immediate versions of these to check for.
7850 break;
7851 }
7852
7853 return SDValue();
7854}
7855
7856/// PerformShiftCombine - Checks for immediate versions of vector shifts and
7857/// lowers them. As with the vector shift intrinsics, this is done during DAG
7858/// combining instead of DAG legalizing because the build_vectors for 64-bit
7859/// vector element shift counts are generally not legal, and it is hard to see
7860/// their values after they get legalized to loads from a constant pool.
7861static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
7862 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00007863 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00007864
7865 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00007866 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7867 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00007868 return SDValue();
7869
7870 assert(ST->hasNEON() && "unexpected vector shift");
7871 int64_t Cnt;
7872
7873 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007874 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00007875
7876 case ISD::SHL:
7877 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
7878 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007879 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007880 break;
7881
7882 case ISD::SRA:
7883 case ISD::SRL:
7884 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
7885 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
7886 ARMISD::VSHRs : ARMISD::VSHRu);
7887 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007888 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007889 }
7890 }
7891 return SDValue();
7892}
7893
7894/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
7895/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
7896static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
7897 const ARMSubtarget *ST) {
7898 SDValue N0 = N->getOperand(0);
7899
7900 // Check for sign- and zero-extensions of vector extract operations of 8-
7901 // and 16-bit vector elements. NEON supports these directly. They are
7902 // handled during DAG combining because type legalization will promote them
7903 // to 32-bit types and it is messy to recognize the operations after that.
7904 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
7905 SDValue Vec = N0.getOperand(0);
7906 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00007907 EVT VT = N->getValueType(0);
7908 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007909 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7910
Owen Anderson825b72b2009-08-11 20:47:22 +00007911 if (VT == MVT::i32 &&
7912 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00007913 TLI.isTypeLegal(Vec.getValueType()) &&
7914 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00007915
7916 unsigned Opc = 0;
7917 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007918 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00007919 case ISD::SIGN_EXTEND:
7920 Opc = ARMISD::VGETLANEs;
7921 break;
7922 case ISD::ZERO_EXTEND:
7923 case ISD::ANY_EXTEND:
7924 Opc = ARMISD::VGETLANEu;
7925 break;
7926 }
7927 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
7928 }
7929 }
7930
7931 return SDValue();
7932}
7933
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007934/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
7935/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
7936static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
7937 const ARMSubtarget *ST) {
7938 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00007939 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007940 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
7941 // a NaN; only do the transformation when it matches that behavior.
7942
7943 // For now only do this when using NEON for FP operations; if using VFP, it
7944 // is not obvious that the benefit outweighs the cost of switching to the
7945 // NEON pipeline.
7946 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
7947 N->getValueType(0) != MVT::f32)
7948 return SDValue();
7949
7950 SDValue CondLHS = N->getOperand(0);
7951 SDValue CondRHS = N->getOperand(1);
7952 SDValue LHS = N->getOperand(2);
7953 SDValue RHS = N->getOperand(3);
7954 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
7955
7956 unsigned Opcode = 0;
7957 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00007958 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007959 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00007960 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007961 IsReversed = true ; // x CC y ? y : x
7962 } else {
7963 return SDValue();
7964 }
7965
Bob Wilsone742bb52010-02-24 22:15:53 +00007966 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007967 switch (CC) {
7968 default: break;
7969 case ISD::SETOLT:
7970 case ISD::SETOLE:
7971 case ISD::SETLT:
7972 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007973 case ISD::SETULT:
7974 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00007975 // If LHS is NaN, an ordered comparison will be false and the result will
7976 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
7977 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
7978 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
7979 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
7980 break;
7981 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
7982 // will return -0, so vmin can only be used for unsafe math or if one of
7983 // the operands is known to be nonzero.
7984 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00007985 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00007986 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
7987 break;
7988 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007989 break;
7990
7991 case ISD::SETOGT:
7992 case ISD::SETOGE:
7993 case ISD::SETGT:
7994 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007995 case ISD::SETUGT:
7996 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00007997 // If LHS is NaN, an ordered comparison will be false and the result will
7998 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
7999 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
8000 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
8001 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8002 break;
8003 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
8004 // will return +0, so vmax can only be used for unsafe math or if one of
8005 // the operands is known to be nonzero.
8006 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008007 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00008008 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8009 break;
8010 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008011 break;
8012 }
8013
8014 if (!Opcode)
8015 return SDValue();
8016 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
8017}
8018
Evan Chenge721f5c2011-07-13 00:42:17 +00008019/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
8020SDValue
8021ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
8022 SDValue Cmp = N->getOperand(4);
8023 if (Cmp.getOpcode() != ARMISD::CMPZ)
8024 // Only looking at EQ and NE cases.
8025 return SDValue();
8026
8027 EVT VT = N->getValueType(0);
8028 DebugLoc dl = N->getDebugLoc();
8029 SDValue LHS = Cmp.getOperand(0);
8030 SDValue RHS = Cmp.getOperand(1);
8031 SDValue FalseVal = N->getOperand(0);
8032 SDValue TrueVal = N->getOperand(1);
8033 SDValue ARMcc = N->getOperand(2);
Jim Grosbachb04546f2011-09-13 20:30:37 +00008034 ARMCC::CondCodes CC =
8035 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chenge721f5c2011-07-13 00:42:17 +00008036
8037 // Simplify
8038 // mov r1, r0
8039 // cmp r1, x
8040 // mov r0, y
8041 // moveq r0, x
8042 // to
8043 // cmp r0, x
8044 // movne r0, y
8045 //
8046 // mov r1, r0
8047 // cmp r1, x
8048 // mov r0, x
8049 // movne r0, y
8050 // to
8051 // cmp r0, x
8052 // movne r0, y
8053 /// FIXME: Turn this into a target neutral optimization?
8054 SDValue Res;
Evan Cheng9b88d2d2011-09-28 23:16:31 +00008055 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chenge721f5c2011-07-13 00:42:17 +00008056 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
8057 N->getOperand(3), Cmp);
8058 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
8059 SDValue ARMcc;
8060 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
8061 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
8062 N->getOperand(3), NewCmp);
8063 }
8064
8065 if (Res.getNode()) {
8066 APInt KnownZero, KnownOne;
8067 APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits());
8068 DAG.ComputeMaskedBits(SDValue(N,0), Mask, KnownZero, KnownOne);
8069 // Capture demanded bits information that would be otherwise lost.
8070 if (KnownZero == 0xfffffffe)
8071 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8072 DAG.getValueType(MVT::i1));
8073 else if (KnownZero == 0xffffff00)
8074 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8075 DAG.getValueType(MVT::i8));
8076 else if (KnownZero == 0xffff0000)
8077 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8078 DAG.getValueType(MVT::i16));
8079 }
8080
8081 return Res;
8082}
8083
Dan Gohman475871a2008-07-27 21:46:04 +00008084SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008085 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008086 switch (N->getOpcode()) {
8087 default: break;
Tanya Lattner189531f2011-06-14 23:48:48 +00008088 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008089 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00008090 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008091 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00008092 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00008093 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00008094 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008095 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00008096 case ISD::STORE: return PerformSTORECombine(N, DCI);
8097 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
8098 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00008099 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008100 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00008101 case ISD::FP_TO_SINT:
8102 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
8103 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008104 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00008105 case ISD::SHL:
8106 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008107 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00008108 case ISD::SIGN_EXTEND:
8109 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008110 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
8111 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00008112 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00008113 case ARMISD::VLD2DUP:
8114 case ARMISD::VLD3DUP:
8115 case ARMISD::VLD4DUP:
8116 return CombineBaseUpdate(N, DCI);
8117 case ISD::INTRINSIC_VOID:
8118 case ISD::INTRINSIC_W_CHAIN:
8119 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
8120 case Intrinsic::arm_neon_vld1:
8121 case Intrinsic::arm_neon_vld2:
8122 case Intrinsic::arm_neon_vld3:
8123 case Intrinsic::arm_neon_vld4:
8124 case Intrinsic::arm_neon_vld2lane:
8125 case Intrinsic::arm_neon_vld3lane:
8126 case Intrinsic::arm_neon_vld4lane:
8127 case Intrinsic::arm_neon_vst1:
8128 case Intrinsic::arm_neon_vst2:
8129 case Intrinsic::arm_neon_vst3:
8130 case Intrinsic::arm_neon_vst4:
8131 case Intrinsic::arm_neon_vst2lane:
8132 case Intrinsic::arm_neon_vst3lane:
8133 case Intrinsic::arm_neon_vst4lane:
8134 return CombineBaseUpdate(N, DCI);
8135 default: break;
8136 }
8137 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008138 }
Dan Gohman475871a2008-07-27 21:46:04 +00008139 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008140}
8141
Evan Cheng31959b12011-02-02 01:06:55 +00008142bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
8143 EVT VT) const {
8144 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
8145}
8146
Bill Wendlingaf566342009-08-15 21:21:19 +00008147bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00008148 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00008149 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00008150
8151 switch (VT.getSimpleVT().SimpleTy) {
8152 default:
8153 return false;
8154 case MVT::i8:
8155 case MVT::i16:
8156 case MVT::i32:
8157 return true;
8158 // FIXME: VLD1 etc with standard alignment is legal.
8159 }
8160}
8161
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008162static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
8163 unsigned AlignCheck) {
8164 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
8165 (DstAlign == 0 || DstAlign % AlignCheck == 0));
8166}
8167
8168EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
8169 unsigned DstAlign, unsigned SrcAlign,
Lang Hamesa1e78882011-11-02 23:37:04 +00008170 bool IsZeroVal,
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008171 bool MemcpyStrSrc,
8172 MachineFunction &MF) const {
8173 const Function *F = MF.getFunction();
8174
8175 // See if we can use NEON instructions for this...
Lang Hamesa1e78882011-11-02 23:37:04 +00008176 if (IsZeroVal &&
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008177 !F->hasFnAttr(Attribute::NoImplicitFloat) &&
8178 Subtarget->hasNEON()) {
8179 if (memOpAlign(SrcAlign, DstAlign, 16) && Size >= 16) {
8180 return MVT::v4i32;
8181 } else if (memOpAlign(SrcAlign, DstAlign, 8) && Size >= 8) {
8182 return MVT::v2i32;
8183 }
8184 }
8185
Lang Hames5207bf22011-11-08 18:56:23 +00008186 // Lowering to i32/i16 if the size permits.
8187 if (Size >= 4) {
8188 return MVT::i32;
8189 } else if (Size >= 2) {
8190 return MVT::i16;
8191 }
8192
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008193 // Let the target-independent logic figure it out.
8194 return MVT::Other;
8195}
8196
Evan Chenge6c835f2009-08-14 20:09:37 +00008197static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
8198 if (V < 0)
8199 return false;
8200
8201 unsigned Scale = 1;
8202 switch (VT.getSimpleVT().SimpleTy) {
8203 default: return false;
8204 case MVT::i1:
8205 case MVT::i8:
8206 // Scale == 1;
8207 break;
8208 case MVT::i16:
8209 // Scale == 2;
8210 Scale = 2;
8211 break;
8212 case MVT::i32:
8213 // Scale == 4;
8214 Scale = 4;
8215 break;
8216 }
8217
8218 if ((V & (Scale - 1)) != 0)
8219 return false;
8220 V /= Scale;
8221 return V == (V & ((1LL << 5) - 1));
8222}
8223
8224static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
8225 const ARMSubtarget *Subtarget) {
8226 bool isNeg = false;
8227 if (V < 0) {
8228 isNeg = true;
8229 V = - V;
8230 }
8231
8232 switch (VT.getSimpleVT().SimpleTy) {
8233 default: return false;
8234 case MVT::i1:
8235 case MVT::i8:
8236 case MVT::i16:
8237 case MVT::i32:
8238 // + imm12 or - imm8
8239 if (isNeg)
8240 return V == (V & ((1LL << 8) - 1));
8241 return V == (V & ((1LL << 12) - 1));
8242 case MVT::f32:
8243 case MVT::f64:
8244 // Same as ARM mode. FIXME: NEON?
8245 if (!Subtarget->hasVFP2())
8246 return false;
8247 if ((V & 3) != 0)
8248 return false;
8249 V >>= 2;
8250 return V == (V & ((1LL << 8) - 1));
8251 }
8252}
8253
Evan Chengb01fad62007-03-12 23:30:29 +00008254/// isLegalAddressImmediate - Return true if the integer value can be used
8255/// as the offset of the target addressing mode for load / store of the
8256/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00008257static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00008258 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00008259 if (V == 0)
8260 return true;
8261
Evan Cheng65011532009-03-09 19:15:00 +00008262 if (!VT.isSimple())
8263 return false;
8264
Evan Chenge6c835f2009-08-14 20:09:37 +00008265 if (Subtarget->isThumb1Only())
8266 return isLegalT1AddressImmediate(V, VT);
8267 else if (Subtarget->isThumb2())
8268 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00008269
Evan Chenge6c835f2009-08-14 20:09:37 +00008270 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00008271 if (V < 0)
8272 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00008273 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00008274 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008275 case MVT::i1:
8276 case MVT::i8:
8277 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00008278 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008279 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008280 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00008281 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008282 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008283 case MVT::f32:
8284 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00008285 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00008286 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00008287 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00008288 return false;
8289 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008290 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00008291 }
Evan Chenga8e29892007-01-19 07:51:42 +00008292}
8293
Evan Chenge6c835f2009-08-14 20:09:37 +00008294bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
8295 EVT VT) const {
8296 int Scale = AM.Scale;
8297 if (Scale < 0)
8298 return false;
8299
8300 switch (VT.getSimpleVT().SimpleTy) {
8301 default: return false;
8302 case MVT::i1:
8303 case MVT::i8:
8304 case MVT::i16:
8305 case MVT::i32:
8306 if (Scale == 1)
8307 return true;
8308 // r + r << imm
8309 Scale = Scale & ~1;
8310 return Scale == 2 || Scale == 4 || Scale == 8;
8311 case MVT::i64:
8312 // r + r
8313 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
8314 return true;
8315 return false;
8316 case MVT::isVoid:
8317 // Note, we allow "void" uses (basically, uses that aren't loads or
8318 // stores), because arm allows folding a scale into many arithmetic
8319 // operations. This should be made more precise and revisited later.
8320
8321 // Allow r << imm, but the imm has to be a multiple of two.
8322 if (Scale & 1) return false;
8323 return isPowerOf2_32(Scale);
8324 }
8325}
8326
Chris Lattner37caf8c2007-04-09 23:33:39 +00008327/// isLegalAddressingMode - Return true if the addressing mode represented
8328/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008329bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008330 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008331 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00008332 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00008333 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008334
Chris Lattner37caf8c2007-04-09 23:33:39 +00008335 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008336 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008337 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008338
Chris Lattner37caf8c2007-04-09 23:33:39 +00008339 switch (AM.Scale) {
8340 case 0: // no scale reg, must be "r+i" or "r", or "i".
8341 break;
8342 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00008343 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00008344 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008345 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00008346 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008347 // ARM doesn't support any R+R*scale+imm addr modes.
8348 if (AM.BaseOffs)
8349 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008350
Bob Wilson2c7dab12009-04-08 17:55:28 +00008351 if (!VT.isSimple())
8352 return false;
8353
Evan Chenge6c835f2009-08-14 20:09:37 +00008354 if (Subtarget->isThumb2())
8355 return isLegalT2ScaledAddressingMode(AM, VT);
8356
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008357 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00008358 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00008359 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008360 case MVT::i1:
8361 case MVT::i8:
8362 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008363 if (Scale < 0) Scale = -Scale;
8364 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008365 return true;
8366 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00008367 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008368 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00008369 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00008370 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008371 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008372 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00008373 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008374
Owen Anderson825b72b2009-08-11 20:47:22 +00008375 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00008376 // Note, we allow "void" uses (basically, uses that aren't loads or
8377 // stores), because arm allows folding a scale into many arithmetic
8378 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008379
Chris Lattner37caf8c2007-04-09 23:33:39 +00008380 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00008381 if (Scale & 1) return false;
8382 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00008383 }
8384 break;
Evan Chengb01fad62007-03-12 23:30:29 +00008385 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00008386 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00008387}
8388
Evan Cheng77e47512009-11-11 19:05:52 +00008389/// isLegalICmpImmediate - Return true if the specified immediate is legal
8390/// icmp immediate, that is the target has icmp instructions which can compare
8391/// a register against the immediate without having to materialize the
8392/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00008393bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00008394 if (!Subtarget->isThumb())
8395 return ARM_AM::getSOImmVal(Imm) != -1;
8396 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00008397 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00008398 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00008399}
8400
Dan Gohmancca82142011-05-03 00:46:49 +00008401/// isLegalAddImmediate - Return true if the specified immediate is legal
8402/// add immediate, that is the target has add instructions which can add
8403/// a register with the immediate without having to materialize the
8404/// immediate into a register.
8405bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
8406 return ARM_AM::getSOImmVal(Imm) != -1;
8407}
8408
Owen Andersone50ed302009-08-10 22:56:29 +00008409static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00008410 bool isSEXTLoad, SDValue &Base,
8411 SDValue &Offset, bool &isInc,
8412 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00008413 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
8414 return false;
8415
Owen Anderson825b72b2009-08-11 20:47:22 +00008416 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00008417 // AddressingMode 3
8418 Base = Ptr->getOperand(0);
8419 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008420 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00008421 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008422 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00008423 isInc = false;
8424 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8425 return true;
8426 }
8427 }
8428 isInc = (Ptr->getOpcode() == ISD::ADD);
8429 Offset = Ptr->getOperand(1);
8430 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00008431 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00008432 // AddressingMode 2
8433 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008434 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00008435 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008436 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00008437 isInc = false;
8438 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8439 Base = Ptr->getOperand(0);
8440 return true;
8441 }
8442 }
8443
8444 if (Ptr->getOpcode() == ISD::ADD) {
8445 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00008446 ARM_AM::ShiftOpc ShOpcVal=
8447 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00008448 if (ShOpcVal != ARM_AM::no_shift) {
8449 Base = Ptr->getOperand(1);
8450 Offset = Ptr->getOperand(0);
8451 } else {
8452 Base = Ptr->getOperand(0);
8453 Offset = Ptr->getOperand(1);
8454 }
8455 return true;
8456 }
8457
8458 isInc = (Ptr->getOpcode() == ISD::ADD);
8459 Base = Ptr->getOperand(0);
8460 Offset = Ptr->getOperand(1);
8461 return true;
8462 }
8463
Jim Grosbache5165492009-11-09 00:11:35 +00008464 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00008465 return false;
8466}
8467
Owen Andersone50ed302009-08-10 22:56:29 +00008468static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00008469 bool isSEXTLoad, SDValue &Base,
8470 SDValue &Offset, bool &isInc,
8471 SelectionDAG &DAG) {
8472 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
8473 return false;
8474
8475 Base = Ptr->getOperand(0);
8476 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
8477 int RHSC = (int)RHS->getZExtValue();
8478 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
8479 assert(Ptr->getOpcode() == ISD::ADD);
8480 isInc = false;
8481 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8482 return true;
8483 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
8484 isInc = Ptr->getOpcode() == ISD::ADD;
8485 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
8486 return true;
8487 }
8488 }
8489
8490 return false;
8491}
8492
Evan Chenga8e29892007-01-19 07:51:42 +00008493/// getPreIndexedAddressParts - returns true by value, base pointer and
8494/// offset pointer and addressing mode by reference if the node's address
8495/// can be legally represented as pre-indexed load / store address.
8496bool
Dan Gohman475871a2008-07-27 21:46:04 +00008497ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
8498 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00008499 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00008500 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008501 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00008502 return false;
8503
Owen Andersone50ed302009-08-10 22:56:29 +00008504 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00008505 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00008506 bool isSEXTLoad = false;
8507 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
8508 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008509 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00008510 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
8511 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
8512 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008513 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00008514 } else
8515 return false;
8516
8517 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00008518 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00008519 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00008520 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
8521 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00008522 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00008523 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00008524 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00008525 if (!isLegal)
8526 return false;
8527
8528 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
8529 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00008530}
8531
8532/// getPostIndexedAddressParts - returns true by value, base pointer and
8533/// offset pointer and addressing mode by reference if this node can be
8534/// combined with a load / store to form a post-indexed load / store.
8535bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00008536 SDValue &Base,
8537 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00008538 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00008539 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008540 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00008541 return false;
8542
Owen Andersone50ed302009-08-10 22:56:29 +00008543 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00008544 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00008545 bool isSEXTLoad = false;
8546 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008547 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00008548 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00008549 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
8550 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008551 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00008552 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00008553 } else
8554 return false;
8555
8556 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00008557 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00008558 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00008559 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00008560 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00008561 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00008562 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
8563 isInc, DAG);
8564 if (!isLegal)
8565 return false;
8566
Evan Cheng28dad2a2010-05-18 21:31:17 +00008567 if (Ptr != Base) {
8568 // Swap base ptr and offset to catch more post-index load / store when
8569 // it's legal. In Thumb2 mode, offset must be an immediate.
8570 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
8571 !Subtarget->isThumb2())
8572 std::swap(Base, Offset);
8573
8574 // Post-indexed load / store update the base pointer.
8575 if (Ptr != Base)
8576 return false;
8577 }
8578
Evan Chenge88d5ce2009-07-02 07:28:31 +00008579 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
8580 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00008581}
8582
Dan Gohman475871a2008-07-27 21:46:04 +00008583void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00008584 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008585 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008586 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008587 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00008588 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008589 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00008590 switch (Op.getOpcode()) {
8591 default: break;
8592 case ARMISD::CMOV: {
8593 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00008594 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00008595 if (KnownZero == 0 && KnownOne == 0) return;
8596
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008597 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00008598 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
8599 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00008600 KnownZero &= KnownZeroRHS;
8601 KnownOne &= KnownOneRHS;
8602 return;
8603 }
8604 }
8605}
8606
8607//===----------------------------------------------------------------------===//
8608// ARM Inline Assembly Support
8609//===----------------------------------------------------------------------===//
8610
Evan Cheng55d42002011-01-08 01:24:27 +00008611bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
8612 // Looking for "rev" which is V6+.
8613 if (!Subtarget->hasV6Ops())
8614 return false;
8615
8616 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
8617 std::string AsmStr = IA->getAsmString();
8618 SmallVector<StringRef, 4> AsmPieces;
8619 SplitString(AsmStr, AsmPieces, ";\n");
8620
8621 switch (AsmPieces.size()) {
8622 default: return false;
8623 case 1:
8624 AsmStr = AsmPieces[0];
8625 AsmPieces.clear();
8626 SplitString(AsmStr, AsmPieces, " \t,");
8627
8628 // rev $0, $1
8629 if (AsmPieces.size() == 3 &&
8630 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
8631 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008632 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00008633 if (Ty && Ty->getBitWidth() == 32)
8634 return IntrinsicLowering::LowerToByteSwap(CI);
8635 }
8636 break;
8637 }
8638
8639 return false;
8640}
8641
Evan Chenga8e29892007-01-19 07:51:42 +00008642/// getConstraintType - Given a constraint letter, return the type of
8643/// constraint it is for this target.
8644ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00008645ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
8646 if (Constraint.size() == 1) {
8647 switch (Constraint[0]) {
8648 default: break;
8649 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008650 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00008651 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00008652 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008653 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00008654 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00008655 // An address with a single base register. Due to the way we
8656 // currently handle addresses it is the same as an 'r' memory constraint.
8657 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00008658 }
Eric Christopher1312ca82011-06-21 22:10:57 +00008659 } else if (Constraint.size() == 2) {
8660 switch (Constraint[0]) {
8661 default: break;
8662 // All 'U+' constraints are addresses.
8663 case 'U': return C_Memory;
8664 }
Evan Chenga8e29892007-01-19 07:51:42 +00008665 }
Chris Lattner4234f572007-03-25 02:14:49 +00008666 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00008667}
8668
John Thompson44ab89e2010-10-29 17:29:13 +00008669/// Examine constraint type and operand type and determine a weight value.
8670/// This object must already have been set up with the operand type
8671/// and the current alternative constraint selected.
8672TargetLowering::ConstraintWeight
8673ARMTargetLowering::getSingleConstraintMatchWeight(
8674 AsmOperandInfo &info, const char *constraint) const {
8675 ConstraintWeight weight = CW_Invalid;
8676 Value *CallOperandVal = info.CallOperandVal;
8677 // If we don't have a value, we can't do a match,
8678 // but allow it at the lowest weight.
8679 if (CallOperandVal == NULL)
8680 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008681 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00008682 // Look at the constraint type.
8683 switch (*constraint) {
8684 default:
8685 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
8686 break;
8687 case 'l':
8688 if (type->isIntegerTy()) {
8689 if (Subtarget->isThumb())
8690 weight = CW_SpecificReg;
8691 else
8692 weight = CW_Register;
8693 }
8694 break;
8695 case 'w':
8696 if (type->isFloatingPointTy())
8697 weight = CW_Register;
8698 break;
8699 }
8700 return weight;
8701}
8702
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008703typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
8704RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00008705ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00008706 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00008707 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00008708 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00008709 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00008710 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00008711 if (Subtarget->isThumb())
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008712 return RCPair(0U, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +00008713 else
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008714 return RCPair(0U, ARM::GPRRegisterClass);
Eric Christopher73744df2011-06-30 23:23:01 +00008715 case 'h': // High regs or no regs.
8716 if (Subtarget->isThumb())
Andrew Trick3af7a672011-09-20 03:06:13 +00008717 return RCPair(0U, ARM::hGPRRegisterClass);
Eric Christopher1070f822011-07-01 00:19:27 +00008718 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008719 case 'r':
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008720 return RCPair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008721 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00008722 if (VT == MVT::f32)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008723 return RCPair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00008724 if (VT.getSizeInBits() == 64)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008725 return RCPair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00008726 if (VT.getSizeInBits() == 128)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008727 return RCPair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008728 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00008729 case 'x':
8730 if (VT == MVT::f32)
Andrew Trick3af7a672011-09-20 03:06:13 +00008731 return RCPair(0U, ARM::SPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008732 if (VT.getSizeInBits() == 64)
Andrew Trick3af7a672011-09-20 03:06:13 +00008733 return RCPair(0U, ARM::DPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008734 if (VT.getSizeInBits() == 128)
Andrew Trick3af7a672011-09-20 03:06:13 +00008735 return RCPair(0U, ARM::QPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008736 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008737 case 't':
8738 if (VT == MVT::f32)
Andrew Trick3af7a672011-09-20 03:06:13 +00008739 return RCPair(0U, ARM::SPRRegisterClass);
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008740 break;
Evan Chenga8e29892007-01-19 07:51:42 +00008741 }
8742 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00008743 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00008744 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00008745
Evan Chenga8e29892007-01-19 07:51:42 +00008746 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
8747}
8748
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008749/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
8750/// vector. If it is invalid, don't add anything to Ops.
8751void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00008752 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008753 std::vector<SDValue>&Ops,
8754 SelectionDAG &DAG) const {
8755 SDValue Result(0, 0);
8756
Eric Christopher100c8332011-06-02 23:16:42 +00008757 // Currently only support length 1 constraints.
8758 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00008759
Eric Christopher100c8332011-06-02 23:16:42 +00008760 char ConstraintLetter = Constraint[0];
8761 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008762 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00008763 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008764 case 'I': case 'J': case 'K': case 'L':
8765 case 'M': case 'N': case 'O':
8766 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
8767 if (!C)
8768 return;
8769
8770 int64_t CVal64 = C->getSExtValue();
8771 int CVal = (int) CVal64;
8772 // None of these constraints allow values larger than 32 bits. Check
8773 // that the value fits in an int.
8774 if (CVal != CVal64)
8775 return;
8776
Eric Christopher100c8332011-06-02 23:16:42 +00008777 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00008778 case 'j':
Andrew Trick3af7a672011-09-20 03:06:13 +00008779 // Constant suitable for movw, must be between 0 and
8780 // 65535.
8781 if (Subtarget->hasV6T2Ops())
8782 if (CVal >= 0 && CVal <= 65535)
8783 break;
8784 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008785 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008786 if (Subtarget->isThumb1Only()) {
8787 // This must be a constant between 0 and 255, for ADD
8788 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008789 if (CVal >= 0 && CVal <= 255)
8790 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008791 } else if (Subtarget->isThumb2()) {
8792 // A constant that can be used as an immediate value in a
8793 // data-processing instruction.
8794 if (ARM_AM::getT2SOImmVal(CVal) != -1)
8795 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008796 } else {
8797 // A constant that can be used as an immediate value in a
8798 // data-processing instruction.
8799 if (ARM_AM::getSOImmVal(CVal) != -1)
8800 break;
8801 }
8802 return;
8803
8804 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008805 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008806 // This must be a constant between -255 and -1, for negated ADD
8807 // immediates. This can be used in GCC with an "n" modifier that
8808 // prints the negated value, for use with SUB instructions. It is
8809 // not useful otherwise but is implemented for compatibility.
8810 if (CVal >= -255 && CVal <= -1)
8811 break;
8812 } else {
8813 // This must be a constant between -4095 and 4095. It is not clear
8814 // what this constraint is intended for. Implemented for
8815 // compatibility with GCC.
8816 if (CVal >= -4095 && CVal <= 4095)
8817 break;
8818 }
8819 return;
8820
8821 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008822 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008823 // A 32-bit value where only one byte has a nonzero value. Exclude
8824 // zero to match GCC. This constraint is used by GCC internally for
8825 // constants that can be loaded with a move/shift combination.
8826 // It is not useful otherwise but is implemented for compatibility.
8827 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
8828 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008829 } else if (Subtarget->isThumb2()) {
8830 // A constant whose bitwise inverse can be used as an immediate
8831 // value in a data-processing instruction. This can be used in GCC
8832 // with a "B" modifier that prints the inverted value, for use with
8833 // BIC and MVN instructions. It is not useful otherwise but is
8834 // implemented for compatibility.
8835 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
8836 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008837 } else {
8838 // A constant whose bitwise inverse can be used as an immediate
8839 // value in a data-processing instruction. This can be used in GCC
8840 // with a "B" modifier that prints the inverted value, for use with
8841 // BIC and MVN instructions. It is not useful otherwise but is
8842 // implemented for compatibility.
8843 if (ARM_AM::getSOImmVal(~CVal) != -1)
8844 break;
8845 }
8846 return;
8847
8848 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008849 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008850 // This must be a constant between -7 and 7,
8851 // for 3-operand ADD/SUB immediate instructions.
8852 if (CVal >= -7 && CVal < 7)
8853 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008854 } else if (Subtarget->isThumb2()) {
8855 // A constant whose negation can be used as an immediate value in a
8856 // data-processing instruction. This can be used in GCC with an "n"
8857 // modifier that prints the negated value, for use with SUB
8858 // instructions. It is not useful otherwise but is implemented for
8859 // compatibility.
8860 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
8861 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008862 } else {
8863 // A constant whose negation can be used as an immediate value in a
8864 // data-processing instruction. This can be used in GCC with an "n"
8865 // modifier that prints the negated value, for use with SUB
8866 // instructions. It is not useful otherwise but is implemented for
8867 // compatibility.
8868 if (ARM_AM::getSOImmVal(-CVal) != -1)
8869 break;
8870 }
8871 return;
8872
8873 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008874 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008875 // This must be a multiple of 4 between 0 and 1020, for
8876 // ADD sp + immediate.
8877 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
8878 break;
8879 } else {
8880 // A power of two or a constant between 0 and 32. This is used in
8881 // GCC for the shift amount on shifted register operands, but it is
8882 // useful in general for any shift amounts.
8883 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
8884 break;
8885 }
8886 return;
8887
8888 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008889 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008890 // This must be a constant between 0 and 31, for shift amounts.
8891 if (CVal >= 0 && CVal <= 31)
8892 break;
8893 }
8894 return;
8895
8896 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008897 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008898 // This must be a multiple of 4 between -508 and 508, for
8899 // ADD/SUB sp = sp + immediate.
8900 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
8901 break;
8902 }
8903 return;
8904 }
8905 Result = DAG.getTargetConstant(CVal, Op.getValueType());
8906 break;
8907 }
8908
8909 if (Result.getNode()) {
8910 Ops.push_back(Result);
8911 return;
8912 }
Dale Johannesen1784d162010-06-25 21:55:36 +00008913 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008914}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00008915
8916bool
8917ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
8918 // The ARM target isn't yet aware of offsets.
8919 return false;
8920}
Evan Cheng39382422009-10-28 01:44:26 +00008921
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008922bool ARM::isBitFieldInvertedMask(unsigned v) {
8923 if (v == 0xffffffff)
8924 return 0;
8925 // there can be 1's on either or both "outsides", all the "inside"
8926 // bits must be 0's
8927 unsigned int lsb = 0, msb = 31;
8928 while (v & (1 << msb)) --msb;
8929 while (v & (1 << lsb)) ++lsb;
8930 for (unsigned int i = lsb; i <= msb; ++i) {
8931 if (v & (1 << i))
8932 return 0;
8933 }
8934 return 1;
8935}
8936
Evan Cheng39382422009-10-28 01:44:26 +00008937/// isFPImmLegal - Returns true if the target can instruction select the
8938/// specified FP immediate natively. If false, the legalizer will
8939/// materialize the FP immediate as a load from a constant pool.
8940bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
8941 if (!Subtarget->hasVFP3())
8942 return false;
8943 if (VT == MVT::f32)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00008944 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00008945 if (VT == MVT::f64)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00008946 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00008947 return false;
8948}
Bob Wilson65ffec42010-09-21 17:56:22 +00008949
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008950/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00008951/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
8952/// specified in the intrinsic calls.
8953bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
8954 const CallInst &I,
8955 unsigned Intrinsic) const {
8956 switch (Intrinsic) {
8957 case Intrinsic::arm_neon_vld1:
8958 case Intrinsic::arm_neon_vld2:
8959 case Intrinsic::arm_neon_vld3:
8960 case Intrinsic::arm_neon_vld4:
8961 case Intrinsic::arm_neon_vld2lane:
8962 case Intrinsic::arm_neon_vld3lane:
8963 case Intrinsic::arm_neon_vld4lane: {
8964 Info.opc = ISD::INTRINSIC_W_CHAIN;
8965 // Conservatively set memVT to the entire set of vectors loaded.
8966 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
8967 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
8968 Info.ptrVal = I.getArgOperand(0);
8969 Info.offset = 0;
8970 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
8971 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
8972 Info.vol = false; // volatile loads with NEON intrinsics not supported
8973 Info.readMem = true;
8974 Info.writeMem = false;
8975 return true;
8976 }
8977 case Intrinsic::arm_neon_vst1:
8978 case Intrinsic::arm_neon_vst2:
8979 case Intrinsic::arm_neon_vst3:
8980 case Intrinsic::arm_neon_vst4:
8981 case Intrinsic::arm_neon_vst2lane:
8982 case Intrinsic::arm_neon_vst3lane:
8983 case Intrinsic::arm_neon_vst4lane: {
8984 Info.opc = ISD::INTRINSIC_VOID;
8985 // Conservatively set memVT to the entire set of vectors stored.
8986 unsigned NumElts = 0;
8987 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008988 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +00008989 if (!ArgTy->isVectorTy())
8990 break;
8991 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
8992 }
8993 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
8994 Info.ptrVal = I.getArgOperand(0);
8995 Info.offset = 0;
8996 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
8997 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
8998 Info.vol = false; // volatile stores with NEON intrinsics not supported
8999 Info.readMem = false;
9000 Info.writeMem = true;
9001 return true;
9002 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009003 case Intrinsic::arm_strexd: {
9004 Info.opc = ISD::INTRINSIC_W_CHAIN;
9005 Info.memVT = MVT::i64;
9006 Info.ptrVal = I.getArgOperand(2);
9007 Info.offset = 0;
9008 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009009 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009010 Info.readMem = false;
9011 Info.writeMem = true;
9012 return true;
9013 }
9014 case Intrinsic::arm_ldrexd: {
9015 Info.opc = ISD::INTRINSIC_W_CHAIN;
9016 Info.memVT = MVT::i64;
9017 Info.ptrVal = I.getArgOperand(0);
9018 Info.offset = 0;
9019 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009020 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009021 Info.readMem = true;
9022 Info.writeMem = false;
9023 return true;
9024 }
Bob Wilson65ffec42010-09-21 17:56:22 +00009025 default:
9026 break;
9027 }
9028
9029 return false;
9030}