blob: 2d282809c9dbfc14142b2cf890d972437ab291d2 [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.
Craig Topperc5eaae42012-03-11 07:57:25 +000090static const uint16_t GPRArgRegs[] = {
Stuart Hastingsc7315872011-04-20 16:47:52 +000091 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
Evan Cheng07043272012-02-21 20:46:00 +0000264 if (Subtarget->isAAPCS_ABI() && !Subtarget->isTargetDarwin()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000265 // Double-precision floating-point arithmetic helper functions
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000266 // RTABI chapter 4.1.2, Table 2
267 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd");
268 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv");
269 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul");
270 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub");
271 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS);
272 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS);
273 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS);
274 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS);
275
276 // Double-precision floating-point comparison helper functions
277 // RTABI chapter 4.1.2, Table 3
278 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq");
279 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
280 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq");
281 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ);
282 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt");
283 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
284 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple");
285 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
286 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge");
287 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
288 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt");
289 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
290 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun");
291 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
292 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun");
293 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
294 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS);
295 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS);
296 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS);
297 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS);
298 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS);
299 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS);
300 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS);
301 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS);
302
303 // Single-precision floating-point arithmetic helper functions
304 // RTABI chapter 4.1.2, Table 4
305 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd");
306 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv");
307 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul");
308 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub");
309 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS);
310 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS);
311 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS);
312 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS);
313
314 // Single-precision floating-point comparison helper functions
315 // RTABI chapter 4.1.2, Table 5
316 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq");
317 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
318 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq");
319 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ);
320 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt");
321 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
322 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple");
323 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
324 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge");
325 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
326 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt");
327 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
328 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun");
329 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
330 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun");
331 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
332 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS);
333 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS);
334 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS);
335 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS);
336 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS);
337 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS);
338 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS);
339 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS);
340
341 // Floating-point to integer conversions.
342 // RTABI chapter 4.1.2, Table 6
343 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz");
344 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz");
345 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz");
346 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz");
347 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz");
348 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz");
349 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz");
350 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz");
351 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS);
352 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS);
353 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS);
354 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS);
355 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS);
356 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS);
357 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS);
358 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS);
359
360 // Conversions between floating types.
361 // RTABI chapter 4.1.2, Table 7
362 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f");
363 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d");
364 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000365 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000366
367 // Integer to floating-point conversions.
368 // RTABI chapter 4.1.2, Table 8
369 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d");
370 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d");
371 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d");
372 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d");
373 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f");
374 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f");
375 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f");
376 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f");
377 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
378 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
379 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
380 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
381 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
382 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
383 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
384 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
385
386 // Long long helper functions
387 // RTABI chapter 4.2, Table 9
388 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000389 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
390 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
391 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
392 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
393 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
394 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
395 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
396 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
397 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
398
399 // Integer division functions
400 // RTABI chapter 4.3.1
401 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
402 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
403 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000404 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000405 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
406 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
407 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000408 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000409 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
410 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
411 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000412 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000413 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS);
414 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000415 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000416 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
Renato Golin1ec11fb2011-05-22 21:41:23 +0000417
418 // Memory operations
419 // RTABI chapter 4.3.4
420 setLibcallName(RTLIB::MEMCPY, "__aeabi_memcpy");
421 setLibcallName(RTLIB::MEMMOVE, "__aeabi_memmove");
422 setLibcallName(RTLIB::MEMSET, "__aeabi_memset");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000423 setLibcallCallingConv(RTLIB::MEMCPY, CallingConv::ARM_AAPCS);
424 setLibcallCallingConv(RTLIB::MEMMOVE, CallingConv::ARM_AAPCS);
425 setLibcallCallingConv(RTLIB::MEMSET, CallingConv::ARM_AAPCS);
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000426 }
427
Bob Wilson2fef4572011-10-07 16:59:21 +0000428 // Use divmod compiler-rt calls for iOS 5.0 and later.
429 if (Subtarget->getTargetTriple().getOS() == Triple::IOS &&
430 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
431 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
432 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
433 }
434
David Goodwinf1daf7d2009-07-08 23:10:31 +0000435 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000436 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000437 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000438 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000439 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
440 !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000441 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000442 if (!Subtarget->isFPOnlySP())
443 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000444
Owen Anderson825b72b2009-08-11 20:47:22 +0000445 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000446 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000447
Eli Friedman9f1f26a2011-11-08 01:43:53 +0000448 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
449 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
450 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
451 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
452 setTruncStoreAction((MVT::SimpleValueType)VT,
453 (MVT::SimpleValueType)InnerVT, Expand);
454 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
455 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
456 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
457 }
458
Lang Hames45b5f882012-03-15 18:49:02 +0000459 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
460
Bob Wilson5bafff32009-06-22 23:27:02 +0000461 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000462 addDRTypeForNEON(MVT::v2f32);
463 addDRTypeForNEON(MVT::v8i8);
464 addDRTypeForNEON(MVT::v4i16);
465 addDRTypeForNEON(MVT::v2i32);
466 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000467
Owen Anderson825b72b2009-08-11 20:47:22 +0000468 addQRTypeForNEON(MVT::v4f32);
469 addQRTypeForNEON(MVT::v2f64);
470 addQRTypeForNEON(MVT::v16i8);
471 addQRTypeForNEON(MVT::v8i16);
472 addQRTypeForNEON(MVT::v4i32);
473 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000474
Bob Wilson74dc72e2009-09-15 23:55:57 +0000475 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
476 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000477 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
478 // supported for v4f32.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000479 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
480 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
481 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000482 // FIXME: Code duplication: FDIV and FREM are expanded always, see
483 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000484 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
485 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000486 // FIXME: Create unittest.
487 // In another words, find a way when "copysign" appears in DAG with vector
488 // operands.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000489 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000490 // FIXME: Code duplication: SETCC has custom operation action, see
491 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sands28b77e92011-09-06 19:07:46 +0000492 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000493 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000494 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
495 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
496 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
497 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
498 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
499 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
500 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
501 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
502 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
503 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
504 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
505 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000506 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000507 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
508 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
509 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
510 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
511 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000512
513 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
514 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
515 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
516 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
517 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
518 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
519 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
520 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
521 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
522 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Bob Wilson74dc72e2009-09-15 23:55:57 +0000523
Bob Wilson642b3292009-09-16 00:32:15 +0000524 // Neon does not support some operations on v1i64 and v2i64 types.
525 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000526 // Custom handling for some quad-vector types to detect VMULL.
527 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
528 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
529 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begeman7973f352011-02-11 20:53:29 +0000530 // Custom handling for some vector types to avoid expensive expansions
531 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
532 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
533 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
534 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000535 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
536 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000537 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy873fd5f2012-02-20 09:24:05 +0000538 // a destination type that is wider than the source, and nor does
539 // it have a FP_TO_[SU]INT instruction with a narrower destination than
540 // source.
Cameron Zwarich3007d332011-03-29 21:41:55 +0000541 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
542 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy873fd5f2012-02-20 09:24:05 +0000543 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
544 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000545
Bob Wilson1c3ef902011-02-07 17:43:21 +0000546 setTargetDAGCombine(ISD::INTRINSIC_VOID);
547 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000548 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
549 setTargetDAGCombine(ISD::SHL);
550 setTargetDAGCombine(ISD::SRL);
551 setTargetDAGCombine(ISD::SRA);
552 setTargetDAGCombine(ISD::SIGN_EXTEND);
553 setTargetDAGCombine(ISD::ZERO_EXTEND);
554 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000555 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000556 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000557 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000558 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
559 setTargetDAGCombine(ISD::STORE);
Chad Rosieref01edf2011-06-24 19:23:04 +0000560 setTargetDAGCombine(ISD::FP_TO_SINT);
561 setTargetDAGCombine(ISD::FP_TO_UINT);
562 setTargetDAGCombine(ISD::FDIV);
Nadav Rotem004a24b2011-10-15 20:03:12 +0000563
James Molloy873fd5f2012-02-20 09:24:05 +0000564 // It is legal to extload from v4i8 to v4i16 or v4i32.
565 MVT Tys[6] = {MVT::v8i8, MVT::v4i8, MVT::v2i8,
566 MVT::v4i16, MVT::v2i16,
567 MVT::v2i32};
568 for (unsigned i = 0; i < 6; ++i) {
569 setLoadExtAction(ISD::EXTLOAD, Tys[i], Legal);
570 setLoadExtAction(ISD::ZEXTLOAD, Tys[i], Legal);
571 setLoadExtAction(ISD::SEXTLOAD, Tys[i], Legal);
572 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000573 }
574
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000575 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000576
577 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000578 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000579
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000580 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000581 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000582
Evan Chenga8e29892007-01-19 07:51:42 +0000583 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000584 if (!Subtarget->isThumb1Only()) {
585 for (unsigned im = (unsigned)ISD::PRE_INC;
586 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000587 setIndexedLoadAction(im, MVT::i1, Legal);
588 setIndexedLoadAction(im, MVT::i8, Legal);
589 setIndexedLoadAction(im, MVT::i16, Legal);
590 setIndexedLoadAction(im, MVT::i32, Legal);
591 setIndexedStoreAction(im, MVT::i1, Legal);
592 setIndexedStoreAction(im, MVT::i8, Legal);
593 setIndexedStoreAction(im, MVT::i16, Legal);
594 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000595 }
Evan Chenga8e29892007-01-19 07:51:42 +0000596 }
597
598 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000599 setOperationAction(ISD::MUL, MVT::i64, Expand);
600 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000601 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000602 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
603 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000604 }
Jim Grosbacha7603982011-07-01 21:12:19 +0000605 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
606 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopher2cc40132011-04-19 18:49:19 +0000607 setOperationAction(ISD::MULHS, MVT::i32, Expand);
608
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000609 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000610 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000611 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000612 setOperationAction(ISD::SRL, MVT::i64, Custom);
613 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000614
Evan Cheng342e3162011-08-30 01:34:54 +0000615 if (!Subtarget->isThumb1Only()) {
616 // FIXME: We should do this for Thumb1 as well.
617 setOperationAction(ISD::ADDC, MVT::i32, Custom);
618 setOperationAction(ISD::ADDE, MVT::i32, Custom);
619 setOperationAction(ISD::SUBC, MVT::i32, Custom);
620 setOperationAction(ISD::SUBE, MVT::i32, Custom);
621 }
622
Evan Chenga8e29892007-01-19 07:51:42 +0000623 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000624 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000625 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000626 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000627 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000628 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000629
Chandler Carruth63974b22011-12-13 01:56:10 +0000630 // These just redirect to CTTZ and CTLZ on ARM.
631 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
632 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
633
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000634 // Only ARMv6 has BSWAP.
635 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000636 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000637
Evan Chenga8e29892007-01-19 07:51:42 +0000638 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000639 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000640 // v7M has a hardware divider
641 setOperationAction(ISD::SDIV, MVT::i32, Expand);
642 setOperationAction(ISD::UDIV, MVT::i32, Expand);
643 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000644 setOperationAction(ISD::SREM, MVT::i32, Expand);
645 setOperationAction(ISD::UREM, MVT::i32, Expand);
646 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
647 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000648
Owen Anderson825b72b2009-08-11 20:47:22 +0000649 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
650 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
651 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
652 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000653 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000654
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000655 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000656
Evan Chenga8e29892007-01-19 07:51:42 +0000657 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000658 setOperationAction(ISD::VASTART, MVT::Other, Custom);
659 setOperationAction(ISD::VAARG, MVT::Other, Expand);
660 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
661 setOperationAction(ISD::VAEND, MVT::Other, Expand);
662 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
663 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendlingbdf9db62012-02-13 23:47:16 +0000664
665 if (!Subtarget->isTargetDarwin()) {
666 // Non-Darwin platforms may return values in these registers via the
667 // personality function.
668 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
669 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
670 setExceptionPointerRegister(ARM::R0);
671 setExceptionSelectorRegister(ARM::R1);
672 }
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000673
Evan Cheng3a1588a2010-04-15 22:20:34 +0000674 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000675 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
676 // the default expansion.
Eli Friedman4db5aca2011-08-29 18:23:02 +0000677 // FIXME: This should be checking for v6k, not just v6.
Evan Cheng11db0682010-08-11 06:22:01 +0000678 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000679 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000680 // membarrier needs custom lowering; the rest are legal and handled
681 // normally.
682 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000683 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Eli Friedman2bdffe42011-08-31 00:31:29 +0000684 // Custom lowering for 64-bit ops
685 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
686 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
687 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
688 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
689 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
690 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Eli Friedman4d3f3292011-08-31 17:52:22 +0000691 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Eli Friedman26689ac2011-08-03 21:06:02 +0000692 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
693 setInsertFencesForAtomic(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000694 } else {
695 // Set them all for expansion, which will force libcalls.
696 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000697 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000698 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000699 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000700 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000701 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000702 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000703 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000704 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000705 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000706 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000707 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000708 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000709 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedman7cc15662011-09-15 22:18:49 +0000710 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
711 // Unordered/Monotonic case.
712 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
713 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000714 // Since the libcalls include locking, fold in the fences
715 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000716 }
Evan Chenga8e29892007-01-19 07:51:42 +0000717
Evan Cheng416941d2010-11-04 05:19:35 +0000718 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000719
Eli Friedmana2c6f452010-06-26 04:36:50 +0000720 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
721 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000722 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
723 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000724 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000725 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000726
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000727 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
728 !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000729 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
730 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000731 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000732 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
733 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000734
735 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000736 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000737 if (Subtarget->isTargetDarwin()) {
738 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
739 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000740 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000741 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000742
Owen Anderson825b72b2009-08-11 20:47:22 +0000743 setOperationAction(ISD::SETCC, MVT::i32, Expand);
744 setOperationAction(ISD::SETCC, MVT::f32, Expand);
745 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000746 setOperationAction(ISD::SELECT, MVT::i32, Custom);
747 setOperationAction(ISD::SELECT, MVT::f32, Custom);
748 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000749 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
750 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
751 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000752
Owen Anderson825b72b2009-08-11 20:47:22 +0000753 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
754 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
755 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
756 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
757 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000758
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000759 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000760 setOperationAction(ISD::FSIN, MVT::f64, Expand);
761 setOperationAction(ISD::FSIN, MVT::f32, Expand);
762 setOperationAction(ISD::FCOS, MVT::f32, Expand);
763 setOperationAction(ISD::FCOS, MVT::f64, Expand);
764 setOperationAction(ISD::FREM, MVT::f64, Expand);
765 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000766 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
767 !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000768 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
769 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000770 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000771 setOperationAction(ISD::FPOW, MVT::f64, Expand);
772 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000773
Cameron Zwarich33390842011-07-08 21:39:21 +0000774 setOperationAction(ISD::FMA, MVT::f64, Expand);
775 setOperationAction(ISD::FMA, MVT::f32, Expand);
776
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000777 // Various VFP goodness
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000778 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000779 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
780 if (Subtarget->hasVFP2()) {
781 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
782 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
783 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
784 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
785 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000786 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000787 if (!Subtarget->hasFP16()) {
788 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
789 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000790 }
Evan Cheng110cf482008-04-01 01:50:16 +0000791 }
Evan Chenga8e29892007-01-19 07:51:42 +0000792
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000793 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000794 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000795 setTargetDAGCombine(ISD::ADD);
796 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000797 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000798
Evan Chengc892aeb2012-02-23 01:19:06 +0000799 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON()) {
Owen Anderson080c0922010-11-05 19:27:46 +0000800 setTargetDAGCombine(ISD::AND);
Evan Chengc892aeb2012-02-23 01:19:06 +0000801 setTargetDAGCombine(ISD::OR);
802 setTargetDAGCombine(ISD::XOR);
803 }
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000804
Evan Cheng5fb468a2012-02-23 02:58:19 +0000805 if (Subtarget->hasV6Ops())
806 setTargetDAGCombine(ISD::SRL);
807
Evan Chenga8e29892007-01-19 07:51:42 +0000808 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000809
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000810 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
811 !Subtarget->hasVFP2())
Evan Chengf7d87ee2010-05-21 00:43:17 +0000812 setSchedulingPreference(Sched::RegPressure);
813 else
814 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000815
Evan Cheng05219282011-01-06 06:52:41 +0000816 //// temporary - rewrite interface to use type
817 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Lang Hames75757f92011-10-26 20:56:52 +0000818 maxStoresPerMemset = 16;
819 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengf6799392010-06-26 01:52:05 +0000820
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000821 // On ARM arguments smaller than 4 bytes are extended, so all arguments
822 // are at least 4 bytes aligned.
823 setMinStackArgumentAlignment(4);
824
Evan Chengfff606d2010-09-24 19:07:23 +0000825 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000826
827 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000828}
829
Andrew Trick32cec0a2011-01-19 02:35:27 +0000830// FIXME: It might make sense to define the representative register class as the
831// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
832// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
833// SPR's representative would be DPR_VFP2. This should work well if register
834// pressure tracking were modified such that a register use would increment the
835// pressure of the register class's representative and all of it's super
836// classes' representatives transitively. We have not implemented this because
837// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000838// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000839// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000840std::pair<const TargetRegisterClass*, uint8_t>
841ARMTargetLowering::findRepresentativeClass(EVT VT) const{
842 const TargetRegisterClass *RRC = 0;
843 uint8_t Cost = 1;
844 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000845 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000846 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000847 // Use DPR as representative register class for all floating point
848 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
849 // the cost is 1 for both f32 and f64.
850 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000851 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000852 RRC = ARM::DPRRegisterClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000853 // When NEON is used for SP, only half of the register file is available
854 // because operations that define both SP and DP results will be constrained
855 // to the VFP2 class (D0-D15). We currently model this constraint prior to
856 // coalescing by double-counting the SP regs. See the FIXME above.
857 if (Subtarget->useNEONForSinglePrecisionFP())
858 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000859 break;
860 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
861 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000862 RRC = ARM::DPRRegisterClass;
863 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000864 break;
865 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000866 RRC = ARM::DPRRegisterClass;
867 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000868 break;
869 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000870 RRC = ARM::DPRRegisterClass;
871 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000872 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000873 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000874 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000875}
876
Evan Chenga8e29892007-01-19 07:51:42 +0000877const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
878 switch (Opcode) {
879 default: return 0;
880 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000881 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000882 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000883 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
884 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000885 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000886 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
887 case ARMISD::tCALL: return "ARMISD::tCALL";
888 case ARMISD::BRCOND: return "ARMISD::BRCOND";
889 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000890 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000891 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
892 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
893 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000894 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000895 case ARMISD::CMPFP: return "ARMISD::CMPFP";
896 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000897 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000898 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chengc892aeb2012-02-23 01:19:06 +0000899
Evan Chenga8e29892007-01-19 07:51:42 +0000900 case ARMISD::CMOV: return "ARMISD::CMOV";
Evan Chengc892aeb2012-02-23 01:19:06 +0000901 case ARMISD::CAND: return "ARMISD::CAND";
902 case ARMISD::COR: return "ARMISD::COR";
903 case ARMISD::CXOR: return "ARMISD::CXOR";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000904
Jim Grosbach3482c802010-01-18 19:58:49 +0000905 case ARMISD::RBIT: return "ARMISD::RBIT";
906
Bob Wilson76a312b2010-03-19 22:51:32 +0000907 case ARMISD::FTOSI: return "ARMISD::FTOSI";
908 case ARMISD::FTOUI: return "ARMISD::FTOUI";
909 case ARMISD::SITOF: return "ARMISD::SITOF";
910 case ARMISD::UITOF: return "ARMISD::UITOF";
911
Evan Chenga8e29892007-01-19 07:51:42 +0000912 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
913 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
914 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000915
Evan Cheng342e3162011-08-30 01:34:54 +0000916 case ARMISD::ADDC: return "ARMISD::ADDC";
917 case ARMISD::ADDE: return "ARMISD::ADDE";
918 case ARMISD::SUBC: return "ARMISD::SUBC";
919 case ARMISD::SUBE: return "ARMISD::SUBE";
920
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000921 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
922 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000923
Evan Chengc5942082009-10-28 06:55:03 +0000924 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
925 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
926
Dale Johannesen51e28e62010-06-03 21:09:53 +0000927 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000928
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000929 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000930
Evan Cheng86198642009-08-07 00:34:42 +0000931 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
932
Jim Grosbach3728e962009-12-10 00:11:09 +0000933 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000934 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000935
Evan Chengdfed19f2010-11-03 06:34:55 +0000936 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
937
Bob Wilson5bafff32009-06-22 23:27:02 +0000938 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000939 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000940 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000941 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
942 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000943 case ARMISD::VCGEU: return "ARMISD::VCGEU";
944 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000945 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
946 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000947 case ARMISD::VCGTU: return "ARMISD::VCGTU";
948 case ARMISD::VTST: return "ARMISD::VTST";
949
950 case ARMISD::VSHL: return "ARMISD::VSHL";
951 case ARMISD::VSHRs: return "ARMISD::VSHRs";
952 case ARMISD::VSHRu: return "ARMISD::VSHRu";
953 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
954 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
955 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
956 case ARMISD::VSHRN: return "ARMISD::VSHRN";
957 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
958 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
959 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
960 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
961 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
962 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
963 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
964 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
965 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
966 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
967 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
968 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
969 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
970 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000971 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000972 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Chengeaa192a2011-11-15 02:12:34 +0000973 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000974 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000975 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000976 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000977 case ARMISD::VREV64: return "ARMISD::VREV64";
978 case ARMISD::VREV32: return "ARMISD::VREV32";
979 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000980 case ARMISD::VZIP: return "ARMISD::VZIP";
981 case ARMISD::VUZP: return "ARMISD::VUZP";
982 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000983 case ARMISD::VTBL1: return "ARMISD::VTBL1";
984 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000985 case ARMISD::VMULLs: return "ARMISD::VMULLs";
986 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000987 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000988 case ARMISD::FMAX: return "ARMISD::FMAX";
989 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000990 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000991 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
992 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000993 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000994 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
995 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
996 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +0000997 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
998 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
999 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1000 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1001 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1002 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1003 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1004 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1005 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1006 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1007 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1008 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1009 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1010 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1011 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1012 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1013 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +00001014 }
1015}
1016
Duncan Sands28b77e92011-09-06 19:07:46 +00001017EVT ARMTargetLowering::getSetCCResultType(EVT VT) const {
1018 if (!VT.isVector()) return getPointerTy();
1019 return VT.changeVectorElementTypeToInteger();
1020}
1021
Evan Cheng06b666c2010-05-15 02:18:07 +00001022/// getRegClassFor - Return the register class that should be used for the
1023/// specified value type.
Craig Topper44d23822012-02-22 05:59:10 +00001024const TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
Evan Cheng06b666c2010-05-15 02:18:07 +00001025 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1026 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1027 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +00001028 if (Subtarget->hasNEON()) {
1029 if (VT == MVT::v4i64)
1030 return ARM::QQPRRegisterClass;
1031 else if (VT == MVT::v8i64)
1032 return ARM::QQQQPRRegisterClass;
1033 }
Evan Cheng06b666c2010-05-15 02:18:07 +00001034 return TargetLowering::getRegClassFor(VT);
1035}
1036
Eric Christopherab695882010-07-21 22:26:11 +00001037// Create a fast isel object.
1038FastISel *
1039ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
1040 return ARM::createFastISel(funcInfo);
1041}
1042
Anton Korobeynikovcec36f42010-07-24 21:52:08 +00001043/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1044/// be used for loads / stores from the global.
1045unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1046 return (Subtarget->isThumb1Only() ? 127 : 4095);
1047}
1048
Evan Cheng1cc39842010-05-20 23:26:43 +00001049Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +00001050 unsigned NumVals = N->getNumValues();
1051 if (!NumVals)
1052 return Sched::RegPressure;
1053
1054 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +00001055 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001056 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +00001057 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +00001058 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman692c1d82011-10-24 17:55:11 +00001059 return Sched::ILP;
Evan Cheng1cc39842010-05-20 23:26:43 +00001060 }
Evan Chengc10f5432010-05-28 23:25:23 +00001061
1062 if (!N->isMachineOpcode())
1063 return Sched::RegPressure;
1064
1065 // Load are scheduled for latency even if there instruction itinerary
1066 // is not available.
1067 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +00001068 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +00001069
Evan Chenge837dea2011-06-28 19:10:37 +00001070 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +00001071 return Sched::RegPressure;
1072 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +00001073 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman692c1d82011-10-24 17:55:11 +00001074 return Sched::ILP;
Evan Chengc10f5432010-05-28 23:25:23 +00001075
Evan Cheng1cc39842010-05-20 23:26:43 +00001076 return Sched::RegPressure;
1077}
1078
Evan Chenga8e29892007-01-19 07:51:42 +00001079//===----------------------------------------------------------------------===//
1080// Lowering Code
1081//===----------------------------------------------------------------------===//
1082
Evan Chenga8e29892007-01-19 07:51:42 +00001083/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1084static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1085 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001086 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +00001087 case ISD::SETNE: return ARMCC::NE;
1088 case ISD::SETEQ: return ARMCC::EQ;
1089 case ISD::SETGT: return ARMCC::GT;
1090 case ISD::SETGE: return ARMCC::GE;
1091 case ISD::SETLT: return ARMCC::LT;
1092 case ISD::SETLE: return ARMCC::LE;
1093 case ISD::SETUGT: return ARMCC::HI;
1094 case ISD::SETUGE: return ARMCC::HS;
1095 case ISD::SETULT: return ARMCC::LO;
1096 case ISD::SETULE: return ARMCC::LS;
1097 }
1098}
1099
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001100/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1101static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001102 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001103 CondCode2 = ARMCC::AL;
1104 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001105 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001106 case ISD::SETEQ:
1107 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1108 case ISD::SETGT:
1109 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1110 case ISD::SETGE:
1111 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1112 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001113 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001114 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1115 case ISD::SETO: CondCode = ARMCC::VC; break;
1116 case ISD::SETUO: CondCode = ARMCC::VS; break;
1117 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1118 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1119 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1120 case ISD::SETLT:
1121 case ISD::SETULT: CondCode = ARMCC::LT; break;
1122 case ISD::SETLE:
1123 case ISD::SETULE: CondCode = ARMCC::LE; break;
1124 case ISD::SETNE:
1125 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1126 }
Evan Chenga8e29892007-01-19 07:51:42 +00001127}
1128
Bob Wilson1f595bb2009-04-17 19:07:39 +00001129//===----------------------------------------------------------------------===//
1130// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001131//===----------------------------------------------------------------------===//
1132
1133#include "ARMGenCallingConv.inc"
1134
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001135/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1136/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001137CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001138 bool Return,
1139 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001140 switch (CC) {
1141 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001142 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001143 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001144 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001145 if (!Subtarget->isAAPCS_ABI())
1146 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1147 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1148 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1149 }
1150 // Fallthrough
1151 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001152 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001153 if (!Subtarget->isAAPCS_ABI())
1154 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1155 else if (Subtarget->hasVFP2() &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001156 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1157 !isVarArg)
Evan Cheng76f920d2010-10-22 18:23:05 +00001158 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1159 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1160 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001161 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikovf349cb82012-01-29 09:06:09 +00001162 if (!isVarArg)
1163 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1164 // Fallthrough
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001165 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001166 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001167 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001168 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001169 }
1170}
1171
Dan Gohman98ca4f22009-08-05 01:29:28 +00001172/// LowerCallResult - Lower the result values of a call into the
1173/// appropriate copies out of appropriate physical registers.
1174SDValue
1175ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001176 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001177 const SmallVectorImpl<ISD::InputArg> &Ins,
1178 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001179 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001180
Bob Wilson1f595bb2009-04-17 19:07:39 +00001181 // Assign locations to each value returned by this call.
1182 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001183 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1184 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001185 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001186 CCAssignFnForNode(CallConv, /* Return*/ true,
1187 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001188
1189 // Copy all of the result registers out of their specified physreg.
1190 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1191 CCValAssign VA = RVLocs[i];
1192
Bob Wilson80915242009-04-25 00:33:20 +00001193 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001194 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001195 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001196 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001197 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001198 Chain = Lo.getValue(1);
1199 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001200 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001201 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001202 InFlag);
1203 Chain = Hi.getValue(1);
1204 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001205 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001206
Owen Anderson825b72b2009-08-11 20:47:22 +00001207 if (VA.getLocVT() == MVT::v2f64) {
1208 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1209 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1210 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001211
1212 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001213 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001214 Chain = Lo.getValue(1);
1215 InFlag = Lo.getValue(2);
1216 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001217 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001218 Chain = Hi.getValue(1);
1219 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001220 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001221 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1222 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001223 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001224 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001225 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1226 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001227 Chain = Val.getValue(1);
1228 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001229 }
Bob Wilson80915242009-04-25 00:33:20 +00001230
1231 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001232 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001233 case CCValAssign::Full: break;
1234 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001235 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001236 break;
1237 }
1238
Dan Gohman98ca4f22009-08-05 01:29:28 +00001239 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001240 }
1241
Dan Gohman98ca4f22009-08-05 01:29:28 +00001242 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001243}
1244
Bob Wilsondee46d72009-04-17 20:35:10 +00001245/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001246SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001247ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1248 SDValue StackPtr, SDValue Arg,
1249 DebugLoc dl, SelectionDAG &DAG,
1250 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001251 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001252 unsigned LocMemOffset = VA.getLocMemOffset();
1253 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1254 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001255 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001256 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001257 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001258}
1259
Dan Gohman98ca4f22009-08-05 01:29:28 +00001260void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001261 SDValue Chain, SDValue &Arg,
1262 RegsToPassVector &RegsToPass,
1263 CCValAssign &VA, CCValAssign &NextVA,
1264 SDValue &StackPtr,
1265 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001266 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001267
Jim Grosbache5165492009-11-09 00:11:35 +00001268 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001269 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001270 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1271
1272 if (NextVA.isRegLoc())
1273 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1274 else {
1275 assert(NextVA.isMemLoc());
1276 if (StackPtr.getNode() == 0)
1277 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1278
Dan Gohman98ca4f22009-08-05 01:29:28 +00001279 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1280 dl, DAG, NextVA,
1281 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001282 }
1283}
1284
Dan Gohman98ca4f22009-08-05 01:29:28 +00001285/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001286/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1287/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001288SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001289ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001290 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001291 bool doesNotRet, bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001292 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001293 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001294 const SmallVectorImpl<ISD::InputArg> &Ins,
1295 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001296 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001297 MachineFunction &MF = DAG.getMachineFunction();
1298 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1299 bool IsSibCall = false;
Bob Wilson6d2f9ce2011-10-07 17:17:49 +00001300 // Disable tail calls if they're not supported.
1301 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Bob Wilson703af3a2010-08-13 22:43:33 +00001302 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001303 if (isTailCall) {
1304 // Check if it's really possible to do a tail call.
1305 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1306 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001307 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001308 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1309 // detected sibcalls.
1310 if (isTailCall) {
1311 ++NumTailCalls;
1312 IsSibCall = true;
1313 }
1314 }
Evan Chenga8e29892007-01-19 07:51:42 +00001315
Bob Wilson1f595bb2009-04-17 19:07:39 +00001316 // Analyze operands of the call, assigning locations to each operand.
1317 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001318 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1319 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001320 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001321 CCAssignFnForNode(CallConv, /* Return*/ false,
1322 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001323
Bob Wilson1f595bb2009-04-17 19:07:39 +00001324 // Get a count of how many bytes are to be pushed on the stack.
1325 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001326
Dale Johannesen51e28e62010-06-03 21:09:53 +00001327 // For tail calls, memory operands are available in our caller's stack.
1328 if (IsSibCall)
1329 NumBytes = 0;
1330
Evan Chenga8e29892007-01-19 07:51:42 +00001331 // Adjust the stack pointer for the new arguments...
1332 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001333 if (!IsSibCall)
1334 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001335
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001336 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001337
Bob Wilson5bafff32009-06-22 23:27:02 +00001338 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001339 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001340
Bob Wilson1f595bb2009-04-17 19:07:39 +00001341 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001342 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001343 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1344 i != e;
1345 ++i, ++realArgIdx) {
1346 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001347 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001348 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001349 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001350
Bob Wilson1f595bb2009-04-17 19:07:39 +00001351 // Promote the value if needed.
1352 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001353 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001354 case CCValAssign::Full: break;
1355 case CCValAssign::SExt:
1356 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1357 break;
1358 case CCValAssign::ZExt:
1359 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1360 break;
1361 case CCValAssign::AExt:
1362 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1363 break;
1364 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001365 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001366 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001367 }
1368
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001369 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001370 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001371 if (VA.getLocVT() == MVT::v2f64) {
1372 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1373 DAG.getConstant(0, MVT::i32));
1374 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1375 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001376
Dan Gohman98ca4f22009-08-05 01:29:28 +00001377 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001378 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1379
1380 VA = ArgLocs[++i]; // skip ahead to next loc
1381 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001382 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001383 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1384 } else {
1385 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001386
Dan Gohman98ca4f22009-08-05 01:29:28 +00001387 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1388 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001389 }
1390 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001391 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001392 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001393 }
1394 } else if (VA.isRegLoc()) {
1395 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001396 } else if (isByVal) {
1397 assert(VA.isMemLoc());
1398 unsigned offset = 0;
1399
1400 // True if this byval aggregate will be split between registers
1401 // and memory.
1402 if (CCInfo.isFirstByValRegValid()) {
1403 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1404 unsigned int i, j;
1405 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1406 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1407 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1408 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1409 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001410 false, false, false, 0);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001411 MemOpChains.push_back(Load.getValue(1));
1412 RegsToPass.push_back(std::make_pair(j, Load));
1413 }
1414 offset = ARM::R4 - CCInfo.getFirstByValReg();
1415 CCInfo.clearFirstByValReg();
1416 }
1417
1418 unsigned LocMemOffset = VA.getLocMemOffset();
1419 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1420 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1421 StkPtrOff);
1422 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1423 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1424 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1425 MVT::i32);
1426 MemOpChains.push_back(DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode,
1427 Flags.getByValAlign(),
1428 /*isVolatile=*/false,
Dan Gohman65fd6562011-11-03 21:49:52 +00001429 /*AlwaysInline=*/false,
Stuart Hastingsc7315872011-04-20 16:47:52 +00001430 MachinePointerInfo(0),
1431 MachinePointerInfo(0)));
1432
1433 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001434 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001435
Dan Gohman98ca4f22009-08-05 01:29:28 +00001436 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1437 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001438 }
Evan Chenga8e29892007-01-19 07:51:42 +00001439 }
1440
1441 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001442 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001443 &MemOpChains[0], MemOpChains.size());
1444
1445 // Build a sequence of copy-to-reg nodes chained together with token chain
1446 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001447 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001448 // Tail call byval lowering might overwrite argument registers so in case of
1449 // tail call optimization the copies to registers are lowered later.
1450 if (!isTailCall)
1451 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1452 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1453 RegsToPass[i].second, InFlag);
1454 InFlag = Chain.getValue(1);
1455 }
Evan Chenga8e29892007-01-19 07:51:42 +00001456
Dale Johannesen51e28e62010-06-03 21:09:53 +00001457 // For tail calls lower the arguments to the 'real' stack slot.
1458 if (isTailCall) {
1459 // Force all the incoming stack arguments to be loaded from the stack
1460 // before any new outgoing arguments are stored to the stack, because the
1461 // outgoing stack slots may alias the incoming argument stack slots, and
1462 // the alias isn't otherwise explicit. This is slightly more conservative
1463 // than necessary, because it means that each store effectively depends
1464 // on every argument instead of just those arguments it would clobber.
1465
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001466 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001467 InFlag = SDValue();
1468 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1469 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1470 RegsToPass[i].second, InFlag);
1471 InFlag = Chain.getValue(1);
1472 }
1473 InFlag =SDValue();
1474 }
1475
Bill Wendling056292f2008-09-16 21:48:12 +00001476 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1477 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1478 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001479 bool isDirect = false;
1480 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001481 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001482 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001483
1484 if (EnableARMLongCalls) {
1485 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1486 && "long-calls with non-static relocation model!");
1487 // Handle a global address or an external symbol. If it's not one of
1488 // those, the target's already in a register, so we don't need to do
1489 // anything extra.
1490 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001491 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001492 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001493 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001494 ARMConstantPoolValue *CPV =
1495 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1496
Jim Grosbache7b52522010-04-14 22:28:31 +00001497 // Get the address of the callee into a register
1498 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1499 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1500 Callee = DAG.getLoad(getPointerTy(), dl,
1501 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001502 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001503 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001504 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1505 const char *Sym = S->getSymbol();
1506
1507 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001508 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001509 ARMConstantPoolValue *CPV =
1510 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1511 ARMPCLabelIndex, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001512 // Get the address of the callee into a register
1513 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1514 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1515 Callee = DAG.getLoad(getPointerTy(), dl,
1516 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001517 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001518 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001519 }
1520 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001521 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001522 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001523 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001524 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001525 getTargetMachine().getRelocationModel() != Reloc::Static;
1526 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001527 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001528 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001529 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001530 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001531 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001532 ARMConstantPoolValue *CPV =
1533 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001534 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001535 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001536 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001537 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001538 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001539 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001540 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001541 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001542 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001543 } else {
1544 // On ELF targets for PIC code, direct calls should go through the PLT
1545 unsigned OpFlags = 0;
1546 if (Subtarget->isTargetELF() &&
1547 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1548 OpFlags = ARMII::MO_PLT;
1549 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1550 }
Bill Wendling056292f2008-09-16 21:48:12 +00001551 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001552 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001553 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001554 getTargetMachine().getRelocationModel() != Reloc::Static;
1555 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001556 // tBX takes a register source operand.
1557 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001558 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001559 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001560 ARMConstantPoolValue *CPV =
1561 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1562 ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001563 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001564 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001565 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001566 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001567 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001568 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001569 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001570 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001571 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001572 } else {
1573 unsigned OpFlags = 0;
1574 // On ELF targets for PIC code, direct calls should go through the PLT
1575 if (Subtarget->isTargetELF() &&
1576 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1577 OpFlags = ARMII::MO_PLT;
1578 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1579 }
Evan Chenga8e29892007-01-19 07:51:42 +00001580 }
1581
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001582 // FIXME: handle tail calls differently.
1583 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001584 if (Subtarget->isThumb()) {
1585 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001586 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001587 else if (doesNotRet && isDirect && !isARMFunc &&
1588 Subtarget->hasRAS() && !Subtarget->isThumb1Only())
1589 // "mov lr, pc; b _foo" to avoid confusing the RSP
1590 CallOpc = ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001591 else
1592 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1593 } else {
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001594 if (!isDirect && !Subtarget->hasV5TOps()) {
1595 CallOpc = ARMISD::CALL_NOLINK;
1596 } else if (doesNotRet && isDirect && Subtarget->hasRAS())
1597 // "mov lr, pc; b _foo" to avoid confusing the RSP
1598 CallOpc = ARMISD::CALL_NOLINK;
1599 else
1600 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001601 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001602
Dan Gohman475871a2008-07-27 21:46:04 +00001603 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001604 Ops.push_back(Chain);
1605 Ops.push_back(Callee);
1606
1607 // Add argument registers to the end of the list so that they are known live
1608 // into the call.
1609 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1610 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1611 RegsToPass[i].second.getValueType()));
1612
Jakob Stoklund Olesenc54f6342012-02-24 01:19:29 +00001613 // Add a register mask operand representing the call-preserved registers.
1614 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
1615 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
1616 assert(Mask && "Missing call preserved mask for calling convention");
1617 Ops.push_back(DAG.getRegisterMask(Mask));
1618
Gabor Greifba36cb52008-08-28 21:40:38 +00001619 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001620 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001621
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001622 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001623 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001624 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001625
Duncan Sands4bdcb612008-07-02 17:40:58 +00001626 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001627 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001628 InFlag = Chain.getValue(1);
1629
Chris Lattnere563bbc2008-10-11 22:08:30 +00001630 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1631 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001632 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001633 InFlag = Chain.getValue(1);
1634
Bob Wilson1f595bb2009-04-17 19:07:39 +00001635 // Handle result values, copying them out of physregs into vregs that we
1636 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001637 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1638 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001639}
1640
Stuart Hastingsf222e592011-02-28 17:17:53 +00001641/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001642/// on the stack. Remember the next parameter register to allocate,
1643/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001644/// this.
1645void
Stuart Hastingsc7315872011-04-20 16:47:52 +00001646llvm::ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
1647 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1648 assert((State->getCallOrPrologue() == Prologue ||
1649 State->getCallOrPrologue() == Call) &&
1650 "unhandled ParmContext");
1651 if ((!State->isFirstByValRegValid()) &&
1652 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1653 State->setFirstByValReg(reg);
1654 // At a call site, a byval parameter that is split between
1655 // registers and memory needs its size truncated here. In a
1656 // function prologue, such byval parameters are reassembled in
1657 // memory, and are not truncated.
1658 if (State->getCallOrPrologue() == Call) {
1659 unsigned excess = 4 * (ARM::R4 - reg);
1660 assert(size >= excess && "expected larger existing stack allocation");
1661 size -= excess;
1662 }
1663 }
1664 // Confiscate any remaining parameter registers to preclude their
1665 // assignment to subsequent parameters.
1666 while (State->AllocateReg(GPRArgRegs, 4))
1667 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001668}
1669
Dale Johannesen51e28e62010-06-03 21:09:53 +00001670/// MatchingStackOffset - Return true if the given stack call argument is
1671/// already available in the same position (relatively) of the caller's
1672/// incoming argument stack.
1673static
1674bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1675 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1676 const ARMInstrInfo *TII) {
1677 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1678 int FI = INT_MAX;
1679 if (Arg.getOpcode() == ISD::CopyFromReg) {
1680 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001681 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001682 return false;
1683 MachineInstr *Def = MRI->getVRegDef(VR);
1684 if (!Def)
1685 return false;
1686 if (!Flags.isByVal()) {
1687 if (!TII->isLoadFromStackSlot(Def, FI))
1688 return false;
1689 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001690 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001691 }
1692 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1693 if (Flags.isByVal())
1694 // ByVal argument is passed in as a pointer but it's now being
1695 // dereferenced. e.g.
1696 // define @foo(%struct.X* %A) {
1697 // tail call @bar(%struct.X* byval %A)
1698 // }
1699 return false;
1700 SDValue Ptr = Ld->getBasePtr();
1701 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1702 if (!FINode)
1703 return false;
1704 FI = FINode->getIndex();
1705 } else
1706 return false;
1707
1708 assert(FI != INT_MAX);
1709 if (!MFI->isFixedObjectIndex(FI))
1710 return false;
1711 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1712}
1713
1714/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1715/// for tail call optimization. Targets which want to do tail call
1716/// optimization should implement this function.
1717bool
1718ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1719 CallingConv::ID CalleeCC,
1720 bool isVarArg,
1721 bool isCalleeStructRet,
1722 bool isCallerStructRet,
1723 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001724 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001725 const SmallVectorImpl<ISD::InputArg> &Ins,
1726 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001727 const Function *CallerF = DAG.getMachineFunction().getFunction();
1728 CallingConv::ID CallerCC = CallerF->getCallingConv();
1729 bool CCMatch = CallerCC == CalleeCC;
1730
1731 // Look for obvious safe cases to perform tail call optimization that do not
1732 // require ABI changes. This is what gcc calls sibcall.
1733
Jim Grosbach7616b642010-06-16 23:45:49 +00001734 // Do not sibcall optimize vararg calls unless the call site is not passing
1735 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001736 if (isVarArg && !Outs.empty())
1737 return false;
1738
1739 // Also avoid sibcall optimization if either caller or callee uses struct
1740 // return semantics.
1741 if (isCalleeStructRet || isCallerStructRet)
1742 return false;
1743
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001744 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001745 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1746 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1747 // support in the assembler and linker to be used. This would need to be
1748 // fixed to fully support tail calls in Thumb1.
1749 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001750 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1751 // LR. This means if we need to reload LR, it takes an extra instructions,
1752 // which outweighs the value of the tail call; but here we don't know yet
1753 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001754 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001755 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001756
1757 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1758 // but we need to make sure there are enough registers; the only valid
1759 // registers are the 4 used for parameters. We don't currently do this
1760 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001761 if (Subtarget->isThumb1Only())
1762 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001763
Dale Johannesen51e28e62010-06-03 21:09:53 +00001764 // If the calling conventions do not match, then we'd better make sure the
1765 // results are returned in the same way as what the caller expects.
1766 if (!CCMatch) {
1767 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001768 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1769 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001770 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1771
1772 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001773 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1774 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001775 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1776
1777 if (RVLocs1.size() != RVLocs2.size())
1778 return false;
1779 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1780 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1781 return false;
1782 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1783 return false;
1784 if (RVLocs1[i].isRegLoc()) {
1785 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1786 return false;
1787 } else {
1788 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1789 return false;
1790 }
1791 }
1792 }
1793
1794 // If the callee takes no arguments then go on to check the results of the
1795 // call.
1796 if (!Outs.empty()) {
1797 // Check if stack adjustment is needed. For now, do not do this if any
1798 // argument is passed on the stack.
1799 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001800 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1801 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001802 CCInfo.AnalyzeCallOperands(Outs,
1803 CCAssignFnForNode(CalleeCC, false, isVarArg));
1804 if (CCInfo.getNextStackOffset()) {
1805 MachineFunction &MF = DAG.getMachineFunction();
1806
1807 // Check if the arguments are already laid out in the right way as
1808 // the caller's fixed stack objects.
1809 MachineFrameInfo *MFI = MF.getFrameInfo();
1810 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1811 const ARMInstrInfo *TII =
1812 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001813 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1814 i != e;
1815 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001816 CCValAssign &VA = ArgLocs[i];
1817 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001818 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001819 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001820 if (VA.getLocInfo() == CCValAssign::Indirect)
1821 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001822 if (VA.needsCustom()) {
1823 // f64 and vector types are split into multiple registers or
1824 // register/stack-slot combinations. The types will not match
1825 // the registers; give up on memory f64 refs until we figure
1826 // out what to do about this.
1827 if (!VA.isRegLoc())
1828 return false;
1829 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001830 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001831 if (RegVT == MVT::v2f64) {
1832 if (!ArgLocs[++i].isRegLoc())
1833 return false;
1834 if (!ArgLocs[++i].isRegLoc())
1835 return false;
1836 }
1837 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001838 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1839 MFI, MRI, TII))
1840 return false;
1841 }
1842 }
1843 }
1844 }
1845
1846 return true;
1847}
1848
Dan Gohman98ca4f22009-08-05 01:29:28 +00001849SDValue
1850ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001851 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001852 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001853 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001854 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001855
Bob Wilsondee46d72009-04-17 20:35:10 +00001856 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001857 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001858
Bob Wilsondee46d72009-04-17 20:35:10 +00001859 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001860 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1861 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001862
Dan Gohman98ca4f22009-08-05 01:29:28 +00001863 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001864 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1865 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001866
1867 // If this is the first return lowered for this function, add
1868 // the regs to the liveout set for the function.
1869 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1870 for (unsigned i = 0; i != RVLocs.size(); ++i)
1871 if (RVLocs[i].isRegLoc())
1872 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001873 }
1874
Bob Wilson1f595bb2009-04-17 19:07:39 +00001875 SDValue Flag;
1876
1877 // Copy the result values into the output registers.
1878 for (unsigned i = 0, realRVLocIdx = 0;
1879 i != RVLocs.size();
1880 ++i, ++realRVLocIdx) {
1881 CCValAssign &VA = RVLocs[i];
1882 assert(VA.isRegLoc() && "Can only return in registers!");
1883
Dan Gohmanc9403652010-07-07 15:54:55 +00001884 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001885
1886 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001887 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001888 case CCValAssign::Full: break;
1889 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001890 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001891 break;
1892 }
1893
Bob Wilson1f595bb2009-04-17 19:07:39 +00001894 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001895 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001896 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001897 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1898 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001899 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001900 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001901
1902 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1903 Flag = Chain.getValue(1);
1904 VA = RVLocs[++i]; // skip ahead to next loc
1905 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1906 HalfGPRs.getValue(1), Flag);
1907 Flag = Chain.getValue(1);
1908 VA = RVLocs[++i]; // skip ahead to next loc
1909
1910 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001911 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1912 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001913 }
1914 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1915 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001916 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001917 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001918 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001919 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001920 VA = RVLocs[++i]; // skip ahead to next loc
1921 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1922 Flag);
1923 } else
1924 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1925
Bob Wilsondee46d72009-04-17 20:35:10 +00001926 // Guarantee that all emitted copies are
1927 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001928 Flag = Chain.getValue(1);
1929 }
1930
1931 SDValue result;
1932 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001933 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001934 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001935 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001936
1937 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001938}
1939
Evan Cheng3d2125c2010-11-30 23:55:39 +00001940bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1941 if (N->getNumValues() != 1)
1942 return false;
1943 if (!N->hasNUsesOfValue(1, 0))
1944 return false;
1945
1946 unsigned NumCopies = 0;
Jason W Kim1de886c2012-02-10 16:07:59 +00001947 SDNode* Copies[2] = { 0, 0 };
Evan Cheng3d2125c2010-11-30 23:55:39 +00001948 SDNode *Use = *N->use_begin();
1949 if (Use->getOpcode() == ISD::CopyToReg) {
1950 Copies[NumCopies++] = Use;
1951 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1952 // f64 returned in a pair of GPRs.
1953 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1954 UI != UE; ++UI) {
1955 if (UI->getOpcode() != ISD::CopyToReg)
1956 return false;
1957 Copies[UI.getUse().getResNo()] = *UI;
1958 ++NumCopies;
1959 }
1960 } else if (Use->getOpcode() == ISD::BITCAST) {
1961 // f32 returned in a single GPR.
1962 if (!Use->hasNUsesOfValue(1, 0))
1963 return false;
1964 Use = *Use->use_begin();
1965 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1966 return false;
1967 Copies[NumCopies++] = Use;
1968 } else {
1969 return false;
1970 }
1971
1972 if (NumCopies != 1 && NumCopies != 2)
1973 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001974
1975 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001976 for (unsigned i = 0; i < NumCopies; ++i) {
1977 SDNode *Copy = Copies[i];
1978 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1979 UI != UE; ++UI) {
1980 if (UI->getOpcode() == ISD::CopyToReg) {
1981 SDNode *Use = *UI;
Jason W Kim1de886c2012-02-10 16:07:59 +00001982 if (Use == Copies[0] || ((NumCopies == 2) && (Use == Copies[1])))
Evan Cheng3d2125c2010-11-30 23:55:39 +00001983 continue;
1984 return false;
1985 }
1986 if (UI->getOpcode() != ARMISD::RET_FLAG)
1987 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001988 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001989 }
1990 }
1991
Evan Cheng1bf891a2010-12-01 22:59:46 +00001992 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001993}
1994
Evan Cheng485fafc2011-03-21 01:19:09 +00001995bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1996 if (!EnableARMTailCalls)
1997 return false;
1998
1999 if (!CI->isTailCall())
2000 return false;
2001
2002 return !Subtarget->isThumb1Only();
2003}
2004
Bob Wilsonb62d2572009-11-03 00:02:05 +00002005// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2006// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2007// one of the above mentioned nodes. It has to be wrapped because otherwise
2008// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2009// be used to form addressing mode. These wrapped nodes will be selected
2010// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00002011static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002012 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002013 // FIXME there is no actual debug info here
2014 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002015 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00002016 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00002017 if (CP->isMachineConstantPoolEntry())
2018 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2019 CP->getAlignment());
2020 else
2021 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2022 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00002023 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00002024}
2025
Jim Grosbache1102ca2010-07-19 17:20:38 +00002026unsigned ARMTargetLowering::getJumpTableEncoding() const {
2027 return MachineJumpTableInfo::EK_Inline;
2028}
2029
Dan Gohmand858e902010-04-17 15:26:15 +00002030SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2031 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00002032 MachineFunction &MF = DAG.getMachineFunction();
2033 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2034 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00002035 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00002036 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00002037 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00002038 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2039 SDValue CPAddr;
2040 if (RelocM == Reloc::Static) {
2041 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2042 } else {
2043 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002044 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00002045 ARMConstantPoolValue *CPV =
2046 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2047 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson907eebd2009-11-02 20:59:23 +00002048 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2049 }
2050 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2051 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002052 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002053 false, false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00002054 if (RelocM == Reloc::Static)
2055 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00002056 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00002057 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00002058}
2059
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002060// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00002061SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002062ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002063 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002064 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002065 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002066 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00002067 MachineFunction &MF = DAG.getMachineFunction();
2068 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002069 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002070 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002071 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2072 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002073 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002074 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00002075 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002076 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002077 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002078 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002079
Evan Chenge7e0d622009-11-06 22:24:13 +00002080 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002081 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002082
2083 // call __tls_get_addr.
2084 ArgListTy Args;
2085 ArgListEntry Entry;
2086 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002087 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002088 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002089 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00002090 std::pair<SDValue, SDValue> CallResult =
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002091 LowerCallTo(Chain, (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00002092 false, false, false, false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00002093 0, CallingConv::C, /*isTailCall=*/false,
2094 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002095 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002096 return CallResult.first;
2097}
2098
2099// Lower ISD::GlobalTLSAddress using the "initial exec" or
2100// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00002101SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002102ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002103 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002104 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002105 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002106 SDValue Offset;
2107 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002108 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002109 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002110 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002111
Chris Lattner4fb63d02009-07-15 04:12:33 +00002112 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002113 MachineFunction &MF = DAG.getMachineFunction();
2114 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002115 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002116 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002117 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2118 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002119 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2120 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2121 true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002122 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002123 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002124 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002125 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002126 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002127 Chain = Offset.getValue(1);
2128
Evan Chenge7e0d622009-11-06 22:24:13 +00002129 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002130 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002131
Evan Cheng9eda6892009-10-31 03:39:36 +00002132 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002133 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002134 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002135 } else {
2136 // local exec model
Bill Wendling5bb77992011-10-01 08:00:54 +00002137 ARMConstantPoolValue *CPV =
2138 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002139 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002140 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002141 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002142 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002143 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002144 }
2145
2146 // The address of the thread local variable is the add of the thread
2147 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002148 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002149}
2150
Dan Gohman475871a2008-07-27 21:46:04 +00002151SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002152ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002153 // TODO: implement the "local dynamic" model
2154 assert(Subtarget->isTargetELF() &&
2155 "TLS not implemented for non-ELF targets");
2156 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2157 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
2158 // otherwise use the "Local Exec" TLS Model
2159 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
2160 return LowerToTLSGeneralDynamicModel(GA, DAG);
2161 else
2162 return LowerToTLSExecModels(GA, DAG);
2163}
2164
Dan Gohman475871a2008-07-27 21:46:04 +00002165SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002166 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002167 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002168 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002169 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002170 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2171 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002172 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002173 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002174 ARMConstantPoolConstant::Create(GV,
2175 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002176 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002177 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002178 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002179 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002180 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002181 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002182 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002183 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002184 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002185 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002186 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002187 MachinePointerInfo::getGOT(),
2188 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002189 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002190 }
2191
2192 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloy015cca62011-10-26 08:53:19 +00002193 // pair. This is always cheaper.
2194 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002195 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002196 // FIXME: Once remat is capable of dealing with instructions with register
2197 // operands, expand this into two nodes.
2198 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2199 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002200 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002201 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2202 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2203 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2204 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002205 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002206 }
2207}
2208
Dan Gohman475871a2008-07-27 21:46:04 +00002209SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002210 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002211 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002212 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002213 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002214 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002215 MachineFunction &MF = DAG.getMachineFunction();
2216 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2217
Jakob Stoklund Olesen8f37a242012-01-07 20:49:15 +00002218 // FIXME: Enable this for static codegen when tool issues are fixed. Also
2219 // update ARMFastISel::ARMMaterializeGV.
Evan Chengf31151f2011-10-26 01:17:44 +00002220 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002221 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002222 // FIXME: Once remat is capable of dealing with instructions with register
2223 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002224 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002225 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2226 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2227
Evan Cheng53519f02011-01-21 18:55:51 +00002228 unsigned Wrapper = (RelocM == Reloc::PIC_)
2229 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2230 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002231 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002232 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2233 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002234 MachinePointerInfo::getGOT(),
2235 false, false, false, 0);
Evan Chengfc8475b2011-01-19 02:16:49 +00002236 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002237 }
2238
2239 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002240 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002241 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002242 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002243 } else {
2244 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002245 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2246 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002247 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue,
2248 PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002249 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002250 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002251 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002252
Evan Cheng9eda6892009-10-31 03:39:36 +00002253 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002254 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002255 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002256 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002257
2258 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002259 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002260 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002261 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002262
Evan Cheng63476a82009-09-03 07:04:02 +00002263 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002264 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002265 false, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002266
2267 return Result;
2268}
2269
Dan Gohman475871a2008-07-27 21:46:04 +00002270SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002271 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002272 assert(Subtarget->isTargetELF() &&
2273 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002274 MachineFunction &MF = DAG.getMachineFunction();
2275 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002276 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002277 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002278 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002279 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingfe31e672011-10-01 08:58:29 +00002280 ARMConstantPoolValue *CPV =
2281 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2282 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002283 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002284 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002285 SDValue Result = 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);
Evan Chenge7e0d622009-11-06 22:24:13 +00002288 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002289 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002290}
2291
Jim Grosbach0e0da732009-05-12 23:59:14 +00002292SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002293ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2294 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002295 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendlingce370cf2011-10-07 21:25:38 +00002296 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2297 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002298 Op.getOperand(1), Val);
2299}
2300
2301SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002302ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2303 DebugLoc dl = Op.getDebugLoc();
2304 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2305 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2306}
2307
2308SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002309ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002310 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002311 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002312 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002313 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002314 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002315 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002316 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002317 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2318 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002319 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002320 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002321 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002322 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002323 EVT PtrVT = getPointerTy();
2324 DebugLoc dl = Op.getDebugLoc();
2325 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2326 SDValue CPAddr;
2327 unsigned PCAdj = (RelocM != Reloc::PIC_)
2328 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002329 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002330 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2331 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002332 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002333 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002334 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002335 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002336 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002337 false, false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002338
2339 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002340 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002341 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2342 }
2343 return Result;
2344 }
Evan Cheng92e39162011-03-29 23:06:19 +00002345 case Intrinsic::arm_neon_vmulls:
2346 case Intrinsic::arm_neon_vmullu: {
2347 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2348 ? ARMISD::VMULLs : ARMISD::VMULLu;
2349 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2350 Op.getOperand(1), Op.getOperand(2));
2351 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002352 }
2353}
2354
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002355static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002356 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002357 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002358 if (!Subtarget->hasDataBarrier()) {
2359 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2360 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2361 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002362 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002363 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002364 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002365 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002366 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002367
2368 SDValue Op5 = Op.getOperand(5);
2369 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2370 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2371 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2372 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2373
2374 ARM_MB::MemBOpt DMBOpt;
2375 if (isDeviceBarrier)
2376 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2377 else
2378 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2379 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2380 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002381}
2382
Eli Friedman26689ac2011-08-03 21:06:02 +00002383
2384static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2385 const ARMSubtarget *Subtarget) {
2386 // FIXME: handle "fence singlethread" more efficiently.
2387 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002388 if (!Subtarget->hasDataBarrier()) {
2389 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2390 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2391 // here.
2392 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2393 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002394 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002395 DAG.getConstant(0, MVT::i32));
2396 }
2397
Eli Friedman26689ac2011-08-03 21:06:02 +00002398 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002399 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002400}
2401
Evan Chengdfed19f2010-11-03 06:34:55 +00002402static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2403 const ARMSubtarget *Subtarget) {
2404 // ARM pre v5TE and Thumb1 does not have preload instructions.
2405 if (!(Subtarget->isThumb2() ||
2406 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2407 // Just preserve the chain.
2408 return Op.getOperand(0);
2409
2410 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002411 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2412 if (!isRead &&
2413 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2414 // ARMv7 with MP extension has PLDW.
2415 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002416
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002417 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2418 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002419 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002420 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002421 isData = ~isData & 1;
2422 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002423
2424 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002425 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2426 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002427}
2428
Dan Gohman1e93df62010-04-17 14:41:14 +00002429static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2430 MachineFunction &MF = DAG.getMachineFunction();
2431 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2432
Evan Chenga8e29892007-01-19 07:51:42 +00002433 // vastart just stores the address of the VarArgsFrameIndex slot into the
2434 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002435 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002436 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002437 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002438 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002439 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2440 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002441}
2442
Dan Gohman475871a2008-07-27 21:46:04 +00002443SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002444ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2445 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002446 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002447 MachineFunction &MF = DAG.getMachineFunction();
2448 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2449
Craig Topper44d23822012-02-22 05:59:10 +00002450 const TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002451 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002452 RC = ARM::tGPRRegisterClass;
2453 else
2454 RC = ARM::GPRRegisterClass;
2455
2456 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002457 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002458 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002459
2460 SDValue ArgValue2;
2461 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002462 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002463 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002464
2465 // Create load node to retrieve arguments from the stack.
2466 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002467 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002468 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002469 false, false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002470 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002471 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002472 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002473 }
2474
Jim Grosbache5165492009-11-09 00:11:35 +00002475 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002476}
2477
Stuart Hastingsc7315872011-04-20 16:47:52 +00002478void
2479ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2480 unsigned &VARegSize, unsigned &VARegSaveSize)
2481 const {
2482 unsigned NumGPRs;
2483 if (CCInfo.isFirstByValRegValid())
2484 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2485 else {
2486 unsigned int firstUnalloced;
2487 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2488 sizeof(GPRArgRegs) /
2489 sizeof(GPRArgRegs[0]));
2490 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2491 }
2492
2493 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2494 VARegSize = NumGPRs * 4;
2495 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2496}
2497
2498// The remaining GPRs hold either the beginning of variable-argument
2499// data, or the beginning of an aggregate passed by value (usuall
2500// byval). Either way, we allocate stack slots adjacent to the data
2501// provided by our caller, and store the unallocated registers there.
2502// If this is a variadic function, the va_list pointer will begin with
2503// these values; otherwise, this reassembles a (byval) structure that
2504// was split between registers and memory.
2505void
2506ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2507 DebugLoc dl, SDValue &Chain,
2508 unsigned ArgOffset) const {
2509 MachineFunction &MF = DAG.getMachineFunction();
2510 MachineFrameInfo *MFI = MF.getFrameInfo();
2511 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2512 unsigned firstRegToSaveIndex;
2513 if (CCInfo.isFirstByValRegValid())
2514 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2515 else {
2516 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2517 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2518 }
2519
2520 unsigned VARegSize, VARegSaveSize;
2521 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2522 if (VARegSaveSize) {
2523 // If this function is vararg, store any remaining integer argument regs
2524 // to their spots on the stack so that they may be loaded by deferencing
2525 // the result of va_next.
2526 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002527 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2528 ArgOffset + VARegSaveSize
2529 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002530 false));
2531 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2532 getPointerTy());
2533
2534 SmallVector<SDValue, 4> MemOps;
2535 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
Craig Topper44d23822012-02-22 05:59:10 +00002536 const TargetRegisterClass *RC;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002537 if (AFI->isThumb1OnlyFunction())
2538 RC = ARM::tGPRRegisterClass;
2539 else
2540 RC = ARM::GPRRegisterClass;
2541
2542 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2543 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2544 SDValue Store =
2545 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002546 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002547 false, false, 0);
2548 MemOps.push_back(Store);
2549 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2550 DAG.getConstant(4, getPointerTy()));
2551 }
2552 if (!MemOps.empty())
2553 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2554 &MemOps[0], MemOps.size());
2555 } else
2556 // This will point to the next argument passed via stack.
2557 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2558}
2559
Bob Wilson5bafff32009-06-22 23:27:02 +00002560SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002561ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002562 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002563 const SmallVectorImpl<ISD::InputArg>
2564 &Ins,
2565 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002566 SmallVectorImpl<SDValue> &InVals)
2567 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002568 MachineFunction &MF = DAG.getMachineFunction();
2569 MachineFrameInfo *MFI = MF.getFrameInfo();
2570
Bob Wilson1f595bb2009-04-17 19:07:39 +00002571 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2572
2573 // Assign locations to all of the incoming arguments.
2574 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002575 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2576 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002577 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002578 CCAssignFnForNode(CallConv, /* Return*/ false,
2579 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002580
2581 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002582 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002583
Stuart Hastingsf222e592011-02-28 17:17:53 +00002584 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002585 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2586 CCValAssign &VA = ArgLocs[i];
2587
Bob Wilsondee46d72009-04-17 20:35:10 +00002588 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002589 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002590 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002591
Bob Wilson1f595bb2009-04-17 19:07:39 +00002592 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002593 // f64 and vector types are split up into multiple registers or
2594 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002595 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002596 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002597 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002598 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002599 SDValue ArgValue2;
2600 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002601 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002602 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2603 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002604 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002605 false, false, false, 0);
Bob Wilson6a234f02010-04-13 22:03:22 +00002606 } else {
2607 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2608 Chain, DAG, dl);
2609 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002610 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2611 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002612 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002613 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002614 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2615 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002616 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002617
Bob Wilson5bafff32009-06-22 23:27:02 +00002618 } else {
Craig Topper44d23822012-02-22 05:59:10 +00002619 const TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002620
Owen Anderson825b72b2009-08-11 20:47:22 +00002621 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002622 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002623 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002624 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002625 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002626 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002627 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002628 RC = (AFI->isThumb1OnlyFunction() ?
2629 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002630 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002631 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002632
2633 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002634 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002635 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002636 }
2637
2638 // If this is an 8 or 16-bit value, it is really passed promoted
2639 // to 32 bits. Insert an assert[sz]ext to capture this, then
2640 // truncate to the right size.
2641 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002642 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002643 case CCValAssign::Full: break;
2644 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002645 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002646 break;
2647 case CCValAssign::SExt:
2648 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2649 DAG.getValueType(VA.getValVT()));
2650 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2651 break;
2652 case CCValAssign::ZExt:
2653 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2654 DAG.getValueType(VA.getValVT()));
2655 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2656 break;
2657 }
2658
Dan Gohman98ca4f22009-08-05 01:29:28 +00002659 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002660
2661 } else { // VA.isRegLoc()
2662
2663 // sanity check
2664 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002665 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002666
Stuart Hastingsf222e592011-02-28 17:17:53 +00002667 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002668
Stuart Hastingsf222e592011-02-28 17:17:53 +00002669 // Some Ins[] entries become multiple ArgLoc[] entries.
2670 // Process them only once.
2671 if (index != lastInsIndex)
2672 {
2673 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002674 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002675 // This can be changed with more analysis.
2676 // In case of tail call optimization mark all arguments mutable.
2677 // Since they could be overwritten by lowering of arguments in case of
2678 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002679 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002680 unsigned VARegSize, VARegSaveSize;
2681 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2682 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2683 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002684 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002685 int FI = MFI->CreateFixedObject(Bytes,
2686 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002687 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2688 } else {
2689 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2690 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002691
Stuart Hastingsf222e592011-02-28 17:17:53 +00002692 // Create load nodes to retrieve arguments from the stack.
2693 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2694 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2695 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002696 false, false, false, 0));
Stuart Hastingsf222e592011-02-28 17:17:53 +00002697 }
2698 lastInsIndex = index;
2699 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002700 }
2701 }
2702
2703 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002704 if (isVarArg)
2705 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002706
Dan Gohman98ca4f22009-08-05 01:29:28 +00002707 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002708}
2709
2710/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002711static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002712 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002713 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002714 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002715 // Maybe this has already been legalized into the constant pool?
2716 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002717 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002718 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002719 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002720 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002721 }
2722 }
2723 return false;
2724}
2725
Evan Chenga8e29892007-01-19 07:51:42 +00002726/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2727/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002728SDValue
2729ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002730 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002731 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002732 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002733 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002734 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002735 // Constant does not fit, try adjusting it by one?
2736 switch (CC) {
2737 default: break;
2738 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002739 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002740 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002741 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002742 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002743 }
2744 break;
2745 case ISD::SETULT:
2746 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002747 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002748 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002749 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002750 }
2751 break;
2752 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002753 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002754 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002755 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002756 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002757 }
2758 break;
2759 case ISD::SETULE:
2760 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002761 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002762 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002763 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002764 }
2765 break;
2766 }
2767 }
2768 }
2769
2770 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002771 ARMISD::NodeType CompareType;
2772 switch (CondCode) {
2773 default:
2774 CompareType = ARMISD::CMP;
2775 break;
2776 case ARMCC::EQ:
2777 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002778 // Uses only Z Flag
2779 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002780 break;
2781 }
Evan Cheng218977b2010-07-13 19:27:42 +00002782 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002783 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002784}
2785
2786/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002787SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002788ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002789 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002790 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002791 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002792 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002793 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002794 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2795 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002796}
2797
Bob Wilson79f56c92011-03-08 01:17:20 +00002798/// duplicateCmp - Glue values can have only one use, so this function
2799/// duplicates a comparison node.
2800SDValue
2801ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2802 unsigned Opc = Cmp.getOpcode();
2803 DebugLoc DL = Cmp.getDebugLoc();
2804 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2805 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2806
2807 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2808 Cmp = Cmp.getOperand(0);
2809 Opc = Cmp.getOpcode();
2810 if (Opc == ARMISD::CMPFP)
2811 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2812 else {
2813 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2814 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2815 }
2816 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2817}
2818
Bill Wendlingde2b1512010-08-11 08:43:16 +00002819SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2820 SDValue Cond = Op.getOperand(0);
2821 SDValue SelectTrue = Op.getOperand(1);
2822 SDValue SelectFalse = Op.getOperand(2);
2823 DebugLoc dl = Op.getDebugLoc();
2824
2825 // Convert:
2826 //
2827 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2828 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2829 //
2830 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2831 const ConstantSDNode *CMOVTrue =
2832 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2833 const ConstantSDNode *CMOVFalse =
2834 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2835
2836 if (CMOVTrue && CMOVFalse) {
2837 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2838 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2839
2840 SDValue True;
2841 SDValue False;
2842 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2843 True = SelectTrue;
2844 False = SelectFalse;
2845 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2846 True = SelectFalse;
2847 False = SelectTrue;
2848 }
2849
2850 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002851 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002852 SDValue ARMcc = Cond.getOperand(2);
2853 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002854 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002855 assert(True.getValueType() == VT);
2856 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002857 }
2858 }
2859 }
2860
Dan Gohmandb953892012-02-24 00:09:36 +00002861 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
2862 // undefined bits before doing a full-word comparison with zero.
2863 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
2864 DAG.getConstant(1, Cond.getValueType()));
2865
Bill Wendlingde2b1512010-08-11 08:43:16 +00002866 return DAG.getSelectCC(dl, Cond,
2867 DAG.getConstant(0, Cond.getValueType()),
2868 SelectTrue, SelectFalse, ISD::SETNE);
2869}
2870
Dan Gohmand858e902010-04-17 15:26:15 +00002871SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002872 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002873 SDValue LHS = Op.getOperand(0);
2874 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002875 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002876 SDValue TrueVal = Op.getOperand(2);
2877 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002878 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002879
Owen Anderson825b72b2009-08-11 20:47:22 +00002880 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002881 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002882 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002883 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Jim Grosbachb04546f2011-09-13 20:30:37 +00002884 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002885 }
2886
2887 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002888 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002889
Evan Cheng218977b2010-07-13 19:27:42 +00002890 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2891 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002892 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002893 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002894 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002895 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002896 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002897 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002898 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002899 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002900 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002901 }
2902 return Result;
2903}
2904
Evan Cheng218977b2010-07-13 19:27:42 +00002905/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2906/// to morph to an integer compare sequence.
2907static bool canChangeToInt(SDValue Op, bool &SeenZero,
2908 const ARMSubtarget *Subtarget) {
2909 SDNode *N = Op.getNode();
2910 if (!N->hasOneUse())
2911 // Otherwise it requires moving the value from fp to integer registers.
2912 return false;
2913 if (!N->getNumValues())
2914 return false;
2915 EVT VT = Op.getValueType();
2916 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2917 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2918 // vmrs are very slow, e.g. cortex-a8.
2919 return false;
2920
2921 if (isFloatingPointZero(Op)) {
2922 SeenZero = true;
2923 return true;
2924 }
2925 return ISD::isNormalLoad(N);
2926}
2927
2928static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2929 if (isFloatingPointZero(Op))
2930 return DAG.getConstant(0, MVT::i32);
2931
2932 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2933 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002934 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002935 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002936 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002937
2938 llvm_unreachable("Unknown VFP cmp argument!");
2939}
2940
2941static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2942 SDValue &RetVal1, SDValue &RetVal2) {
2943 if (isFloatingPointZero(Op)) {
2944 RetVal1 = DAG.getConstant(0, MVT::i32);
2945 RetVal2 = DAG.getConstant(0, MVT::i32);
2946 return;
2947 }
2948
2949 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2950 SDValue Ptr = Ld->getBasePtr();
2951 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2952 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002953 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002954 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002955 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002956
2957 EVT PtrType = Ptr.getValueType();
2958 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2959 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2960 PtrType, Ptr, DAG.getConstant(4, PtrType));
2961 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2962 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002963 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002964 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002965 Ld->isInvariant(), NewAlign);
Evan Cheng218977b2010-07-13 19:27:42 +00002966 return;
2967 }
2968
2969 llvm_unreachable("Unknown VFP cmp argument!");
2970}
2971
2972/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2973/// f32 and even f64 comparisons to integer ones.
2974SDValue
2975ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2976 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002977 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002978 SDValue LHS = Op.getOperand(2);
2979 SDValue RHS = Op.getOperand(3);
2980 SDValue Dest = Op.getOperand(4);
2981 DebugLoc dl = Op.getDebugLoc();
2982
Evan Chengfc501a32012-03-01 23:27:13 +00002983 bool LHSSeenZero = false;
2984 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
2985 bool RHSSeenZero = false;
2986 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
2987 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson1b772f92011-03-08 01:17:16 +00002988 // If unsafe fp math optimization is enabled and there are no other uses of
2989 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00002990 // to an integer comparison.
2991 if (CC == ISD::SETOEQ)
2992 CC = ISD::SETEQ;
2993 else if (CC == ISD::SETUNE)
2994 CC = ISD::SETNE;
2995
Evan Chengfc501a32012-03-01 23:27:13 +00002996 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002997 SDValue ARMcc;
2998 if (LHS.getValueType() == MVT::f32) {
Evan Chengfc501a32012-03-01 23:27:13 +00002999 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3000 bitcastf32Toi32(LHS, DAG), Mask);
3001 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3002 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003003 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3004 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3005 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3006 Chain, Dest, ARMcc, CCR, Cmp);
3007 }
3008
3009 SDValue LHS1, LHS2;
3010 SDValue RHS1, RHS2;
3011 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3012 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengfc501a32012-03-01 23:27:13 +00003013 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3014 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003015 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3016 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003017 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003018 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
3019 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
3020 }
3021
3022 return SDValue();
3023}
3024
3025SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3026 SDValue Chain = Op.getOperand(0);
3027 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3028 SDValue LHS = Op.getOperand(2);
3029 SDValue RHS = Op.getOperand(3);
3030 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00003031 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003032
Owen Anderson825b72b2009-08-11 20:47:22 +00003033 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00003034 SDValue ARMcc;
3035 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003036 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00003037 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00003038 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00003039 }
3040
Owen Anderson825b72b2009-08-11 20:47:22 +00003041 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00003042
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003043 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng218977b2010-07-13 19:27:42 +00003044 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3045 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3046 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3047 if (Result.getNode())
3048 return Result;
3049 }
3050
Evan Chenga8e29892007-01-19 07:51:42 +00003051 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00003052 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003053
Evan Cheng218977b2010-07-13 19:27:42 +00003054 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3055 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003056 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003057 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003058 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00003059 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003060 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00003061 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3062 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00003063 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003064 }
3065 return Res;
3066}
3067
Dan Gohmand858e902010-04-17 15:26:15 +00003068SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00003069 SDValue Chain = Op.getOperand(0);
3070 SDValue Table = Op.getOperand(1);
3071 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003072 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003073
Owen Andersone50ed302009-08-10 22:56:29 +00003074 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00003075 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3076 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00003077 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00003078 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00003079 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00003080 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3081 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00003082 if (Subtarget->isThumb2()) {
3083 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3084 // which does another jump to the destination. This also makes it easier
3085 // to translate it to TBB / TBH later.
3086 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00003087 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00003088 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003089 }
Evan Cheng66ac5312009-07-25 00:33:29 +00003090 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00003091 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003092 MachinePointerInfo::getJumpTable(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003093 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003094 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003095 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00003096 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003097 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00003098 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003099 MachinePointerInfo::getJumpTable(),
3100 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003101 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003102 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003103 }
Evan Chenga8e29892007-01-19 07:51:42 +00003104}
3105
Eli Friedman14e809c2011-11-09 23:36:02 +00003106static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy873fd5f2012-02-20 09:24:05 +00003107 EVT VT = Op.getValueType();
3108 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14e809c2011-11-09 23:36:02 +00003109
James Molloy873fd5f2012-02-20 09:24:05 +00003110 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3111 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3112 return Op;
3113 return DAG.UnrollVectorOp(Op.getNode());
3114 }
3115
3116 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3117 "Invalid type for custom lowering!");
3118 if (VT != MVT::v4i16)
3119 return DAG.UnrollVectorOp(Op.getNode());
3120
3121 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3122 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman14e809c2011-11-09 23:36:02 +00003123}
3124
Bob Wilson76a312b2010-03-19 22:51:32 +00003125static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman14e809c2011-11-09 23:36:02 +00003126 EVT VT = Op.getValueType();
3127 if (VT.isVector())
3128 return LowerVectorFP_TO_INT(Op, DAG);
3129
Bob Wilson76a312b2010-03-19 22:51:32 +00003130 DebugLoc dl = Op.getDebugLoc();
3131 unsigned Opc;
3132
3133 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003134 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003135 case ISD::FP_TO_SINT:
3136 Opc = ARMISD::FTOSI;
3137 break;
3138 case ISD::FP_TO_UINT:
3139 Opc = ARMISD::FTOUI;
3140 break;
3141 }
3142 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003143 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003144}
3145
Cameron Zwarich3007d332011-03-29 21:41:55 +00003146static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3147 EVT VT = Op.getValueType();
3148 DebugLoc dl = Op.getDebugLoc();
3149
Eli Friedman14e809c2011-11-09 23:36:02 +00003150 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3151 if (VT.getVectorElementType() == MVT::f32)
3152 return Op;
3153 return DAG.UnrollVectorOp(Op.getNode());
3154 }
3155
Duncan Sands1f6a3292011-08-12 14:54:45 +00003156 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3157 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003158 if (VT != MVT::v4f32)
3159 return DAG.UnrollVectorOp(Op.getNode());
3160
3161 unsigned CastOpc;
3162 unsigned Opc;
3163 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003164 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003165 case ISD::SINT_TO_FP:
3166 CastOpc = ISD::SIGN_EXTEND;
3167 Opc = ISD::SINT_TO_FP;
3168 break;
3169 case ISD::UINT_TO_FP:
3170 CastOpc = ISD::ZERO_EXTEND;
3171 Opc = ISD::UINT_TO_FP;
3172 break;
3173 }
3174
3175 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3176 return DAG.getNode(Opc, dl, VT, Op);
3177}
3178
Bob Wilson76a312b2010-03-19 22:51:32 +00003179static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3180 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003181 if (VT.isVector())
3182 return LowerVectorINT_TO_FP(Op, DAG);
3183
Bob Wilson76a312b2010-03-19 22:51:32 +00003184 DebugLoc dl = Op.getDebugLoc();
3185 unsigned Opc;
3186
3187 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003188 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003189 case ISD::SINT_TO_FP:
3190 Opc = ARMISD::SITOF;
3191 break;
3192 case ISD::UINT_TO_FP:
3193 Opc = ARMISD::UITOF;
3194 break;
3195 }
3196
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003197 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003198 return DAG.getNode(Opc, dl, VT, Op);
3199}
3200
Evan Cheng515fe3a2010-07-08 02:08:50 +00003201SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003202 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003203 SDValue Tmp0 = Op.getOperand(0);
3204 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003205 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003206 EVT VT = Op.getValueType();
3207 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003208 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3209 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3210 bool UseNEON = !InGPR && Subtarget->hasNEON();
3211
3212 if (UseNEON) {
3213 // Use VBSL to copy the sign bit.
3214 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3215 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3216 DAG.getTargetConstant(EncodedVal, MVT::i32));
3217 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3218 if (VT == MVT::f64)
3219 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3220 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3221 DAG.getConstant(32, MVT::i32));
3222 else /*if (VT == MVT::f32)*/
3223 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3224 if (SrcVT == MVT::f32) {
3225 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3226 if (VT == MVT::f64)
3227 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3228 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3229 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003230 } else if (VT == MVT::f32)
3231 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3232 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3233 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003234 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3235 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3236
3237 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3238 MVT::i32);
3239 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3240 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3241 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003242
Evan Chenge573fb32011-02-23 02:24:55 +00003243 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3244 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3245 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003246 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003247 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3248 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3249 DAG.getConstant(0, MVT::i32));
3250 } else {
3251 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3252 }
3253
3254 return Res;
3255 }
Evan Chengc143dd42011-02-11 02:28:55 +00003256
3257 // Bitcast operand 1 to i32.
3258 if (SrcVT == MVT::f64)
3259 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3260 &Tmp1, 1).getValue(1);
3261 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3262
Evan Chenge573fb32011-02-23 02:24:55 +00003263 // Or in the signbit with integer operations.
3264 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3265 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3266 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3267 if (VT == MVT::f32) {
3268 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3269 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3270 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3271 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003272 }
3273
Evan Chenge573fb32011-02-23 02:24:55 +00003274 // f64: Or the high part with signbit and then combine two parts.
3275 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3276 &Tmp0, 1);
3277 SDValue Lo = Tmp0.getValue(0);
3278 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3279 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3280 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003281}
3282
Evan Cheng2457f2c2010-05-22 01:47:14 +00003283SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3284 MachineFunction &MF = DAG.getMachineFunction();
3285 MachineFrameInfo *MFI = MF.getFrameInfo();
3286 MFI->setReturnAddressIsTaken(true);
3287
3288 EVT VT = Op.getValueType();
3289 DebugLoc dl = Op.getDebugLoc();
3290 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3291 if (Depth) {
3292 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3293 SDValue Offset = DAG.getConstant(4, MVT::i32);
3294 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3295 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003296 MachinePointerInfo(), false, false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003297 }
3298
3299 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003300 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003301 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3302}
3303
Dan Gohmand858e902010-04-17 15:26:15 +00003304SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003305 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3306 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003307
Owen Andersone50ed302009-08-10 22:56:29 +00003308 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003309 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3310 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003311 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003312 ? ARM::R7 : ARM::R11;
3313 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3314 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003315 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3316 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003317 false, false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003318 return FrameAddr;
3319}
3320
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003321/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003322/// expand a bit convert where either the source or destination type is i64 to
3323/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3324/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3325/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003326static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003327 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3328 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003329 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003330
Bob Wilson9f3f0612010-04-17 05:30:19 +00003331 // This function is only supposed to be called for i64 types, either as the
3332 // source or destination of the bit convert.
3333 EVT SrcVT = Op.getValueType();
3334 EVT DstVT = N->getValueType(0);
3335 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003336 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003337
Bob Wilson9f3f0612010-04-17 05:30:19 +00003338 // Turn i64->f64 into VMOVDRR.
3339 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003340 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3341 DAG.getConstant(0, MVT::i32));
3342 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3343 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003344 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003345 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003346 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003347
Jim Grosbache5165492009-11-09 00:11:35 +00003348 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003349 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3350 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3351 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3352 // Merge the pieces into a single i64 value.
3353 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3354 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003355
Bob Wilson9f3f0612010-04-17 05:30:19 +00003356 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003357}
3358
Bob Wilson5bafff32009-06-22 23:27:02 +00003359/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003360/// Zero vectors are used to represent vector negation and in those cases
3361/// will be implemented with the NEON VNEG instruction. However, VNEG does
3362/// not support i64 elements, so sometimes the zero vectors will need to be
3363/// explicitly constructed. Regardless, use a canonical VMOV to create the
3364/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003365static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003366 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003367 // The canonical modified immediate encoding of a zero vector is....0!
3368 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3369 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3370 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003371 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003372}
3373
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003374/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3375/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003376SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3377 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003378 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3379 EVT VT = Op.getValueType();
3380 unsigned VTBits = VT.getSizeInBits();
3381 DebugLoc dl = Op.getDebugLoc();
3382 SDValue ShOpLo = Op.getOperand(0);
3383 SDValue ShOpHi = Op.getOperand(1);
3384 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003385 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003386 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003387
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003388 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3389
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003390 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3391 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3392 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3393 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3394 DAG.getConstant(VTBits, MVT::i32));
3395 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3396 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003397 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003398
3399 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3400 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003401 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003402 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003403 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003404 CCR, Cmp);
3405
3406 SDValue Ops[2] = { Lo, Hi };
3407 return DAG.getMergeValues(Ops, 2, dl);
3408}
3409
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003410/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3411/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003412SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3413 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003414 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3415 EVT VT = Op.getValueType();
3416 unsigned VTBits = VT.getSizeInBits();
3417 DebugLoc dl = Op.getDebugLoc();
3418 SDValue ShOpLo = Op.getOperand(0);
3419 SDValue ShOpHi = Op.getOperand(1);
3420 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003421 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003422
3423 assert(Op.getOpcode() == ISD::SHL_PARTS);
3424 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3425 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3426 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3427 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3428 DAG.getConstant(VTBits, MVT::i32));
3429 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3430 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3431
3432 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3433 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3434 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003435 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003436 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003437 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003438 CCR, Cmp);
3439
3440 SDValue Ops[2] = { Lo, Hi };
3441 return DAG.getMergeValues(Ops, 2, dl);
3442}
3443
Jim Grosbach4725ca72010-09-08 03:54:02 +00003444SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003445 SelectionDAG &DAG) const {
3446 // The rounding mode is in bits 23:22 of the FPSCR.
3447 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3448 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3449 // so that the shift + and get folded into a bitfield extract.
3450 DebugLoc dl = Op.getDebugLoc();
3451 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3452 DAG.getConstant(Intrinsic::arm_get_fpscr,
3453 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003454 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003455 DAG.getConstant(1U << 22, MVT::i32));
3456 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3457 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003458 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003459 DAG.getConstant(3, MVT::i32));
3460}
3461
Jim Grosbach3482c802010-01-18 19:58:49 +00003462static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3463 const ARMSubtarget *ST) {
3464 EVT VT = N->getValueType(0);
3465 DebugLoc dl = N->getDebugLoc();
3466
3467 if (!ST->hasV6T2Ops())
3468 return SDValue();
3469
3470 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3471 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3472}
3473
Bob Wilson5bafff32009-06-22 23:27:02 +00003474static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3475 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003476 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003477 DebugLoc dl = N->getDebugLoc();
3478
Bob Wilsond5448bb2010-11-18 21:16:28 +00003479 if (!VT.isVector())
3480 return SDValue();
3481
Bob Wilson5bafff32009-06-22 23:27:02 +00003482 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003483 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003484
Bob Wilsond5448bb2010-11-18 21:16:28 +00003485 // Left shifts translate directly to the vshiftu intrinsic.
3486 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003487 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003488 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3489 N->getOperand(0), N->getOperand(1));
3490
3491 assert((N->getOpcode() == ISD::SRA ||
3492 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3493
3494 // NEON uses the same intrinsics for both left and right shifts. For
3495 // right shifts, the shift amounts are negative, so negate the vector of
3496 // shift amounts.
3497 EVT ShiftVT = N->getOperand(1).getValueType();
3498 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3499 getZeroVector(ShiftVT, DAG, dl),
3500 N->getOperand(1));
3501 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3502 Intrinsic::arm_neon_vshifts :
3503 Intrinsic::arm_neon_vshiftu);
3504 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3505 DAG.getConstant(vshiftInt, MVT::i32),
3506 N->getOperand(0), NegatedCount);
3507}
3508
3509static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3510 const ARMSubtarget *ST) {
3511 EVT VT = N->getValueType(0);
3512 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003513
Eli Friedmance392eb2009-08-22 03:13:10 +00003514 // We can get here for a node like i32 = ISD::SHL i32, i64
3515 if (VT != MVT::i64)
3516 return SDValue();
3517
3518 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003519 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003520
Chris Lattner27a6c732007-11-24 07:07:01 +00003521 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3522 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003523 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003524 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003525
Chris Lattner27a6c732007-11-24 07:07:01 +00003526 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003527 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003528
Chris Lattner27a6c732007-11-24 07:07:01 +00003529 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003530 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003531 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003532 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003533 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003534
Chris Lattner27a6c732007-11-24 07:07:01 +00003535 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3536 // captures the result into a carry flag.
3537 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003538 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003539
Chris Lattner27a6c732007-11-24 07:07:01 +00003540 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003541 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003542
Chris Lattner27a6c732007-11-24 07:07:01 +00003543 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003544 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003545}
3546
Bob Wilson5bafff32009-06-22 23:27:02 +00003547static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3548 SDValue TmpOp0, TmpOp1;
3549 bool Invert = false;
3550 bool Swap = false;
3551 unsigned Opc = 0;
3552
3553 SDValue Op0 = Op.getOperand(0);
3554 SDValue Op1 = Op.getOperand(1);
3555 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003556 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003557 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3558 DebugLoc dl = Op.getDebugLoc();
3559
3560 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3561 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003562 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003563 case ISD::SETUNE:
3564 case ISD::SETNE: Invert = true; // Fallthrough
3565 case ISD::SETOEQ:
3566 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3567 case ISD::SETOLT:
3568 case ISD::SETLT: Swap = true; // Fallthrough
3569 case ISD::SETOGT:
3570 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3571 case ISD::SETOLE:
3572 case ISD::SETLE: Swap = true; // Fallthrough
3573 case ISD::SETOGE:
3574 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3575 case ISD::SETUGE: Swap = true; // Fallthrough
3576 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3577 case ISD::SETUGT: Swap = true; // Fallthrough
3578 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3579 case ISD::SETUEQ: Invert = true; // Fallthrough
3580 case ISD::SETONE:
3581 // Expand this to (OLT | OGT).
3582 TmpOp0 = Op0;
3583 TmpOp1 = Op1;
3584 Opc = ISD::OR;
3585 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3586 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3587 break;
3588 case ISD::SETUO: Invert = true; // Fallthrough
3589 case ISD::SETO:
3590 // Expand this to (OLT | OGE).
3591 TmpOp0 = Op0;
3592 TmpOp1 = Op1;
3593 Opc = ISD::OR;
3594 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3595 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3596 break;
3597 }
3598 } else {
3599 // Integer comparisons.
3600 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003601 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003602 case ISD::SETNE: Invert = true;
3603 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3604 case ISD::SETLT: Swap = true;
3605 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3606 case ISD::SETLE: Swap = true;
3607 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3608 case ISD::SETULT: Swap = true;
3609 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3610 case ISD::SETULE: Swap = true;
3611 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3612 }
3613
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003614 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003615 if (Opc == ARMISD::VCEQ) {
3616
3617 SDValue AndOp;
3618 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3619 AndOp = Op0;
3620 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3621 AndOp = Op1;
3622
3623 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003624 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003625 AndOp = AndOp.getOperand(0);
3626
3627 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3628 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003629 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3630 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003631 Invert = !Invert;
3632 }
3633 }
3634 }
3635
3636 if (Swap)
3637 std::swap(Op0, Op1);
3638
Owen Andersonc24cb352010-11-08 23:21:22 +00003639 // If one of the operands is a constant vector zero, attempt to fold the
3640 // comparison to a specialized compare-against-zero form.
3641 SDValue SingleOp;
3642 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3643 SingleOp = Op0;
3644 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3645 if (Opc == ARMISD::VCGE)
3646 Opc = ARMISD::VCLEZ;
3647 else if (Opc == ARMISD::VCGT)
3648 Opc = ARMISD::VCLTZ;
3649 SingleOp = Op1;
3650 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003651
Owen Andersonc24cb352010-11-08 23:21:22 +00003652 SDValue Result;
3653 if (SingleOp.getNode()) {
3654 switch (Opc) {
3655 case ARMISD::VCEQ:
3656 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3657 case ARMISD::VCGE:
3658 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3659 case ARMISD::VCLEZ:
3660 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3661 case ARMISD::VCGT:
3662 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3663 case ARMISD::VCLTZ:
3664 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3665 default:
3666 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3667 }
3668 } else {
3669 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3670 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003671
3672 if (Invert)
3673 Result = DAG.getNOT(dl, Result, VT);
3674
3675 return Result;
3676}
3677
Lang Hames45b5f882012-03-15 18:49:02 +00003678SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
3679 const ARMSubtarget *ST) const {
3680 if (!ST->useNEONForSinglePrecisionFP() || !ST->hasVFP3() || ST->hasD16())
3681 return SDValue();
3682
3683 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
3684 assert(Op.getValueType() == MVT::f32 &&
3685 "ConstantFP custom lowering should only occur for f32.");
3686
3687 APFloat FPVal = CFP->getValueAPF();
3688 int ImmVal = ARM_AM::getFP32Imm(FPVal);
3689 if (ImmVal == -1)
3690 return SDValue();
3691
3692 DebugLoc DL = Op.getDebugLoc();
3693 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
3694 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32, NewVal);
3695 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
3696 DAG.getConstant(0, MVT::i32));
3697}
3698
Bob Wilsond3c42842010-06-14 22:19:57 +00003699/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3700/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003701/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003702static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3703 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003704 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003705 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003706
Bob Wilson827b2102010-06-15 19:05:35 +00003707 // SplatBitSize is set to the smallest size that splats the vector, so a
3708 // zero vector will always have SplatBitSize == 8. However, NEON modified
3709 // immediate instructions others than VMOV do not support the 8-bit encoding
3710 // of a zero vector, and the default encoding of zero is supposed to be the
3711 // 32-bit version.
3712 if (SplatBits == 0)
3713 SplatBitSize = 32;
3714
Bob Wilson5bafff32009-06-22 23:27:02 +00003715 switch (SplatBitSize) {
3716 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003717 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003718 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003719 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003720 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003721 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003722 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003723 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003724 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003725
3726 case 16:
3727 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003728 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003729 if ((SplatBits & ~0xff) == 0) {
3730 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003731 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003732 Imm = SplatBits;
3733 break;
3734 }
3735 if ((SplatBits & ~0xff00) == 0) {
3736 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003737 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003738 Imm = SplatBits >> 8;
3739 break;
3740 }
3741 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003742
3743 case 32:
3744 // NEON's 32-bit VMOV supports splat values where:
3745 // * only one byte is nonzero, or
3746 // * the least significant byte is 0xff and the second byte is nonzero, or
3747 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003748 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003749 if ((SplatBits & ~0xff) == 0) {
3750 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003751 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003752 Imm = SplatBits;
3753 break;
3754 }
3755 if ((SplatBits & ~0xff00) == 0) {
3756 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003757 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003758 Imm = SplatBits >> 8;
3759 break;
3760 }
3761 if ((SplatBits & ~0xff0000) == 0) {
3762 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003763 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003764 Imm = SplatBits >> 16;
3765 break;
3766 }
3767 if ((SplatBits & ~0xff000000) == 0) {
3768 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003769 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003770 Imm = SplatBits >> 24;
3771 break;
3772 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003773
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003774 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3775 if (type == OtherModImm) return SDValue();
3776
Bob Wilson5bafff32009-06-22 23:27:02 +00003777 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003778 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3779 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003780 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003781 Imm = SplatBits >> 8;
3782 SplatBits |= 0xff;
3783 break;
3784 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003785
3786 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003787 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3788 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003789 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003790 Imm = SplatBits >> 16;
3791 SplatBits |= 0xffff;
3792 break;
3793 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003794
3795 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3796 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3797 // VMOV.I32. A (very) minor optimization would be to replicate the value
3798 // and fall through here to test for a valid 64-bit splat. But, then the
3799 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003800 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003801
3802 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003803 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003804 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003805 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003806 uint64_t BitMask = 0xff;
3807 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003808 unsigned ImmMask = 1;
3809 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003810 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003811 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003812 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003813 Imm |= ImmMask;
3814 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003815 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003816 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003817 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003818 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003819 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003820 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003821 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003822 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003823 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003824 break;
3825 }
3826
Bob Wilson1a913ed2010-06-11 21:34:50 +00003827 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003828 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003829 }
3830
Bob Wilsoncba270d2010-07-13 21:16:48 +00003831 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3832 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003833}
3834
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003835static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003836 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003837 unsigned NumElts = VT.getVectorNumElements();
3838 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003839
3840 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3841 if (M[0] < 0)
3842 return false;
3843
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003844 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003845
3846 // If this is a VEXT shuffle, the immediate value is the index of the first
3847 // element. The other shuffle indices must be the successive elements after
3848 // the first one.
3849 unsigned ExpectedElt = Imm;
3850 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003851 // Increment the expected index. If it wraps around, it may still be
3852 // a VEXT but the source vectors must be swapped.
3853 ExpectedElt += 1;
3854 if (ExpectedElt == NumElts * 2) {
3855 ExpectedElt = 0;
3856 ReverseVEXT = true;
3857 }
3858
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003859 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003860 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003861 return false;
3862 }
3863
3864 // Adjust the index value if the source operands will be swapped.
3865 if (ReverseVEXT)
3866 Imm -= NumElts;
3867
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003868 return true;
3869}
3870
Bob Wilson8bb9e482009-07-26 00:39:34 +00003871/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3872/// instruction with the specified blocksize. (The order of the elements
3873/// within each block of the vector is reversed.)
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003874static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003875 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3876 "Only possible block sizes for VREV are: 16, 32, 64");
3877
Bob Wilson8bb9e482009-07-26 00:39:34 +00003878 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003879 if (EltSz == 64)
3880 return false;
3881
3882 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003883 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003884 // If the first shuffle index is UNDEF, be optimistic.
3885 if (M[0] < 0)
3886 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003887
3888 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3889 return false;
3890
3891 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003892 if (M[i] < 0) continue; // ignore UNDEF indices
3893 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003894 return false;
3895 }
3896
3897 return true;
3898}
3899
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003900static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003901 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3902 // range, then 0 is placed into the resulting vector. So pretty much any mask
3903 // of 8 elements can work here.
3904 return VT == MVT::v8i8 && M.size() == 8;
3905}
3906
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003907static bool isVTRNMask(ArrayRef<int> M, EVT VT, 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 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003915 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3916 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003917 return false;
3918 }
3919 return true;
3920}
3921
Bob Wilson324f4f12009-12-03 06:40:55 +00003922/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3923/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3924/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003925static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00003926 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3927 if (EltSz == 64)
3928 return false;
3929
3930 unsigned NumElts = VT.getVectorNumElements();
3931 WhichResult = (M[0] == 0 ? 0 : 1);
3932 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003933 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3934 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003935 return false;
3936 }
3937 return true;
3938}
3939
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003940static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003941 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3942 if (EltSz == 64)
3943 return false;
3944
Bob Wilsonc692cb72009-08-21 20:54:19 +00003945 unsigned NumElts = VT.getVectorNumElements();
3946 WhichResult = (M[0] == 0 ? 0 : 1);
3947 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003948 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003949 if ((unsigned) M[i] != 2 * i + WhichResult)
3950 return false;
3951 }
3952
3953 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003954 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003955 return false;
3956
3957 return true;
3958}
3959
Bob Wilson324f4f12009-12-03 06:40:55 +00003960/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3961/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3962/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003963static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00003964 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3965 if (EltSz == 64)
3966 return false;
3967
3968 unsigned Half = VT.getVectorNumElements() / 2;
3969 WhichResult = (M[0] == 0 ? 0 : 1);
3970 for (unsigned j = 0; j != 2; ++j) {
3971 unsigned Idx = WhichResult;
3972 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003973 int MIdx = M[i + j * Half];
3974 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003975 return false;
3976 Idx += 2;
3977 }
3978 }
3979
3980 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3981 if (VT.is64BitVector() && EltSz == 32)
3982 return false;
3983
3984 return true;
3985}
3986
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003987static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003988 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3989 if (EltSz == 64)
3990 return false;
3991
Bob Wilsonc692cb72009-08-21 20:54:19 +00003992 unsigned NumElts = VT.getVectorNumElements();
3993 WhichResult = (M[0] == 0 ? 0 : 1);
3994 unsigned Idx = WhichResult * NumElts / 2;
3995 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003996 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3997 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003998 return false;
3999 Idx += 1;
4000 }
4001
4002 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004003 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004004 return false;
4005
4006 return true;
4007}
4008
Bob Wilson324f4f12009-12-03 06:40:55 +00004009/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
4010/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4011/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004012static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004013 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4014 if (EltSz == 64)
4015 return false;
4016
4017 unsigned NumElts = VT.getVectorNumElements();
4018 WhichResult = (M[0] == 0 ? 0 : 1);
4019 unsigned Idx = WhichResult * NumElts / 2;
4020 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004021 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4022 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00004023 return false;
4024 Idx += 1;
4025 }
4026
4027 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4028 if (VT.is64BitVector() && EltSz == 32)
4029 return false;
4030
4031 return true;
4032}
4033
Dale Johannesenf630c712010-07-29 20:10:08 +00004034// If N is an integer constant that can be moved into a register in one
4035// instruction, return an SDValue of such a constant (will become a MOV
4036// instruction). Otherwise return null.
4037static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
4038 const ARMSubtarget *ST, DebugLoc dl) {
4039 uint64_t Val;
4040 if (!isa<ConstantSDNode>(N))
4041 return SDValue();
4042 Val = cast<ConstantSDNode>(N)->getZExtValue();
4043
4044 if (ST->isThumb1Only()) {
4045 if (Val <= 255 || ~Val <= 255)
4046 return DAG.getConstant(Val, MVT::i32);
4047 } else {
4048 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
4049 return DAG.getConstant(Val, MVT::i32);
4050 }
4051 return SDValue();
4052}
4053
Bob Wilson5bafff32009-06-22 23:27:02 +00004054// If this is a case we can't handle, return null and let the default
4055// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00004056SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
4057 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00004058 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00004059 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004060 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004061
4062 APInt SplatBits, SplatUndef;
4063 unsigned SplatBitSize;
4064 bool HasAnyUndefs;
4065 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004066 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00004067 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00004068 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00004069 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00004070 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004071 DAG, VmovVT, VT.is128BitVector(),
4072 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004073 if (Val.getNode()) {
4074 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004075 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004076 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004077
4078 // Try an immediate VMVN.
Eli Friedman8e4d0422011-10-13 22:40:23 +00004079 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004080 Val = isNEONModifiedImm(NegatedImm,
4081 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004082 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004083 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004084 if (Val.getNode()) {
4085 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004086 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004087 }
Evan Chengeaa192a2011-11-15 02:12:34 +00004088
4089 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedman2f21e8c2011-12-15 22:56:53 +00004090 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedmaneffab8f2011-12-09 23:54:42 +00004091 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Chengeaa192a2011-11-15 02:12:34 +00004092 if (ImmVal != -1) {
4093 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4094 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4095 }
4096 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004097 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00004098 }
4099
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004100 // Scan through the operands to see if only one value is used.
4101 unsigned NumElts = VT.getVectorNumElements();
4102 bool isOnlyLowElement = true;
4103 bool usesOnlyOneValue = true;
4104 bool isConstant = true;
4105 SDValue Value;
4106 for (unsigned i = 0; i < NumElts; ++i) {
4107 SDValue V = Op.getOperand(i);
4108 if (V.getOpcode() == ISD::UNDEF)
4109 continue;
4110 if (i > 0)
4111 isOnlyLowElement = false;
4112 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4113 isConstant = false;
4114
4115 if (!Value.getNode())
4116 Value = V;
4117 else if (V != Value)
4118 usesOnlyOneValue = false;
4119 }
4120
4121 if (!Value.getNode())
4122 return DAG.getUNDEF(VT);
4123
4124 if (isOnlyLowElement)
4125 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4126
Dale Johannesenf630c712010-07-29 20:10:08 +00004127 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4128
Dale Johannesen575cd142010-10-19 20:00:17 +00004129 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4130 // i32 and try again.
4131 if (usesOnlyOneValue && EltSize <= 32) {
4132 if (!isConstant)
4133 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4134 if (VT.getVectorElementType().isFloatingPoint()) {
4135 SmallVector<SDValue, 8> Ops;
4136 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004137 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00004138 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00004139 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
4140 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00004141 Val = LowerBUILD_VECTOR(Val, DAG, ST);
4142 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004143 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004144 }
Dale Johannesen575cd142010-10-19 20:00:17 +00004145 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
4146 if (Val.getNode())
4147 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004148 }
4149
4150 // If all elements are constants and the case above didn't get hit, fall back
4151 // to the default expansion, which will generate a load from the constant
4152 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004153 if (isConstant)
4154 return SDValue();
4155
Bob Wilson11a1dff2011-01-07 21:37:30 +00004156 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4157 if (NumElts >= 4) {
4158 SDValue shuffle = ReconstructShuffle(Op, DAG);
4159 if (shuffle != SDValue())
4160 return shuffle;
4161 }
4162
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004163 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004164 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4165 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004166 if (EltSize >= 32) {
4167 // Do the expansion with floating-point types, since that is what the VFP
4168 // registers are defined to use, and since i64 is not legal.
4169 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4170 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004171 SmallVector<SDValue, 8> Ops;
4172 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004173 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004174 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004175 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004176 }
4177
4178 return SDValue();
4179}
4180
Bob Wilson11a1dff2011-01-07 21:37:30 +00004181// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004182// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004183SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4184 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004185 DebugLoc dl = Op.getDebugLoc();
4186 EVT VT = Op.getValueType();
4187 unsigned NumElts = VT.getVectorNumElements();
4188
4189 SmallVector<SDValue, 2> SourceVecs;
4190 SmallVector<unsigned, 2> MinElts;
4191 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004192
Bob Wilson11a1dff2011-01-07 21:37:30 +00004193 for (unsigned i = 0; i < NumElts; ++i) {
4194 SDValue V = Op.getOperand(i);
4195 if (V.getOpcode() == ISD::UNDEF)
4196 continue;
4197 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4198 // A shuffle can only come from building a vector from various
4199 // elements of other vectors.
4200 return SDValue();
Eli Friedman46995fa2011-10-14 23:58:49 +00004201 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
4202 VT.getVectorElementType()) {
4203 // This code doesn't know how to handle shuffles where the vector
4204 // element types do not match (this happens because type legalization
4205 // promotes the return type of EXTRACT_VECTOR_ELT).
4206 // FIXME: It might be appropriate to extend this code to handle
4207 // mismatched types.
4208 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004209 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004210
Bob Wilson11a1dff2011-01-07 21:37:30 +00004211 // Record this extraction against the appropriate vector if possible...
4212 SDValue SourceVec = V.getOperand(0);
4213 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4214 bool FoundSource = false;
4215 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4216 if (SourceVecs[j] == SourceVec) {
4217 if (MinElts[j] > EltNo)
4218 MinElts[j] = EltNo;
4219 if (MaxElts[j] < EltNo)
4220 MaxElts[j] = EltNo;
4221 FoundSource = true;
4222 break;
4223 }
4224 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004225
Bob Wilson11a1dff2011-01-07 21:37:30 +00004226 // Or record a new source if not...
4227 if (!FoundSource) {
4228 SourceVecs.push_back(SourceVec);
4229 MinElts.push_back(EltNo);
4230 MaxElts.push_back(EltNo);
4231 }
4232 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004233
Bob Wilson11a1dff2011-01-07 21:37:30 +00004234 // Currently only do something sane when at most two source vectors
4235 // involved.
4236 if (SourceVecs.size() > 2)
4237 return SDValue();
4238
4239 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4240 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004241
Bob Wilson11a1dff2011-01-07 21:37:30 +00004242 // This loop extracts the usage patterns of the source vectors
4243 // and prepares appropriate SDValues for a shuffle if possible.
4244 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4245 if (SourceVecs[i].getValueType() == VT) {
4246 // No VEXT necessary
4247 ShuffleSrcs[i] = SourceVecs[i];
4248 VEXTOffsets[i] = 0;
4249 continue;
4250 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4251 // It probably isn't worth padding out a smaller vector just to
4252 // break it down again in a shuffle.
4253 return SDValue();
4254 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004255
Bob Wilson11a1dff2011-01-07 21:37:30 +00004256 // Since only 64-bit and 128-bit vectors are legal on ARM and
4257 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004258 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4259 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004260
Bob Wilson11a1dff2011-01-07 21:37:30 +00004261 if (MaxElts[i] - MinElts[i] >= NumElts) {
4262 // Span too large for a VEXT to cope
4263 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004264 }
4265
Bob Wilson11a1dff2011-01-07 21:37:30 +00004266 if (MinElts[i] >= NumElts) {
4267 // The extraction can just take the second half
4268 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004269 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4270 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004271 DAG.getIntPtrConstant(NumElts));
4272 } else if (MaxElts[i] < NumElts) {
4273 // The extraction can just take the first half
4274 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004275 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4276 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004277 DAG.getIntPtrConstant(0));
4278 } else {
4279 // An actual VEXT is needed
4280 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004281 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4282 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004283 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004284 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4285 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004286 DAG.getIntPtrConstant(NumElts));
4287 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4288 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4289 }
4290 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004291
Bob Wilson11a1dff2011-01-07 21:37:30 +00004292 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004293
Bob Wilson11a1dff2011-01-07 21:37:30 +00004294 for (unsigned i = 0; i < NumElts; ++i) {
4295 SDValue Entry = Op.getOperand(i);
4296 if (Entry.getOpcode() == ISD::UNDEF) {
4297 Mask.push_back(-1);
4298 continue;
4299 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004300
Bob Wilson11a1dff2011-01-07 21:37:30 +00004301 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004302 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4303 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004304 if (ExtractVec == SourceVecs[0]) {
4305 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4306 } else {
4307 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4308 }
4309 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004310
Bob Wilson11a1dff2011-01-07 21:37:30 +00004311 // Final check before we try to produce nonsense...
4312 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004313 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4314 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004315
Bob Wilson11a1dff2011-01-07 21:37:30 +00004316 return SDValue();
4317}
4318
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004319/// isShuffleMaskLegal - Targets can use this to indicate that they only
4320/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4321/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4322/// are assumed to be legal.
4323bool
4324ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4325 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004326 if (VT.getVectorNumElements() == 4 &&
4327 (VT.is128BitVector() || VT.is64BitVector())) {
4328 unsigned PFIndexes[4];
4329 for (unsigned i = 0; i != 4; ++i) {
4330 if (M[i] < 0)
4331 PFIndexes[i] = 8;
4332 else
4333 PFIndexes[i] = M[i];
4334 }
4335
4336 // Compute the index in the perfect shuffle table.
4337 unsigned PFTableIndex =
4338 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4339 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4340 unsigned Cost = (PFEntry >> 30);
4341
4342 if (Cost <= 4)
4343 return true;
4344 }
4345
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004346 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004347 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004348
Bob Wilson53dd2452010-06-07 23:53:38 +00004349 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4350 return (EltSize >= 32 ||
4351 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004352 isVREVMask(M, VT, 64) ||
4353 isVREVMask(M, VT, 32) ||
4354 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004355 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004356 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004357 isVTRNMask(M, VT, WhichResult) ||
4358 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004359 isVZIPMask(M, VT, WhichResult) ||
4360 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4361 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4362 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004363}
4364
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004365/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4366/// the specified operations to build the shuffle.
4367static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4368 SDValue RHS, SelectionDAG &DAG,
4369 DebugLoc dl) {
4370 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4371 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4372 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4373
4374 enum {
4375 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4376 OP_VREV,
4377 OP_VDUP0,
4378 OP_VDUP1,
4379 OP_VDUP2,
4380 OP_VDUP3,
4381 OP_VEXT1,
4382 OP_VEXT2,
4383 OP_VEXT3,
4384 OP_VUZPL, // VUZP, left result
4385 OP_VUZPR, // VUZP, right result
4386 OP_VZIPL, // VZIP, left result
4387 OP_VZIPR, // VZIP, right result
4388 OP_VTRNL, // VTRN, left result
4389 OP_VTRNR // VTRN, right result
4390 };
4391
4392 if (OpNum == OP_COPY) {
4393 if (LHSID == (1*9+2)*9+3) return LHS;
4394 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4395 return RHS;
4396 }
4397
4398 SDValue OpLHS, OpRHS;
4399 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4400 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4401 EVT VT = OpLHS.getValueType();
4402
4403 switch (OpNum) {
4404 default: llvm_unreachable("Unknown shuffle opcode!");
4405 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004406 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004407 if (VT.getVectorElementType() == MVT::i32 ||
4408 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004409 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4410 // vrev <4 x i16> -> VREV32
4411 if (VT.getVectorElementType() == MVT::i16)
4412 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4413 // vrev <4 x i8> -> VREV16
4414 assert(VT.getVectorElementType() == MVT::i8);
4415 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004416 case OP_VDUP0:
4417 case OP_VDUP1:
4418 case OP_VDUP2:
4419 case OP_VDUP3:
4420 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004421 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004422 case OP_VEXT1:
4423 case OP_VEXT2:
4424 case OP_VEXT3:
4425 return DAG.getNode(ARMISD::VEXT, dl, VT,
4426 OpLHS, OpRHS,
4427 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4428 case OP_VUZPL:
4429 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004430 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004431 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4432 case OP_VZIPL:
4433 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004434 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004435 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4436 case OP_VTRNL:
4437 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004438 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4439 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004440 }
4441}
4442
Bill Wendling69a05a72011-03-14 23:02:38 +00004443static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004444 ArrayRef<int> ShuffleMask,
Bill Wendling69a05a72011-03-14 23:02:38 +00004445 SelectionDAG &DAG) {
4446 // Check to see if we can use the VTBL instruction.
4447 SDValue V1 = Op.getOperand(0);
4448 SDValue V2 = Op.getOperand(1);
4449 DebugLoc DL = Op.getDebugLoc();
4450
4451 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004452 for (ArrayRef<int>::iterator
Bill Wendling69a05a72011-03-14 23:02:38 +00004453 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4454 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4455
4456 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4457 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4458 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4459 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004460
Owen Anderson76706012011-04-05 21:48:57 +00004461 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004462 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4463 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004464}
4465
Bob Wilson5bafff32009-06-22 23:27:02 +00004466static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004467 SDValue V1 = Op.getOperand(0);
4468 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004469 DebugLoc dl = Op.getDebugLoc();
4470 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004471 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsond8e17572009-08-12 22:31:50 +00004472
Bob Wilson28865062009-08-13 02:13:04 +00004473 // Convert shuffles that are directly supported on NEON to target-specific
4474 // DAG nodes, instead of keeping them as shuffles and matching them again
4475 // during code selection. This is more efficient and avoids the possibility
4476 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004477 // FIXME: floating-point vectors should be canonicalized to integer vectors
4478 // of the same time so that they get CSEd properly.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004479 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004480
Bob Wilson53dd2452010-06-07 23:53:38 +00004481 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4482 if (EltSize <= 32) {
4483 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4484 int Lane = SVN->getSplatIndex();
4485 // If this is undef splat, generate it via "just" vdup, if possible.
4486 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004487
Dan Gohman65fd6562011-11-03 21:49:52 +00004488 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson53dd2452010-06-07 23:53:38 +00004489 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4490 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4491 }
Dan Gohman65fd6562011-11-03 21:49:52 +00004492 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
4493 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
4494 // reaches it).
4495 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
4496 !isa<ConstantSDNode>(V1.getOperand(0))) {
4497 bool IsScalarToVector = true;
4498 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
4499 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
4500 IsScalarToVector = false;
4501 break;
4502 }
4503 if (IsScalarToVector)
4504 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4505 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004506 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4507 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004508 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004509
4510 bool ReverseVEXT;
4511 unsigned Imm;
4512 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4513 if (ReverseVEXT)
4514 std::swap(V1, V2);
4515 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4516 DAG.getConstant(Imm, MVT::i32));
4517 }
4518
4519 if (isVREVMask(ShuffleMask, VT, 64))
4520 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4521 if (isVREVMask(ShuffleMask, VT, 32))
4522 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4523 if (isVREVMask(ShuffleMask, VT, 16))
4524 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4525
4526 // Check for Neon shuffles that modify both input vectors in place.
4527 // If both results are used, i.e., if there are two shuffles with the same
4528 // source operands and with masks corresponding to both results of one of
4529 // these operations, DAG memoization will ensure that a single node is
4530 // used for both shuffles.
4531 unsigned WhichResult;
4532 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4533 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4534 V1, V2).getValue(WhichResult);
4535 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4536 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4537 V1, V2).getValue(WhichResult);
4538 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4539 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4540 V1, V2).getValue(WhichResult);
4541
4542 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4543 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4544 V1, V1).getValue(WhichResult);
4545 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4546 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4547 V1, V1).getValue(WhichResult);
4548 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4549 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4550 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004551 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004552
Bob Wilsonc692cb72009-08-21 20:54:19 +00004553 // If the shuffle is not directly supported and it has 4 elements, use
4554 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004555 unsigned NumElts = VT.getVectorNumElements();
4556 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004557 unsigned PFIndexes[4];
4558 for (unsigned i = 0; i != 4; ++i) {
4559 if (ShuffleMask[i] < 0)
4560 PFIndexes[i] = 8;
4561 else
4562 PFIndexes[i] = ShuffleMask[i];
4563 }
4564
4565 // Compute the index in the perfect shuffle table.
4566 unsigned PFTableIndex =
4567 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004568 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4569 unsigned Cost = (PFEntry >> 30);
4570
4571 if (Cost <= 4)
4572 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4573 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004574
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004575 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004576 if (EltSize >= 32) {
4577 // Do the expansion with floating-point types, since that is what the VFP
4578 // registers are defined to use, and since i64 is not legal.
4579 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4580 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004581 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4582 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004583 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004584 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004585 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004586 Ops.push_back(DAG.getUNDEF(EltVT));
4587 else
4588 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4589 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4590 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4591 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004592 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004593 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004594 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004595 }
4596
Bill Wendling69a05a72011-03-14 23:02:38 +00004597 if (VT == MVT::v8i8) {
4598 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4599 if (NewOp.getNode())
4600 return NewOp;
4601 }
4602
Bob Wilson22cac0d2009-08-14 05:16:33 +00004603 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004604}
4605
Eli Friedman5c89cb82011-10-24 23:08:52 +00004606static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
4607 // INSERT_VECTOR_ELT is legal only for immediate indexes.
4608 SDValue Lane = Op.getOperand(2);
4609 if (!isa<ConstantSDNode>(Lane))
4610 return SDValue();
4611
4612 return Op;
4613}
4614
Bob Wilson5bafff32009-06-22 23:27:02 +00004615static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004616 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004617 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004618 if (!isa<ConstantSDNode>(Lane))
4619 return SDValue();
4620
4621 SDValue Vec = Op.getOperand(0);
4622 if (Op.getValueType() == MVT::i32 &&
4623 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4624 DebugLoc dl = Op.getDebugLoc();
4625 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4626 }
4627
4628 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004629}
4630
Bob Wilsona6d65862009-08-03 20:36:38 +00004631static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4632 // The only time a CONCAT_VECTORS operation can have legal types is when
4633 // two 64-bit vectors are concatenated to a 128-bit vector.
4634 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4635 "unexpected CONCAT_VECTORS");
4636 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004637 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004638 SDValue Op0 = Op.getOperand(0);
4639 SDValue Op1 = Op.getOperand(1);
4640 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004641 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004642 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004643 DAG.getIntPtrConstant(0));
4644 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004645 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004646 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004647 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004648 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004649}
4650
Bob Wilson626613d2010-11-23 19:38:38 +00004651/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4652/// element has been zero/sign-extended, depending on the isSigned parameter,
4653/// from an integer type half its size.
4654static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4655 bool isSigned) {
4656 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4657 EVT VT = N->getValueType(0);
4658 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4659 SDNode *BVN = N->getOperand(0).getNode();
4660 if (BVN->getValueType(0) != MVT::v4i32 ||
4661 BVN->getOpcode() != ISD::BUILD_VECTOR)
4662 return false;
4663 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4664 unsigned HiElt = 1 - LoElt;
4665 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4666 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4667 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4668 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4669 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4670 return false;
4671 if (isSigned) {
4672 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4673 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4674 return true;
4675 } else {
4676 if (Hi0->isNullValue() && Hi1->isNullValue())
4677 return true;
4678 }
4679 return false;
4680 }
4681
4682 if (N->getOpcode() != ISD::BUILD_VECTOR)
4683 return false;
4684
4685 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4686 SDNode *Elt = N->getOperand(i).getNode();
4687 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4688 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4689 unsigned HalfSize = EltSize / 2;
4690 if (isSigned) {
Bob Wilson9d45de22011-10-18 18:46:49 +00004691 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004692 return false;
4693 } else {
Bob Wilson9d45de22011-10-18 18:46:49 +00004694 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004695 return false;
4696 }
4697 continue;
4698 }
4699 return false;
4700 }
4701
4702 return true;
4703}
4704
4705/// isSignExtended - Check if a node is a vector value that is sign-extended
4706/// or a constant BUILD_VECTOR with sign-extended elements.
4707static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4708 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4709 return true;
4710 if (isExtendedBUILD_VECTOR(N, DAG, true))
4711 return true;
4712 return false;
4713}
4714
4715/// isZeroExtended - Check if a node is a vector value that is zero-extended
4716/// or a constant BUILD_VECTOR with zero-extended elements.
4717static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4718 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4719 return true;
4720 if (isExtendedBUILD_VECTOR(N, DAG, false))
4721 return true;
4722 return false;
4723}
4724
4725/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4726/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004727static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4728 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4729 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004730 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4731 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4732 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004733 LD->isNonTemporal(), LD->isInvariant(),
4734 LD->getAlignment());
Bob Wilson626613d2010-11-23 19:38:38 +00004735 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4736 // have been legalized as a BITCAST from v4i32.
4737 if (N->getOpcode() == ISD::BITCAST) {
4738 SDNode *BVN = N->getOperand(0).getNode();
4739 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4740 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4741 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4742 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4743 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4744 }
4745 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4746 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4747 EVT VT = N->getValueType(0);
4748 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4749 unsigned NumElts = VT.getVectorNumElements();
4750 MVT TruncVT = MVT::getIntegerVT(EltSize);
4751 SmallVector<SDValue, 8> Ops;
4752 for (unsigned i = 0; i != NumElts; ++i) {
4753 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4754 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004755 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004756 }
4757 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4758 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004759}
4760
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004761static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4762 unsigned Opcode = N->getOpcode();
4763 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4764 SDNode *N0 = N->getOperand(0).getNode();
4765 SDNode *N1 = N->getOperand(1).getNode();
4766 return N0->hasOneUse() && N1->hasOneUse() &&
4767 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4768 }
4769 return false;
4770}
4771
4772static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4773 unsigned Opcode = N->getOpcode();
4774 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4775 SDNode *N0 = N->getOperand(0).getNode();
4776 SDNode *N1 = N->getOperand(1).getNode();
4777 return N0->hasOneUse() && N1->hasOneUse() &&
4778 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4779 }
4780 return false;
4781}
4782
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004783static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4784 // Multiplications are only custom-lowered for 128-bit vectors so that
4785 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4786 EVT VT = Op.getValueType();
4787 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4788 SDNode *N0 = Op.getOperand(0).getNode();
4789 SDNode *N1 = Op.getOperand(1).getNode();
4790 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004791 bool isMLA = false;
4792 bool isN0SExt = isSignExtended(N0, DAG);
4793 bool isN1SExt = isSignExtended(N1, DAG);
4794 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004795 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004796 else {
4797 bool isN0ZExt = isZeroExtended(N0, DAG);
4798 bool isN1ZExt = isZeroExtended(N1, DAG);
4799 if (isN0ZExt && isN1ZExt)
4800 NewOpc = ARMISD::VMULLu;
4801 else if (isN1SExt || isN1ZExt) {
4802 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4803 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4804 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4805 NewOpc = ARMISD::VMULLs;
4806 isMLA = true;
4807 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4808 NewOpc = ARMISD::VMULLu;
4809 isMLA = true;
4810 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4811 std::swap(N0, N1);
4812 NewOpc = ARMISD::VMULLu;
4813 isMLA = true;
4814 }
4815 }
4816
4817 if (!NewOpc) {
4818 if (VT == MVT::v2i64)
4819 // Fall through to expand this. It is not legal.
4820 return SDValue();
4821 else
4822 // Other vector multiplications are legal.
4823 return Op;
4824 }
4825 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004826
4827 // Legalize to a VMULL instruction.
4828 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004829 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004830 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004831 if (!isMLA) {
4832 Op0 = SkipExtension(N0, DAG);
4833 assert(Op0.getValueType().is64BitVector() &&
4834 Op1.getValueType().is64BitVector() &&
4835 "unexpected types for extended operands to VMULL");
4836 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4837 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004838
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004839 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4840 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4841 // vmull q0, d4, d6
4842 // vmlal q0, d5, d6
4843 // is faster than
4844 // vaddl q0, d4, d5
4845 // vmovl q1, d6
4846 // vmul q0, q0, q1
4847 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4848 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4849 EVT Op1VT = Op1.getValueType();
4850 return DAG.getNode(N0->getOpcode(), DL, VT,
4851 DAG.getNode(NewOpc, DL, VT,
4852 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4853 DAG.getNode(NewOpc, DL, VT,
4854 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004855}
4856
Owen Anderson76706012011-04-05 21:48:57 +00004857static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004858LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4859 // Convert to float
4860 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4861 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4862 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4863 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4864 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4865 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4866 // Get reciprocal estimate.
4867 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004868 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004869 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4870 // Because char has a smaller range than uchar, we can actually get away
4871 // without any newton steps. This requires that we use a weird bias
4872 // of 0xb000, however (again, this has been exhaustively tested).
4873 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4874 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4875 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4876 Y = DAG.getConstant(0xb000, MVT::i32);
4877 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4878 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4879 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4880 // Convert back to short.
4881 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4882 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4883 return X;
4884}
4885
Owen Anderson76706012011-04-05 21:48:57 +00004886static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004887LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4888 SDValue N2;
4889 // Convert to float.
4890 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4891 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4892 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4893 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4894 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4895 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004896
Nate Begeman7973f352011-02-11 20:53:29 +00004897 // Use reciprocal estimate and one refinement step.
4898 // float4 recip = vrecpeq_f32(yf);
4899 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004900 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004901 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004902 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004903 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4904 N1, N2);
4905 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4906 // Because short has a smaller range than ushort, we can actually get away
4907 // with only a single newton step. This requires that we use a weird bias
4908 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004909 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004910 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4911 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004912 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004913 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4914 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4915 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4916 // Convert back to integer and return.
4917 // return vmovn_s32(vcvt_s32_f32(result));
4918 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4919 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4920 return N0;
4921}
4922
4923static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4924 EVT VT = Op.getValueType();
4925 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4926 "unexpected type for custom-lowering ISD::SDIV");
4927
4928 DebugLoc dl = Op.getDebugLoc();
4929 SDValue N0 = Op.getOperand(0);
4930 SDValue N1 = Op.getOperand(1);
4931 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004932
Nate Begeman7973f352011-02-11 20:53:29 +00004933 if (VT == MVT::v8i8) {
4934 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4935 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004936
Nate Begeman7973f352011-02-11 20:53:29 +00004937 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4938 DAG.getIntPtrConstant(4));
4939 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004940 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004941 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4942 DAG.getIntPtrConstant(0));
4943 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4944 DAG.getIntPtrConstant(0));
4945
4946 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4947 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4948
4949 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4950 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004951
Nate Begeman7973f352011-02-11 20:53:29 +00004952 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
4953 return N0;
4954 }
4955 return LowerSDIV_v4i16(N0, N1, dl, DAG);
4956}
4957
4958static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
4959 EVT VT = Op.getValueType();
4960 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4961 "unexpected type for custom-lowering ISD::UDIV");
4962
4963 DebugLoc dl = Op.getDebugLoc();
4964 SDValue N0 = Op.getOperand(0);
4965 SDValue N1 = Op.getOperand(1);
4966 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004967
Nate Begeman7973f352011-02-11 20:53:29 +00004968 if (VT == MVT::v8i8) {
4969 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
4970 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004971
Nate Begeman7973f352011-02-11 20:53:29 +00004972 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4973 DAG.getIntPtrConstant(4));
4974 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004975 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004976 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4977 DAG.getIntPtrConstant(0));
4978 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4979 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00004980
Nate Begeman7973f352011-02-11 20:53:29 +00004981 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
4982 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00004983
Nate Begeman7973f352011-02-11 20:53:29 +00004984 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4985 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004986
4987 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00004988 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
4989 N0);
4990 return N0;
4991 }
Owen Anderson76706012011-04-05 21:48:57 +00004992
Nate Begeman7973f352011-02-11 20:53:29 +00004993 // v4i16 sdiv ... Convert to float.
4994 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
4995 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
4996 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
4997 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
4998 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004999 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00005000
5001 // Use reciprocal estimate and two refinement steps.
5002 // float4 recip = vrecpeq_f32(yf);
5003 // recip *= vrecpsq_f32(yf, recip);
5004 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00005005 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005006 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00005007 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005008 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005009 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005010 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00005011 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005012 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005013 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005014 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5015 // Simply multiplying by the reciprocal estimate can leave us a few ulps
5016 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
5017 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005018 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00005019 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5020 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
5021 N1 = DAG.getConstant(2, MVT::i32);
5022 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5023 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5024 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5025 // Convert back to integer and return.
5026 // return vmovn_u32(vcvt_s32_f32(result));
5027 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5028 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5029 return N0;
5030}
5031
Evan Cheng342e3162011-08-30 01:34:54 +00005032static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
5033 EVT VT = Op.getNode()->getValueType(0);
5034 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
5035
5036 unsigned Opc;
5037 bool ExtraOp = false;
5038 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00005039 default: llvm_unreachable("Invalid code");
Evan Cheng342e3162011-08-30 01:34:54 +00005040 case ISD::ADDC: Opc = ARMISD::ADDC; break;
5041 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
5042 case ISD::SUBC: Opc = ARMISD::SUBC; break;
5043 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
5044 }
5045
5046 if (!ExtraOp)
5047 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5048 Op.getOperand(1));
5049 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5050 Op.getOperand(1), Op.getOperand(2));
5051}
5052
Eli Friedman74bf18c2011-09-15 22:26:18 +00005053static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedman7cc15662011-09-15 22:18:49 +00005054 // Monotonic load/store is legal for all targets
5055 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
5056 return Op;
5057
5058 // Aquire/Release load/store is not legal for targets without a
5059 // dmb or equivalent available.
5060 return SDValue();
5061}
5062
5063
Eli Friedman2bdffe42011-08-31 00:31:29 +00005064static void
Eli Friedman4d3f3292011-08-31 17:52:22 +00005065ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
5066 SelectionDAG &DAG, unsigned NewOp) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005067 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +00005068 assert (Node->getValueType(0) == MVT::i64 &&
5069 "Only know how to expand i64 atomics");
Eli Friedman2bdffe42011-08-31 00:31:29 +00005070
Eli Friedman4d3f3292011-08-31 17:52:22 +00005071 SmallVector<SDValue, 6> Ops;
5072 Ops.push_back(Node->getOperand(0)); // Chain
5073 Ops.push_back(Node->getOperand(1)); // Ptr
5074 // Low part of Val1
5075 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5076 Node->getOperand(2), DAG.getIntPtrConstant(0)));
5077 // High part of Val1
5078 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5079 Node->getOperand(2), DAG.getIntPtrConstant(1)));
Andrew Trick3af7a672011-09-20 03:06:13 +00005080 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) {
Eli Friedman4d3f3292011-08-31 17:52:22 +00005081 // High part of Val1
5082 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5083 Node->getOperand(3), DAG.getIntPtrConstant(0)));
5084 // High part of Val2
5085 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5086 Node->getOperand(3), DAG.getIntPtrConstant(1)));
5087 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005088 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
5089 SDValue Result =
Eli Friedman4d3f3292011-08-31 17:52:22 +00005090 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64,
Eli Friedman2bdffe42011-08-31 00:31:29 +00005091 cast<MemSDNode>(Node)->getMemOperand());
Eli Friedman4d3f3292011-08-31 17:52:22 +00005092 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) };
Eli Friedman2bdffe42011-08-31 00:31:29 +00005093 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
5094 Results.push_back(Result.getValue(2));
5095}
5096
Dan Gohmand858e902010-04-17 15:26:15 +00005097SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005098 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005099 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00005100 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00005101 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005102 case ISD::GlobalAddress:
5103 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
5104 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00005105 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00005106 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00005107 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
5108 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005109 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00005110 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00005111 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00005112 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00005113 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00005114 case ISD::SINT_TO_FP:
5115 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
5116 case ISD::FP_TO_SINT:
5117 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005118 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00005119 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00005120 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005121 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00005122 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00005123 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00005124 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
5125 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00005126 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005127 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00005128 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00005129 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00005130 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00005131 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00005132 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00005133 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Duncan Sands28b77e92011-09-06 19:07:46 +00005134 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hames45b5f882012-03-15 18:49:02 +00005135 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesenf630c712010-07-29 20:10:08 +00005136 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005137 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedman5c89cb82011-10-24 23:08:52 +00005138 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005139 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00005140 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00005141 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005142 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00005143 case ISD::SDIV: return LowerSDIV(Op, DAG);
5144 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00005145 case ISD::ADDC:
5146 case ISD::ADDE:
5147 case ISD::SUBC:
5148 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Eli Friedman7cc15662011-09-15 22:18:49 +00005149 case ISD::ATOMIC_LOAD:
Eli Friedman74bf18c2011-09-15 22:26:18 +00005150 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005151 }
Evan Chenga8e29892007-01-19 07:51:42 +00005152}
5153
Duncan Sands1607f052008-12-01 11:39:25 +00005154/// ReplaceNodeResults - Replace the results of node with an illegal result
5155/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00005156void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
5157 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005158 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00005159 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00005160 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00005161 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005162 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005163 case ISD::BITCAST:
5164 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005165 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005166 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00005167 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00005168 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005169 break;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005170 case ISD::ATOMIC_LOAD_ADD:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005171 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005172 return;
5173 case ISD::ATOMIC_LOAD_AND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005174 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005175 return;
5176 case ISD::ATOMIC_LOAD_NAND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005177 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005178 return;
5179 case ISD::ATOMIC_LOAD_OR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005180 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005181 return;
5182 case ISD::ATOMIC_LOAD_SUB:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005183 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005184 return;
5185 case ISD::ATOMIC_LOAD_XOR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005186 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005187 return;
5188 case ISD::ATOMIC_SWAP:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005189 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005190 return;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005191 case ISD::ATOMIC_CMP_SWAP:
5192 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG);
5193 return;
Duncan Sands1607f052008-12-01 11:39:25 +00005194 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00005195 if (Res.getNode())
5196 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00005197}
Chris Lattner27a6c732007-11-24 07:07:01 +00005198
Evan Chenga8e29892007-01-19 07:51:42 +00005199//===----------------------------------------------------------------------===//
5200// ARM Scheduler Hooks
5201//===----------------------------------------------------------------------===//
5202
5203MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005204ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
5205 MachineBasicBlock *BB,
5206 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005207 unsigned dest = MI->getOperand(0).getReg();
5208 unsigned ptr = MI->getOperand(1).getReg();
5209 unsigned oldval = MI->getOperand(2).getReg();
5210 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005211 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5212 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005213 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005214
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005215 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5216 unsigned scratch =
Cameron Zwarich141ec632011-05-18 02:29:50 +00005217 MRI.createVirtualRegister(isThumb2 ? ARM::rGPRRegisterClass
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005218 : ARM::GPRRegisterClass);
5219
5220 if (isThumb2) {
Cameron Zwarich141ec632011-05-18 02:29:50 +00005221 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5222 MRI.constrainRegClass(oldval, ARM::rGPRRegisterClass);
5223 MRI.constrainRegClass(newval, ARM::rGPRRegisterClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005224 }
5225
Jim Grosbach5278eb82009-12-11 01:42:04 +00005226 unsigned ldrOpc, strOpc;
5227 switch (Size) {
5228 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005229 case 1:
5230 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00005231 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005232 break;
5233 case 2:
5234 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5235 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5236 break;
5237 case 4:
5238 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5239 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5240 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00005241 }
5242
5243 MachineFunction *MF = BB->getParent();
5244 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5245 MachineFunction::iterator It = BB;
5246 ++It; // insert the new blocks after the current block
5247
5248 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5249 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5250 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5251 MF->insert(It, loop1MBB);
5252 MF->insert(It, loop2MBB);
5253 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005254
5255 // Transfer the remainder of BB and its successor edges to exitMBB.
5256 exitMBB->splice(exitMBB->begin(), BB,
5257 llvm::next(MachineBasicBlock::iterator(MI)),
5258 BB->end());
5259 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005260
5261 // thisMBB:
5262 // ...
5263 // fallthrough --> loop1MBB
5264 BB->addSuccessor(loop1MBB);
5265
5266 // loop1MBB:
5267 // ldrex dest, [ptr]
5268 // cmp dest, oldval
5269 // bne exitMBB
5270 BB = loop1MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005271 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5272 if (ldrOpc == ARM::t2LDREX)
5273 MIB.addImm(0);
5274 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005275 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005276 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005277 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5278 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005279 BB->addSuccessor(loop2MBB);
5280 BB->addSuccessor(exitMBB);
5281
5282 // loop2MBB:
5283 // strex scratch, newval, [ptr]
5284 // cmp scratch, #0
5285 // bne loop1MBB
5286 BB = loop2MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005287 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr);
5288 if (strOpc == ARM::t2STREX)
5289 MIB.addImm(0);
5290 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005291 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005292 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005293 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5294 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005295 BB->addSuccessor(loop1MBB);
5296 BB->addSuccessor(exitMBB);
5297
5298 // exitMBB:
5299 // ...
5300 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005301
Dan Gohman14152b42010-07-06 20:24:04 +00005302 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005303
Jim Grosbach5278eb82009-12-11 01:42:04 +00005304 return BB;
5305}
5306
5307MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005308ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5309 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005310 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5311 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5312
5313 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005314 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005315 MachineFunction::iterator It = BB;
5316 ++It;
5317
5318 unsigned dest = MI->getOperand(0).getReg();
5319 unsigned ptr = MI->getOperand(1).getReg();
5320 unsigned incr = MI->getOperand(2).getReg();
5321 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005322 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005323
5324 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5325 if (isThumb2) {
5326 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5327 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5328 }
5329
Jim Grosbachc3c23542009-12-14 04:22:04 +00005330 unsigned ldrOpc, strOpc;
5331 switch (Size) {
5332 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005333 case 1:
5334 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005335 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005336 break;
5337 case 2:
5338 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5339 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5340 break;
5341 case 4:
5342 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5343 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5344 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005345 }
5346
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005347 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5348 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5349 MF->insert(It, loopMBB);
5350 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005351
5352 // Transfer the remainder of BB and its successor edges to exitMBB.
5353 exitMBB->splice(exitMBB->begin(), BB,
5354 llvm::next(MachineBasicBlock::iterator(MI)),
5355 BB->end());
5356 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005357
Craig Topper44d23822012-02-22 05:59:10 +00005358 const TargetRegisterClass *TRC =
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005359 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5360 unsigned scratch = MRI.createVirtualRegister(TRC);
5361 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005362
5363 // thisMBB:
5364 // ...
5365 // fallthrough --> loopMBB
5366 BB->addSuccessor(loopMBB);
5367
5368 // loopMBB:
5369 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005370 // <binop> scratch2, dest, incr
5371 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005372 // cmp scratch, #0
5373 // bne- loopMBB
5374 // fallthrough --> exitMBB
5375 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005376 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5377 if (ldrOpc == ARM::t2LDREX)
5378 MIB.addImm(0);
5379 AddDefaultPred(MIB);
Jim Grosbachc67b5562009-12-15 00:12:35 +00005380 if (BinOpcode) {
5381 // operand order needs to go the other way for NAND
5382 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5383 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5384 addReg(incr).addReg(dest)).addReg(0);
5385 else
5386 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5387 addReg(dest).addReg(incr)).addReg(0);
5388 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005389
Jim Grosbachb6aed502011-09-09 18:37:27 +00005390 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5391 if (strOpc == ARM::t2STREX)
5392 MIB.addImm(0);
5393 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005394 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005395 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005396 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5397 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005398
5399 BB->addSuccessor(loopMBB);
5400 BB->addSuccessor(exitMBB);
5401
5402 // exitMBB:
5403 // ...
5404 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005405
Dan Gohman14152b42010-07-06 20:24:04 +00005406 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005407
Jim Grosbachc3c23542009-12-14 04:22:04 +00005408 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005409}
5410
Jim Grosbachf7da8822011-04-26 19:44:18 +00005411MachineBasicBlock *
5412ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5413 MachineBasicBlock *BB,
5414 unsigned Size,
5415 bool signExtend,
5416 ARMCC::CondCodes Cond) const {
5417 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5418
5419 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5420 MachineFunction *MF = BB->getParent();
5421 MachineFunction::iterator It = BB;
5422 ++It;
5423
5424 unsigned dest = MI->getOperand(0).getReg();
5425 unsigned ptr = MI->getOperand(1).getReg();
5426 unsigned incr = MI->getOperand(2).getReg();
5427 unsigned oldval = dest;
5428 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005429 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005430
5431 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5432 if (isThumb2) {
5433 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5434 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5435 }
5436
Jim Grosbachf7da8822011-04-26 19:44:18 +00005437 unsigned ldrOpc, strOpc, extendOpc;
5438 switch (Size) {
5439 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5440 case 1:
5441 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5442 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005443 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005444 break;
5445 case 2:
5446 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5447 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005448 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005449 break;
5450 case 4:
5451 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5452 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5453 extendOpc = 0;
5454 break;
5455 }
5456
5457 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5458 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5459 MF->insert(It, loopMBB);
5460 MF->insert(It, exitMBB);
5461
5462 // Transfer the remainder of BB and its successor edges to exitMBB.
5463 exitMBB->splice(exitMBB->begin(), BB,
5464 llvm::next(MachineBasicBlock::iterator(MI)),
5465 BB->end());
5466 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5467
Craig Topper44d23822012-02-22 05:59:10 +00005468 const TargetRegisterClass *TRC =
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005469 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5470 unsigned scratch = MRI.createVirtualRegister(TRC);
5471 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005472
5473 // thisMBB:
5474 // ...
5475 // fallthrough --> loopMBB
5476 BB->addSuccessor(loopMBB);
5477
5478 // loopMBB:
5479 // ldrex dest, ptr
5480 // (sign extend dest, if required)
5481 // cmp dest, incr
5482 // cmov.cond scratch2, dest, incr
5483 // strex scratch, scratch2, ptr
5484 // cmp scratch, #0
5485 // bne- loopMBB
5486 // fallthrough --> exitMBB
5487 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005488 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5489 if (ldrOpc == ARM::t2LDREX)
5490 MIB.addImm(0);
5491 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005492
5493 // Sign extend the value, if necessary.
5494 if (signExtend && extendOpc) {
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005495 oldval = MRI.createVirtualRegister(ARM::GPRRegisterClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005496 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5497 .addReg(dest)
5498 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005499 }
5500
5501 // Build compare and cmov instructions.
5502 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5503 .addReg(oldval).addReg(incr));
5504 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5505 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5506
Jim Grosbachb6aed502011-09-09 18:37:27 +00005507 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5508 if (strOpc == ARM::t2STREX)
5509 MIB.addImm(0);
5510 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005511 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5512 .addReg(scratch).addImm(0));
5513 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5514 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5515
5516 BB->addSuccessor(loopMBB);
5517 BB->addSuccessor(exitMBB);
5518
5519 // exitMBB:
5520 // ...
5521 BB = exitMBB;
5522
5523 MI->eraseFromParent(); // The instruction is gone now.
5524
5525 return BB;
5526}
5527
Eli Friedman2bdffe42011-08-31 00:31:29 +00005528MachineBasicBlock *
5529ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
5530 unsigned Op1, unsigned Op2,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005531 bool NeedsCarry, bool IsCmpxchg) const {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005532 // This also handles ATOMIC_SWAP, indicated by Op1==0.
5533 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5534
5535 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5536 MachineFunction *MF = BB->getParent();
5537 MachineFunction::iterator It = BB;
5538 ++It;
5539
5540 unsigned destlo = MI->getOperand(0).getReg();
5541 unsigned desthi = MI->getOperand(1).getReg();
5542 unsigned ptr = MI->getOperand(2).getReg();
5543 unsigned vallo = MI->getOperand(3).getReg();
5544 unsigned valhi = MI->getOperand(4).getReg();
5545 DebugLoc dl = MI->getDebugLoc();
5546 bool isThumb2 = Subtarget->isThumb2();
5547
5548 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5549 if (isThumb2) {
5550 MRI.constrainRegClass(destlo, ARM::rGPRRegisterClass);
5551 MRI.constrainRegClass(desthi, ARM::rGPRRegisterClass);
5552 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5553 }
5554
5555 unsigned ldrOpc = isThumb2 ? ARM::t2LDREXD : ARM::LDREXD;
5556 unsigned strOpc = isThumb2 ? ARM::t2STREXD : ARM::STREXD;
5557
5558 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman7df496d2011-09-01 22:27:41 +00005559 MachineBasicBlock *contBB = 0, *cont2BB = 0;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005560 if (IsCmpxchg) {
5561 contBB = MF->CreateMachineBasicBlock(LLVM_BB);
5562 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB);
5563 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005564 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5565 MF->insert(It, loopMBB);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005566 if (IsCmpxchg) {
5567 MF->insert(It, contBB);
5568 MF->insert(It, cont2BB);
5569 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005570 MF->insert(It, exitMBB);
5571
5572 // Transfer the remainder of BB and its successor edges to exitMBB.
5573 exitMBB->splice(exitMBB->begin(), BB,
5574 llvm::next(MachineBasicBlock::iterator(MI)),
5575 BB->end());
5576 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5577
Craig Topper44d23822012-02-22 05:59:10 +00005578 const TargetRegisterClass *TRC =
Eli Friedman2bdffe42011-08-31 00:31:29 +00005579 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5580 unsigned storesuccess = MRI.createVirtualRegister(TRC);
5581
5582 // thisMBB:
5583 // ...
5584 // fallthrough --> loopMBB
5585 BB->addSuccessor(loopMBB);
5586
5587 // loopMBB:
5588 // ldrexd r2, r3, ptr
5589 // <binopa> r0, r2, incr
5590 // <binopb> r1, r3, incr
5591 // strexd storesuccess, r0, r1, ptr
5592 // cmp storesuccess, #0
5593 // bne- loopMBB
5594 // fallthrough --> exitMBB
5595 //
5596 // Note that the registers are explicitly specified because there is not any
5597 // way to force the register allocator to allocate a register pair.
5598 //
Andrew Trick3af7a672011-09-20 03:06:13 +00005599 // FIXME: The hardcoded registers are not necessary for Thumb2, but we
Eli Friedman2bdffe42011-08-31 00:31:29 +00005600 // need to properly enforce the restriction that the two output registers
5601 // for ldrexd must be different.
5602 BB = loopMBB;
5603 // Load
5604 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc))
5605 .addReg(ARM::R2, RegState::Define)
5606 .addReg(ARM::R3, RegState::Define).addReg(ptr));
5607 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
5608 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo).addReg(ARM::R2);
5609 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi).addReg(ARM::R3);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005610
5611 if (IsCmpxchg) {
5612 // Add early exit
5613 for (unsigned i = 0; i < 2; i++) {
5614 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr :
5615 ARM::CMPrr))
5616 .addReg(i == 0 ? destlo : desthi)
5617 .addReg(i == 0 ? vallo : valhi));
5618 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5619 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5620 BB->addSuccessor(exitMBB);
5621 BB->addSuccessor(i == 0 ? contBB : cont2BB);
5622 BB = (i == 0 ? contBB : cont2BB);
5623 }
5624
5625 // Copy to physregs for strexd
5626 unsigned setlo = MI->getOperand(5).getReg();
5627 unsigned sethi = MI->getOperand(6).getReg();
5628 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(setlo);
5629 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(sethi);
5630 } else if (Op1) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005631 // Perform binary operation
5632 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), ARM::R0)
5633 .addReg(destlo).addReg(vallo))
5634 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
5635 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), ARM::R1)
5636 .addReg(desthi).addReg(valhi)).addReg(0);
5637 } else {
5638 // Copy to physregs for strexd
5639 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(vallo);
5640 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(valhi);
5641 }
5642
5643 // Store
5644 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), storesuccess)
5645 .addReg(ARM::R0).addReg(ARM::R1).addReg(ptr));
5646 // Cmp+jump
5647 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5648 .addReg(storesuccess).addImm(0));
5649 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5650 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5651
5652 BB->addSuccessor(loopMBB);
5653 BB->addSuccessor(exitMBB);
5654
5655 // exitMBB:
5656 // ...
5657 BB = exitMBB;
5658
5659 MI->eraseFromParent(); // The instruction is gone now.
5660
5661 return BB;
5662}
5663
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005664/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
5665/// registers the function context.
5666void ARMTargetLowering::
5667SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
5668 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005669 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5670 DebugLoc dl = MI->getDebugLoc();
5671 MachineFunction *MF = MBB->getParent();
5672 MachineRegisterInfo *MRI = &MF->getRegInfo();
5673 MachineConstantPool *MCP = MF->getConstantPool();
5674 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5675 const Function *F = MF->getFunction();
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005676
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005677 bool isThumb = Subtarget->isThumb();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005678 bool isThumb2 = Subtarget->isThumb2();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005679
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005680 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005681 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005682 ARMConstantPoolValue *CPV =
5683 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
5684 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
5685
5686 const TargetRegisterClass *TRC =
5687 isThumb ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5688
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005689 // Grab constant pool and fixed stack memory operands.
5690 MachineMemOperand *CPMMO =
5691 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
5692 MachineMemOperand::MOLoad, 4, 4);
5693
5694 MachineMemOperand *FIMMOSt =
5695 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
5696 MachineMemOperand::MOStore, 4, 4);
5697
5698 // Load the address of the dispatch MBB into the jump buffer.
5699 if (isThumb2) {
5700 // Incoming value: jbuf
5701 // ldr.n r5, LCPI1_1
5702 // orr r5, r5, #1
5703 // add r5, pc
5704 // str r5, [$jbuf, #+4] ; &jbuf[1]
5705 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5706 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
5707 .addConstantPoolIndex(CPI)
5708 .addMemOperand(CPMMO));
5709 // Set the low bit because of thumb mode.
5710 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5711 AddDefaultCC(
5712 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
5713 .addReg(NewVReg1, RegState::Kill)
5714 .addImm(0x01)));
5715 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5716 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
5717 .addReg(NewVReg2, RegState::Kill)
5718 .addImm(PCLabelId);
5719 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
5720 .addReg(NewVReg3, RegState::Kill)
5721 .addFrameIndex(FI)
5722 .addImm(36) // &jbuf[1] :: pc
5723 .addMemOperand(FIMMOSt));
5724 } else if (isThumb) {
5725 // Incoming value: jbuf
5726 // ldr.n r1, LCPI1_4
5727 // add r1, pc
5728 // mov r2, #1
5729 // orrs r1, r2
5730 // add r2, $jbuf, #+4 ; &jbuf[1]
5731 // str r1, [r2]
5732 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5733 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
5734 .addConstantPoolIndex(CPI)
5735 .addMemOperand(CPMMO));
5736 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5737 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
5738 .addReg(NewVReg1, RegState::Kill)
5739 .addImm(PCLabelId);
5740 // Set the low bit because of thumb mode.
5741 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5742 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
5743 .addReg(ARM::CPSR, RegState::Define)
5744 .addImm(1));
5745 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5746 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
5747 .addReg(ARM::CPSR, RegState::Define)
5748 .addReg(NewVReg2, RegState::Kill)
5749 .addReg(NewVReg3, RegState::Kill));
5750 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5751 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
5752 .addFrameIndex(FI)
5753 .addImm(36)); // &jbuf[1] :: pc
5754 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
5755 .addReg(NewVReg4, RegState::Kill)
5756 .addReg(NewVReg5, RegState::Kill)
5757 .addImm(0)
5758 .addMemOperand(FIMMOSt));
5759 } else {
5760 // Incoming value: jbuf
5761 // ldr r1, LCPI1_1
5762 // add r1, pc, r1
5763 // str r1, [$jbuf, #+4] ; &jbuf[1]
5764 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5765 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
5766 .addConstantPoolIndex(CPI)
5767 .addImm(0)
5768 .addMemOperand(CPMMO));
5769 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5770 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
5771 .addReg(NewVReg1, RegState::Kill)
5772 .addImm(PCLabelId));
5773 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
5774 .addReg(NewVReg2, RegState::Kill)
5775 .addFrameIndex(FI)
5776 .addImm(36) // &jbuf[1] :: pc
5777 .addMemOperand(FIMMOSt));
5778 }
5779}
5780
5781MachineBasicBlock *ARMTargetLowering::
5782EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
5783 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5784 DebugLoc dl = MI->getDebugLoc();
5785 MachineFunction *MF = MBB->getParent();
5786 MachineRegisterInfo *MRI = &MF->getRegInfo();
5787 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5788 MachineFrameInfo *MFI = MF->getFrameInfo();
5789 int FI = MFI->getFunctionContextIndex();
5790
5791 const TargetRegisterClass *TRC =
5792 Subtarget->isThumb() ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5793
Bill Wendling04f15b42011-10-06 21:29:56 +00005794 // Get a mapping of the call site numbers to all of the landing pads they're
5795 // associated with.
Bill Wendling2a850152011-10-05 00:02:33 +00005796 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
5797 unsigned MaxCSNum = 0;
5798 MachineModuleInfo &MMI = MF->getMMI();
5799 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E; ++BB) {
5800 if (!BB->isLandingPad()) continue;
5801
5802 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
5803 // pad.
5804 for (MachineBasicBlock::iterator
5805 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
5806 if (!II->isEHLabel()) continue;
5807
5808 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendling5cbef192011-10-05 23:28:57 +00005809 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling2a850152011-10-05 00:02:33 +00005810
Bill Wendling5cbef192011-10-05 23:28:57 +00005811 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
5812 for (SmallVectorImpl<unsigned>::iterator
5813 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
5814 CSI != CSE; ++CSI) {
5815 CallSiteNumToLPad[*CSI].push_back(BB);
5816 MaxCSNum = std::max(MaxCSNum, *CSI);
5817 }
Bill Wendling2a850152011-10-05 00:02:33 +00005818 break;
5819 }
5820 }
5821
5822 // Get an ordered list of the machine basic blocks for the jump table.
5823 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling2acf6382011-10-07 23:18:02 +00005824 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling2a850152011-10-05 00:02:33 +00005825 LPadList.reserve(CallSiteNumToLPad.size());
5826 for (unsigned I = 1; I <= MaxCSNum; ++I) {
5827 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
5828 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00005829 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling2a850152011-10-05 00:02:33 +00005830 LPadList.push_back(*II);
Bill Wendling2acf6382011-10-07 23:18:02 +00005831 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
5832 }
Bill Wendling2a850152011-10-05 00:02:33 +00005833 }
5834
Bill Wendling5cbef192011-10-05 23:28:57 +00005835 assert(!LPadList.empty() &&
5836 "No landing pad destinations for the dispatch jump table!");
5837
Bill Wendling04f15b42011-10-06 21:29:56 +00005838 // Create the jump table and associated information.
Bill Wendling2a850152011-10-05 00:02:33 +00005839 MachineJumpTableInfo *JTI =
5840 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
5841 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
5842 unsigned UId = AFI->createJumpTableUId();
5843
Bill Wendling04f15b42011-10-06 21:29:56 +00005844 // Create the MBBs for the dispatch code.
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005845
5846 // Shove the dispatch's address into the return slot in the function context.
5847 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
5848 DispatchBB->setIsLandingPad();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005849
Bill Wendlingbb734682011-10-05 00:39:32 +00005850 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Bill Wendling083a8eb2011-10-06 23:37:36 +00005851 BuildMI(TrapBB, dl, TII->get(Subtarget->isThumb() ? ARM::tTRAP : ARM::TRAP));
Bill Wendlingbb734682011-10-05 00:39:32 +00005852 DispatchBB->addSuccessor(TrapBB);
5853
5854 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
5855 DispatchBB->addSuccessor(DispContBB);
Bill Wendling2a850152011-10-05 00:02:33 +00005856
Bill Wendlinga48ed4f2011-10-17 21:32:56 +00005857 // Insert and MBBs.
Bill Wendling930193c2011-10-06 00:53:33 +00005858 MF->insert(MF->end(), DispatchBB);
5859 MF->insert(MF->end(), DispContBB);
5860 MF->insert(MF->end(), TrapBB);
Bill Wendling930193c2011-10-06 00:53:33 +00005861
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005862 // Insert code into the entry block that creates and registers the function
5863 // context.
5864 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
5865
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005866 MachineMemOperand *FIMMOLd =
Bill Wendling04f15b42011-10-06 21:29:56 +00005867 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendling083a8eb2011-10-06 23:37:36 +00005868 MachineMemOperand::MOLoad |
5869 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling930193c2011-10-06 00:53:33 +00005870
Bob Wilsonf4aea8f2011-12-22 23:39:48 +00005871 if (AFI->isThumb1OnlyFunction())
5872 BuildMI(DispatchBB, dl, TII->get(ARM::tInt_eh_sjlj_dispatchsetup));
5873 else if (!Subtarget->hasVFP2())
5874 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup_nofp));
5875 else
5876 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
Bob Wilsoneaab6ef2011-11-16 07:11:57 +00005877
Bill Wendling952cb502011-10-18 22:49:07 +00005878 unsigned NumLPads = LPadList.size();
Bill Wendling95ce2e92011-10-06 22:53:00 +00005879 if (Subtarget->isThumb2()) {
5880 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5881 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
5882 .addFrameIndex(FI)
5883 .addImm(4)
5884 .addMemOperand(FIMMOLd));
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005885
Bill Wendling952cb502011-10-18 22:49:07 +00005886 if (NumLPads < 256) {
5887 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
5888 .addReg(NewVReg1)
5889 .addImm(LPadList.size()));
5890 } else {
5891 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5892 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00005893 .addImm(NumLPads & 0xFFFF));
5894
5895 unsigned VReg2 = VReg1;
5896 if ((NumLPads & 0xFFFF0000) != 0) {
5897 VReg2 = MRI->createVirtualRegister(TRC);
5898 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
5899 .addReg(VReg1)
5900 .addImm(NumLPads >> 16));
5901 }
5902
Bill Wendling952cb502011-10-18 22:49:07 +00005903 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
5904 .addReg(NewVReg1)
5905 .addReg(VReg2));
5906 }
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005907
Bill Wendling95ce2e92011-10-06 22:53:00 +00005908 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
5909 .addMBB(TrapBB)
5910 .addImm(ARMCC::HI)
5911 .addReg(ARM::CPSR);
Bill Wendlingbb734682011-10-05 00:39:32 +00005912
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005913 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5914 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005915 .addJumpTableIndex(MJTI)
5916 .addImm(UId));
Bill Wendling2a850152011-10-05 00:02:33 +00005917
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005918 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005919 AddDefaultCC(
5920 AddDefaultPred(
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005921 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
5922 .addReg(NewVReg3, RegState::Kill)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005923 .addReg(NewVReg1)
5924 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
5925
5926 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005927 .addReg(NewVReg4, RegState::Kill)
Bill Wendling2a850152011-10-05 00:02:33 +00005928 .addReg(NewVReg1)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005929 .addJumpTableIndex(MJTI)
5930 .addImm(UId);
5931 } else if (Subtarget->isThumb()) {
Bill Wendling083a8eb2011-10-06 23:37:36 +00005932 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5933 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
5934 .addFrameIndex(FI)
5935 .addImm(1)
5936 .addMemOperand(FIMMOLd));
Bill Wendlingf1083d42011-10-07 22:08:37 +00005937
Bill Wendlinga5871dc2011-10-18 23:11:05 +00005938 if (NumLPads < 256) {
5939 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
5940 .addReg(NewVReg1)
5941 .addImm(NumLPads));
5942 } else {
5943 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling922ad782011-10-19 09:24:02 +00005944 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
5945 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
5946
5947 // MachineConstantPool wants an explicit alignment.
5948 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
5949 if (Align == 0)
5950 Align = getTargetData()->getTypeAllocSize(C->getType());
5951 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendlinga5871dc2011-10-18 23:11:05 +00005952
5953 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5954 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
5955 .addReg(VReg1, RegState::Define)
5956 .addConstantPoolIndex(Idx));
5957 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
5958 .addReg(NewVReg1)
5959 .addReg(VReg1));
5960 }
5961
Bill Wendling083a8eb2011-10-06 23:37:36 +00005962 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
5963 .addMBB(TrapBB)
5964 .addImm(ARMCC::HI)
5965 .addReg(ARM::CPSR);
5966
5967 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5968 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
5969 .addReg(ARM::CPSR, RegState::Define)
5970 .addReg(NewVReg1)
5971 .addImm(2));
5972
5973 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling217f0e92011-10-06 23:41:14 +00005974 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendling083a8eb2011-10-06 23:37:36 +00005975 .addJumpTableIndex(MJTI)
5976 .addImm(UId));
5977
5978 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5979 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
5980 .addReg(ARM::CPSR, RegState::Define)
5981 .addReg(NewVReg2, RegState::Kill)
5982 .addReg(NewVReg3));
5983
5984 MachineMemOperand *JTMMOLd =
5985 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
5986 MachineMemOperand::MOLoad, 4, 4);
5987
5988 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5989 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
5990 .addReg(NewVReg4, RegState::Kill)
5991 .addImm(0)
5992 .addMemOperand(JTMMOLd));
5993
5994 unsigned NewVReg6 = MRI->createVirtualRegister(TRC);
5995 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
5996 .addReg(ARM::CPSR, RegState::Define)
5997 .addReg(NewVReg5, RegState::Kill)
5998 .addReg(NewVReg3));
5999
6000 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6001 .addReg(NewVReg6, RegState::Kill)
6002 .addJumpTableIndex(MJTI)
6003 .addImm(UId);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006004 } else {
6005 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6006 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6007 .addFrameIndex(FI)
6008 .addImm(4)
6009 .addMemOperand(FIMMOLd));
Bill Wendling564392b2011-10-18 22:11:18 +00006010
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006011 if (NumLPads < 256) {
6012 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6013 .addReg(NewVReg1)
6014 .addImm(NumLPads));
Bill Wendling922ad782011-10-19 09:24:02 +00006015 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006016 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6017 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00006018 .addImm(NumLPads & 0xFFFF));
6019
6020 unsigned VReg2 = VReg1;
6021 if ((NumLPads & 0xFFFF0000) != 0) {
6022 VReg2 = MRI->createVirtualRegister(TRC);
6023 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6024 .addReg(VReg1)
6025 .addImm(NumLPads >> 16));
6026 }
6027
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006028 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6029 .addReg(NewVReg1)
6030 .addReg(VReg2));
Bill Wendling922ad782011-10-19 09:24:02 +00006031 } else {
6032 MachineConstantPool *ConstantPool = MF->getConstantPool();
6033 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6034 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6035
6036 // MachineConstantPool wants an explicit alignment.
6037 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
6038 if (Align == 0)
6039 Align = getTargetData()->getTypeAllocSize(C->getType());
6040 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6041
6042 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6043 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6044 .addReg(VReg1, RegState::Define)
Bill Wendling767f8be2011-10-20 20:37:11 +00006045 .addConstantPoolIndex(Idx)
6046 .addImm(0));
Bill Wendling922ad782011-10-19 09:24:02 +00006047 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6048 .addReg(NewVReg1)
6049 .addReg(VReg1, RegState::Kill));
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006050 }
6051
Bill Wendling95ce2e92011-10-06 22:53:00 +00006052 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6053 .addMBB(TrapBB)
6054 .addImm(ARMCC::HI)
6055 .addReg(ARM::CPSR);
Bill Wendling2a850152011-10-05 00:02:33 +00006056
Bill Wendling564392b2011-10-18 22:11:18 +00006057 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006058 AddDefaultCC(
Bill Wendling564392b2011-10-18 22:11:18 +00006059 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006060 .addReg(NewVReg1)
6061 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling564392b2011-10-18 22:11:18 +00006062 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6063 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006064 .addJumpTableIndex(MJTI)
6065 .addImm(UId));
6066
6067 MachineMemOperand *JTMMOLd =
6068 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6069 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling564392b2011-10-18 22:11:18 +00006070 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006071 AddDefaultPred(
Bill Wendling564392b2011-10-18 22:11:18 +00006072 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6073 .addReg(NewVReg3, RegState::Kill)
6074 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006075 .addImm(0)
6076 .addMemOperand(JTMMOLd));
6077
6078 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
Bill Wendling564392b2011-10-18 22:11:18 +00006079 .addReg(NewVReg5, RegState::Kill)
6080 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006081 .addJumpTableIndex(MJTI)
6082 .addImm(UId);
6083 }
Bill Wendling2a850152011-10-05 00:02:33 +00006084
Bill Wendlingbb734682011-10-05 00:39:32 +00006085 // Add the jump table entries as successors to the MBB.
Bill Wendling2acf6382011-10-07 23:18:02 +00006086 MachineBasicBlock *PrevMBB = 0;
Bill Wendlingbb734682011-10-05 00:39:32 +00006087 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006088 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6089 MachineBasicBlock *CurMBB = *I;
6090 if (PrevMBB != CurMBB)
6091 DispContBB->addSuccessor(CurMBB);
6092 PrevMBB = CurMBB;
6093 }
6094
Bill Wendling24bb9252011-10-17 05:25:09 +00006095 // N.B. the order the invoke BBs are processed in doesn't matter here.
Bill Wendling969c9ef2011-10-14 23:34:37 +00006096 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6097 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
Craig Topper015f2282012-03-04 03:33:22 +00006098 const uint16_t *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006099 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling2acf6382011-10-07 23:18:02 +00006100 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6101 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6102 MachineBasicBlock *BB = *I;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006103
6104 // Remove the landing pad successor from the invoke block and replace it
6105 // with the new dispatch block.
Bill Wendlingde39d862011-10-26 07:16:18 +00006106 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6107 BB->succ_end());
6108 while (!Successors.empty()) {
6109 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling2acf6382011-10-07 23:18:02 +00006110 if (SMBB->isLandingPad()) {
6111 BB->removeSuccessor(SMBB);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006112 MBBLPads.push_back(SMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006113 }
6114 }
6115
6116 BB->addSuccessor(DispatchBB);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006117
6118 // Find the invoke call and mark all of the callee-saved registers as
6119 // 'implicit defined' so that they're spilled. This prevents code from
6120 // moving instructions to before the EH block, where they will never be
6121 // executed.
6122 for (MachineBasicBlock::reverse_iterator
6123 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006124 if (!II->isCall()) continue;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006125
6126 DenseMap<unsigned, bool> DefRegs;
6127 for (MachineInstr::mop_iterator
6128 OI = II->operands_begin(), OE = II->operands_end();
6129 OI != OE; ++OI) {
6130 if (!OI->isReg()) continue;
6131 DefRegs[OI->getReg()] = true;
6132 }
6133
6134 MachineInstrBuilder MIB(&*II);
6135
Bill Wendling5d798592011-10-14 23:55:44 +00006136 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006137 unsigned Reg = SavedRegs[i];
6138 if (Subtarget->isThumb2() &&
6139 !ARM::tGPRRegisterClass->contains(Reg) &&
6140 !ARM::hGPRRegisterClass->contains(Reg))
6141 continue;
6142 else if (Subtarget->isThumb1Only() &&
6143 !ARM::tGPRRegisterClass->contains(Reg))
6144 continue;
6145 else if (!Subtarget->isThumb() &&
6146 !ARM::GPRRegisterClass->contains(Reg))
6147 continue;
6148 if (!DefRegs[Reg])
6149 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling5d798592011-10-14 23:55:44 +00006150 }
Bill Wendling969c9ef2011-10-14 23:34:37 +00006151
6152 break;
6153 }
Bill Wendling2acf6382011-10-07 23:18:02 +00006154 }
Bill Wendlingbb734682011-10-05 00:39:32 +00006155
Bill Wendlingf7b02072011-10-18 18:30:49 +00006156 // Mark all former landing pads as non-landing pads. The dispatch is the only
6157 // landing pad now.
6158 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6159 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6160 (*I)->setIsLandingPad(false);
6161
Bill Wendlingbb734682011-10-05 00:39:32 +00006162 // The instruction is gone now.
6163 MI->eraseFromParent();
6164
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006165 return MBB;
6166}
6167
Evan Cheng218977b2010-07-13 19:27:42 +00006168static
6169MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6170 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6171 E = MBB->succ_end(); I != E; ++I)
6172 if (*I != Succ)
6173 return *I;
6174 llvm_unreachable("Expecting a BB with two successors!");
6175}
6176
Jim Grosbache801dc42009-12-12 01:40:06 +00006177MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006178ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00006179 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006180 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00006181 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006182 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00006183 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00006184 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00006185 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00006186 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00006187 }
Jim Grosbachee2c2a42011-09-16 21:55:56 +00006188 // The Thumb2 pre-indexed stores have the same MI operands, they just
6189 // define them differently in the .td files from the isel patterns, so
6190 // they need pseudos.
6191 case ARM::t2STR_preidx:
6192 MI->setDesc(TII->get(ARM::t2STR_PRE));
6193 return BB;
6194 case ARM::t2STRB_preidx:
6195 MI->setDesc(TII->get(ARM::t2STRB_PRE));
6196 return BB;
6197 case ARM::t2STRH_preidx:
6198 MI->setDesc(TII->get(ARM::t2STRH_PRE));
6199 return BB;
6200
Jim Grosbach19dec202011-08-05 20:35:44 +00006201 case ARM::STRi_preidx:
6202 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00006203 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00006204 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
6205 // Decode the offset.
6206 unsigned Offset = MI->getOperand(4).getImm();
6207 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
6208 Offset = ARM_AM::getAM2Offset(Offset);
6209 if (isSub)
6210 Offset = -Offset;
6211
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006212 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00006213 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00006214 .addOperand(MI->getOperand(0)) // Rn_wb
6215 .addOperand(MI->getOperand(1)) // Rt
6216 .addOperand(MI->getOperand(2)) // Rn
6217 .addImm(Offset) // offset (skip GPR==zero_reg)
6218 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006219 .addOperand(MI->getOperand(6))
6220 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00006221 MI->eraseFromParent();
6222 return BB;
6223 }
6224 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00006225 case ARM::STRBr_preidx:
6226 case ARM::STRH_preidx: {
6227 unsigned NewOpc;
6228 switch (MI->getOpcode()) {
6229 default: llvm_unreachable("unexpected opcode!");
6230 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
6231 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
6232 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
6233 }
Jim Grosbach19dec202011-08-05 20:35:44 +00006234 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
6235 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
6236 MIB.addOperand(MI->getOperand(i));
6237 MI->eraseFromParent();
6238 return BB;
6239 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006240 case ARM::ATOMIC_LOAD_ADD_I8:
6241 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6242 case ARM::ATOMIC_LOAD_ADD_I16:
6243 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6244 case ARM::ATOMIC_LOAD_ADD_I32:
6245 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006246
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006247 case ARM::ATOMIC_LOAD_AND_I8:
6248 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6249 case ARM::ATOMIC_LOAD_AND_I16:
6250 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6251 case ARM::ATOMIC_LOAD_AND_I32:
6252 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006253
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006254 case ARM::ATOMIC_LOAD_OR_I8:
6255 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6256 case ARM::ATOMIC_LOAD_OR_I16:
6257 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6258 case ARM::ATOMIC_LOAD_OR_I32:
6259 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006260
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006261 case ARM::ATOMIC_LOAD_XOR_I8:
6262 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6263 case ARM::ATOMIC_LOAD_XOR_I16:
6264 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6265 case ARM::ATOMIC_LOAD_XOR_I32:
6266 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006267
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006268 case ARM::ATOMIC_LOAD_NAND_I8:
6269 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6270 case ARM::ATOMIC_LOAD_NAND_I16:
6271 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6272 case ARM::ATOMIC_LOAD_NAND_I32:
6273 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006274
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006275 case ARM::ATOMIC_LOAD_SUB_I8:
6276 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6277 case ARM::ATOMIC_LOAD_SUB_I16:
6278 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6279 case ARM::ATOMIC_LOAD_SUB_I32:
6280 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006281
Jim Grosbachf7da8822011-04-26 19:44:18 +00006282 case ARM::ATOMIC_LOAD_MIN_I8:
6283 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
6284 case ARM::ATOMIC_LOAD_MIN_I16:
6285 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
6286 case ARM::ATOMIC_LOAD_MIN_I32:
6287 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
6288
6289 case ARM::ATOMIC_LOAD_MAX_I8:
6290 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
6291 case ARM::ATOMIC_LOAD_MAX_I16:
6292 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
6293 case ARM::ATOMIC_LOAD_MAX_I32:
6294 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
6295
6296 case ARM::ATOMIC_LOAD_UMIN_I8:
6297 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
6298 case ARM::ATOMIC_LOAD_UMIN_I16:
6299 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
6300 case ARM::ATOMIC_LOAD_UMIN_I32:
6301 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
6302
6303 case ARM::ATOMIC_LOAD_UMAX_I8:
6304 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
6305 case ARM::ATOMIC_LOAD_UMAX_I16:
6306 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
6307 case ARM::ATOMIC_LOAD_UMAX_I32:
6308 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
6309
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006310 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
6311 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
6312 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00006313
6314 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
6315 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
6316 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006317
Eli Friedman2bdffe42011-08-31 00:31:29 +00006318
6319 case ARM::ATOMADD6432:
6320 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006321 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr,
6322 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006323 case ARM::ATOMSUB6432:
6324 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006325 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6326 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006327 case ARM::ATOMOR6432:
6328 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006329 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006330 case ARM::ATOMXOR6432:
6331 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006332 isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006333 case ARM::ATOMAND6432:
6334 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006335 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006336 case ARM::ATOMSWAP6432:
6337 return EmitAtomicBinary64(MI, BB, 0, 0, false);
Eli Friedman4d3f3292011-08-31 17:52:22 +00006338 case ARM::ATOMCMPXCHG6432:
6339 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
6340 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6341 /*NeedsCarry*/ false, /*IsCmpxchg*/true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006342
Evan Cheng007ea272009-08-12 05:17:19 +00006343 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00006344 // To "insert" a SELECT_CC instruction, we actually have to insert the
6345 // diamond control-flow pattern. The incoming instruction knows the
6346 // destination vreg to set, the condition code register to branch on, the
6347 // true/false values to select between, and a branch opcode to use.
6348 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006349 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00006350 ++It;
6351
6352 // thisMBB:
6353 // ...
6354 // TrueVal = ...
6355 // cmpTY ccX, r1, r2
6356 // bCC copy1MBB
6357 // fallthrough --> copy0MBB
6358 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006359 MachineFunction *F = BB->getParent();
6360 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6361 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00006362 F->insert(It, copy0MBB);
6363 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006364
6365 // Transfer the remainder of BB and its successor edges to sinkMBB.
6366 sinkMBB->splice(sinkMBB->begin(), BB,
6367 llvm::next(MachineBasicBlock::iterator(MI)),
6368 BB->end());
6369 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6370
Dan Gohman258c58c2010-07-06 15:49:48 +00006371 BB->addSuccessor(copy0MBB);
6372 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00006373
Dan Gohman14152b42010-07-06 20:24:04 +00006374 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
6375 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
6376
Evan Chenga8e29892007-01-19 07:51:42 +00006377 // copy0MBB:
6378 // %FalseValue = ...
6379 // # fallthrough to sinkMBB
6380 BB = copy0MBB;
6381
6382 // Update machine-CFG edges
6383 BB->addSuccessor(sinkMBB);
6384
6385 // sinkMBB:
6386 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6387 // ...
6388 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00006389 BuildMI(*BB, BB->begin(), dl,
6390 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00006391 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6392 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6393
Dan Gohman14152b42010-07-06 20:24:04 +00006394 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00006395 return BB;
6396 }
Evan Cheng86198642009-08-07 00:34:42 +00006397
Evan Cheng218977b2010-07-13 19:27:42 +00006398 case ARM::BCCi64:
6399 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00006400 // If there is an unconditional branch to the other successor, remove it.
6401 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00006402
Evan Cheng218977b2010-07-13 19:27:42 +00006403 // Compare both parts that make up the double comparison separately for
6404 // equality.
6405 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
6406
6407 unsigned LHS1 = MI->getOperand(1).getReg();
6408 unsigned LHS2 = MI->getOperand(2).getReg();
6409 if (RHSisZero) {
6410 AddDefaultPred(BuildMI(BB, dl,
6411 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6412 .addReg(LHS1).addImm(0));
6413 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6414 .addReg(LHS2).addImm(0)
6415 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6416 } else {
6417 unsigned RHS1 = MI->getOperand(3).getReg();
6418 unsigned RHS2 = MI->getOperand(4).getReg();
6419 AddDefaultPred(BuildMI(BB, dl,
6420 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6421 .addReg(LHS1).addReg(RHS1));
6422 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6423 .addReg(LHS2).addReg(RHS2)
6424 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6425 }
6426
6427 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
6428 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
6429 if (MI->getOperand(0).getImm() == ARMCC::NE)
6430 std::swap(destMBB, exitMBB);
6431
6432 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6433 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson51f6a7a2011-09-09 21:48:23 +00006434 if (isThumb2)
6435 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
6436 else
6437 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng218977b2010-07-13 19:27:42 +00006438
6439 MI->eraseFromParent(); // The pseudo instruction is gone now.
6440 return BB;
6441 }
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006442
Bill Wendling5bc85282011-10-17 20:37:20 +00006443 case ARM::Int_eh_sjlj_setjmp:
6444 case ARM::Int_eh_sjlj_setjmp_nofp:
6445 case ARM::tInt_eh_sjlj_setjmp:
6446 case ARM::t2Int_eh_sjlj_setjmp:
6447 case ARM::t2Int_eh_sjlj_setjmp_nofp:
6448 EmitSjLjDispatchBlock(MI, BB);
6449 return BB;
6450
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006451 case ARM::ABS:
6452 case ARM::t2ABS: {
6453 // To insert an ABS instruction, we have to insert the
6454 // diamond control-flow pattern. The incoming instruction knows the
6455 // source vreg to test against 0, the destination vreg to set,
6456 // the condition code register to branch on, the
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006457 // true/false values to select between, and a branch opcode to use.
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006458 // It transforms
6459 // V1 = ABS V0
6460 // into
6461 // V2 = MOVS V0
6462 // BCC (branch to SinkBB if V0 >= 0)
6463 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006464 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006465 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6466 MachineFunction::iterator BBI = BB;
6467 ++BBI;
6468 MachineFunction *Fn = BB->getParent();
6469 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6470 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6471 Fn->insert(BBI, RSBBB);
6472 Fn->insert(BBI, SinkBB);
6473
6474 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
6475 unsigned int ABSDstReg = MI->getOperand(0).getReg();
6476 bool isThumb2 = Subtarget->isThumb2();
6477 MachineRegisterInfo &MRI = Fn->getRegInfo();
6478 // In Thumb mode S must not be specified if source register is the SP or
6479 // PC and if destination register is the SP, so restrict register class
6480 unsigned NewMovDstReg = MRI.createVirtualRegister(
6481 isThumb2 ? ARM::rGPRRegisterClass : ARM::GPRRegisterClass);
6482 unsigned NewRsbDstReg = MRI.createVirtualRegister(
6483 isThumb2 ? ARM::rGPRRegisterClass : ARM::GPRRegisterClass);
6484
6485 // Transfer the remainder of BB and its successor edges to sinkMBB.
6486 SinkBB->splice(SinkBB->begin(), BB,
6487 llvm::next(MachineBasicBlock::iterator(MI)),
6488 BB->end());
6489 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
6490
6491 BB->addSuccessor(RSBBB);
6492 BB->addSuccessor(SinkBB);
6493
6494 // fall through to SinkMBB
6495 RSBBB->addSuccessor(SinkBB);
6496
6497 // insert a movs at the end of BB
6498 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVr : ARM::MOVr),
6499 NewMovDstReg)
6500 .addReg(ABSSrcReg, RegState::Kill)
6501 .addImm((unsigned)ARMCC::AL).addReg(0)
6502 .addReg(ARM::CPSR, RegState::Define);
6503
6504 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006505 BuildMI(BB, dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006506 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
6507 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
6508
6509 // insert rsbri in RSBBB
6510 // Note: BCC and rsbri will be converted into predicated rsbmi
6511 // by if-conversion pass
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006512 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006513 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
6514 .addReg(NewMovDstReg, RegState::Kill)
6515 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
6516
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006517 // insert PHI in SinkBB,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006518 // reuse ABSDstReg to not change uses of ABS instruction
6519 BuildMI(*SinkBB, SinkBB->begin(), dl,
6520 TII->get(ARM::PHI), ABSDstReg)
6521 .addReg(NewRsbDstReg).addMBB(RSBBB)
6522 .addReg(NewMovDstReg).addMBB(BB);
6523
6524 // remove ABS instruction
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006525 MI->eraseFromParent();
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006526
6527 // return last added BB
6528 return SinkBB;
6529 }
Evan Chenga8e29892007-01-19 07:51:42 +00006530 }
6531}
6532
Evan Cheng37fefc22011-08-30 19:09:48 +00006533void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
6534 SDNode *Node) const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006535 if (!MI->hasPostISelHook()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006536 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
6537 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
6538 return;
6539 }
6540
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006541 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick4815d562011-09-20 03:17:40 +00006542 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
6543 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
6544 // operand is still set to noreg. If needed, set the optional operand's
6545 // register to CPSR, and remove the redundant implicit def.
Andrew Trick3be654f2011-09-21 02:20:46 +00006546 //
Andrew Trick90b7b122011-10-18 19:18:52 +00006547 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick4815d562011-09-20 03:17:40 +00006548
Andrew Trick3be654f2011-09-21 02:20:46 +00006549 // Rename pseudo opcodes.
6550 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
6551 if (NewOpc) {
6552 const ARMBaseInstrInfo *TII =
6553 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick90b7b122011-10-18 19:18:52 +00006554 MCID = &TII->get(NewOpc);
6555
6556 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
6557 "converted opcode should be the same except for cc_out");
6558
6559 MI->setDesc(*MCID);
6560
6561 // Add the optional cc_out operand
6562 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick3be654f2011-09-21 02:20:46 +00006563 }
Andrew Trick90b7b122011-10-18 19:18:52 +00006564 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick4815d562011-09-20 03:17:40 +00006565
6566 // Any ARM instruction that sets the 's' bit should specify an optional
6567 // "cc_out" operand in the last operand position.
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006568 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006569 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006570 return;
6571 }
Andrew Trick3be654f2011-09-21 02:20:46 +00006572 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
6573 // since we already have an optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006574 bool definesCPSR = false;
6575 bool deadCPSR = false;
Andrew Trick90b7b122011-10-18 19:18:52 +00006576 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick4815d562011-09-20 03:17:40 +00006577 i != e; ++i) {
6578 const MachineOperand &MO = MI->getOperand(i);
6579 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
6580 definesCPSR = true;
6581 if (MO.isDead())
6582 deadCPSR = true;
6583 MI->RemoveOperand(i);
6584 break;
Evan Cheng37fefc22011-08-30 19:09:48 +00006585 }
6586 }
Andrew Trick4815d562011-09-20 03:17:40 +00006587 if (!definesCPSR) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006588 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006589 return;
6590 }
6591 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick3be654f2011-09-21 02:20:46 +00006592 if (deadCPSR) {
6593 assert(!MI->getOperand(ccOutIdx).getReg() &&
6594 "expect uninitialized optional cc_out operand");
Andrew Trick4815d562011-09-20 03:17:40 +00006595 return;
Andrew Trick3be654f2011-09-21 02:20:46 +00006596 }
Andrew Trick4815d562011-09-20 03:17:40 +00006597
Andrew Trick3be654f2011-09-21 02:20:46 +00006598 // If this instruction was defined with an optional CPSR def and its dag node
6599 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006600 MachineOperand &MO = MI->getOperand(ccOutIdx);
6601 MO.setReg(ARM::CPSR);
6602 MO.setIsDef(true);
Evan Cheng37fefc22011-08-30 19:09:48 +00006603}
6604
Evan Chenga8e29892007-01-19 07:51:42 +00006605//===----------------------------------------------------------------------===//
6606// ARM Optimization Hooks
6607//===----------------------------------------------------------------------===//
6608
Chris Lattnerd1980a52009-03-12 06:52:53 +00006609static
6610SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
6611 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00006612 SelectionDAG &DAG = DCI.DAG;
6613 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00006614 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00006615 unsigned Opc = N->getOpcode();
6616 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
6617 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
6618 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
6619 ISD::CondCode CC = ISD::SETCC_INVALID;
6620
6621 if (isSlctCC) {
6622 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
6623 } else {
6624 SDValue CCOp = Slct.getOperand(0);
6625 if (CCOp.getOpcode() == ISD::SETCC)
6626 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
6627 }
6628
6629 bool DoXform = false;
6630 bool InvCC = false;
6631 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
6632 "Bad input!");
6633
6634 if (LHS.getOpcode() == ISD::Constant &&
6635 cast<ConstantSDNode>(LHS)->isNullValue()) {
6636 DoXform = true;
6637 } else if (CC != ISD::SETCC_INVALID &&
6638 RHS.getOpcode() == ISD::Constant &&
6639 cast<ConstantSDNode>(RHS)->isNullValue()) {
6640 std::swap(LHS, RHS);
6641 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006642 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00006643 Op0.getOperand(0).getValueType();
6644 bool isInt = OpVT.isInteger();
6645 CC = ISD::getSetCCInverse(CC, isInt);
6646
6647 if (!TLI.isCondCodeLegal(CC, OpVT))
6648 return SDValue(); // Inverse operator isn't legal.
6649
6650 DoXform = true;
6651 InvCC = true;
6652 }
6653
6654 if (DoXform) {
6655 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
6656 if (isSlctCC)
6657 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
6658 Slct.getOperand(0), Slct.getOperand(1), CC);
6659 SDValue CCOp = Slct.getOperand(0);
6660 if (InvCC)
6661 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
6662 CCOp.getOperand(0), CCOp.getOperand(1), CC);
6663 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
6664 CCOp, OtherOp, Result);
6665 }
6666 return SDValue();
6667}
6668
Eric Christopherfa6f5912011-06-29 21:10:36 +00006669// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00006670// (only after legalization).
6671static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
6672 TargetLowering::DAGCombinerInfo &DCI,
6673 const ARMSubtarget *Subtarget) {
6674
6675 // Only perform optimization if after legalize, and if NEON is available. We
6676 // also expected both operands to be BUILD_VECTORs.
6677 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
6678 || N0.getOpcode() != ISD::BUILD_VECTOR
6679 || N1.getOpcode() != ISD::BUILD_VECTOR)
6680 return SDValue();
6681
6682 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
6683 EVT VT = N->getValueType(0);
6684 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
6685 return SDValue();
6686
6687 // Check that the vector operands are of the right form.
6688 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
6689 // operands, where N is the size of the formed vector.
6690 // Each EXTRACT_VECTOR should have the same input vector and odd or even
6691 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00006692
6693 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00006694 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00006695 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00006696 SDValue Vec = N0->getOperand(0)->getOperand(0);
6697 SDNode *V = Vec.getNode();
6698 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00006699
Eric Christopherfa6f5912011-06-29 21:10:36 +00006700 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00006701 // check to see if each of their operands are an EXTRACT_VECTOR with
6702 // the same vector and appropriate index.
6703 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
6704 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
6705 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00006706
Tanya Lattner189531f2011-06-14 23:48:48 +00006707 SDValue ExtVec0 = N0->getOperand(i);
6708 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00006709
Tanya Lattner189531f2011-06-14 23:48:48 +00006710 // First operand is the vector, verify its the same.
6711 if (V != ExtVec0->getOperand(0).getNode() ||
6712 V != ExtVec1->getOperand(0).getNode())
6713 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00006714
Tanya Lattner189531f2011-06-14 23:48:48 +00006715 // Second is the constant, verify its correct.
6716 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
6717 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00006718
Tanya Lattner189531f2011-06-14 23:48:48 +00006719 // For the constant, we want to see all the even or all the odd.
6720 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
6721 || C1->getZExtValue() != nextIndex+1)
6722 return SDValue();
6723
6724 // Increment index.
6725 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00006726 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00006727 return SDValue();
6728 }
6729
6730 // Create VPADDL node.
6731 SelectionDAG &DAG = DCI.DAG;
6732 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00006733
6734 // Build operand list.
6735 SmallVector<SDValue, 8> Ops;
6736 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
6737 TLI.getPointerTy()));
6738
6739 // Input is the vector.
6740 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00006741
Tanya Lattner189531f2011-06-14 23:48:48 +00006742 // Get widened type and narrowed type.
6743 MVT widenType;
6744 unsigned numElem = VT.getVectorNumElements();
6745 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
6746 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
6747 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
6748 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
6749 default:
Craig Topperbc219812012-02-07 02:50:20 +00006750 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattner189531f2011-06-14 23:48:48 +00006751 }
6752
6753 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
6754 widenType, &Ops[0], Ops.size());
6755 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
6756}
6757
Bob Wilson3d5792a2010-07-29 20:34:14 +00006758/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
6759/// operands N0 and N1. This is a helper for PerformADDCombine that is
6760/// called with the default operands, and if that fails, with commuted
6761/// operands.
6762static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00006763 TargetLowering::DAGCombinerInfo &DCI,
6764 const ARMSubtarget *Subtarget){
6765
6766 // Attempt to create vpaddl for this add.
6767 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
6768 if (Result.getNode())
6769 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00006770
Chris Lattnerd1980a52009-03-12 06:52:53 +00006771 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
6772 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
6773 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
6774 if (Result.getNode()) return Result;
6775 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00006776 return SDValue();
6777}
6778
Bob Wilson3d5792a2010-07-29 20:34:14 +00006779/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
6780///
6781static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00006782 TargetLowering::DAGCombinerInfo &DCI,
6783 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00006784 SDValue N0 = N->getOperand(0);
6785 SDValue N1 = N->getOperand(1);
6786
6787 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00006788 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00006789 if (Result.getNode())
6790 return Result;
6791
6792 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00006793 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00006794}
6795
Chris Lattnerd1980a52009-03-12 06:52:53 +00006796/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00006797///
Chris Lattnerd1980a52009-03-12 06:52:53 +00006798static SDValue PerformSUBCombine(SDNode *N,
6799 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00006800 SDValue N0 = N->getOperand(0);
6801 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00006802
Chris Lattnerd1980a52009-03-12 06:52:53 +00006803 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
6804 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
6805 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
6806 if (Result.getNode()) return Result;
6807 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00006808
Chris Lattnerd1980a52009-03-12 06:52:53 +00006809 return SDValue();
6810}
6811
Evan Cheng463d3582011-03-31 19:38:48 +00006812/// PerformVMULCombine
6813/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
6814/// special multiplier accumulator forwarding.
6815/// vmul d3, d0, d2
6816/// vmla d3, d1, d2
6817/// is faster than
6818/// vadd d3, d0, d1
6819/// vmul d3, d3, d2
6820static SDValue PerformVMULCombine(SDNode *N,
6821 TargetLowering::DAGCombinerInfo &DCI,
6822 const ARMSubtarget *Subtarget) {
6823 if (!Subtarget->hasVMLxForwarding())
6824 return SDValue();
6825
6826 SelectionDAG &DAG = DCI.DAG;
6827 SDValue N0 = N->getOperand(0);
6828 SDValue N1 = N->getOperand(1);
6829 unsigned Opcode = N0.getOpcode();
6830 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
6831 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00006832 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00006833 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
6834 Opcode != ISD::FADD && Opcode != ISD::FSUB)
6835 return SDValue();
6836 std::swap(N0, N1);
6837 }
6838
6839 EVT VT = N->getValueType(0);
6840 DebugLoc DL = N->getDebugLoc();
6841 SDValue N00 = N0->getOperand(0);
6842 SDValue N01 = N0->getOperand(1);
6843 return DAG.getNode(Opcode, DL, VT,
6844 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
6845 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
6846}
6847
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006848static SDValue PerformMULCombine(SDNode *N,
6849 TargetLowering::DAGCombinerInfo &DCI,
6850 const ARMSubtarget *Subtarget) {
6851 SelectionDAG &DAG = DCI.DAG;
6852
6853 if (Subtarget->isThumb1Only())
6854 return SDValue();
6855
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006856 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
6857 return SDValue();
6858
6859 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00006860 if (VT.is64BitVector() || VT.is128BitVector())
6861 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006862 if (VT != MVT::i32)
6863 return SDValue();
6864
6865 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
6866 if (!C)
6867 return SDValue();
6868
6869 uint64_t MulAmt = C->getZExtValue();
6870 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
6871 ShiftAmt = ShiftAmt & (32 - 1);
6872 SDValue V = N->getOperand(0);
6873 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006874
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006875 SDValue Res;
6876 MulAmt >>= ShiftAmt;
6877 if (isPowerOf2_32(MulAmt - 1)) {
6878 // (mul x, 2^N + 1) => (add (shl x, N), x)
6879 Res = DAG.getNode(ISD::ADD, DL, VT,
6880 V, DAG.getNode(ISD::SHL, DL, VT,
6881 V, DAG.getConstant(Log2_32(MulAmt-1),
6882 MVT::i32)));
6883 } else if (isPowerOf2_32(MulAmt + 1)) {
6884 // (mul x, 2^N - 1) => (sub (shl x, N), x)
6885 Res = DAG.getNode(ISD::SUB, DL, VT,
6886 DAG.getNode(ISD::SHL, DL, VT,
6887 V, DAG.getConstant(Log2_32(MulAmt+1),
6888 MVT::i32)),
6889 V);
6890 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006891 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006892
6893 if (ShiftAmt != 0)
6894 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
6895 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006896
6897 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006898 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006899 return SDValue();
6900}
6901
Evan Chengc892aeb2012-02-23 01:19:06 +00006902static bool isCMOVWithZeroOrAllOnesLHS(SDValue N, bool AllOnes) {
6903 if (N.getOpcode() != ARMISD::CMOV || !N.getNode()->hasOneUse())
6904 return false;
6905
6906 SDValue FalseVal = N.getOperand(0);
6907 ConstantSDNode *C = dyn_cast<ConstantSDNode>(FalseVal);
6908 if (!C)
6909 return false;
6910 if (AllOnes)
6911 return C->isAllOnesValue();
6912 return C->isNullValue();
6913}
6914
6915/// formConditionalOp - Combine an operation with a conditional move operand
6916/// to form a conditional op. e.g. (or x, (cmov 0, y, cond)) => (or.cond x, y)
6917/// (and x, (cmov -1, y, cond)) => (and.cond, x, y)
6918static SDValue formConditionalOp(SDNode *N, SelectionDAG &DAG,
6919 bool Commutable) {
6920 SDValue N0 = N->getOperand(0);
6921 SDValue N1 = N->getOperand(1);
6922
6923 bool isAND = N->getOpcode() == ISD::AND;
6924 bool isCand = isCMOVWithZeroOrAllOnesLHS(N1, isAND);
6925 if (!isCand && Commutable) {
6926 isCand = isCMOVWithZeroOrAllOnesLHS(N0, isAND);
6927 if (isCand)
6928 std::swap(N0, N1);
6929 }
6930 if (!isCand)
6931 return SDValue();
6932
6933 unsigned Opc = 0;
6934 switch (N->getOpcode()) {
6935 default: llvm_unreachable("Unexpected node");
6936 case ISD::AND: Opc = ARMISD::CAND; break;
6937 case ISD::OR: Opc = ARMISD::COR; break;
6938 case ISD::XOR: Opc = ARMISD::CXOR; break;
6939 }
6940 return DAG.getNode(Opc, N->getDebugLoc(), N->getValueType(0), N0,
6941 N1.getOperand(1), N1.getOperand(2), N1.getOperand(3),
6942 N1.getOperand(4));
6943}
6944
Owen Anderson080c0922010-11-05 19:27:46 +00006945static SDValue PerformANDCombine(SDNode *N,
Evan Chengc892aeb2012-02-23 01:19:06 +00006946 TargetLowering::DAGCombinerInfo &DCI,
6947 const ARMSubtarget *Subtarget) {
Owen Anderson76706012011-04-05 21:48:57 +00006948
Owen Anderson080c0922010-11-05 19:27:46 +00006949 // Attempt to use immediate-form VBIC
6950 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
6951 DebugLoc dl = N->getDebugLoc();
6952 EVT VT = N->getValueType(0);
6953 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006954
Tanya Lattner0433b212011-04-07 15:24:20 +00006955 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
6956 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00006957
Owen Anderson080c0922010-11-05 19:27:46 +00006958 APInt SplatBits, SplatUndef;
6959 unsigned SplatBitSize;
6960 bool HasAnyUndefs;
6961 if (BVN &&
6962 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
6963 if (SplatBitSize <= 64) {
6964 EVT VbicVT;
6965 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
6966 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006967 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00006968 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00006969 if (Val.getNode()) {
6970 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006971 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00006972 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006973 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00006974 }
6975 }
6976 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006977
Evan Chengc892aeb2012-02-23 01:19:06 +00006978 if (!Subtarget->isThumb1Only()) {
6979 // (and x, (cmov -1, y, cond)) => (and.cond x, y)
6980 SDValue CAND = formConditionalOp(N, DAG, true);
6981 if (CAND.getNode())
6982 return CAND;
6983 }
6984
Owen Anderson080c0922010-11-05 19:27:46 +00006985 return SDValue();
6986}
6987
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006988/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
6989static SDValue PerformORCombine(SDNode *N,
6990 TargetLowering::DAGCombinerInfo &DCI,
6991 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00006992 // Attempt to use immediate-form VORR
6993 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
6994 DebugLoc dl = N->getDebugLoc();
6995 EVT VT = N->getValueType(0);
6996 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006997
Tanya Lattner0433b212011-04-07 15:24:20 +00006998 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
6999 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00007000
Owen Anderson60f48702010-11-03 23:15:26 +00007001 APInt SplatBits, SplatUndef;
7002 unsigned SplatBitSize;
7003 bool HasAnyUndefs;
7004 if (BVN && Subtarget->hasNEON() &&
7005 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7006 if (SplatBitSize <= 64) {
7007 EVT VorrVT;
7008 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
7009 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00007010 DAG, VorrVT, VT.is128BitVector(),
7011 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00007012 if (Val.getNode()) {
7013 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007014 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00007015 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007016 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00007017 }
7018 }
7019 }
7020
Evan Chengc892aeb2012-02-23 01:19:06 +00007021 if (!Subtarget->isThumb1Only()) {
7022 // (or x, (cmov 0, y, cond)) => (or.cond x, y)
7023 SDValue COR = formConditionalOp(N, DAG, true);
7024 if (COR.getNode())
7025 return COR;
7026 }
7027
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007028 SDValue N0 = N->getOperand(0);
7029 if (N0.getOpcode() != ISD::AND)
7030 return SDValue();
7031 SDValue N1 = N->getOperand(1);
7032
7033 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
7034 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
7035 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
7036 APInt SplatUndef;
7037 unsigned SplatBitSize;
7038 bool HasAnyUndefs;
7039
7040 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
7041 APInt SplatBits0;
7042 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
7043 HasAnyUndefs) && !HasAnyUndefs) {
7044 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
7045 APInt SplatBits1;
7046 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
7047 HasAnyUndefs) && !HasAnyUndefs &&
7048 SplatBits0 == ~SplatBits1) {
7049 // Canonicalize the vector type to make instruction selection simpler.
7050 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
7051 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
7052 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00007053 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007054 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
7055 }
7056 }
7057 }
7058
Jim Grosbach54238562010-07-17 03:30:54 +00007059 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
7060 // reasonable.
7061
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007062 // BFI is only available on V6T2+
7063 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
7064 return SDValue();
7065
Jim Grosbach54238562010-07-17 03:30:54 +00007066 DebugLoc DL = N->getDebugLoc();
7067 // 1) or (and A, mask), val => ARMbfi A, val, mask
7068 // iff (val & mask) == val
7069 //
7070 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
7071 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00007072 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00007073 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00007074 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00007075 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007076
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007077 if (VT != MVT::i32)
7078 return SDValue();
7079
Evan Cheng30fb13f2010-12-13 20:32:54 +00007080 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00007081
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007082 // The value and the mask need to be constants so we can verify this is
7083 // actually a bitfield set. If the mask is 0xffff, we can do better
7084 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00007085 SDValue MaskOp = N0.getOperand(1);
7086 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
7087 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007088 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00007089 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007090 if (Mask == 0xffff)
7091 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007092 SDValue Res;
7093 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00007094 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
7095 if (N1C) {
7096 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00007097 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00007098 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007099
Evan Chenga9688c42010-12-11 04:11:38 +00007100 if (ARM::isBitFieldInvertedMask(Mask)) {
7101 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007102
Evan Cheng30fb13f2010-12-13 20:32:54 +00007103 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00007104 DAG.getConstant(Val, MVT::i32),
7105 DAG.getConstant(Mask, MVT::i32));
7106
7107 // Do not add new nodes to DAG combiner worklist.
7108 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007109 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00007110 }
Jim Grosbach54238562010-07-17 03:30:54 +00007111 } else if (N1.getOpcode() == ISD::AND) {
7112 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00007113 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7114 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00007115 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00007116 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007117
Eric Christopher29aeed12011-03-26 01:21:03 +00007118 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
7119 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00007120 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007121 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007122 // The pack halfword instruction works better for masks that fit it,
7123 // so use that when it's available.
7124 if (Subtarget->hasT2ExtractPack() &&
7125 (Mask == 0xffff || Mask == 0xffff0000))
7126 return SDValue();
7127 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00007128 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00007129 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00007130 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00007131 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00007132 DAG.getConstant(Mask, MVT::i32));
7133 // Do not add new nodes to DAG combiner worklist.
7134 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007135 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007136 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007137 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007138 // The pack halfword instruction works better for masks that fit it,
7139 // so use that when it's available.
7140 if (Subtarget->hasT2ExtractPack() &&
7141 (Mask2 == 0xffff || Mask2 == 0xffff0000))
7142 return SDValue();
7143 // 2b
7144 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007145 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00007146 DAG.getConstant(lsb, MVT::i32));
7147 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00007148 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00007149 // Do not add new nodes to DAG combiner worklist.
7150 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007151 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007152 }
7153 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007154
Evan Cheng30fb13f2010-12-13 20:32:54 +00007155 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
7156 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
7157 ARM::isBitFieldInvertedMask(~Mask)) {
7158 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
7159 // where lsb(mask) == #shamt and masked bits of B are known zero.
7160 SDValue ShAmt = N00.getOperand(1);
7161 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
7162 unsigned LSB = CountTrailingZeros_32(Mask);
7163 if (ShAmtC != LSB)
7164 return SDValue();
7165
7166 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
7167 DAG.getConstant(~Mask, MVT::i32));
7168
7169 // Do not add new nodes to DAG combiner worklist.
7170 DCI.CombineTo(N, Res, false);
7171 }
7172
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007173 return SDValue();
7174}
7175
Evan Chengc892aeb2012-02-23 01:19:06 +00007176static SDValue PerformXORCombine(SDNode *N,
7177 TargetLowering::DAGCombinerInfo &DCI,
7178 const ARMSubtarget *Subtarget) {
7179 EVT VT = N->getValueType(0);
7180 SelectionDAG &DAG = DCI.DAG;
7181
7182 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7183 return SDValue();
7184
7185 if (!Subtarget->isThumb1Only()) {
7186 // (xor x, (cmov 0, y, cond)) => (xor.cond x, y)
7187 SDValue CXOR = formConditionalOp(N, DAG, true);
7188 if (CXOR.getNode())
7189 return CXOR;
7190 }
7191
7192 return SDValue();
7193}
7194
Evan Chengbf188ae2011-06-15 01:12:31 +00007195/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
7196/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00007197static SDValue PerformBFICombine(SDNode *N,
7198 TargetLowering::DAGCombinerInfo &DCI) {
7199 SDValue N1 = N->getOperand(1);
7200 if (N1.getOpcode() == ISD::AND) {
7201 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7202 if (!N11C)
7203 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007204 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
7205 unsigned LSB = CountTrailingZeros_32(~InvMask);
7206 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
7207 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00007208 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007209 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00007210 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
7211 N->getOperand(0), N1.getOperand(0),
7212 N->getOperand(2));
7213 }
7214 return SDValue();
7215}
7216
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007217/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
7218/// ARMISD::VMOVRRD.
7219static SDValue PerformVMOVRRDCombine(SDNode *N,
7220 TargetLowering::DAGCombinerInfo &DCI) {
7221 // vmovrrd(vmovdrr x, y) -> x,y
7222 SDValue InDouble = N->getOperand(0);
7223 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
7224 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00007225
7226 // vmovrrd(load f64) -> (load i32), (load i32)
7227 SDNode *InNode = InDouble.getNode();
7228 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
7229 InNode->getValueType(0) == MVT::f64 &&
7230 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
7231 !cast<LoadSDNode>(InNode)->isVolatile()) {
7232 // TODO: Should this be done for non-FrameIndex operands?
7233 LoadSDNode *LD = cast<LoadSDNode>(InNode);
7234
7235 SelectionDAG &DAG = DCI.DAG;
7236 DebugLoc DL = LD->getDebugLoc();
7237 SDValue BasePtr = LD->getBasePtr();
7238 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
7239 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007240 LD->isNonTemporal(), LD->isInvariant(),
7241 LD->getAlignment());
Cameron Zwarich4071a712011-04-02 02:40:43 +00007242
7243 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7244 DAG.getConstant(4, MVT::i32));
7245 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
7246 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007247 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich4071a712011-04-02 02:40:43 +00007248 std::min(4U, LD->getAlignment() / 2));
7249
7250 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
7251 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
7252 DCI.RemoveFromWorklist(LD);
7253 DAG.DeleteNode(LD);
7254 return Result;
7255 }
7256
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007257 return SDValue();
7258}
7259
7260/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
7261/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
7262static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
7263 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
7264 SDValue Op0 = N->getOperand(0);
7265 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007266 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007267 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007268 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007269 Op1 = Op1.getOperand(0);
7270 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
7271 Op0.getNode() == Op1.getNode() &&
7272 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007273 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007274 N->getValueType(0), Op0.getOperand(0));
7275 return SDValue();
7276}
7277
Bob Wilson31600902010-12-21 06:43:19 +00007278/// PerformSTORECombine - Target-specific dag combine xforms for
7279/// ISD::STORE.
7280static SDValue PerformSTORECombine(SDNode *N,
7281 TargetLowering::DAGCombinerInfo &DCI) {
7282 // Bitcast an i64 store extracted from a vector to f64.
7283 // Otherwise, the i64 value will be legalized to a pair of i32 values.
7284 StoreSDNode *St = cast<StoreSDNode>(N);
7285 SDValue StVal = St->getValue();
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007286 if (!ISD::isNormalStore(St) || St->isVolatile())
7287 return SDValue();
7288
7289 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
7290 StVal.getNode()->hasOneUse() && !St->isVolatile()) {
7291 SelectionDAG &DAG = DCI.DAG;
7292 DebugLoc DL = St->getDebugLoc();
7293 SDValue BasePtr = St->getBasePtr();
7294 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
7295 StVal.getNode()->getOperand(0), BasePtr,
7296 St->getPointerInfo(), St->isVolatile(),
7297 St->isNonTemporal(), St->getAlignment());
7298
7299 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7300 DAG.getConstant(4, MVT::i32));
7301 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
7302 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
7303 St->isNonTemporal(),
7304 std::min(4U, St->getAlignment() / 2));
7305 }
7306
7307 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00007308 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
7309 return SDValue();
7310
7311 SelectionDAG &DAG = DCI.DAG;
7312 DebugLoc dl = StVal.getDebugLoc();
7313 SDValue IntVec = StVal.getOperand(0);
7314 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7315 IntVec.getValueType().getVectorNumElements());
7316 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
7317 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
7318 Vec, StVal.getOperand(1));
7319 dl = N->getDebugLoc();
7320 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
7321 // Make the DAGCombiner fold the bitcasts.
7322 DCI.AddToWorklist(Vec.getNode());
7323 DCI.AddToWorklist(ExtElt.getNode());
7324 DCI.AddToWorklist(V.getNode());
7325 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
7326 St->getPointerInfo(), St->isVolatile(),
7327 St->isNonTemporal(), St->getAlignment(),
7328 St->getTBAAInfo());
7329}
7330
7331/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
7332/// are normal, non-volatile loads. If so, it is profitable to bitcast an
7333/// i64 vector to have f64 elements, since the value can then be loaded
7334/// directly into a VFP register.
7335static bool hasNormalLoadOperand(SDNode *N) {
7336 unsigned NumElts = N->getValueType(0).getVectorNumElements();
7337 for (unsigned i = 0; i < NumElts; ++i) {
7338 SDNode *Elt = N->getOperand(i).getNode();
7339 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
7340 return true;
7341 }
7342 return false;
7343}
7344
Bob Wilson75f02882010-09-17 22:59:05 +00007345/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
7346/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00007347static SDValue PerformBUILD_VECTORCombine(SDNode *N,
7348 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00007349 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
7350 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
7351 // into a pair of GPRs, which is fine when the value is used as a scalar,
7352 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00007353 SelectionDAG &DAG = DCI.DAG;
7354 if (N->getNumOperands() == 2) {
7355 SDValue RV = PerformVMOVDRRCombine(N, DAG);
7356 if (RV.getNode())
7357 return RV;
7358 }
Bob Wilson75f02882010-09-17 22:59:05 +00007359
Bob Wilson31600902010-12-21 06:43:19 +00007360 // Load i64 elements as f64 values so that type legalization does not split
7361 // them up into i32 values.
7362 EVT VT = N->getValueType(0);
7363 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
7364 return SDValue();
7365 DebugLoc dl = N->getDebugLoc();
7366 SmallVector<SDValue, 8> Ops;
7367 unsigned NumElts = VT.getVectorNumElements();
7368 for (unsigned i = 0; i < NumElts; ++i) {
7369 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
7370 Ops.push_back(V);
7371 // Make the DAGCombiner fold the bitcast.
7372 DCI.AddToWorklist(V.getNode());
7373 }
7374 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
7375 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
7376 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
7377}
7378
7379/// PerformInsertEltCombine - Target-specific dag combine xforms for
7380/// ISD::INSERT_VECTOR_ELT.
7381static SDValue PerformInsertEltCombine(SDNode *N,
7382 TargetLowering::DAGCombinerInfo &DCI) {
7383 // Bitcast an i64 load inserted into a vector to f64.
7384 // Otherwise, the i64 value will be legalized to a pair of i32 values.
7385 EVT VT = N->getValueType(0);
7386 SDNode *Elt = N->getOperand(1).getNode();
7387 if (VT.getVectorElementType() != MVT::i64 ||
7388 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
7389 return SDValue();
7390
7391 SelectionDAG &DAG = DCI.DAG;
7392 DebugLoc dl = N->getDebugLoc();
7393 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7394 VT.getVectorNumElements());
7395 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
7396 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
7397 // Make the DAGCombiner fold the bitcasts.
7398 DCI.AddToWorklist(Vec.getNode());
7399 DCI.AddToWorklist(V.getNode());
7400 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
7401 Vec, V, N->getOperand(2));
7402 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00007403}
7404
Bob Wilsonf20700c2010-10-27 20:38:28 +00007405/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
7406/// ISD::VECTOR_SHUFFLE.
7407static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
7408 // The LLVM shufflevector instruction does not require the shuffle mask
7409 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
7410 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
7411 // operands do not match the mask length, they are extended by concatenating
7412 // them with undef vectors. That is probably the right thing for other
7413 // targets, but for NEON it is better to concatenate two double-register
7414 // size vector operands into a single quad-register size vector. Do that
7415 // transformation here:
7416 // shuffle(concat(v1, undef), concat(v2, undef)) ->
7417 // shuffle(concat(v1, v2), undef)
7418 SDValue Op0 = N->getOperand(0);
7419 SDValue Op1 = N->getOperand(1);
7420 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
7421 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
7422 Op0.getNumOperands() != 2 ||
7423 Op1.getNumOperands() != 2)
7424 return SDValue();
7425 SDValue Concat0Op1 = Op0.getOperand(1);
7426 SDValue Concat1Op1 = Op1.getOperand(1);
7427 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
7428 Concat1Op1.getOpcode() != ISD::UNDEF)
7429 return SDValue();
7430 // Skip the transformation if any of the types are illegal.
7431 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7432 EVT VT = N->getValueType(0);
7433 if (!TLI.isTypeLegal(VT) ||
7434 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
7435 !TLI.isTypeLegal(Concat1Op1.getValueType()))
7436 return SDValue();
7437
7438 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
7439 Op0.getOperand(0), Op1.getOperand(0));
7440 // Translate the shuffle mask.
7441 SmallVector<int, 16> NewMask;
7442 unsigned NumElts = VT.getVectorNumElements();
7443 unsigned HalfElts = NumElts/2;
7444 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
7445 for (unsigned n = 0; n < NumElts; ++n) {
7446 int MaskElt = SVN->getMaskElt(n);
7447 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007448 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00007449 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007450 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00007451 NewElt = HalfElts + MaskElt - NumElts;
7452 NewMask.push_back(NewElt);
7453 }
7454 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
7455 DAG.getUNDEF(VT), NewMask.data());
7456}
7457
Bob Wilson1c3ef902011-02-07 17:43:21 +00007458/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
7459/// NEON load/store intrinsics to merge base address updates.
7460static SDValue CombineBaseUpdate(SDNode *N,
7461 TargetLowering::DAGCombinerInfo &DCI) {
7462 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7463 return SDValue();
7464
7465 SelectionDAG &DAG = DCI.DAG;
7466 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
7467 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
7468 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
7469 SDValue Addr = N->getOperand(AddrOpIdx);
7470
7471 // Search for a use of the address operand that is an increment.
7472 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
7473 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
7474 SDNode *User = *UI;
7475 if (User->getOpcode() != ISD::ADD ||
7476 UI.getUse().getResNo() != Addr.getResNo())
7477 continue;
7478
7479 // Check that the add is independent of the load/store. Otherwise, folding
7480 // it would create a cycle.
7481 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
7482 continue;
7483
7484 // Find the new opcode for the updating load/store.
7485 bool isLoad = true;
7486 bool isLaneOp = false;
7487 unsigned NewOpc = 0;
7488 unsigned NumVecs = 0;
7489 if (isIntrinsic) {
7490 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
7491 switch (IntNo) {
Craig Topperbc219812012-02-07 02:50:20 +00007492 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00007493 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
7494 NumVecs = 1; break;
7495 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
7496 NumVecs = 2; break;
7497 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
7498 NumVecs = 3; break;
7499 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
7500 NumVecs = 4; break;
7501 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
7502 NumVecs = 2; isLaneOp = true; break;
7503 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
7504 NumVecs = 3; isLaneOp = true; break;
7505 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
7506 NumVecs = 4; isLaneOp = true; break;
7507 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
7508 NumVecs = 1; isLoad = false; break;
7509 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
7510 NumVecs = 2; isLoad = false; break;
7511 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
7512 NumVecs = 3; isLoad = false; break;
7513 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
7514 NumVecs = 4; isLoad = false; break;
7515 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
7516 NumVecs = 2; isLoad = false; isLaneOp = true; break;
7517 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
7518 NumVecs = 3; isLoad = false; isLaneOp = true; break;
7519 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
7520 NumVecs = 4; isLoad = false; isLaneOp = true; break;
7521 }
7522 } else {
7523 isLaneOp = true;
7524 switch (N->getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00007525 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00007526 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
7527 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
7528 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
7529 }
7530 }
7531
7532 // Find the size of memory referenced by the load/store.
7533 EVT VecTy;
7534 if (isLoad)
7535 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00007536 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00007537 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
7538 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
7539 if (isLaneOp)
7540 NumBytes /= VecTy.getVectorNumElements();
7541
7542 // If the increment is a constant, it must match the memory ref size.
7543 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
7544 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
7545 uint64_t IncVal = CInc->getZExtValue();
7546 if (IncVal != NumBytes)
7547 continue;
7548 } else if (NumBytes >= 3 * 16) {
7549 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
7550 // separate instructions that make it harder to use a non-constant update.
7551 continue;
7552 }
7553
7554 // Create the new updating load/store node.
7555 EVT Tys[6];
7556 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
7557 unsigned n;
7558 for (n = 0; n < NumResultVecs; ++n)
7559 Tys[n] = VecTy;
7560 Tys[n++] = MVT::i32;
7561 Tys[n] = MVT::Other;
7562 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
7563 SmallVector<SDValue, 8> Ops;
7564 Ops.push_back(N->getOperand(0)); // incoming chain
7565 Ops.push_back(N->getOperand(AddrOpIdx));
7566 Ops.push_back(Inc);
7567 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
7568 Ops.push_back(N->getOperand(i));
7569 }
7570 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
7571 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
7572 Ops.data(), Ops.size(),
7573 MemInt->getMemoryVT(),
7574 MemInt->getMemOperand());
7575
7576 // Update the uses.
7577 std::vector<SDValue> NewResults;
7578 for (unsigned i = 0; i < NumResultVecs; ++i) {
7579 NewResults.push_back(SDValue(UpdN.getNode(), i));
7580 }
7581 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
7582 DCI.CombineTo(N, NewResults);
7583 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
7584
7585 break;
Owen Anderson76706012011-04-05 21:48:57 +00007586 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00007587 return SDValue();
7588}
7589
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007590/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
7591/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
7592/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
7593/// return true.
7594static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
7595 SelectionDAG &DAG = DCI.DAG;
7596 EVT VT = N->getValueType(0);
7597 // vldN-dup instructions only support 64-bit vectors for N > 1.
7598 if (!VT.is64BitVector())
7599 return false;
7600
7601 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
7602 SDNode *VLD = N->getOperand(0).getNode();
7603 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
7604 return false;
7605 unsigned NumVecs = 0;
7606 unsigned NewOpc = 0;
7607 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
7608 if (IntNo == Intrinsic::arm_neon_vld2lane) {
7609 NumVecs = 2;
7610 NewOpc = ARMISD::VLD2DUP;
7611 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
7612 NumVecs = 3;
7613 NewOpc = ARMISD::VLD3DUP;
7614 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
7615 NumVecs = 4;
7616 NewOpc = ARMISD::VLD4DUP;
7617 } else {
7618 return false;
7619 }
7620
7621 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
7622 // numbers match the load.
7623 unsigned VLDLaneNo =
7624 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
7625 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
7626 UI != UE; ++UI) {
7627 // Ignore uses of the chain result.
7628 if (UI.getUse().getResNo() == NumVecs)
7629 continue;
7630 SDNode *User = *UI;
7631 if (User->getOpcode() != ARMISD::VDUPLANE ||
7632 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
7633 return false;
7634 }
7635
7636 // Create the vldN-dup node.
7637 EVT Tys[5];
7638 unsigned n;
7639 for (n = 0; n < NumVecs; ++n)
7640 Tys[n] = VT;
7641 Tys[n] = MVT::Other;
7642 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
7643 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
7644 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
7645 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
7646 Ops, 2, VLDMemInt->getMemoryVT(),
7647 VLDMemInt->getMemOperand());
7648
7649 // Update the uses.
7650 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
7651 UI != UE; ++UI) {
7652 unsigned ResNo = UI.getUse().getResNo();
7653 // Ignore uses of the chain result.
7654 if (ResNo == NumVecs)
7655 continue;
7656 SDNode *User = *UI;
7657 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
7658 }
7659
7660 // Now the vldN-lane intrinsic is dead except for its chain result.
7661 // Update uses of the chain.
7662 std::vector<SDValue> VLDDupResults;
7663 for (unsigned n = 0; n < NumVecs; ++n)
7664 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
7665 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
7666 DCI.CombineTo(VLD, VLDDupResults);
7667
7668 return true;
7669}
7670
Bob Wilson9e82bf12010-07-14 01:22:12 +00007671/// PerformVDUPLANECombine - Target-specific dag combine xforms for
7672/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007673static SDValue PerformVDUPLANECombine(SDNode *N,
7674 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00007675 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007676
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007677 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
7678 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
7679 if (CombineVLDDUP(N, DCI))
7680 return SDValue(N, 0);
7681
7682 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
7683 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007684 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00007685 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00007686 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00007687 return SDValue();
7688
7689 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
7690 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
7691 // The canonical VMOV for a zero vector uses a 32-bit element size.
7692 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7693 unsigned EltBits;
7694 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
7695 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007696 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007697 if (EltSize > VT.getVectorElementType().getSizeInBits())
7698 return SDValue();
7699
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007700 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007701}
7702
Eric Christopherfa6f5912011-06-29 21:10:36 +00007703// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00007704// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
7705static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
7706{
Chad Rosier118c9a02011-06-28 17:26:57 +00007707 integerPart cN;
7708 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00007709 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
7710 I != E; I++) {
7711 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
7712 if (!C)
7713 return false;
7714
Eric Christopherfa6f5912011-06-29 21:10:36 +00007715 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00007716 APFloat APF = C->getValueAPF();
7717 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
7718 != APFloat::opOK || !isExact)
7719 return false;
7720
7721 c0 = (I == 0) ? cN : c0;
7722 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
7723 return false;
7724 }
7725 C = c0;
7726 return true;
7727}
7728
7729/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
7730/// can replace combinations of VMUL and VCVT (floating-point to integer)
7731/// when the VMUL has a constant operand that is a power of 2.
7732///
7733/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
7734/// vmul.f32 d16, d17, d16
7735/// vcvt.s32.f32 d16, d16
7736/// becomes:
7737/// vcvt.s32.f32 d16, d16, #3
7738static SDValue PerformVCVTCombine(SDNode *N,
7739 TargetLowering::DAGCombinerInfo &DCI,
7740 const ARMSubtarget *Subtarget) {
7741 SelectionDAG &DAG = DCI.DAG;
7742 SDValue Op = N->getOperand(0);
7743
7744 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
7745 Op.getOpcode() != ISD::FMUL)
7746 return SDValue();
7747
7748 uint64_t C;
7749 SDValue N0 = Op->getOperand(0);
7750 SDValue ConstVec = Op->getOperand(1);
7751 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
7752
Eric Christopherfa6f5912011-06-29 21:10:36 +00007753 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00007754 !isConstVecPow2(ConstVec, isSigned, C))
7755 return SDValue();
7756
7757 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
7758 Intrinsic::arm_neon_vcvtfp2fxu;
7759 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7760 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00007761 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00007762 DAG.getConstant(Log2_64(C), MVT::i32));
7763}
7764
7765/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
7766/// can replace combinations of VCVT (integer to floating-point) and VDIV
7767/// when the VDIV has a constant operand that is a power of 2.
7768///
7769/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
7770/// vcvt.f32.s32 d16, d16
7771/// vdiv.f32 d16, d17, d16
7772/// becomes:
7773/// vcvt.f32.s32 d16, d16, #3
7774static SDValue PerformVDIVCombine(SDNode *N,
7775 TargetLowering::DAGCombinerInfo &DCI,
7776 const ARMSubtarget *Subtarget) {
7777 SelectionDAG &DAG = DCI.DAG;
7778 SDValue Op = N->getOperand(0);
7779 unsigned OpOpcode = Op.getNode()->getOpcode();
7780
7781 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
7782 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
7783 return SDValue();
7784
7785 uint64_t C;
7786 SDValue ConstVec = N->getOperand(1);
7787 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
7788
7789 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
7790 !isConstVecPow2(ConstVec, isSigned, C))
7791 return SDValue();
7792
Eric Christopherfa6f5912011-06-29 21:10:36 +00007793 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00007794 Intrinsic::arm_neon_vcvtfxu2fp;
7795 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7796 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00007797 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00007798 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
7799}
7800
7801/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00007802/// operand of a vector shift operation, where all the elements of the
7803/// build_vector must have the same constant integer value.
7804static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
7805 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007806 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00007807 Op = Op.getOperand(0);
7808 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
7809 APInt SplatBits, SplatUndef;
7810 unsigned SplatBitSize;
7811 bool HasAnyUndefs;
7812 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
7813 HasAnyUndefs, ElementBits) ||
7814 SplatBitSize > ElementBits)
7815 return false;
7816 Cnt = SplatBits.getSExtValue();
7817 return true;
7818}
7819
7820/// isVShiftLImm - Check if this is a valid build_vector for the immediate
7821/// operand of a vector shift left operation. That value must be in the range:
7822/// 0 <= Value < ElementBits for a left shift; or
7823/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00007824static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00007825 assert(VT.isVector() && "vector shift count is not a vector type");
7826 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
7827 if (! getVShiftImm(Op, ElementBits, Cnt))
7828 return false;
7829 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
7830}
7831
7832/// isVShiftRImm - Check if this is a valid build_vector for the immediate
7833/// operand of a vector shift right operation. For a shift opcode, the value
7834/// is positive, but for an intrinsic the value count must be negative. The
7835/// absolute value must be in the range:
7836/// 1 <= |Value| <= ElementBits for a right shift; or
7837/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00007838static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00007839 int64_t &Cnt) {
7840 assert(VT.isVector() && "vector shift count is not a vector type");
7841 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
7842 if (! getVShiftImm(Op, ElementBits, Cnt))
7843 return false;
7844 if (isIntrinsic)
7845 Cnt = -Cnt;
7846 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
7847}
7848
7849/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
7850static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
7851 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
7852 switch (IntNo) {
7853 default:
7854 // Don't do anything for most intrinsics.
7855 break;
7856
7857 // Vector shifts: check for immediate versions and lower them.
7858 // Note: This is done during DAG combining instead of DAG legalizing because
7859 // the build_vectors for 64-bit vector element shift counts are generally
7860 // not legal, and it is hard to see their values after they get legalized to
7861 // loads from a constant pool.
7862 case Intrinsic::arm_neon_vshifts:
7863 case Intrinsic::arm_neon_vshiftu:
7864 case Intrinsic::arm_neon_vshiftls:
7865 case Intrinsic::arm_neon_vshiftlu:
7866 case Intrinsic::arm_neon_vshiftn:
7867 case Intrinsic::arm_neon_vrshifts:
7868 case Intrinsic::arm_neon_vrshiftu:
7869 case Intrinsic::arm_neon_vrshiftn:
7870 case Intrinsic::arm_neon_vqshifts:
7871 case Intrinsic::arm_neon_vqshiftu:
7872 case Intrinsic::arm_neon_vqshiftsu:
7873 case Intrinsic::arm_neon_vqshiftns:
7874 case Intrinsic::arm_neon_vqshiftnu:
7875 case Intrinsic::arm_neon_vqshiftnsu:
7876 case Intrinsic::arm_neon_vqrshiftns:
7877 case Intrinsic::arm_neon_vqrshiftnu:
7878 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00007879 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007880 int64_t Cnt;
7881 unsigned VShiftOpc = 0;
7882
7883 switch (IntNo) {
7884 case Intrinsic::arm_neon_vshifts:
7885 case Intrinsic::arm_neon_vshiftu:
7886 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
7887 VShiftOpc = ARMISD::VSHL;
7888 break;
7889 }
7890 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
7891 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
7892 ARMISD::VSHRs : ARMISD::VSHRu);
7893 break;
7894 }
7895 return SDValue();
7896
7897 case Intrinsic::arm_neon_vshiftls:
7898 case Intrinsic::arm_neon_vshiftlu:
7899 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
7900 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007901 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007902
7903 case Intrinsic::arm_neon_vrshifts:
7904 case Intrinsic::arm_neon_vrshiftu:
7905 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
7906 break;
7907 return SDValue();
7908
7909 case Intrinsic::arm_neon_vqshifts:
7910 case Intrinsic::arm_neon_vqshiftu:
7911 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
7912 break;
7913 return SDValue();
7914
7915 case Intrinsic::arm_neon_vqshiftsu:
7916 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
7917 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007918 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007919
7920 case Intrinsic::arm_neon_vshiftn:
7921 case Intrinsic::arm_neon_vrshiftn:
7922 case Intrinsic::arm_neon_vqshiftns:
7923 case Intrinsic::arm_neon_vqshiftnu:
7924 case Intrinsic::arm_neon_vqshiftnsu:
7925 case Intrinsic::arm_neon_vqrshiftns:
7926 case Intrinsic::arm_neon_vqrshiftnu:
7927 case Intrinsic::arm_neon_vqrshiftnsu:
7928 // Narrowing shifts require an immediate right shift.
7929 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
7930 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00007931 llvm_unreachable("invalid shift count for narrowing vector shift "
7932 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007933
7934 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00007935 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00007936 }
7937
7938 switch (IntNo) {
7939 case Intrinsic::arm_neon_vshifts:
7940 case Intrinsic::arm_neon_vshiftu:
7941 // Opcode already set above.
7942 break;
7943 case Intrinsic::arm_neon_vshiftls:
7944 case Intrinsic::arm_neon_vshiftlu:
7945 if (Cnt == VT.getVectorElementType().getSizeInBits())
7946 VShiftOpc = ARMISD::VSHLLi;
7947 else
7948 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
7949 ARMISD::VSHLLs : ARMISD::VSHLLu);
7950 break;
7951 case Intrinsic::arm_neon_vshiftn:
7952 VShiftOpc = ARMISD::VSHRN; break;
7953 case Intrinsic::arm_neon_vrshifts:
7954 VShiftOpc = ARMISD::VRSHRs; break;
7955 case Intrinsic::arm_neon_vrshiftu:
7956 VShiftOpc = ARMISD::VRSHRu; break;
7957 case Intrinsic::arm_neon_vrshiftn:
7958 VShiftOpc = ARMISD::VRSHRN; break;
7959 case Intrinsic::arm_neon_vqshifts:
7960 VShiftOpc = ARMISD::VQSHLs; break;
7961 case Intrinsic::arm_neon_vqshiftu:
7962 VShiftOpc = ARMISD::VQSHLu; break;
7963 case Intrinsic::arm_neon_vqshiftsu:
7964 VShiftOpc = ARMISD::VQSHLsu; break;
7965 case Intrinsic::arm_neon_vqshiftns:
7966 VShiftOpc = ARMISD::VQSHRNs; break;
7967 case Intrinsic::arm_neon_vqshiftnu:
7968 VShiftOpc = ARMISD::VQSHRNu; break;
7969 case Intrinsic::arm_neon_vqshiftnsu:
7970 VShiftOpc = ARMISD::VQSHRNsu; break;
7971 case Intrinsic::arm_neon_vqrshiftns:
7972 VShiftOpc = ARMISD::VQRSHRNs; break;
7973 case Intrinsic::arm_neon_vqrshiftnu:
7974 VShiftOpc = ARMISD::VQRSHRNu; break;
7975 case Intrinsic::arm_neon_vqrshiftnsu:
7976 VShiftOpc = ARMISD::VQRSHRNsu; break;
7977 }
7978
7979 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007980 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007981 }
7982
7983 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00007984 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007985 int64_t Cnt;
7986 unsigned VShiftOpc = 0;
7987
7988 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
7989 VShiftOpc = ARMISD::VSLI;
7990 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
7991 VShiftOpc = ARMISD::VSRI;
7992 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00007993 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007994 }
7995
7996 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
7997 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00007998 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007999 }
8000
8001 case Intrinsic::arm_neon_vqrshifts:
8002 case Intrinsic::arm_neon_vqrshiftu:
8003 // No immediate versions of these to check for.
8004 break;
8005 }
8006
8007 return SDValue();
8008}
8009
8010/// PerformShiftCombine - Checks for immediate versions of vector shifts and
8011/// lowers them. As with the vector shift intrinsics, this is done during DAG
8012/// combining instead of DAG legalizing because the build_vectors for 64-bit
8013/// vector element shift counts are generally not legal, and it is hard to see
8014/// their values after they get legalized to loads from a constant pool.
8015static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
8016 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00008017 EVT VT = N->getValueType(0);
Evan Cheng5fb468a2012-02-23 02:58:19 +00008018 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
8019 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
8020 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
8021 SDValue N1 = N->getOperand(1);
8022 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
8023 SDValue N0 = N->getOperand(0);
8024 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
8025 DAG.MaskedValueIsZero(N0.getOperand(0),
8026 APInt::getHighBitsSet(32, 16)))
8027 return DAG.getNode(ISD::ROTR, N->getDebugLoc(), VT, N0, N1);
8028 }
8029 }
Bob Wilson5bafff32009-06-22 23:27:02 +00008030
8031 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00008032 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8033 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00008034 return SDValue();
8035
8036 assert(ST->hasNEON() && "unexpected vector shift");
8037 int64_t Cnt;
8038
8039 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008040 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00008041
8042 case ISD::SHL:
8043 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
8044 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008045 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008046 break;
8047
8048 case ISD::SRA:
8049 case ISD::SRL:
8050 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
8051 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
8052 ARMISD::VSHRs : ARMISD::VSHRu);
8053 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008054 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008055 }
8056 }
8057 return SDValue();
8058}
8059
8060/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
8061/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
8062static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
8063 const ARMSubtarget *ST) {
8064 SDValue N0 = N->getOperand(0);
8065
8066 // Check for sign- and zero-extensions of vector extract operations of 8-
8067 // and 16-bit vector elements. NEON supports these directly. They are
8068 // handled during DAG combining because type legalization will promote them
8069 // to 32-bit types and it is messy to recognize the operations after that.
8070 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
8071 SDValue Vec = N0.getOperand(0);
8072 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00008073 EVT VT = N->getValueType(0);
8074 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008075 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8076
Owen Anderson825b72b2009-08-11 20:47:22 +00008077 if (VT == MVT::i32 &&
8078 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00008079 TLI.isTypeLegal(Vec.getValueType()) &&
8080 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00008081
8082 unsigned Opc = 0;
8083 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008084 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00008085 case ISD::SIGN_EXTEND:
8086 Opc = ARMISD::VGETLANEs;
8087 break;
8088 case ISD::ZERO_EXTEND:
8089 case ISD::ANY_EXTEND:
8090 Opc = ARMISD::VGETLANEu;
8091 break;
8092 }
8093 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
8094 }
8095 }
8096
8097 return SDValue();
8098}
8099
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008100/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
8101/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
8102static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
8103 const ARMSubtarget *ST) {
8104 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00008105 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008106 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
8107 // a NaN; only do the transformation when it matches that behavior.
8108
8109 // For now only do this when using NEON for FP operations; if using VFP, it
8110 // is not obvious that the benefit outweighs the cost of switching to the
8111 // NEON pipeline.
8112 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
8113 N->getValueType(0) != MVT::f32)
8114 return SDValue();
8115
8116 SDValue CondLHS = N->getOperand(0);
8117 SDValue CondRHS = N->getOperand(1);
8118 SDValue LHS = N->getOperand(2);
8119 SDValue RHS = N->getOperand(3);
8120 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
8121
8122 unsigned Opcode = 0;
8123 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00008124 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008125 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00008126 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008127 IsReversed = true ; // x CC y ? y : x
8128 } else {
8129 return SDValue();
8130 }
8131
Bob Wilsone742bb52010-02-24 22:15:53 +00008132 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008133 switch (CC) {
8134 default: break;
8135 case ISD::SETOLT:
8136 case ISD::SETOLE:
8137 case ISD::SETLT:
8138 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008139 case ISD::SETULT:
8140 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00008141 // If LHS is NaN, an ordered comparison will be false and the result will
8142 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
8143 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
8144 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
8145 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8146 break;
8147 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
8148 // will return -0, so vmin can only be used for unsafe math or if one of
8149 // the operands is known to be nonzero.
8150 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008151 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00008152 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8153 break;
8154 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008155 break;
8156
8157 case ISD::SETOGT:
8158 case ISD::SETOGE:
8159 case ISD::SETGT:
8160 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008161 case ISD::SETUGT:
8162 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00008163 // If LHS is NaN, an ordered comparison will be false and the result will
8164 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
8165 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
8166 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
8167 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8168 break;
8169 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
8170 // will return +0, so vmax can only be used for unsafe math or if one of
8171 // the operands is known to be nonzero.
8172 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008173 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00008174 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8175 break;
8176 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008177 break;
8178 }
8179
8180 if (!Opcode)
8181 return SDValue();
8182 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
8183}
8184
Evan Chenge721f5c2011-07-13 00:42:17 +00008185/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
8186SDValue
8187ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
8188 SDValue Cmp = N->getOperand(4);
8189 if (Cmp.getOpcode() != ARMISD::CMPZ)
8190 // Only looking at EQ and NE cases.
8191 return SDValue();
8192
8193 EVT VT = N->getValueType(0);
8194 DebugLoc dl = N->getDebugLoc();
8195 SDValue LHS = Cmp.getOperand(0);
8196 SDValue RHS = Cmp.getOperand(1);
8197 SDValue FalseVal = N->getOperand(0);
8198 SDValue TrueVal = N->getOperand(1);
8199 SDValue ARMcc = N->getOperand(2);
Jim Grosbachb04546f2011-09-13 20:30:37 +00008200 ARMCC::CondCodes CC =
8201 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chenge721f5c2011-07-13 00:42:17 +00008202
8203 // Simplify
8204 // mov r1, r0
8205 // cmp r1, x
8206 // mov r0, y
8207 // moveq r0, x
8208 // to
8209 // cmp r0, x
8210 // movne r0, y
8211 //
8212 // mov r1, r0
8213 // cmp r1, x
8214 // mov r0, x
8215 // movne r0, y
8216 // to
8217 // cmp r0, x
8218 // movne r0, y
8219 /// FIXME: Turn this into a target neutral optimization?
8220 SDValue Res;
Evan Cheng9b88d2d2011-09-28 23:16:31 +00008221 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chenge721f5c2011-07-13 00:42:17 +00008222 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
8223 N->getOperand(3), Cmp);
8224 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
8225 SDValue ARMcc;
8226 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
8227 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
8228 N->getOperand(3), NewCmp);
8229 }
8230
8231 if (Res.getNode()) {
8232 APInt KnownZero, KnownOne;
8233 APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits());
8234 DAG.ComputeMaskedBits(SDValue(N,0), Mask, KnownZero, KnownOne);
8235 // Capture demanded bits information that would be otherwise lost.
8236 if (KnownZero == 0xfffffffe)
8237 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8238 DAG.getValueType(MVT::i1));
8239 else if (KnownZero == 0xffffff00)
8240 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8241 DAG.getValueType(MVT::i8));
8242 else if (KnownZero == 0xffff0000)
8243 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8244 DAG.getValueType(MVT::i16));
8245 }
8246
8247 return Res;
8248}
8249
Dan Gohman475871a2008-07-27 21:46:04 +00008250SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008251 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008252 switch (N->getOpcode()) {
8253 default: break;
Tanya Lattner189531f2011-06-14 23:48:48 +00008254 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008255 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00008256 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008257 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chengc892aeb2012-02-23 01:19:06 +00008258 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
8259 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Cheng0c1aec12010-12-14 03:22:07 +00008260 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00008261 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008262 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00008263 case ISD::STORE: return PerformSTORECombine(N, DCI);
8264 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
8265 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00008266 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008267 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00008268 case ISD::FP_TO_SINT:
8269 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
8270 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008271 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00008272 case ISD::SHL:
8273 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008274 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00008275 case ISD::SIGN_EXTEND:
8276 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008277 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
8278 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00008279 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00008280 case ARMISD::VLD2DUP:
8281 case ARMISD::VLD3DUP:
8282 case ARMISD::VLD4DUP:
8283 return CombineBaseUpdate(N, DCI);
8284 case ISD::INTRINSIC_VOID:
8285 case ISD::INTRINSIC_W_CHAIN:
8286 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
8287 case Intrinsic::arm_neon_vld1:
8288 case Intrinsic::arm_neon_vld2:
8289 case Intrinsic::arm_neon_vld3:
8290 case Intrinsic::arm_neon_vld4:
8291 case Intrinsic::arm_neon_vld2lane:
8292 case Intrinsic::arm_neon_vld3lane:
8293 case Intrinsic::arm_neon_vld4lane:
8294 case Intrinsic::arm_neon_vst1:
8295 case Intrinsic::arm_neon_vst2:
8296 case Intrinsic::arm_neon_vst3:
8297 case Intrinsic::arm_neon_vst4:
8298 case Intrinsic::arm_neon_vst2lane:
8299 case Intrinsic::arm_neon_vst3lane:
8300 case Intrinsic::arm_neon_vst4lane:
8301 return CombineBaseUpdate(N, DCI);
8302 default: break;
8303 }
8304 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008305 }
Dan Gohman475871a2008-07-27 21:46:04 +00008306 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008307}
8308
Evan Cheng31959b12011-02-02 01:06:55 +00008309bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
8310 EVT VT) const {
8311 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
8312}
8313
Bill Wendlingaf566342009-08-15 21:21:19 +00008314bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00008315 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00008316 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00008317
8318 switch (VT.getSimpleVT().SimpleTy) {
8319 default:
8320 return false;
8321 case MVT::i8:
8322 case MVT::i16:
8323 case MVT::i32:
8324 return true;
8325 // FIXME: VLD1 etc with standard alignment is legal.
8326 }
8327}
8328
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008329static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
8330 unsigned AlignCheck) {
8331 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
8332 (DstAlign == 0 || DstAlign % AlignCheck == 0));
8333}
8334
8335EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
8336 unsigned DstAlign, unsigned SrcAlign,
Lang Hamesa1e78882011-11-02 23:37:04 +00008337 bool IsZeroVal,
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008338 bool MemcpyStrSrc,
8339 MachineFunction &MF) const {
8340 const Function *F = MF.getFunction();
8341
8342 // See if we can use NEON instructions for this...
Lang Hamesa1e78882011-11-02 23:37:04 +00008343 if (IsZeroVal &&
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008344 !F->hasFnAttr(Attribute::NoImplicitFloat) &&
8345 Subtarget->hasNEON()) {
8346 if (memOpAlign(SrcAlign, DstAlign, 16) && Size >= 16) {
8347 return MVT::v4i32;
8348 } else if (memOpAlign(SrcAlign, DstAlign, 8) && Size >= 8) {
8349 return MVT::v2i32;
8350 }
8351 }
8352
Lang Hames5207bf22011-11-08 18:56:23 +00008353 // Lowering to i32/i16 if the size permits.
8354 if (Size >= 4) {
8355 return MVT::i32;
8356 } else if (Size >= 2) {
8357 return MVT::i16;
8358 }
8359
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008360 // Let the target-independent logic figure it out.
8361 return MVT::Other;
8362}
8363
Evan Chenge6c835f2009-08-14 20:09:37 +00008364static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
8365 if (V < 0)
8366 return false;
8367
8368 unsigned Scale = 1;
8369 switch (VT.getSimpleVT().SimpleTy) {
8370 default: return false;
8371 case MVT::i1:
8372 case MVT::i8:
8373 // Scale == 1;
8374 break;
8375 case MVT::i16:
8376 // Scale == 2;
8377 Scale = 2;
8378 break;
8379 case MVT::i32:
8380 // Scale == 4;
8381 Scale = 4;
8382 break;
8383 }
8384
8385 if ((V & (Scale - 1)) != 0)
8386 return false;
8387 V /= Scale;
8388 return V == (V & ((1LL << 5) - 1));
8389}
8390
8391static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
8392 const ARMSubtarget *Subtarget) {
8393 bool isNeg = false;
8394 if (V < 0) {
8395 isNeg = true;
8396 V = - V;
8397 }
8398
8399 switch (VT.getSimpleVT().SimpleTy) {
8400 default: return false;
8401 case MVT::i1:
8402 case MVT::i8:
8403 case MVT::i16:
8404 case MVT::i32:
8405 // + imm12 or - imm8
8406 if (isNeg)
8407 return V == (V & ((1LL << 8) - 1));
8408 return V == (V & ((1LL << 12) - 1));
8409 case MVT::f32:
8410 case MVT::f64:
8411 // Same as ARM mode. FIXME: NEON?
8412 if (!Subtarget->hasVFP2())
8413 return false;
8414 if ((V & 3) != 0)
8415 return false;
8416 V >>= 2;
8417 return V == (V & ((1LL << 8) - 1));
8418 }
8419}
8420
Evan Chengb01fad62007-03-12 23:30:29 +00008421/// isLegalAddressImmediate - Return true if the integer value can be used
8422/// as the offset of the target addressing mode for load / store of the
8423/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00008424static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00008425 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00008426 if (V == 0)
8427 return true;
8428
Evan Cheng65011532009-03-09 19:15:00 +00008429 if (!VT.isSimple())
8430 return false;
8431
Evan Chenge6c835f2009-08-14 20:09:37 +00008432 if (Subtarget->isThumb1Only())
8433 return isLegalT1AddressImmediate(V, VT);
8434 else if (Subtarget->isThumb2())
8435 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00008436
Evan Chenge6c835f2009-08-14 20:09:37 +00008437 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00008438 if (V < 0)
8439 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00008440 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00008441 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008442 case MVT::i1:
8443 case MVT::i8:
8444 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00008445 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008446 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008447 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00008448 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008449 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008450 case MVT::f32:
8451 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00008452 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00008453 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00008454 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00008455 return false;
8456 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008457 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00008458 }
Evan Chenga8e29892007-01-19 07:51:42 +00008459}
8460
Evan Chenge6c835f2009-08-14 20:09:37 +00008461bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
8462 EVT VT) const {
8463 int Scale = AM.Scale;
8464 if (Scale < 0)
8465 return false;
8466
8467 switch (VT.getSimpleVT().SimpleTy) {
8468 default: return false;
8469 case MVT::i1:
8470 case MVT::i8:
8471 case MVT::i16:
8472 case MVT::i32:
8473 if (Scale == 1)
8474 return true;
8475 // r + r << imm
8476 Scale = Scale & ~1;
8477 return Scale == 2 || Scale == 4 || Scale == 8;
8478 case MVT::i64:
8479 // r + r
8480 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
8481 return true;
8482 return false;
8483 case MVT::isVoid:
8484 // Note, we allow "void" uses (basically, uses that aren't loads or
8485 // stores), because arm allows folding a scale into many arithmetic
8486 // operations. This should be made more precise and revisited later.
8487
8488 // Allow r << imm, but the imm has to be a multiple of two.
8489 if (Scale & 1) return false;
8490 return isPowerOf2_32(Scale);
8491 }
8492}
8493
Chris Lattner37caf8c2007-04-09 23:33:39 +00008494/// isLegalAddressingMode - Return true if the addressing mode represented
8495/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008496bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008497 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008498 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00008499 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00008500 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008501
Chris Lattner37caf8c2007-04-09 23:33:39 +00008502 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008503 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008504 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008505
Chris Lattner37caf8c2007-04-09 23:33:39 +00008506 switch (AM.Scale) {
8507 case 0: // no scale reg, must be "r+i" or "r", or "i".
8508 break;
8509 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00008510 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00008511 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008512 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00008513 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008514 // ARM doesn't support any R+R*scale+imm addr modes.
8515 if (AM.BaseOffs)
8516 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008517
Bob Wilson2c7dab12009-04-08 17:55:28 +00008518 if (!VT.isSimple())
8519 return false;
8520
Evan Chenge6c835f2009-08-14 20:09:37 +00008521 if (Subtarget->isThumb2())
8522 return isLegalT2ScaledAddressingMode(AM, VT);
8523
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008524 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00008525 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00008526 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008527 case MVT::i1:
8528 case MVT::i8:
8529 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008530 if (Scale < 0) Scale = -Scale;
8531 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008532 return true;
8533 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00008534 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008535 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00008536 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00008537 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008538 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008539 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00008540 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008541
Owen Anderson825b72b2009-08-11 20:47:22 +00008542 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00008543 // Note, we allow "void" uses (basically, uses that aren't loads or
8544 // stores), because arm allows folding a scale into many arithmetic
8545 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008546
Chris Lattner37caf8c2007-04-09 23:33:39 +00008547 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00008548 if (Scale & 1) return false;
8549 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00008550 }
Evan Chengb01fad62007-03-12 23:30:29 +00008551 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00008552 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00008553}
8554
Evan Cheng77e47512009-11-11 19:05:52 +00008555/// isLegalICmpImmediate - Return true if the specified immediate is legal
8556/// icmp immediate, that is the target has icmp instructions which can compare
8557/// a register against the immediate without having to materialize the
8558/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00008559bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00008560 if (!Subtarget->isThumb())
8561 return ARM_AM::getSOImmVal(Imm) != -1;
8562 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00008563 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00008564 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00008565}
8566
Dan Gohmancca82142011-05-03 00:46:49 +00008567/// isLegalAddImmediate - Return true if the specified immediate is legal
8568/// add immediate, that is the target has add instructions which can add
8569/// a register with the immediate without having to materialize the
8570/// immediate into a register.
8571bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
8572 return ARM_AM::getSOImmVal(Imm) != -1;
8573}
8574
Owen Andersone50ed302009-08-10 22:56:29 +00008575static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00008576 bool isSEXTLoad, SDValue &Base,
8577 SDValue &Offset, bool &isInc,
8578 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00008579 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
8580 return false;
8581
Owen Anderson825b72b2009-08-11 20:47:22 +00008582 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00008583 // AddressingMode 3
8584 Base = Ptr->getOperand(0);
8585 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008586 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00008587 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008588 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00008589 isInc = false;
8590 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8591 return true;
8592 }
8593 }
8594 isInc = (Ptr->getOpcode() == ISD::ADD);
8595 Offset = Ptr->getOperand(1);
8596 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00008597 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00008598 // AddressingMode 2
8599 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008600 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00008601 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008602 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00008603 isInc = false;
8604 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8605 Base = Ptr->getOperand(0);
8606 return true;
8607 }
8608 }
8609
8610 if (Ptr->getOpcode() == ISD::ADD) {
8611 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00008612 ARM_AM::ShiftOpc ShOpcVal=
8613 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00008614 if (ShOpcVal != ARM_AM::no_shift) {
8615 Base = Ptr->getOperand(1);
8616 Offset = Ptr->getOperand(0);
8617 } else {
8618 Base = Ptr->getOperand(0);
8619 Offset = Ptr->getOperand(1);
8620 }
8621 return true;
8622 }
8623
8624 isInc = (Ptr->getOpcode() == ISD::ADD);
8625 Base = Ptr->getOperand(0);
8626 Offset = Ptr->getOperand(1);
8627 return true;
8628 }
8629
Jim Grosbache5165492009-11-09 00:11:35 +00008630 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00008631 return false;
8632}
8633
Owen Andersone50ed302009-08-10 22:56:29 +00008634static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00008635 bool isSEXTLoad, SDValue &Base,
8636 SDValue &Offset, bool &isInc,
8637 SelectionDAG &DAG) {
8638 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
8639 return false;
8640
8641 Base = Ptr->getOperand(0);
8642 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
8643 int RHSC = (int)RHS->getZExtValue();
8644 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
8645 assert(Ptr->getOpcode() == ISD::ADD);
8646 isInc = false;
8647 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8648 return true;
8649 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
8650 isInc = Ptr->getOpcode() == ISD::ADD;
8651 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
8652 return true;
8653 }
8654 }
8655
8656 return false;
8657}
8658
Evan Chenga8e29892007-01-19 07:51:42 +00008659/// getPreIndexedAddressParts - returns true by value, base pointer and
8660/// offset pointer and addressing mode by reference if the node's address
8661/// can be legally represented as pre-indexed load / store address.
8662bool
Dan Gohman475871a2008-07-27 21:46:04 +00008663ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
8664 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00008665 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00008666 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008667 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00008668 return false;
8669
Owen Andersone50ed302009-08-10 22:56:29 +00008670 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00008671 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00008672 bool isSEXTLoad = false;
8673 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
8674 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008675 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00008676 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
8677 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
8678 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008679 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00008680 } else
8681 return false;
8682
8683 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00008684 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00008685 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00008686 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
8687 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00008688 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00008689 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00008690 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00008691 if (!isLegal)
8692 return false;
8693
8694 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
8695 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00008696}
8697
8698/// getPostIndexedAddressParts - returns true by value, base pointer and
8699/// offset pointer and addressing mode by reference if this node can be
8700/// combined with a load / store to form a post-indexed load / store.
8701bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00008702 SDValue &Base,
8703 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00008704 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00008705 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008706 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00008707 return false;
8708
Owen Andersone50ed302009-08-10 22:56:29 +00008709 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00008710 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00008711 bool isSEXTLoad = false;
8712 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008713 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00008714 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00008715 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
8716 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008717 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00008718 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00008719 } else
8720 return false;
8721
8722 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00008723 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00008724 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00008725 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00008726 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00008727 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00008728 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
8729 isInc, DAG);
8730 if (!isLegal)
8731 return false;
8732
Evan Cheng28dad2a2010-05-18 21:31:17 +00008733 if (Ptr != Base) {
8734 // Swap base ptr and offset to catch more post-index load / store when
8735 // it's legal. In Thumb2 mode, offset must be an immediate.
8736 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
8737 !Subtarget->isThumb2())
8738 std::swap(Base, Offset);
8739
8740 // Post-indexed load / store update the base pointer.
8741 if (Ptr != Base)
8742 return false;
8743 }
8744
Evan Chenge88d5ce2009-07-02 07:28:31 +00008745 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
8746 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00008747}
8748
Dan Gohman475871a2008-07-27 21:46:04 +00008749void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00008750 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008751 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008752 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008753 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00008754 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008755 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00008756 switch (Op.getOpcode()) {
8757 default: break;
8758 case ARMISD::CMOV: {
8759 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00008760 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00008761 if (KnownZero == 0 && KnownOne == 0) return;
8762
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008763 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00008764 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
8765 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00008766 KnownZero &= KnownZeroRHS;
8767 KnownOne &= KnownOneRHS;
8768 return;
8769 }
8770 }
8771}
8772
8773//===----------------------------------------------------------------------===//
8774// ARM Inline Assembly Support
8775//===----------------------------------------------------------------------===//
8776
Evan Cheng55d42002011-01-08 01:24:27 +00008777bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
8778 // Looking for "rev" which is V6+.
8779 if (!Subtarget->hasV6Ops())
8780 return false;
8781
8782 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
8783 std::string AsmStr = IA->getAsmString();
8784 SmallVector<StringRef, 4> AsmPieces;
8785 SplitString(AsmStr, AsmPieces, ";\n");
8786
8787 switch (AsmPieces.size()) {
8788 default: return false;
8789 case 1:
8790 AsmStr = AsmPieces[0];
8791 AsmPieces.clear();
8792 SplitString(AsmStr, AsmPieces, " \t,");
8793
8794 // rev $0, $1
8795 if (AsmPieces.size() == 3 &&
8796 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
8797 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008798 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00008799 if (Ty && Ty->getBitWidth() == 32)
8800 return IntrinsicLowering::LowerToByteSwap(CI);
8801 }
8802 break;
8803 }
8804
8805 return false;
8806}
8807
Evan Chenga8e29892007-01-19 07:51:42 +00008808/// getConstraintType - Given a constraint letter, return the type of
8809/// constraint it is for this target.
8810ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00008811ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
8812 if (Constraint.size() == 1) {
8813 switch (Constraint[0]) {
8814 default: break;
8815 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008816 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00008817 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00008818 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008819 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00008820 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00008821 // An address with a single base register. Due to the way we
8822 // currently handle addresses it is the same as an 'r' memory constraint.
8823 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00008824 }
Eric Christopher1312ca82011-06-21 22:10:57 +00008825 } else if (Constraint.size() == 2) {
8826 switch (Constraint[0]) {
8827 default: break;
8828 // All 'U+' constraints are addresses.
8829 case 'U': return C_Memory;
8830 }
Evan Chenga8e29892007-01-19 07:51:42 +00008831 }
Chris Lattner4234f572007-03-25 02:14:49 +00008832 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00008833}
8834
John Thompson44ab89e2010-10-29 17:29:13 +00008835/// Examine constraint type and operand type and determine a weight value.
8836/// This object must already have been set up with the operand type
8837/// and the current alternative constraint selected.
8838TargetLowering::ConstraintWeight
8839ARMTargetLowering::getSingleConstraintMatchWeight(
8840 AsmOperandInfo &info, const char *constraint) const {
8841 ConstraintWeight weight = CW_Invalid;
8842 Value *CallOperandVal = info.CallOperandVal;
8843 // If we don't have a value, we can't do a match,
8844 // but allow it at the lowest weight.
8845 if (CallOperandVal == NULL)
8846 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008847 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00008848 // Look at the constraint type.
8849 switch (*constraint) {
8850 default:
8851 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
8852 break;
8853 case 'l':
8854 if (type->isIntegerTy()) {
8855 if (Subtarget->isThumb())
8856 weight = CW_SpecificReg;
8857 else
8858 weight = CW_Register;
8859 }
8860 break;
8861 case 'w':
8862 if (type->isFloatingPointTy())
8863 weight = CW_Register;
8864 break;
8865 }
8866 return weight;
8867}
8868
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008869typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
8870RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00008871ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00008872 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00008873 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00008874 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00008875 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00008876 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00008877 if (Subtarget->isThumb())
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008878 return RCPair(0U, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +00008879 else
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008880 return RCPair(0U, ARM::GPRRegisterClass);
Eric Christopher73744df2011-06-30 23:23:01 +00008881 case 'h': // High regs or no regs.
8882 if (Subtarget->isThumb())
Andrew Trick3af7a672011-09-20 03:06:13 +00008883 return RCPair(0U, ARM::hGPRRegisterClass);
Eric Christopher1070f822011-07-01 00:19:27 +00008884 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008885 case 'r':
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008886 return RCPair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008887 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00008888 if (VT == MVT::f32)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008889 return RCPair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00008890 if (VT.getSizeInBits() == 64)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008891 return RCPair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00008892 if (VT.getSizeInBits() == 128)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008893 return RCPair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008894 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00008895 case 'x':
8896 if (VT == MVT::f32)
Andrew Trick3af7a672011-09-20 03:06:13 +00008897 return RCPair(0U, ARM::SPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008898 if (VT.getSizeInBits() == 64)
Andrew Trick3af7a672011-09-20 03:06:13 +00008899 return RCPair(0U, ARM::DPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008900 if (VT.getSizeInBits() == 128)
Andrew Trick3af7a672011-09-20 03:06:13 +00008901 return RCPair(0U, ARM::QPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008902 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008903 case 't':
8904 if (VT == MVT::f32)
Andrew Trick3af7a672011-09-20 03:06:13 +00008905 return RCPair(0U, ARM::SPRRegisterClass);
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008906 break;
Evan Chenga8e29892007-01-19 07:51:42 +00008907 }
8908 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00008909 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00008910 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00008911
Evan Chenga8e29892007-01-19 07:51:42 +00008912 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
8913}
8914
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008915/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
8916/// vector. If it is invalid, don't add anything to Ops.
8917void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00008918 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008919 std::vector<SDValue>&Ops,
8920 SelectionDAG &DAG) const {
8921 SDValue Result(0, 0);
8922
Eric Christopher100c8332011-06-02 23:16:42 +00008923 // Currently only support length 1 constraints.
8924 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00008925
Eric Christopher100c8332011-06-02 23:16:42 +00008926 char ConstraintLetter = Constraint[0];
8927 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008928 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00008929 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008930 case 'I': case 'J': case 'K': case 'L':
8931 case 'M': case 'N': case 'O':
8932 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
8933 if (!C)
8934 return;
8935
8936 int64_t CVal64 = C->getSExtValue();
8937 int CVal = (int) CVal64;
8938 // None of these constraints allow values larger than 32 bits. Check
8939 // that the value fits in an int.
8940 if (CVal != CVal64)
8941 return;
8942
Eric Christopher100c8332011-06-02 23:16:42 +00008943 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00008944 case 'j':
Andrew Trick3af7a672011-09-20 03:06:13 +00008945 // Constant suitable for movw, must be between 0 and
8946 // 65535.
8947 if (Subtarget->hasV6T2Ops())
8948 if (CVal >= 0 && CVal <= 65535)
8949 break;
8950 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008951 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008952 if (Subtarget->isThumb1Only()) {
8953 // This must be a constant between 0 and 255, for ADD
8954 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008955 if (CVal >= 0 && CVal <= 255)
8956 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008957 } else if (Subtarget->isThumb2()) {
8958 // A constant that can be used as an immediate value in a
8959 // data-processing instruction.
8960 if (ARM_AM::getT2SOImmVal(CVal) != -1)
8961 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008962 } else {
8963 // A constant that can be used as an immediate value in a
8964 // data-processing instruction.
8965 if (ARM_AM::getSOImmVal(CVal) != -1)
8966 break;
8967 }
8968 return;
8969
8970 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008971 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008972 // This must be a constant between -255 and -1, for negated ADD
8973 // immediates. This can be used in GCC with an "n" modifier that
8974 // prints the negated value, for use with SUB instructions. It is
8975 // not useful otherwise but is implemented for compatibility.
8976 if (CVal >= -255 && CVal <= -1)
8977 break;
8978 } else {
8979 // This must be a constant between -4095 and 4095. It is not clear
8980 // what this constraint is intended for. Implemented for
8981 // compatibility with GCC.
8982 if (CVal >= -4095 && CVal <= 4095)
8983 break;
8984 }
8985 return;
8986
8987 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008988 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008989 // A 32-bit value where only one byte has a nonzero value. Exclude
8990 // zero to match GCC. This constraint is used by GCC internally for
8991 // constants that can be loaded with a move/shift combination.
8992 // It is not useful otherwise but is implemented for compatibility.
8993 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
8994 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008995 } else if (Subtarget->isThumb2()) {
8996 // A constant whose bitwise inverse can be used as an immediate
8997 // value in a data-processing instruction. This can be used in GCC
8998 // with a "B" modifier that prints the inverted value, for use with
8999 // BIC and MVN instructions. It is not useful otherwise but is
9000 // implemented for compatibility.
9001 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
9002 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009003 } else {
9004 // A constant whose bitwise inverse can be used as an immediate
9005 // value in a data-processing instruction. This can be used in GCC
9006 // with a "B" modifier that prints the inverted value, for use with
9007 // BIC and MVN instructions. It is not useful otherwise but is
9008 // implemented for compatibility.
9009 if (ARM_AM::getSOImmVal(~CVal) != -1)
9010 break;
9011 }
9012 return;
9013
9014 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009015 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009016 // This must be a constant between -7 and 7,
9017 // for 3-operand ADD/SUB immediate instructions.
9018 if (CVal >= -7 && CVal < 7)
9019 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009020 } else if (Subtarget->isThumb2()) {
9021 // A constant whose negation can be used as an immediate value in a
9022 // data-processing instruction. This can be used in GCC with an "n"
9023 // modifier that prints the negated value, for use with SUB
9024 // instructions. It is not useful otherwise but is implemented for
9025 // compatibility.
9026 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
9027 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009028 } else {
9029 // A constant whose negation can be used as an immediate value in a
9030 // data-processing instruction. This can be used in GCC with an "n"
9031 // modifier that prints the negated value, for use with SUB
9032 // instructions. It is not useful otherwise but is implemented for
9033 // compatibility.
9034 if (ARM_AM::getSOImmVal(-CVal) != -1)
9035 break;
9036 }
9037 return;
9038
9039 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009040 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009041 // This must be a multiple of 4 between 0 and 1020, for
9042 // ADD sp + immediate.
9043 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
9044 break;
9045 } else {
9046 // A power of two or a constant between 0 and 32. This is used in
9047 // GCC for the shift amount on shifted register operands, but it is
9048 // useful in general for any shift amounts.
9049 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
9050 break;
9051 }
9052 return;
9053
9054 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009055 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009056 // This must be a constant between 0 and 31, for shift amounts.
9057 if (CVal >= 0 && CVal <= 31)
9058 break;
9059 }
9060 return;
9061
9062 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009063 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009064 // This must be a multiple of 4 between -508 and 508, for
9065 // ADD/SUB sp = sp + immediate.
9066 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
9067 break;
9068 }
9069 return;
9070 }
9071 Result = DAG.getTargetConstant(CVal, Op.getValueType());
9072 break;
9073 }
9074
9075 if (Result.getNode()) {
9076 Ops.push_back(Result);
9077 return;
9078 }
Dale Johannesen1784d162010-06-25 21:55:36 +00009079 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009080}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00009081
9082bool
9083ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
9084 // The ARM target isn't yet aware of offsets.
9085 return false;
9086}
Evan Cheng39382422009-10-28 01:44:26 +00009087
Jim Grosbach469bbdb2010-07-16 23:05:05 +00009088bool ARM::isBitFieldInvertedMask(unsigned v) {
9089 if (v == 0xffffffff)
9090 return 0;
9091 // there can be 1's on either or both "outsides", all the "inside"
9092 // bits must be 0's
9093 unsigned int lsb = 0, msb = 31;
9094 while (v & (1 << msb)) --msb;
9095 while (v & (1 << lsb)) ++lsb;
9096 for (unsigned int i = lsb; i <= msb; ++i) {
9097 if (v & (1 << i))
9098 return 0;
9099 }
9100 return 1;
9101}
9102
Evan Cheng39382422009-10-28 01:44:26 +00009103/// isFPImmLegal - Returns true if the target can instruction select the
9104/// specified FP immediate natively. If false, the legalizer will
9105/// materialize the FP immediate as a load from a constant pool.
9106bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
9107 if (!Subtarget->hasVFP3())
9108 return false;
9109 if (VT == MVT::f32)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00009110 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00009111 if (VT == MVT::f64)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00009112 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00009113 return false;
9114}
Bob Wilson65ffec42010-09-21 17:56:22 +00009115
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009116/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00009117/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
9118/// specified in the intrinsic calls.
9119bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
9120 const CallInst &I,
9121 unsigned Intrinsic) const {
9122 switch (Intrinsic) {
9123 case Intrinsic::arm_neon_vld1:
9124 case Intrinsic::arm_neon_vld2:
9125 case Intrinsic::arm_neon_vld3:
9126 case Intrinsic::arm_neon_vld4:
9127 case Intrinsic::arm_neon_vld2lane:
9128 case Intrinsic::arm_neon_vld3lane:
9129 case Intrinsic::arm_neon_vld4lane: {
9130 Info.opc = ISD::INTRINSIC_W_CHAIN;
9131 // Conservatively set memVT to the entire set of vectors loaded.
9132 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
9133 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
9134 Info.ptrVal = I.getArgOperand(0);
9135 Info.offset = 0;
9136 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
9137 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
9138 Info.vol = false; // volatile loads with NEON intrinsics not supported
9139 Info.readMem = true;
9140 Info.writeMem = false;
9141 return true;
9142 }
9143 case Intrinsic::arm_neon_vst1:
9144 case Intrinsic::arm_neon_vst2:
9145 case Intrinsic::arm_neon_vst3:
9146 case Intrinsic::arm_neon_vst4:
9147 case Intrinsic::arm_neon_vst2lane:
9148 case Intrinsic::arm_neon_vst3lane:
9149 case Intrinsic::arm_neon_vst4lane: {
9150 Info.opc = ISD::INTRINSIC_VOID;
9151 // Conservatively set memVT to the entire set of vectors stored.
9152 unsigned NumElts = 0;
9153 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009154 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +00009155 if (!ArgTy->isVectorTy())
9156 break;
9157 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
9158 }
9159 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
9160 Info.ptrVal = I.getArgOperand(0);
9161 Info.offset = 0;
9162 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
9163 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
9164 Info.vol = false; // volatile stores with NEON intrinsics not supported
9165 Info.readMem = false;
9166 Info.writeMem = true;
9167 return true;
9168 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009169 case Intrinsic::arm_strexd: {
9170 Info.opc = ISD::INTRINSIC_W_CHAIN;
9171 Info.memVT = MVT::i64;
9172 Info.ptrVal = I.getArgOperand(2);
9173 Info.offset = 0;
9174 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009175 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009176 Info.readMem = false;
9177 Info.writeMem = true;
9178 return true;
9179 }
9180 case Intrinsic::arm_ldrexd: {
9181 Info.opc = ISD::INTRINSIC_W_CHAIN;
9182 Info.memVT = MVT::i64;
9183 Info.ptrVal = I.getArgOperand(0);
9184 Info.offset = 0;
9185 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009186 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009187 Info.readMem = true;
9188 Info.writeMem = false;
9189 return true;
9190 }
Bob Wilson65ffec42010-09-21 17:56:22 +00009191 default:
9192 break;
9193 }
9194
9195 return false;
9196}